|
|
|
@ -8,42 +8,6 @@ export interface IWebrequestContructorOptions {
|
|
|
|
|
* web request
|
|
|
|
|
*/
|
|
|
|
|
export class WebRequest {
|
|
|
|
|
private static polyfillStatusEvaluated = false;
|
|
|
|
|
private static neededPolyfillsLoadedDeferred = plugins.smartpromise.defer<{
|
|
|
|
|
fetch: typeof fetch;
|
|
|
|
|
Response: typeof Response;
|
|
|
|
|
AbortController?: typeof AbortController;
|
|
|
|
|
helixTimeout?: any;
|
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* loads the needed polyfill
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
public static async loadNeededPolyfills() {
|
|
|
|
|
if (this.polyfillStatusEvaluated) {
|
|
|
|
|
return this.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
}
|
|
|
|
|
this.polyfillStatusEvaluated = true;
|
|
|
|
|
const smartenv = new plugins.smartenv.Smartenv();
|
|
|
|
|
if (!smartenv.isBrowser) {
|
|
|
|
|
this.polyfillStatusEvaluated = true;
|
|
|
|
|
const fetchMod = await smartenv.getSafeNodeModule('@adobe/fetch');
|
|
|
|
|
this.neededPolyfillsLoadedDeferred.resolve({
|
|
|
|
|
fetch: fetchMod.fetch,
|
|
|
|
|
Response: fetchMod.Response,
|
|
|
|
|
helixTimeout: fetchMod.timeoutSignal,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.neededPolyfillsLoadedDeferred.resolve({
|
|
|
|
|
fetch: (...args) => {
|
|
|
|
|
return fetch(...args);
|
|
|
|
|
},
|
|
|
|
|
Response: globalThis.Response,
|
|
|
|
|
AbortController: globalThis.AbortController,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public cacheStore = new plugins.webstore.WebStore({
|
|
|
|
|
dbName: 'webrequest',
|
|
|
|
@ -57,11 +21,9 @@ export class WebRequest {
|
|
|
|
|
logging: true,
|
|
|
|
|
...optionsArg,
|
|
|
|
|
};
|
|
|
|
|
WebRequest.loadNeededPolyfills();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async getJson(urlArg: string, useCacheArg: boolean = false) {
|
|
|
|
|
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
const response: Response = await this.request(urlArg, {
|
|
|
|
|
method: 'GET',
|
|
|
|
|
useCache: useCacheArg,
|
|
|
|
@ -75,7 +37,6 @@ export class WebRequest {
|
|
|
|
|
* postJson
|
|
|
|
|
*/
|
|
|
|
|
public async postJson(urlArg: string, requestBody?: any, useCacheArg: boolean = false) {
|
|
|
|
|
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
const response: Response = await this.request(urlArg, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
@ -93,7 +54,6 @@ export class WebRequest {
|
|
|
|
|
* put js
|
|
|
|
|
*/
|
|
|
|
|
public async putJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
|
|
|
|
|
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
const response: Response = await this.request(urlArg, {
|
|
|
|
|
method: 'PUT',
|
|
|
|
|
headers: {
|
|
|
|
@ -110,7 +70,6 @@ export class WebRequest {
|
|
|
|
|
* put js
|
|
|
|
|
*/
|
|
|
|
|
public async deleteJson(urlArg: string, useStoreAsFallback: boolean = false) {
|
|
|
|
|
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
const response: Response = await this.request(urlArg, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
@ -138,20 +97,10 @@ export class WebRequest {
|
|
|
|
|
...optionsArg,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fetchObject = await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
let controller: AbortController;
|
|
|
|
|
if (optionsArg.timeoutMs) {
|
|
|
|
|
if (fetchObject.AbortController) {
|
|
|
|
|
controller = new fetchObject.AbortController();
|
|
|
|
|
plugins.smartdelay.delayFor(optionsArg.timeoutMs).then(() => {
|
|
|
|
|
controller.abort();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
controller = {
|
|
|
|
|
signal: fetchObject.helixTimeout(optionsArg.timeoutMs),
|
|
|
|
|
} as any;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let controller = new AbortController();
|
|
|
|
|
plugins.smartdelay.delayFor(optionsArg.timeoutMs).then(() => {
|
|
|
|
|
controller.abort();
|
|
|
|
|
});
|
|
|
|
|
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
|
|
|
|
let cacheUsed = false;
|
|
|
|
|
if (optionsArg.useCache && (await this.cacheStore.check(urlArg))) {
|
|
|
|
@ -160,9 +109,8 @@ export class WebRequest {
|
|
|
|
|
} else {
|
|
|
|
|
cachedResponseDeferred.resolve(null);
|
|
|
|
|
}
|
|
|
|
|
let response: Response = await fetchObject
|
|
|
|
|
.fetch(urlArg, {
|
|
|
|
|
signal: controller ? controller.signal : null,
|
|
|
|
|
let response: Response = await fetch(urlArg, {
|
|
|
|
|
signal: controller.signal,
|
|
|
|
|
method: optionsArg.method,
|
|
|
|
|
headers: {
|
|
|
|
|
...(optionsArg.headers || {}),
|
|
|
|
@ -201,7 +149,7 @@ export class WebRequest {
|
|
|
|
|
headers?: HeadersInit;
|
|
|
|
|
}
|
|
|
|
|
): Promise<Response> {
|
|
|
|
|
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
|
|
|
|
|
|
|
|
|
let allUrls: string[];
|
|
|
|
|
let usedUrlIndex = 0;
|
|
|
|
|
|
|
|
|
|