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

response-objects

v4.1.0

Published

simple value objects representing HTTP responses

Downloads

1,929

Readme

response-objects

Value objects representing HTTP responses

npm i -S response-objects

Requires Node.js 4+

This package is general-purpose, but is particularly useful with koa-detour. Put simply, the idea is to raise the level of abstraction by having routes or resources return objects or throw errors that represent HTTP responses, rather than imperatively doing things like res.write() (or in Koa ctx.body = ...)

// this isn't a complete working example, it just shows the concepts
import Koa from "koa";
import Router from "koa-detour";
import R from "response-objects";

const app = new Koa()
const router = new Router();
router.route("/user/:id", {
  async GET (ctx) {
    if (!areUsersConnected(ctx.userId, ctx.params.id)) {
      // router handles sending this as a 403
      throw R.Forbidden("You are not connected to that user")
    }

    // router handles sending this as a 200
    return R.Ok(await getUser())
  }
});
app.use(router.middleware());

Available methods:

R(status, body, headers)
R.Continue(body, headers)
R.SwitchingProtocols(body, headers)
R.Processing(body, headers)
R.OK(body, headers)
R.Created(body, headers)
R.Accepted(body, headers)
R.NonAuthoritativeInformation(body, headers)
R.NoContent(body, headers)
R.ResetContent(body, headers)
R.PartialContent(body, headers)
R.MultiStatus(body, headers)
R.AlreadyReported(body, headers)
R.IMUsed(body, headers)
R.MultipleChoices(body, headers)
R.MovedPermanently(body, headers)
R.Found(body, headers)
R.SeeOther(body, headers)
R.NotModified(body, headers)
R.UseProxy(body, headers)
R.TemporaryRedirect(body, headers)
R.PermanentRedirect(body, headers)
R.BadRequest(body, headers)
R.Unauthorized(body, headers)
R.PaymentRequired(body, headers)
R.Forbidden(body, headers)
R.NotFound(body, headers)
R.MethodNotAllowed(body, headers)
R.NotAcceptable(body, headers)
R.ProxyAuthenticationRequired(body, headers)
R.RequestTimeout(body, headers)
R.Conflict(body, headers)
R.Gone(body, headers)
R.LengthRequired(body, headers)
R.PreconditionFailed(body, headers)
R.PayloadTooLarge(body, headers)
R.URITooLong(body, headers)
R.UnsupportedMediaType(body, headers)
R.RangeNotSatisfiable(body, headers)
R.ExpectationFailed(body, headers)
R.MisdirectedRequest(body, headers)
R.UnprocessableEntity(body, headers)
R.Locked(body, headers)
R.FailedDependency(body, headers)
R.UnorderedCollection(body, headers)
R.UpgradeRequired(body, headers)
R.PreconditionRequired(body, headers)
R.TooManyRequests(body, headers)
R.RequestHeaderFieldsTooLarge(body, headers)
R.UnavailableForLegalReasons(body, headers)
R.InternalServerError(body, headers)
R.NotImplemented(body, headers)
R.BadGateway(body, headers)
R.ServiceUnavailable(body, headers)
R.GatewayTimeout(body, headers)
R.HTTPVersionNotSupported(body, headers)
R.VariantAlsoNegotiates(body, headers)
R.InsufficientStorage(body, headers)
R.LoopDetected(body, headers)
R.BandwidthLimitExceeded(body, headers)
R.NotExtended(body, headers)
R.NetworkAuthenticationRequired(body, headers)