0.0.28 • Published 3 years ago

dry-express-responses v0.0.28

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

A small ExpressJS middleware written in TypeScript that wraps around http-status-codes to send consistent responses, reducing the instances of res.status, res.send and having to pass a status code and its associated message.

How to use

Step 1

Create a file in src called dry-express-responses.d.ts and copy the following content, this is required for the types to work.

import {ResponsePayload} from 'dry-express-responses';

declare global {
	declare namespace Express {
		export interface Response {
			ok(payload: Omit<ResponsePayload, 'errors'>): void;

			created(payload: Omit<ResponsePayload, 'errors'>): void;

			badRequest(payload: ResponsePayload): void;

			unauthorized(payload: ResponsePayload): void;

			forbidden(payload: ResponsePayload): void;

			notFound(payload: ResponsePayload): void;

			internalServerError(payload: ResponsePayload): void;
		}
	}
}

Step 2

Register the middleware.

import {dryExpressResponses} from "./dry-express-responses";
import express from 'express';

const app = express();

app.use(dryExpressResponses);

Example usage

app.get('/', (req, res) => {
	res.badRequest({
		data: {
			email: 'foo@bar.com',
		},
		message: 'This is a bad request',
		errors: [
			{
				field: 'email',
				message: 'invalid email',
			},
		],
	});
});
0.0.28

3 years ago

0.0.27

3 years ago

0.0.26

3 years ago

0.0.24

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.21

3 years ago

0.0.20

3 years ago

0.0.19

3 years ago

0.0.18

3 years ago

0.0.17

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.4-alpha.0

3 years ago

0.0.3

3 years ago