@nicolasmondain/http-status

1.1.14 • Public • Published

http-status

Send, receive and check HTTP statuses in JavaScript

Version Downloads Contributors Issues License

Installation

npm install @nicolasmondain/http-status --save

Usage

Browser

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

const URL  = '';
const BODY = {};

window.fetch(URL, {

	method : 'POST',
	body   : JSON.stringify(BODY),
	headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}

})
.then((httpResponse) => httpResponse.json()).then((httpResponse) => {

	if(httpStatus.isSuccess(httpResponse.status)){

		//

	}else if(httpStatus.isClientError(httpResponse.status)){

		//

	}else if(httpStatus.isServerError(httpResponse.status)){

		//

	}else{

		//

	}

})
.catch((error) => {

	reject(error);

});
import httpStatus from '@nicolasmondain/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 '@nicolasmondain/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/@nicolasmondain/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('@nicolasmondain/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 '@nicolasmondain/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 '@nicolasmondain/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 '@nicolasmondain/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 '@nicolasmondain/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('@nicolasmondain/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('@nicolasmondain/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 '@nicolasmondain/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

		};

	}

Package Sidebar

Install

npm i @nicolasmondain/http-status

Weekly Downloads

0

Version

1.1.14

License

MIT

Unpacked Size

89.4 kB

Total Files

18

Last publish

Collaborators

  • nicolasmondain