-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Request.ts
38 lines (31 loc) · 1.47 KB
/
Request.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
import type Request from '@src/types/onyx/Request';
import type Response from '@src/types/onyx/Response';
import HttpUtils from './HttpUtils';
import type Middleware from './Middleware/types';
import enhanceParameters from './Network/enhanceParameters';
import * as NetworkStore from './Network/NetworkStore';
let middlewares: Middleware[] = [];
function makeXHR(request: Request): Promise<Response | void> {
const finalParameters = enhanceParameters(request.command, request?.data ?? {});
return NetworkStore.hasReadRequiredDataFromStorage().then((): Promise<Response | void> => {
// If we're using the Supportal token and this is not a Supportal request
// let's just return a promise that will resolve itself.
if (NetworkStore.isSupportAuthToken() && !NetworkStore.isSupportRequest(request.command)) {
return new Promise<void>((resolve) => {
resolve();
});
}
return HttpUtils.xhr(request.command, finalParameters, request.type, request.shouldUseSecure);
});
}
function processWithMiddleware(request: Request, isFromSequentialQueue = false): Promise<Response | void> {
return middlewares.reduce((last, middleware) => middleware(last, request, isFromSequentialQueue), makeXHR(request));
}
function use(middleware: Middleware) {
middlewares.push(middleware);
}
function clearMiddlewares() {
middlewares = [];
}
export {clearMiddlewares, processWithMiddleware, use};
export type {Middleware};