UNPKG

@poppanator/http-constants

Version:

This package contains various HTTP constants: http status codes and texts, HTTP header names and HTTP methods, and a few related utility function.

1 lines 1.84 kB
{"version":3,"file":"functions.cjs","sources":["../src/functions.ts"],"sourcesContent":["import type { Maybe } from './_type-types.js'\nimport * as tuples from './tuples.js'\n\nconst is =\n (res: number) =>\n (code: number): boolean =>\n Math.floor(code / 100) === res\n\n/** Returns true if `code` is an \"informational\" status code, e.g. 100-199 */\nexport const isInformational = is(1)\n\n/** Returns true if `code` is a \"successful\" status code, e.g. 200-299 */\nexport const isSuccess = is(2)\n\n/** Returns true if `code` is a \"redirection\" status code, e.g. 300-399 */\nexport const isRedirect = is(3)\n\n/** Returns true if `code` is a \"client error\" status code, e.g. 400-499 */\nexport const isClientError = is(4)\n\n/** Returns true if `code` is a \"server error\" status code, e.g. 500-599 */\nexport const isServerError = is(5)\n\n/**\n * Check if `code` is an existing HTTP status code\n */\nexport function isHttpStatus(code: number): boolean {\n return Object.values(tuples).some(([status]) => code === status)\n}\n\n/**\n * Returns the associated message with the status code `status`, or `undefined`\n * if not found\n */\nexport function getStatusText(status: number): Maybe<string> {\n for (const [code, text] of Object.values(tuples)) {\n if (code === status) {\n return text\n }\n }\n}\n"],"names":["is","res","code","Math","floor","isInformational","isSuccess","isRedirect","isClientError","isServerError","status","text","Object","values","tuples","some"],"mappings":"yLAGA,MAAMA,EACHC,GACAC,GACCC,KAAKC,MAAMF,EAAO,OAASD,EAGlBI,EAAkBL,EAAG,GAGrBM,EAAYN,EAAG,GAGfO,EAAaP,EAAG,GAGhBQ,EAAgBR,EAAG,GAGnBS,EAAgBT,EAAG,yBAazB,SAAuBU,GACjB,IAAA,MAACR,EAAMS,KAASC,OAAOC,OAAOC,EAAAA,QACvC,GAAIZ,IAASQ,EACJ,OAAAC,CAGb,+CAdO,SAAsBT,GACpB,OAAAU,OAAOC,OAAOC,EAAAA,QAAQC,MAAK,EAAEL,KAAYR,IAASQ,GAC3D"}