1 | import axios, {
|
2 | AxiosError,
|
3 | AxiosInstance,
|
4 | AxiosPromise,
|
5 | AxiosRequestConfig,
|
6 | AxiosResponse
|
7 | } from 'axios';
|
8 | import { Dictionary } from '@brandingbrand/fsfoundation';
|
9 |
|
10 | // Export AxiosResponse type so its type definitions are not "private" and other packages depending
|
11 | // on FSNetwork can use our type aliases
|
12 | export { AxiosPromise, AxiosRequestConfig, AxiosResponse };
|
13 |
|
14 | // As of Axios 0.18, the typings don't show the Axios property even though it exists
|
15 |
|
16 | /**
|
17 | * Error response object from the network.
|
18 | */
|
19 | export type FSNetworkError = AxiosError;
|
20 |
|
21 | /**
|
22 | * Configuration for the network request.
|
23 | *
|
24 | * @see https://github.com/axios/axios#request-config
|
25 | */
|
26 | export interface FSNetworkRequestConfig extends AxiosRequestConfig {
|
27 | // Function that is called to intercept any responses
|
28 | responseIntercept?: (response: AxiosResponse) => AxiosResponse;
|
29 | // Function that is called to intercept any response errors
|
30 | responseError?: (error: any) => any;
|
31 | }
|
32 |
|
33 | /**
|
34 | * A promise of a response from the network.
|
35 | *
|
36 | * @see https://github.com/axios/axios#response-schema
|
37 | */
|
38 | export type FSNetworkPromise<T = any> = AxiosPromise<T>;
|
39 |
|
40 | /**
|
41 | * A response object from the network.
|
42 | *
|
43 | * @see https://github.com/axios/axios#response-schema
|
44 | */
|
45 | export type FSNetworkResponse<T = any> = AxiosResponse<T>;
|
46 |
|
47 | /**
|
48 | * The body of a network equest.
|
49 | */
|
50 | export type FSNetworkRequestData =
|
51 | ArrayBuffer |
|
52 | ArrayBufferView |
|
53 | Dictionary |
|
54 | string |
|
55 | URLSearchParams;
|
56 |
|
57 | /**
|
58 | * Manages network requests, optionally adding a set of default configuration to each request.
|
59 | */
|
60 | export default class FSNetwork {
|
61 | private instance: AxiosInstance;
|
62 | private interceptor?: number;
|
63 |
|
64 | /**
|
65 | * Creates a new instance of FSNetwork.
|
66 | *
|
67 | * @param {FSNetworkRequestConfig} config Default configuration to apply to every request.
|
68 | */
|
69 | constructor(config?: FSNetworkRequestConfig) {
|
70 | if (config) {
|
71 | const {
|
72 | responseIntercept,
|
73 | responseError,
|
74 | ...axiosConfig
|
75 | } = config;
|
76 | this.instance = axios.create(axiosConfig);
|
77 | this.setInterceptor(config);
|
78 | } else {
|
79 | this.instance = axios.create();
|
80 | }
|
81 | }
|
82 |
|
83 | removeInterceptor(): void {
|
84 | if (this.interceptor !== undefined) {
|
85 | this.instance.interceptors.response.eject(this.interceptor);
|
86 | this.interceptor = undefined;
|
87 | }
|
88 | }
|
89 |
|
90 | setInterceptor(config?: FSNetworkRequestConfig): void {
|
91 | this.removeInterceptor();
|
92 | if (config && (config.responseIntercept || config.responseError)) {
|
93 | this.interceptor = this.instance.interceptors.response.use(
|
94 | config.responseIntercept,
|
95 | config.responseError
|
96 | );
|
97 | }
|
98 | }
|
99 |
|
100 | /**
|
101 | * Performs a generic request.
|
102 | *
|
103 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
104 | * @returns {FSNetworkPromise<T>} A promise of the network response.
|
105 | */
|
106 | request<T = any>(config: FSNetworkRequestConfig): FSNetworkPromise<T> {
|
107 | return this.instance.request(config);
|
108 | }
|
109 |
|
110 | /**
|
111 | * Performs a GET request.
|
112 | *
|
113 | * @template T The response data type.
|
114 | * @param {string} uri A URI or path to request.
|
115 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
116 | * @returns {FSNetworkPromise<T>} A promise of the network response.
|
117 | */
|
118 | get<T = any>(uri: string, config?: FSNetworkRequestConfig): FSNetworkPromise<T> {
|
119 | // TODO: caching
|
120 | return this.instance.get(uri, config);
|
121 | }
|
122 |
|
123 | /**
|
124 | * Performs a DELETE request.
|
125 | *
|
126 | * @param {string} uri A URI or path to request.
|
127 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
128 | * @returns {FSNetworkPromise} A promise of the network response.
|
129 | */
|
130 | delete(uri: string, config?: FSNetworkRequestConfig): FSNetworkPromise {
|
131 | return this.instance.delete(uri, config);
|
132 | }
|
133 |
|
134 | /**
|
135 | * Performs a HEAD request.
|
136 | *
|
137 | * @param {string} uri A URI or path to request.
|
138 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
139 | * @returns {FSNetworkPromise} A promise of the network response.
|
140 | */
|
141 | head(uri: string, config?: FSNetworkRequestConfig): FSNetworkPromise {
|
142 | return this.instance.head(uri, config);
|
143 | }
|
144 |
|
145 | /**
|
146 | * Performs a POST request.
|
147 | *
|
148 | * @template T The response data type.
|
149 | * @param {string} uri A URI or path to request.
|
150 | * @param {FSNetworkRequestData} data The body of the request.
|
151 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
152 | * @returns {FSNetworkPromise<T>} A promise of the network response.
|
153 | */
|
154 | post<T = any>(
|
155 | uri: string,
|
156 | data?: FSNetworkRequestData,
|
157 | config?: FSNetworkRequestConfig
|
158 | ): FSNetworkPromise<T> {
|
159 | return this.instance.post(uri, data, config);
|
160 | }
|
161 |
|
162 | /**
|
163 | * Performs a PUT request.
|
164 | *
|
165 | * @template T The response data type.
|
166 | * @param {string} uri A URI or path to request.
|
167 | * @param {FSNetworkRequestData} data The body of the request.
|
168 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
169 | * @returns {FSNetworkPromise<T>} A promise of the network response.
|
170 | */
|
171 | put<T = any>(
|
172 | uri: string,
|
173 | data?: FSNetworkRequestData,
|
174 | config?: FSNetworkRequestConfig
|
175 | ): FSNetworkPromise<T> {
|
176 | return this.instance.put(uri, data, config);
|
177 | }
|
178 |
|
179 | /**
|
180 | * Performs a PATCH request.
|
181 | *
|
182 | * @template T The response data type.
|
183 | * @param {string} uri A URI or path to request.
|
184 | * @param {FSNetworkRequestData} data The body of the request.
|
185 | * @param {FSNetworkRequestConfig} config Configuration for the request.
|
186 | * @returns {FSNetworkPromise<T>} A promise of the network response.
|
187 | */
|
188 | patch<T = any>(
|
189 | uri: string,
|
190 | data?: FSNetworkRequestData,
|
191 | config?: FSNetworkRequestConfig
|
192 | ): FSNetworkPromise<T> {
|
193 | return this.instance.patch(uri, data, config);
|
194 | }
|
195 | }
|