0.0.3 • Published 2 years ago

@ibrahimanshor/my-express v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

My Express

Create App

const { createApp } = require('@ibrahimanshor/my-express');

const app = createApp({
  port: 4000, // default 4000
  env: 'development', // default 'development'
  staticPath: '', // default current directory
  staticDir: 'public', // default 'public'
});

app.run();

Add Middleware

const { createApp } = require('@ibrahimanshor/my-express');

const myMiddleware = (app) => {
  app.use((req, res, next) => {
    console.log('running middleware');

    next();
  });
};

const app = createApp({
  setupMiddleware: myMiddleware,
});

app.run();

Add Route

const { createApp } = require('@ibrahimanshor/my-express');

const myRouter = (app) => {
  app.get('/', (req, res) => res.send('hello world'));
};

const app = createApp({
  setupRoute: myRouter,
});

app.run();

Translations

const { createApp } = require('@ibrahimanshor/my-express');

// default en or from header accept language
const messages = {
  en: {
    greet: 'hello world',
  },
  id: {
    greet: 'halo dunia',
  },
};

// usage: req.polyglot.t(key)
const myRouter = (app) => {
  app.get('/', (req, res) => res.send(req.polyglot.t('greet')));
};

const app = createApp({
  messages,
  locale: 'en', // default en
});

Container

const { Container } = require('@ibrahimanshor/my-express');

// Register
Container.register('greet', 'Hello');

// Factory
Container.register('myFunction', () => {
  console.log('my factory function');
});

// Factory With Depedency
Container.register('myGreetFunction', ({ greet }) => {
  console.log(greet);
});

// Get
console.log(Container.get('greet')); // Hello
console.log(Container.get('greet')); // Hello
console.log(Container.get('myFunction')); // my factory function
console.log(Container.get('myGreetFunction')); // Hello

Helper

Request Validator

const { createApp } = require('@ibrahimanshor/my-express');

const {
  helpers: { createRequestValidator },
} = require('@ibrahimanshor/my-express');
const { body } = require('express-validator');

const postRequest = [body('name').exists().isString().notEmpty()];
const myRouter = (app) => {
  app.post('/', createRequestValidator(postRequest), (req, res) =>
    res.json(req.body)
  );
};

const app = createApp({
  messages,
});

Exceptions

// Import Exceptions
const {
  exceptions: {
    HttpException,
    UnprocessableEntityException,
    ConflictException,
    NotFoundException,
    UnauthorizedException,
    ForbiddenException,
  },
} = require('@ibrahimanshor/my-express');

// Usage
const { createApp } = require('@ibrahimanshor/my-express');

const myRouter = (app) => {
  app.get('/', (req, res, next) => {
    try {
      throw new NotFoundException();
    } catch (err) {
      next(err);
    }
  });
};

const app = createApp();

app.run();

// GET / return 404

Responses

// Import Responses
const {
  responses: { SuccessReponse, CreatedResponse },
} = require('@ibrahimanshor/my-express');

// Usage
const { createApp } = require('@ibrahimanshor/my-express');

const myRouter = (app) => {
  app.get('/', (req, res) => {
    return new SuccessReponse().send(req, res);
  });
};

const app = createApp();

app.run();

// GET / return 200

Utils

Check

const {
  utils: {
    check: { isConflict, isForbidden, isNotFound, isUndefined },
  },
} = require('@ibrahimanshor/my-express');

const hasProblem = true;

isConflict(hasProblem); // throw ConflictException if argument is true
isForbidden(hasProblem); // throw ForbiddenException if argument is true
isNotFound(hasProblem); // throw NotFoundException if argument is true
isNotFound(hasProblem); // throw NotFoundException if argument is true
isUndefined(user); // return true if argument undefined

Converter

const {
  utils: {
    converter: { stringToBoolean },
  },
} = require('@ibrahimanshor/my-express');

stringToBoolean('true'); // true
stringToBoolean('false'); // false
stringToBoolean('error'); // false

Database

const {
  utils: {
    database: { paginate },
  },
} = require('@ibrahimanshor/my-express');

paginate({ page: 1, limit: 10 }); // { offset: 0, limit: 10 }
paginate({ page: 2, limit: 10 }); // { offset: 10, limit: 10 }
paginate({ page: 3, limit: 30 }); // { offset: 60, limit: 30 }

Request Query

const {
  utils: {
    query: { extractQueryOrder, extractQueryPage },
  },
} = require('@ibrahimanshor/my-express');

const getPostsRoute = (req, res, next) => {
  const order = extractQueryOrder(req.query);

  return res.json(order); // { order: { column: req.query.sort, direction: req.query.order } }
};
const getUserssRoute = (req, res, next) => {
  const page = extractQueryPage(req.query);

  return res.json(page); // { page: { page: req.query.page, limit: req.query.limit } }
};

Validator

const {
  utils: {
    validator: { confirmed, related },
  },
} = require('@ibrahimanshor/my-express');
const { body } = require('express-validator');

const postRequest = [
  body('password')
    .exists()
    .isString()
    .notEmpty()
    .confirmed('password_validation'),
  body('password_validation').exists().isString().notEmpty(),
  body('email').exists().isString().notEmpty().related('name'),
  body('name').exists().isString().notEmpty(),
];