Wondering what’s next for npm?Check out our public roadmap! »

    standard-rest-response
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.1 • Public • Published

    NPM Version CI codecov Dependency Status Dev Dependency Status

    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
    

    Install

    npm i standard-rest-response

    DownloadsWeekly Downloads

    17

    Version

    1.0.1

    License

    MIT

    Unpacked Size

    65.9 kB

    Total Files

    88

    Last publish

    Collaborators

    • avatar