-
Notifications
You must be signed in to change notification settings - Fork 167
Expand file tree
/
Copy pathrequest.ts
More file actions
33 lines (28 loc) · 1.05 KB
/
request.ts
File metadata and controls
33 lines (28 loc) · 1.05 KB
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
import axios, { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';
/* eslint-disable @typescript-eslint/no-empty-interface */
export interface RequestConfig extends AxiosRequestConfig {}
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface Response<T = any> extends AxiosResponse<T> {}
export class Request {
constructor(private request = axios) {}
public get<T>(url: string, config: RequestConfig = {}): Promise<Response<T>> {
return this.request.get<T, Response<T>>(url, config);
}
public static isRequestError(error: Error): boolean {
return !!(
(error as AxiosError).response && (error as AxiosError).response?.status
);
}
public static extractErrorData(
error: unknown
): Pick<AxiosResponse, 'data' | 'status'> {
const axiosError = error as AxiosError;
if (axiosError.response && axiosError.response.status) {
return {
data: axiosError.response.data,
status: axiosError.response.status,
};
}
throw Error(`The error ${error} is not a Request Error`);
}
}