-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.ts
64 lines (55 loc) · 1.54 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { StatusCode } from "./StatusCode";
import { StatusPhrase } from "./StatusPhrase";
import { StatusDescription } from "./StatusDescription";
import { StatusLabel } from "./StatusLabel";
import { IHttpResponsesDictionary } from "./Interfaces";
/**
* Creates a dictionary containing all HTTP Statuses containing code, phrase and description.
*
* @returns dictionary
*/
export const makeHttpResponsesDictionary = () => {
let dictionary: IHttpResponsesDictionary = {};
for (const statusCode in StatusLabel) {
if (Number.isNaN(Number(StatusLabel[statusCode]))) {
continue;
}
// @ts-ignore
const phrase = StatusPhrase[statusCode];
// @ts-ignore
const description = StatusDescription[statusCode];
dictionary = {
...dictionary,
[StatusCode[statusCode]]: {
code: StatusCode[statusCode],
phrase: phrase,
description: description,
},
};
}
return dictionary;
};
/**
* Get a status phrase from a given HTTP Status code
*
* @param code {StatusCode|number}
* @returns {string}
*/
export const getStatusPhraseByCode = (
code: number | StatusCode
): StatusPhrase => {
const httpDictionary = makeHttpResponsesDictionary();
return httpDictionary[code].phrase;
};
/**
* Get a description from a given HTTP Status code
*
* @param code {StatusCode|number}
* @returns {string}
*/
export const getStatusDescriptionByCode = (
code: number | StatusCode
): StatusDescription => {
const httpDictionary = makeHttpResponsesDictionary();
return httpDictionary[code].description;
};