3.8.1 • Published 4 years ago

express-mate v3.8.1

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

express-mate

Helper library that makes your life a little easier when working with Express REST APIs

Setup

The express-mate library is designed to be 'plug-and-play'. It exposes a bunch of helper functions and classes that can be used in your project however you like.

Error Handler

The custom express-mate error handler catches ApiObjects thrown in next(err) and handles them appropriately by responding to the client.

import { errorHandler } from 'express-mate';

express.use(errorHandler());

Handlers

Handlers are express request handlers that handle the express-mate repsonse objects. createHandler creates a handler function that can be added to an express router.

import { createHandler } from 'express-mate';

export const helloWorld = createHandler((req, res) => {
  return new ApiSuccess(res, 'Hello World!');
  /**
   * Response:
   * {
   *   "status": "success",
   *   "data": "Hello World!"
   * }
   */
});

// Add the handler to the express router
router.get('/hello-world', helloWorld);

Hooks

Hooks are functions that 'hook' into a express router with a endpoints all starting at a specified endpoint.

import { createHook } from 'express-mate';

export const helloWorldGet = createHandler((req, res) => {
  return new ApiSuccess(res, 'Hello World!');
  /**
   * Response:
   * {
   *   "status": "success",
   *   "data": "Hello World!"
   * }
   */
});

export const helloWorld = createHook('/hello-world', router => {
  // Endpoint: /hello-world/hello
  router.get('/hello', helloWorldGet);
});

// Add the hook to the express router
router.use('/', helloWorld);

Helpers

checkReq

The checkReq function checks the express request handlers req object for variables that may have been added to it and want to be accessed safely.

import { createHandler, checkReq } from 'express-mate';

export const helloWorld = createHandler((req, res) => {
  const hello = checkReq('hello', req);
  // If hello exists: hello = req.hello
  // Else if hello not found: throw Error('Expected req.hello');
});

Response Objects

All response objects follow the JSend guidelines.

  • ApiSuccess
import { ApiSuccess } from 'express-mate';

router.post('/hello-world', (req, res, next) => {
  try {
    return ApiSuccess.respond(res, 'Hello World!');
  } catch (err) {
    return next(err);
  }
});

/**
 * Response:
 *
 * HTTP 200
 * {
 *   "status": "success",
 *   "data": "Hello World!"
 * }
 */
  • ApiError
import { ApiError } from 'express-mate';

router.post('/hello-world', (req, res, next) => {
  try {
    throw new ApiError(res, 'Hello World!');
  } catch (err) {
    return next(err);
  }
});

/**
 * Response:
 *
 * HTTP 500
 * {
 *   "status": "error",
 *   "message": "Hello World!"
 * }
 */
  • ApiFail
  • ApiForbidden
  • ApiUnauthorized
  • ApiNotFound
3.8.1

4 years ago

3.11.4

4 years ago

3.11.2

4 years ago

3.11.1

4 years ago

3.11.0

4 years ago

3.10.1

4 years ago

3.10.0

4 years ago

3.9.2

4 years ago

3.9.1

4 years ago

3.9.0

4 years ago

3.8.0

4 years ago

3.7.2

4 years ago

3.7.1

4 years ago

3.6.1

4 years ago

3.6.0

4 years ago

3.7.0

4 years ago

3.5.0

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.6

4 years ago

3.2.5

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.2

5 years ago

3.2.1

5 years ago

3.2.0

5 years ago

3.1.0

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.4

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

1.6.13

5 years ago

1.6.12

5 years ago

1.6.11

5 years ago

1.6.10

5 years ago

1.6.9

5 years ago

1.6.8

5 years ago

1.6.7

5 years ago

1.6.6

5 years ago

1.6.5

5 years ago

1.6.4

5 years ago

1.6.3

5 years ago

1.6.2

5 years ago

1.6.1

5 years ago

1.6.0

5 years ago

1.5.4

5 years ago

1.5.3

5 years ago

1.5.2

5 years ago

1.5.1

5 years ago

1.5.0

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.1.10

6 years ago

1.1.9

6 years ago

1.1.8

6 years ago

1.1.7

6 years ago

1.1.6

6 years ago

1.1.5

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago