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

@poppanator/http-constants

v3.0.0

Published

This package contains various HTTP constants: http status codes and texts, HTTP header names and HTTP methods, and a few related utility function.

Downloads

328

Readme

HTTP Constants

This package contains various HTTP constants: http status codes and texts, HTTP header names and HTTP methods, and a few related utility function.

Install

# NPM
npm i @poppanator/http-constants

# Yarn
yarn add @poppanator/http-constants

Usage

The following symbols are exported

status

Contains all possible HTTP status codes

import { status } from '@poppanator/http-constants'

status.Continue // 100
status.Ok // 200
status.Created // 201
// ...
status.NotFound // 404
// ...
status.InternalServerError // 500
// ... and so on

statusText

Contains all possible status texts

import { statusText } from '@poppanator/http-constants'

statusText.Continue // Continue
statusText.Ok // OK
statusText.Created // Created
// ...
statusText.NotFound // Not Found
// ...
statusText.InternalServerError // Internal Server Error
// ... and so on

tuple

Contains all status codes and texts as tuples where the first index is the status code and the second index the status text.

import { tuple } from '@poppanator/http-constants'

const [code, text] = tuple.Unauthorized // [401, 'Unauthorized']
const [code, text] = tuple.NotFound // [404, 'Not Found']

header

Contains all standardizes HTTP header names (and a few non- but defacto-standard ones).

import { header } from '@poppanator/http-constants'

header.AcceptEncoding // Accept-Encoding
header.IfModifiedSince // If-Modified-Since
header.AccessControlAllowMethods // Access-Control-Allow-Methods

method

Contains all standardized HTTP method names.

import { method, type Method } from '@poppanator/http-constants'

method.Post // POST
method.Options // OPTIONS

// ...

interface MyHttpInterface {
  method: Method
}

const http: MyHttpInterface = {
  method: 'FAKE', // Error
}

const http: MyHttpInterface = {
  method: 'POST', // OK
}

Utility functions

Some utility functions are exported from the root of the package

import {
  isInformational,
  isSuccess,
  isRedirect,
  isClientError,
  isServerError,
  isHttpStatus,
  getStatusText,
} from '@poppanator/http-constants'

// True for status codes in the 100-199 range
isInformational(100) // true
isInformational(102) // true
isInformational(201) // false
isInformational(403) // false

// True for status codes in the 200-299 range
isSuccess(200) // true
isSuccess(202) // true
isSuccess(100) // false
isSuccess(302) // false

// True for status codes in the 300-399 range
isRedirect(301) // true
isRedirect(307) // true
isRedirect(200) // false
isRedirect(500) // false

// True for status codes in the 400-499 range
isClientError(404) // true
isClientError(406) // true
isClientError(500) // false
isClientError(302) // false

// True for status codes in the 500-599 range
isServerError(500) // true
isServerError(503) // true
isServerError(200) // false
isServerError(308) // false

// True if the value is a known HTTP status code
isHttpStatus(201) // true
isHttpStatus(404) // true
isHttpStatus(503) // true
isHttpStatus(261) // false
isHttpStatus(210) // false
isHttpStatus(444) // false

// Returns the status code's corresponding status text
getStatusText(100) // Continue
getStatusText(201) // Created
getStatusText(304) // Not Modified
getStatusText(405) // Method Not Allowed
getStatusText(500) // Internal Server Error