-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisServerErrorStatus.ts
51 lines (46 loc) · 1.93 KB
/
isServerErrorStatus.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
import { HttpServerErrorStatusCodes } from '../HttpStatusCodes';
import { HttpServerErrorReasonPhrases } from '../HttpReasonPhrases';
/**
* Checks whether the status code belongs to `HttpServerErrorStatusCodes` enum.
* The range is all standard code between [500 - 599]
*
* To check the entire 5xx range use `is5xxServerErrorStatusCode(code: number)` instead.
* @param statusCode - The integer status code. e.g. 100
* @returns `true` if matches `false` otherwise
*/
export const isServerErrorStatusCode = (statusCode: number): boolean =>
HttpServerErrorStatusCodes[statusCode] !== undefined;
/**
* Checks whether the status code belongs to 5xx family of status codes.
*
* @param statusCode - The integer status code. e.g. 100
* @returns `true` if matches `false` otherwise
*/
export const is5xxServerErrorStatusCode = (statusCode: number): boolean =>
statusCode >= 500 && statusCode <= 599;
/**
* Checks whether the input string belongs to `HttpServerErrorReasonPhrases` enum.
*
* The match is case sensitive
*
* @param reasonPhrase - The reason phrase. e.g. 'Ok'
* @returns `true` if matches `false` otherwise
*/
export const isServerErrorReasonPhrase = (reasonPhrase: string): boolean =>
(Object.values(HttpServerErrorReasonPhrases) as string[]).includes(
reasonPhrase
) === true;
/**
* Checks whether the input integer or string belongs to
* `HttpServerErrorStatusCodes` or `HttpServerErrorReasonPhrases` enum.
* For integer input, the range is all standard code between [500 - 599].
* For string input, the match is case sensitive.
*
* To check the entire 5xx range use `is5xxServerErrorStatusCode(code: number)` instead.
* @param status - e.g. 'Ok' or 200
* @returns `true` if matches `false` otherwise
*/
export const isServerErrorStatus = (status: string | number): boolean =>
isServerErrorStatusCode(status as number) ||
isServerErrorReasonPhrase(status as string);
export default isServerErrorStatus;