npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hitokoto/koa-respond

v3.0.3

Published

Add useful methods to the Koa context for sending a response

Downloads

9

Readme

koa-respond

npm Build Status Coverage Status license JavaScript Style Guide

Middleware for Koa that adds useful methods to the Koa context.

Installation

npm install --save koa-respond

Usage

// Install it
const respond = require('koa-respond');

// For Koa v2 - if you are looking for v1, scroll to the bottom.
app.use(respond());

// Use it
app.use((ctx) => {
  // Sets status to 200 and the body to `{ id: 123, name: 'Dat Boi' }`
  ctx.ok({ id: 123, name: 'Dat Boi' });

  // Both of these set status to 404 and
  // the body to `{ message: 'Not found, boii' }`
  ctx.notFound('Not found, boii');
  ctx.notFound({ message: 'Not found, boii' });

  // And everyone's favorite..
  ctx.badRequest({ error: 'missing input' });

  // Or if you prefer to do it yourself..
  // Both of these send a HTTP 201 with a body
  // of `{ message: 'new beginnings!' }`
  ctx.send(201, 'new beginnings!');
  ctx.send(201, { message: 'new beginnings!' });
});

Methods

All methods call the send method with the corresponding status code as well as the body. That means they support the same overloads as send:

  • With a string; wraps it in an object with a message property. That means the following 2 calls do the same thing:

    ctx.send(400, 'lol no');
    ctx.send(400, { message: 'lol no' });
  • With an object; sends the object as JSON.

    ctx.send(200, { id: 123, name: 'new entity' });

If you wish to disable the automatic wrapping of strings globally, you can instantiate koa-respond with autoMessage: false.

app.use(respond({
  autoMessage: false
}))

All functions return the Koa context itself (chainable)

ctx.ok().set({ 'X-Some-Header': 'awesome' })

All functions are also bound to the context. This means you can pass the function as a reference without having to bind it first.

app.use((ctx) => somePromiseCall().then(ctx.ok))

Available methods

  • ok - HTTP 200
  • created - HTTP 201
  • noContent - HTTP 204 - always sends an empty response!
  • badRequest - HTTP 400
  • unauthorized - HTTP 401
  • forbidden - HTTP 403
  • notFound - HTTP 404
  • locked - HTTP 423
  • internalServerError - HTTP 500
  • notImplemented - HTTP 501

Does this work for Koa 1?

Not out of the box, because it's time you move on to v2.

To use koa-respond in Koa v1, you need to patch the context yourself. This is what the v2 middleware does.

const respond = require('koa-respond');

// Middleware to install koa-respond.
app.use(function *(next) {
  respond().patch(this);
  yield next;
});

// Now the methods are available.
app.use(function *() {
  this.ok({ id: 123, name: 'Bob' });
});

Adding additional methods

If you feel like some methods are missing, you can add them yourself, like so:

app.use(respond({
  statusMethods: {
    imATeapot: 418,
    enhanceYourCalm: 420
  }
}));

app.use((ctx) => {
  ctx.imATeapot('Hello, a Teapot I am.');
  ctx.enhanceYourCalm({ todo: 'blaze it' });
});

Even more custom methods

If you just want to add shortcuts without adding an additional middleware, you can do that, too.

app.use(respond({
  methods: {
    shizzle: (ctx, message) => {
      ctx.send(200, message + ', fo-shizzle');
    }
  }
}));

app.use((ctx) => {
  // HTTP 200 { message: 'Koa is the best, fo-shizzle' }
  ctx.shizzle('Koa is the best');
});

Contributing

npm run scripts

  • npm run test: Runs tests once
  • npm run test-watch: Runs tests in watch-mode
  • npm run lint: Lints the code once
  • npm run lint-watch: Lints the code in watch-mode
  • npm run cover: Runs code coverage using istanbul
  • npm run coveralls: Used by coveralls

Author

Jeff Hansen - @Jeffijoe