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

ts-http-status-utils

v1.0.2

Published

HTTP status code declarations, descriptions and utils

Downloads

35

Readme

ts-http-status-utils :hammer_and_wrench: :computer: :toolbox:

HTTP status code declarations, status phrases, descriptions and util functions

Motivation

Working on different projects dealing with microservices or different responses from distributed APIs I noticed that some developers used to default failed responses to Http code 500 - Internal Server Error instead of an appropriate Http code making the debug process harder and misleading depending how you implement logging tools.

Taking advantage of TypeScript and Docstrings, this library helps developers to handle and understand HTTP responses easily as well making it easy to build, integrate or debug microservices or APIs.

How to use


Available enums

RequestMethod, StatusCode, StatusPhrase, StatusDescription, StatusLabel

RequestMethod

RequestMethod.GET;

// Return "GET"

RequestMethod.POST;

// Return "POST"

StatusCode

StatusCode.CONTINUE;

// Return 100

StatusCode.OK;

// Return 200

StatusPhrase

StatusPhrase.PROCESSING;

// Return "Processing"

StatusPhrase.MOVED_PERMANENTLY;

// Return "Moved Permanently"

StatusDescription

StatusDescription.CREATED;

// Return "The request succeeded, and a new resource was created as a result. This is typically the response sent after POST requests, or some PUT requests."

StatusLabel

StatusLabel.CREATED;

// Return "CREATED"

StatusLabel.ALREADY_REPORTED;

// Return "ALREADY_REPORTED"

Available functions

getStatusPhraseByCode, getStatusDescriptionByCode, makeHttpResponsesDictionary

getStatusPhraseByCode

getStatusPhraseByCode(StatusCode.PROXY_AUTHENTICATION_REQUIRED);

// Return "Proxy Authentication Required"

getStatusDescriptionByCode

getStatusDescriptionByCode(StatusCode.BAD_GATEWAY);

// Return "This error response means that the server, while working as a gateway to get a response needed to handle the request, got an invalid response."

makeHttpResponsesDictionary

Create a dictionary containing all HTTP responses

{
'100': {
    code: 100,
    phrase: 'Continue',
    description: 'This interim response indicates that the client should continue the request or ignore the response if the request is already finished.'
},
'101': {
    code: 101,
    phrase: 'Switching Protocols',
    description: 'This code is sent in response to an Upgrade request header from the client and indicates the protocol the server is switching to.'
},
    ...

'511': {
    code: 511,
    phrase: 'Network Authentication Required',
    description: 'Indicates that the client needs to authenticate to gain network access.'
}

const dictionary = makeHttpResponsesDictionary();

dictionary[StatusCode.OK];

/**
return
    '200': {
        code: 200,
        phrase: 'OK',
        description: 'The request succeeded. The result meaning of `success` depends on the HTTP method.'
    }
* /

How to use in a real project

import {
  StatusCode,
  getStatusPhraseByCode,
  RequestMethod,
  getStatusDescriptionByCode,
} from "ts-http-status-utils";

export const lambdaHandler = async (
  event: APIGatewayProxyEvent,
  context: Context
): Promise<APIGatewayProxyResult> => {
  try {
    // RequestMethod enum
    if (event.httpMethod !== RequestMethod.GET) {
      // getStatusPhraseByCode and getStatusDescriptionByCode util functions & StatusCode.METHOD_NOT_ALLOWED
      logError({
        method: event.httpMethod,
        statusCode: StatusCode.METHOD_NOT_ALLOWED,
        statusPhrase: getStatusPhraseByCode(StatusCode.METHOD_NOT_ALLOWED),
        statusDescription: getStatusDescriptionByCode(
          StatusCode.METHOD_NOT_ALLOWED
        ),
      });

      return {
        // StatusCode enum
        statusCode: StatusCode.METHOD_NOT_ALLOWED,
        headers: {},
        body: JSON.stringify({
          // getStatusPhraseByCode util function
          message: getStatusPhraseByCode(StatusCode.METHOD_NOT_ALLOWED),
        }),
      };
    }

    return {
      // StatusCode enum
      statusCode: StatusCode.OK,
      // getStatusPhraseByCode util function
      body: JSON.stringify({
        message: `${getStatusPhraseByCode(
          StatusCode.OK
        )} - Response from lambda`,
      }),
    };
  } catch (error) {
    // getStatusPhraseByCode and getStatusDescriptionByCode util functions
    logError({
      method: event.httpMethod,
      statusCode: error.statusCode,
      statusPhrase: getStatusPhraseByCode(error.statusCode),
      statusDescription: getStatusDescriptionByCode(error.statusCode),
    });

    return {
      statusCode: error.statusCode,
      body: JSON.stringify({
        message:
          error instanceof Error ? error.stack : JSON.stringify(error, null, 2),
      }),
    };
  }
};

Resources

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

https://datatracker.ietf.org/doc/html/rfc7231