Code Monkey home page Code Monkey logo

koa-7xx-statuses's Introduction

Koa 7xx Statuses

NPM version node version npm download

Are your koajs REST APIs just not ‘expressive’ enough? Do you spend your days wishing you could be truly transparent in how you represent failed HTTP calls to your consumers? Well, now you can! With koa-7xx-statuses!

Leverage some of those good old standbys such as:

  • 735: Fucking IE
  • 745: I don’t always test my code, but when I do, I do it in production
  • 747: Motherfucking Snakes on the Motherfucking Plane!
  • 799: The End of the World

…and many, many more!

(Full list can be found @ RFC for the 7XX Range of HTTP Status codes - Developer Errors)

The module definition is extremely simple. Just mount the module onto the app using koa-mount and provide additional codes as a JSON object if you wish.

Most credit goes to Srinivas Iyer who originally wrote the fantastic package called koa-custom-statuses which i simply improved upon. Thank you Srini!

const Koa = require('koa');
const mount = require('koa-mount');
const k7xxStatuses = require('koa-7xx-statuses');
const app = new Koa()

const developerStatusCodes = new k7xxStatuses();
app.use(mount(developerStatusCodes));

app.use(async (ctx, next) => {
        await next();
        ctx.status = 799;
});

Of course, if you wish to overwrite, or add your own status codes, just provide them as an argument:

const Koa = require('koa');
const mount = require('koa-mount');
const k7xxStatuses = require('koa-7xx-statuses');
const app = new Koa()

// Cause you feel like it
optionalCodes = {
                    '200': 'Im Loving it!!',
                    '499': 'Cos I can come that close to 500',
                    '999': 'Polish Programmer'
                };

const developerStatusCodes = new k7xxStatuses(optionalCodes);

There are just some basics you need to keep in mind for OPTIONALLY ADDED codes:

  • The status codes can be 3 digit numbers only.
  • The codes you declare will overwrite the Koa Defaults.
  • You can only use default Koa codes or the codes defined in the module. Any others will throw an error

License

MIT

koa-7xx-statuses's People

Contributors

darekulti avatar murac avatar srinivasiyer avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.