-
Notifications
You must be signed in to change notification settings - Fork 1
/
etools-ajax-request.js
44 lines (41 loc) · 1.37 KB
/
etools-ajax-request.js
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
/* eslint-disable linebreak-style */
import {requestIsCacheable, getFromCache, cacheEndpointResponse} from '@unicef-polymer/etools-dexie-caching';
import {doHttpRequest} from './etools-iron-request';
import {getIronRequestConfigOptions} from './etools-ajax-utils';
/**
* Check endpoint info to see if data is cacheable,
* if so try to return from cache, if nothing there, fire new request
* and cache response if applicable
* returns Promise
* @param {
* endpoint: {
* url: string,
* exp?: number,
* cacheTableName?: string,
* cachingKey?: string
* },
* body: any,
* method: string,
* headers: any,
* csrfCheck: string // 'disabled',
* timeout: number,
* sync: boolean,
* handleAs: string,
* jsonPrefix: string,
* rejectWithRequest: boolean,
* withCredentials: boolean,
* params?: object
* } reqConfig
* @param {string} requestKey
*/
export async function sendRequest(reqConfig, requestKey) {
const ironRequestConfigOptions = await getIronRequestConfigOptions(reqConfig);
if (requestIsCacheable(reqConfig.method, reqConfig.endpoint)) {
return getFromCache(reqConfig.endpoint).catch(() => {
return doHttpRequest(ironRequestConfigOptions, requestKey).then((response) =>
cacheEndpointResponse(response, reqConfig.endpoint)
);
});
}
return doHttpRequest(ironRequestConfigOptions, requestKey);
}