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

@sharingbox/http-status

v1.1.12

Published

Send, receive and check HTTP statuses in JavaScript

Downloads

35

Readme

http-status

Send, receive and check HTTP statuses in JavaScript

npm version

Installation

npm install @sharingbox/http-status --save

Usage

Browser

import httpStatus from '@sharingbox/http-status/dist/browser';

const httpResponse = httpStatus.formatResponse(response.status, response.data, null);

httpStatus.isOK(httpResponse.statusText); // boolean
httpStatus.isOK(httpResponse.status); // boolean
httpStatus.isOK(httpResponse); // boolean
import httpStatus from '@sharingbox/http-status/dist/browser';

const MAX_PEOPLE = 50;

const data              = {people: {total: 250}};
const httpResponse      = httpStatus.formatResponse(200, data); // {status: 200, statusText: 'OK', data}
const checkResponseData = httpStatus.checkResponseData(httpResponse);

checkResponseData.expect('people.total').toBeLessThan(MAX_PEOPLE); // false
checkResponseData.expect('people.total').toBeTypeof('number'); // true
<script src="../node_modules/@sharingbox/http-status/dist/browser.js"></script>
<script>

	console.log(httpStatus.isInformative(100)); // true
	console.log(httpStatus.isSuccess(200)); // true

</script>

Node.js

const httpStatus = require('@sharingbox/http-status/dist/node');

const httpResponse = httpStatus.responseOK(results);

res.status(httpResponse.status).send(httpResponse.data); // res.status(200).send({status: 200, statusText: 'OK', data: results, error: null});

Methods

Generated from the standard http status codes

Determine the type of the HTTP response

Each of these methods must be called with 1 parameter. This parameter can be the status (number) or the statusText (string) of the analyzed HTTP response, or the HTTP response itself. They return a boolean.

These methods are used to determine the type of the HTTP response: informative, success, redirection, client error, server error.

  • isInformative
  • isSuccess
  • isRedirection
  • isClientError
  • isServerError
import httpStatus from '@sharingbox/http-status/dist/browser';

const response = {status: 405, statusText: 'Method Not Allowed'};

httpStatus.isClientError(response); // true
httpStatus.isClientError(response.status); // true
httpStatus.isClientError(response.statusText); // true
import httpStatus from '@sharingbox/http-status/dist/browser';

const response = {status: 201, statusText: 'Created'};

httpStatus.isSuccess(response); // true
httpStatus.isSuccess(response.status); // true
httpStatus.isSuccess(response.statusText); // true

Determine the exact status of the HTTP response

Each of these methods must be called with 1 parameter. This parameter can be the status (number) or the statusText (string) of the analyzed HTTP response, or the HTTP response itself. They return a boolean.

These methods allow you to check the exact status of the HTTP response.

  • isContinue
  • isSwitchingProtocol
  • isProcessingWebDAV
  • isOK
  • isCreated
  • isAccepted
  • isNonAuthoritativeInformation
  • isNoContent
  • isResetContent
  • isPartialContent
  • isMultiStatusWebDAV
  • isIMUsedHTTPDeltaencoding
  • isMultipleChoice
  • isMovedPermanently
  • isFound
  • isSeeOther
  • isNotModified
  • isUseProxy
  • isunused
  • isTemporaryRedirect
  • isPermanentRedirect
  • isBadRequest
  • isUnauthorized
  • isPaymentRequired
  • isForbidden
  • isNotFound
  • isMethodNotAllowed
  • isNotAcceptable
  • isProxyAuthenticationRequired
  • isRequestTimeout
  • isConflict
  • isGone
  • isLengthRequired
  • isPreconditionFailed
  • isPayloadTooLarge
  • isURITooLong
  • isUnsupportedMediaType
  • isRequestedRangeNotSatisfiable
  • isExpectationFailed
  • isImateapot
  • isMisdirectedRequest
  • isUnprocessableEntityWebDAV
  • isLockedWebDAV
  • isFailedDependencyWebDAV
  • isUpgradeRequired
  • isPreconditionRequired
  • isTooManyRequests
  • isRequestHeaderFieldsTooLarge
  • isUnavailableForLegalReasons
  • isInternalServerError
  • isNotImplemented
  • isBadGateway
  • isServiceUnavailable
  • isGatewayTimeout
  • isHTTPVersionNotSupported
  • isVariantAlsoNegotiates
  • isInsufficientStorage
  • isLoopDetectedWebDAV
  • isNotExtended
  • isNetworkAuthenticationRequired
import httpStatus from '@sharingbox/http-status/dist/browser';

const response = {status: 200, statusText: 'OK'};

httpStatus.isOK(response); // true
httpStatus.isOK(response.status); // true
httpStatus.isOK(response.statusText); // true
import httpStatus from '@sharingbox/http-status/dist/browser';

const response = {status: 400, statusText: 'Bad Request'};

httpStatus.isBadRequest(response); // true
httpStatus.isBadRequest(response.status); // true
httpStatus.isBadRequest(response.statusText); // true

Format a specific HTTP response

Each of these methods can be called with 2 parameters (optional):

  • data: optional
  • error: optional

The method returns the formalized HTTP response: {status: number, statusText: string, data: any, error: any}

  • responseContinue
  • responseSwitchingProtocol
  • responseProcessingWebDAV
  • responseOK
  • responseCreated
  • responseAccepted
  • responseNonAuthoritativeInformation
  • responseNoContent
  • responseResetContent
  • responsePartialContent
  • responseMultiStatusWebDAV
  • responseIMUsedHTTPDeltaencoding
  • responseMultipleChoice
  • responseMovedPermanently
  • responseFound
  • responseSeeOther
  • responseNotModified
  • responseUseProxy
  • responseunused
  • responseTemporaryRedirect
  • responsePermanentRedirect
  • responseBadRequest
  • responseUnauthorized
  • responsePaymentRequired
  • responseForbidden
  • responseNotFound
  • responseMethodNotAllowed
  • responseNotAcceptable
  • responseProxyAuthenticationRequired
  • responseRequestTimeout
  • responseConflict
  • responseGone
  • responseLengthRequired
  • responsePreconditionFailed
  • responsePayloadTooLarge
  • responseURITooLong
  • responseUnsupportedMediaType
  • responseRequestedRangeNotSatisfiable
  • responseExpectationFailed
  • responseImateapot
  • responseMisdirectedRequest
  • responseUnprocessableEntityWebDAV
  • responseLockedWebDAV
  • responseFailedDependencyWebDAV
  • responseUpgradeRequired
  • responsePreconditionRequired
  • responseTooManyRequests
  • responseRequestHeaderFieldsTooLarge
  • responseUnavailableForLegalReasons
  • responseInternalServerError
  • responseNotImplemented
  • responseBadGateway
  • responseServiceUnavailable
  • responseGatewayTimeout
  • responseHTTPVersionNotSupported
  • responseVariantAlsoNegotiates
  • responseInsufficientStorage
  • responseLoopDetectedWebDAV
  • responseNotExtended
  • responseNetworkAuthenticationRequired
const httpStatus = require('@sharingbox/http-status/dist/node');

const httpResponse = httpStatus.responseOK(results);

res.status(httpResponse.status).send(httpResponse.data); // res.status(200).send({status: 200, statusText: 'OK', data: results, error: null});
const httpStatus = require('@sharingbox/http-status/dist/node');

const httpResponse = httpStatus.responseUnauthorized();

res.status(httpResponse.status).send(httpResponse.data); // res.status(401).send({status: 401, statusText: 'Unauthorized', data: null, error: null});

Check HTTP response data content

import httpStatus from '@sharingbox/http-status/dist/browser';

const data              = {a: 'a', b: {b1: {b1a: 'b1a', b1b: 'b1b'}}, c: 'c', d: 'd', e: 10};
const httpResponse      = httpStatus.formatResponse(200, data); // {status: 200, statusText: 'OK', data}
const checkResponseData = httpStatus.checkResponseData(httpResponse);

const check1 = checkResponseData.expect('b.b1.b1a').toBeEqualTo('b1a');
const check2 = checkResponseData.expect('e').toBeEqualTo(10);
const check3 = checkResponseData.expect('e').toBeGreaterThan(9);
const check4 = checkResponseData.expect('e').toBeLessThan(11);
const check5 = checkResponseData.expect('e').toBeTypeof('number');
const check6 = checkResponseData.expect('b.b1.b1a').toMatch(/b1a/u);
const check7 = checkResponseData.expect('b.b1.b1c').toBeNull();

checkResponseData

checkResponseData instantiate a class (HttpCheck). The accessible methods are:

  • toBeEqualTo
  • toBeGreaterThan
  • toBeLessThan
  • toBeTypeof
  • toMatch
  • toBeNull
checkResponseData(response: httpResponse): HttpCheck{

	return new HttpCheck(response);

}

Other methods

findStatus

findStatus must be called with 1 parameter:

  • key: this parameter can be the status (number) or the statusText (string) of the analyzed HTTP response, or the HTTP response itself.

findStatus returns the formalized status code: {status: number, statusText: string}.

findStatus(key: httpStatusKey | httpResponse): httpStatusCode

formatResponse

formatResponse must be called with 3 parameters.

  • key: this parameter can be the status (number) or the statusText (string) of the analyzed HTTP response, or the HTTP response itself.
  • data: optional
  • error: optional

formatResponse returns the formalized HTTP response: {status: number, statusText: string, data: any, error: any}.

formatResponse(key: httpStatusKey, data: any, error: any): httpResponse{

		const code = this.findStatus(key);

		return{

			status    : code.status,
			statusText: code.statusText,
			data,
			error

		};

	}