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

http-response-helpers

v1.5.0

Published

An error for each HTTP status code that you can throw as well as an HTTPRedirect class.

Downloads

141

Readme

http-response-helpers

In this module:

  • Error classes for each 4xx and 5xx http status code.
  • An HTTP redirect class that can be returned to inform your router to redirect somewhere
  • Type guard helper classes to determine if an unknown variable is an HTTP error or HTTP redirect.

Usage

function route() {
    throw new HTTPError.BadRequest('INVALID_EMAIL', { email: '@bademail' });
}

try {
    const result = route();
    if(isHTTPRedirect(result)) {
        // redirect to result.location with status result.status
    }
    else {
        // do something else
    }
}
catch(error) {
    if(isHTTPError(error)) {
        console.log('got an http error');
    }
}

Helper Classes

isHTTPError(maybeHTTPError: unknown, code?: HTTPErrorCode): : maybeHTTPError is HTTPError

Returns true if maybeHTTPError is a valid HTTPError and provides a type guard to that effect.

  • maybeHTTPError: any variable that you want to confirm is a HTTPError or not.
  • code: optional HTTPErrorCode (4xx or 5xx). If this parameter is specified then the function will only return true if maybeHTTPError is an HTTPError and matches the specified code.
isHTTPRedirect(maybeHTTPRedirect: unknown, status?: HTTPRedirect): : maybeHTTPRedirect is HTTPRedirect

Returns true if maybeHTTPRedirect is a valid HTTPRedirect and provides a type guard to that effect.

  • maybeHTTPRedirect: any variable that you want to confirm is a HTTPRedirect or not.
  • status: optional number (3xx). If this parameter is specified then the function will only return true if maybeHTTPRedirect is an HTTPRedirect with a status that matches the specified status.

HTTPRedirect

new HTTPRedirect(location: string, status: number = 302);

The HTTPRedirect object is a special object you can return from your routes that notifies the server to redirect to a specific location with the specified redirect status code.

  • location: a string that specifies where you should be redirected to.
  • status: optional number (default 302). If this parameter is specified then the function will only return true if maybeHTTPRedirect is an HTTPRedirect with a status that matches the specified status.

HTTPError

new HTTPError.BadRequest(message: string, params: Jsonifiable = null)

The HTTPError object is a special object you can return from your routes that notifies the server which type of error should be returned fro the route.

  • message: a string that specifies which message (in addition to the error code) should be returned with the error.
  • params: optional JSON object. This is a way to pass additional information back with the error apart from the error code and message.

Supported classes

  • HTTPError.BadRequest
  • HTTPError.Unauthorized
  • HTTPError.PaymentRequired
  • HTTPError.Forbidden
  • HTTPError.NotFound
  • HTTPError.MethodNotAllowed
  • HTTPError.NotAcceptable
  • HTTPError.ProxyAuthenticationRequired
  • HTTPError.RequestTimeout
  • HTTPError.Conflict
  • HTTPError.Gone
  • HTTPError.LengthRequired
  • HTTPError.PreconditionFailed
  • HTTPError.PayloadTooLarge
  • HTTPError.URITooLong
  • HTTPError.UnsupportedMediaType
  • HTTPError.RangeNotSatisfiable
  • HTTPError.ExpectationFailed
  • HTTPError.MisdirectedRequest
  • HTTPError.UnprocessableEntity
  • HTTPError.Locked
  • HTTPError.FailedDependency
  • HTTPError.TooEarly
  • HTTPError.UpgradeRequired
  • HTTPError.PreconditionRequired
  • HTTPError.TooManyRequests
  • HTTPError.RequestHeaderFieldsTooLarge
  • HTTPError.UnavailableForLegalReasons
  • HTTPError.InternalServerError
  • HTTPError.NotImplemented
  • HTTPError.BadGateway
  • HTTPError.ServiceUnavailable
  • HTTPError.GatewayTimeout
  • HTTPError.HTTPVersionNotSupported
  • HTTPError.VariantAlsoNegotiates
  • HTTPError.InsufficientStorage
  • HTTPError.LoopDetected
  • HTTPError.NotExtended
  • HTTPError.NetworkAuthenticationRequired