http-response-status-code
Version:
A lightweight utility for retrieving HTTP status codes, names, and descriptions. Easily validate, categorize, and manage HTTP responses with built-in methods for informational, success, redirection, client, and server error codes.
74 lines (73 loc) • 3.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.listCodes = exports.mapCodesToDescriptions = exports.mapNamesToCodes = exports.mapCodesToNames = void 0;
/**
* A mapping of HTTP status codes to their respective status names.
* @param {IHttpStatusMappings} httpStatusMappings - The HTTP status code mappings.
* @returns {IHttpStatusNames} A mapping of HTTP status codes to their respective status names.
* @type {Object<number, string>}
*/
function mapCodesToNames(httpStatusMappings) {
var result = {};
for (var key in httpStatusMappings) {
if (Object.prototype.hasOwnProperty.call(httpStatusMappings, key)) {
result[httpStatusMappings[key].code] = key;
}
}
return Object.freeze(result);
}
exports.mapCodesToNames = mapCodesToNames;
/**
* A mapping of HTTP status code names to their respective status codes.
* @param {IHttpStatusMappings} httpStatusMappings - The HTTP status code mappings.
* @returns {IHttpStatusCodes} A mapping of HTTP status code names to their respective status codes.
* @type {Object<string, number>}
*/
function mapNamesToCodes(httpStatusMappings) {
var result = {};
for (var key in httpStatusMappings) {
if (Object.prototype.hasOwnProperty.call(httpStatusMappings, key)) {
result[key] = httpStatusMappings[key].code;
}
}
return Object.freeze(result);
}
exports.mapNamesToCodes = mapNamesToCodes;
/**
* A mapping of HTTP status codes to their respective status descriptions.
* @param {IHttpStatusMappings} httpStatusMappings - The HTTP status code mappings.
* @returns {IHttpStatusDescriptions} A mapping of HTTP status codes to their respective status descriptions.
* @type {Object<number, string>}
*/
function mapCodesToDescriptions(httpStatusMappings) {
var result = {};
for (var key in httpStatusMappings) {
if (Object.prototype.hasOwnProperty.call(httpStatusMappings, key)) {
result[httpStatusMappings[key].code] =
httpStatusMappings[key].description;
}
}
return Object.freeze(result);
}
exports.mapCodesToDescriptions = mapCodesToDescriptions;
/**
* Provides a list of HTTP status codes for a given type.
* @param {IClassLimits} classLimits - The class limits for HTTP status codes.
* @param {typeof CODES} codesEnum - The enumeration of HTTP status codes.
* @param {string} type - The type of HTTP status code to list.
* @returns {(ICodeList | Error)} A list of HTTP status codes for the given type, or an error if the type does not exist.
* @type {Object<number, string>}
*/
function listCodes(classLimits, codesEnum, type) {
var codes = [];
if (Object.prototype.hasOwnProperty.call(classLimits, type)) {
for (var code = classLimits[type].MIN; code <= classLimits[type].MAX; code++) {
if (Object.prototype.hasOwnProperty.call(codesEnum, code)) {
codes.push(code);
}
}
return Object.freeze(codes);
}
throw new Error("Type of HTTP status does not exist: ".concat(type));
}
exports.listCodes = listCodes;