UNPKG

ts-http-status-utils

Version:

HTTP status code declarations, descriptions and utils

66 lines (65 loc) 2.36 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getStatusDescriptionByCode = exports.getStatusPhraseByCode = exports.makeHttpResponsesDictionary = void 0; var StatusCode_1 = require("./StatusCode"); var StatusPhrase_1 = require("./StatusPhrase"); var StatusDescription_1 = require("./StatusDescription"); var StatusLabel_1 = require("./StatusLabel"); /** * Creates a dictionary containing all HTTP Statuses containing code, phrase and description. * * @returns dictionary */ var makeHttpResponsesDictionary = function () { var _a; var dictionary = {}; for (var statusCode in StatusLabel_1.StatusLabel) { if (Number.isNaN(Number(StatusLabel_1.StatusLabel[statusCode]))) { continue; } // @ts-ignore var phrase = StatusPhrase_1.StatusPhrase[statusCode]; // @ts-ignore var description = StatusDescription_1.StatusDescription[statusCode]; dictionary = __assign(__assign({}, dictionary), (_a = {}, _a[StatusCode_1.StatusCode[statusCode]] = { code: StatusCode_1.StatusCode[statusCode], phrase: phrase, description: description, }, _a)); } return dictionary; }; exports.makeHttpResponsesDictionary = makeHttpResponsesDictionary; /** * Get a status phrase from a given HTTP Status code * * @param code {StatusCode|number} * @returns {string} */ var getStatusPhraseByCode = function (code) { var httpDictionary = (0, exports.makeHttpResponsesDictionary)(); return httpDictionary[code].phrase; }; exports.getStatusPhraseByCode = getStatusPhraseByCode; /** * Get a description from a given HTTP Status code * * @param code {StatusCode|number} * @returns {string} */ var getStatusDescriptionByCode = function (code) { var httpDictionary = (0, exports.makeHttpResponsesDictionary)(); return httpDictionary[code].description; }; exports.getStatusDescriptionByCode = getStatusDescriptionByCode;