http-status-codes
Version:
Constants enumerating the HTTP status codes. Based on the Java Apache HttpStatus API.
42 lines (41 loc) • 1.45 kB
JavaScript
import { statusCodeToReasonPhrase, reasonPhraseToStatusCode, } from './utils';
/**
* Returns the reason phrase for the given status code.
* If the given status code does not exist, an error is thrown.
*
* @param {number|string} statusCode The HTTP status code
* @returns {string} The associated reason phrase (e.g. "Bad Request", "OK")
* */
export function getReasonPhrase(statusCode) {
var result = statusCodeToReasonPhrase[statusCode.toString()];
if (!result) {
throw new Error("Status code does not exist: " + statusCode);
}
return result;
}
/**
* Returns the status code for the given reason phrase.
* If the given reason phrase does not exist, undefined is returned.
*
* @param {string} reasonPhrase The HTTP reason phrase (e.g. "Bad Request", "OK")
* @returns {string} The associated status code
* */
export function getStatusCode(reasonPhrase) {
var result = reasonPhraseToStatusCode[reasonPhrase];
if (!result) {
throw new Error("Reason phrase does not exist: " + reasonPhrase);
}
return result;
}
/**
* @deprecated
*
* Returns the reason phrase for the given status code.
* If the given status code does not exist, undefined is returned.
*
* Deprecated in favor of getReasonPhrase
*
* @param {number|string} statusCode The HTTP status code
* @returns {string|undefined} The associated reason phrase (e.g. "Bad Request", "OK")
* */
export var getStatusText = getReasonPhrase;