-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisClientErrorStatus.ts
49 lines (45 loc) · 1.89 KB
/
isClientErrorStatus.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
import { HttpClientErrorStatusCodes } from '../HttpStatusCodes';
import { HttpClientErrorReasonPhrases } from '../HttpReasonPhrases';
/**
* Checks whether the status code belongs to `HttpClientErrorStatusCodes` enum.
* The range is all standard code between [400 - 499]
*
* To check the entire 4xx range use `is4xxClientErrorStatusCode(code: number)` instead.
* @param statusCode - The integer status code. e.g. 100
* @returns `true` if matches `false` otherwise
*/
export const isClientErrorStatusCode = (statusCode: number): boolean =>
HttpClientErrorStatusCodes[statusCode] !== undefined;
/**
* Checks whether the status code belongs to 4xx family of status codes.
*
* @param statusCode - The integer status code. e.g. 100
* @returns `true` if matches `false` otherwise
*/
export const is4xxClientErrorStatusCode = (statusCode: number): boolean =>
statusCode >= 400 && statusCode <= 499;
/**
* Checks whether the input string belongs to `HttpClientErrorReasonPhrases` enum.
*
* The match is case sensitive
*
* @param reasonPhrase - The reason phrase. e.g. 'Ok'
* @returns `true` if matches `false` otherwise
*/
export const isClientErrorReasonPhrase = (reasonPhrase: string): boolean =>
(Object.values(HttpClientErrorReasonPhrases) as string[]).includes(
reasonPhrase
) === true;
/**
* Checks whether the input integer or string belongs to
* `HttpClientErrorStatusCodes` or `HttpClientErrorReasonPhrases` enum.
* For integer input, the range is all standard code between [400 - 499].
* For string input, the match is case sensitive.
*
* To check the entire 4xx range use `is4xxClientErrorStatusCode(code: number)` instead.
* @param status - e.g. 'Ok' or 200
* @returns `true` if matches `false` otherwise
*/
export const isClientErrorStatus = (status: string | number): boolean =>
isClientErrorStatusCode(status as number) ||
isClientErrorReasonPhrase(status as string);