0.0.28 • Published 2 years ago

dry-express-responses v0.0.28

Weekly downloads
-
License
ISC
Repository
github
Last release
2 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

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.4-alpha.0

2 years ago

0.0.3

2 years ago