UNPKG

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.

59 lines (58 loc) 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStatusDescription = exports.getStatusName = exports.getStatusCode = void 0; var constants_1 = require("../../constants"); /** * Returns the HTTP status code from status code name. * @name Get Status Code * @param {string} name - The name of the status code (e.g., `"IM_A_TEAPOT"`). * @returns {number} code - The code number of the status if code exists. * @throws {Error} An error object if something goes wrong, containing details about the issue. * * @example * var STATUS_CODES = require('http-response-status-code'); * console.log(STATUS_CODES.getStatusCode("IM_A_TEAPOT")); // 418 */ function getStatusCode(name) { if (Object.prototype.hasOwnProperty.call(constants_1.HTTP_STATUS_CODES, "".concat(name))) { return constants_1.HTTP_STATUS_CODES[name]; } throw new Error("Status code does not exist: ".concat(name)); } exports.getStatusCode = getStatusCode; /** * Returns the HTTP status code name from status code (e.g., `418`). * @name Get Status Name * @param {number} code - The code number of the status (e.g., `418`). * @returns {string} name - The name of the status code if name exists. * @throws {Error} An error object if something goes wrong, containing details about the issue. * * @example * var STATUS_CODES = require('http-response-status-code'); * console.log(STATUS_CODES.getStatusName(418)); // "IM_A_TEAPOT" */ function getStatusName(code) { if (Object.prototype.hasOwnProperty.call(constants_1.HTTP_STATUS_NAMES, code)) { return constants_1.HTTP_STATUS_NAMES[code]; } throw new Error("Status code does not exist: ".concat(code)); } exports.getStatusName = getStatusName; /** * Returns the status description from HTTP status code (e.g., 418). * @name Get Status Description * @param {number} code - The code number of the status (e.g., `418`). * @returns {string} description - The description of the status code if code exists. * @throws {Error} An error object if something goes wrong, containing details about the issue. * * @example * var STATUS_CODES = require('http-response-status-code'); * console.log(STATUS_CODES.getStatusDescription(500)); // "Internal Server Error" */ function getStatusDescription(code) { if (Object.prototype.hasOwnProperty.call(constants_1.HTTP_STATUS_DESCRIPTION, code)) { return constants_1.HTTP_STATUS_DESCRIPTION[code]; } throw new Error("Status code does not exist: ".concat(code)); } exports.getStatusDescription = getStatusDescription;