fix(core): update
This commit is contained in:
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/webrequest',
|
||||
version: '3.0.4',
|
||||
version: '3.0.5',
|
||||
description: 'securely request from browsers'
|
||||
}
|
||||
|
75
ts/index.ts
75
ts/index.ts
@ -14,20 +14,27 @@ export class WebRequest {
|
||||
const smartenv = new plugins.smartenv.Smartenv();
|
||||
if (!smartenv.isBrowser) {
|
||||
this.polyfillStatusEvaluated = true;
|
||||
const fetch = await smartenv.getSafeNodeModule('node-fetch');
|
||||
globalThis.fetch = fetch.default;
|
||||
const fetchMod = await smartenv.getSafeNodeModule('node-fetch');
|
||||
globalThis.Response = fetchMod.Response;
|
||||
globalThis.fetch = fetchMod.default;
|
||||
}
|
||||
this.neededPolyfillsLoadedDeferred.resolve();
|
||||
}
|
||||
|
||||
public cacheStore = new plugins.webstore.WebStore({
|
||||
dbName: 'webrequest',
|
||||
storeName: 'webrequest',
|
||||
});
|
||||
|
||||
constructor() {
|
||||
WebRequest.loadNeededPolyfills();
|
||||
}
|
||||
|
||||
public async getJson(urlArg: string | string[]) {
|
||||
public async getJson(urlArg: string, useCacheArg: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET',
|
||||
useCache: useCacheArg,
|
||||
});
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
@ -37,7 +44,7 @@ export class WebRequest {
|
||||
/**
|
||||
* postJson
|
||||
*/
|
||||
public async postJson(urlArg: string, requestBody?: any) {
|
||||
public async postJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
@ -51,7 +58,7 @@ export class WebRequest {
|
||||
/**
|
||||
* put js
|
||||
*/
|
||||
public async putJson(urlArg: string, requestBody?: any) {
|
||||
public async putJson(urlArg: string, requestBody?: any, useStoreAsFallback: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
@ -65,7 +72,7 @@ export class WebRequest {
|
||||
/**
|
||||
* put js
|
||||
*/
|
||||
public async deleteJson(urlArg: string) {
|
||||
public async deleteJson(urlArg: string, useStoreAsFallback: boolean = false) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET',
|
||||
@ -75,10 +82,60 @@ export class WebRequest {
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* a fault tolerant request
|
||||
*/
|
||||
public async request(
|
||||
urlArg: string,
|
||||
optionsArg: {
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
body?: any;
|
||||
headers?: HeadersInit;
|
||||
useCache?: boolean;
|
||||
timeoutMs?: number;
|
||||
}
|
||||
) {
|
||||
await WebRequest.neededPolyfillsLoadedDeferred.promise;
|
||||
const controller = new AbortController();
|
||||
if (optionsArg.timeoutMs) {
|
||||
plugins.smartdelay.delayFor(optionsArg.timeoutMs).then(() => {
|
||||
controller.abort();
|
||||
});
|
||||
}
|
||||
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
||||
let cacheUsed = false;
|
||||
if (optionsArg.useCache && this.cacheStore.check(urlArg)) {
|
||||
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg)
|
||||
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
||||
} else {
|
||||
cachedResponseDeferred.resolve(null);
|
||||
}
|
||||
const response: Response = await fetch(urlArg, {
|
||||
signal: controller.signal,
|
||||
method: optionsArg.method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...(optionsArg.headers || {}),
|
||||
},
|
||||
body: optionsArg.body,
|
||||
}).catch(async err => {
|
||||
if (optionsArg.useCache && await cachedResponseDeferred.promise) {
|
||||
cacheUsed = true;
|
||||
const cachedResponse = cachedResponseDeferred.promise;
|
||||
return cachedResponse;
|
||||
} else {
|
||||
return err;
|
||||
}
|
||||
});
|
||||
if (!cacheUsed && optionsArg.useCache && response.status < 300) {
|
||||
const buffer = await response.clone().arrayBuffer();
|
||||
await this.cacheStore.set(urlArg, buffer);
|
||||
}
|
||||
console.log(`${urlArg} answers with status: ${response.status}`);
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* a multi endpoint, fault tolerant request function
|
||||
*/
|
||||
public async requestMultiEndpoint(
|
||||
urlArg: string | string[],
|
||||
optionsArg: {
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
|
@ -2,5 +2,6 @@ import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartenv from '@pushrocks/smartenv';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as webstore from '@pushrocks/webstore';
|
||||
|
||||
export { smartdelay, smartenv, smartjson, smartpromise };
|
||||
export { smartdelay, smartenv, smartjson, smartpromise, webstore };
|
||||
|
Reference in New Issue
Block a user