standard-rest-response

Tools for building standard responses for REST APIs

Usage no npm install needed!

<script type="module">
  import standardRestResponse from 'https://cdn.skypack.dev/standard-rest-response';
</script>

README

NPM Version CI codecov

Standard REST Response

Tools for building standard responses for REST APIs.

Table of Contents

Features

  • 💥 Simple classes for http status codes.
  • 🚀 Middleware to catch errors.
  • 🔎 Detect http errors with instanceof.
  • 🏄‍♂️ Zero dependencies.
  • 🎉 Written in TypeScript.

Installation

npm install standard-rest-response --save

Usage

Errors

Predefined classes for common HTTP status codes.

import { BadRequest } from 'standard-rest-response';

throw new BadRequest(`That doesn't work`);
Status Class
400 BadRequest
401 Unauthorized
403 Forbidden
404 NotFound
405 MethodNotAllowed
406 NotAcceptable
408 RequestTimeout
409 Conflict
410 Gone
412 PreconditionFailed
413 PayloadTooLarge
415 UnsupportedMediaType
418 ImATeaPotSupported
421 Misdirected
422 UnprocessableEntity
500 InternalServerError
501 NotImplemented
502 BadGateway
503 ServiceUnavailable
504 GatewayTime
505 HttpVersionNotSupported

You can also use the base HttpError class to return any status.

import { HttpError } from 'standard-rest-response';

throw new HttpError(999, 'Something crazy!');

Middleware

Provide catch and response to HTTP errors with frameworks like ExpressJS.

Note: In Express v5, this works with routes that return a promise, like async functions. In older versions of Express, use express-async-errors.

import { catchHttpErrors, UnprocessableEntity } from 'standard-rest-response';

app.use(catchHttpErrors);

app.get('/', async (res, req) => {
  if (!res.user) {
    throw new Unauthorized('Nice try');
  }

  // ...
});

Development

npm install
npm run build