fix(core): update
This commit is contained in:
parent
f4374942ad
commit
14cca11c55
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/webrequest',
|
name: '@pushrocks/webrequest',
|
||||||
version: '3.0.6',
|
version: '3.0.7',
|
||||||
description: 'securely request from browsers'
|
description: 'securely request from browsers'
|
||||||
}
|
}
|
||||||
|
14
ts/index.ts
14
ts/index.ts
@ -49,7 +49,7 @@ export class WebRequest {
|
|||||||
const response: Response = await this.request(urlArg, {
|
const response: Response = await this.request(urlArg, {
|
||||||
body: plugins.smartjson.stringify(requestBody),
|
body: plugins.smartjson.stringify(requestBody),
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
useCache: useCacheArg
|
useCache: useCacheArg,
|
||||||
});
|
});
|
||||||
const responseText = await response.text();
|
const responseText = await response.text();
|
||||||
const responseResult = plugins.smartjson.parse(responseText);
|
const responseResult = plugins.smartjson.parse(responseText);
|
||||||
@ -103,12 +103,12 @@ export class WebRequest {
|
|||||||
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
||||||
let cacheUsed = false;
|
let cacheUsed = false;
|
||||||
if (optionsArg.useCache && this.cacheStore.check(urlArg)) {
|
if (optionsArg.useCache && this.cacheStore.check(urlArg)) {
|
||||||
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg)
|
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg);
|
||||||
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
||||||
} else {
|
} else {
|
||||||
cachedResponseDeferred.resolve(null);
|
cachedResponseDeferred.resolve(null);
|
||||||
}
|
}
|
||||||
const response: Response = await fetch(urlArg, {
|
let response: Response = await fetch(urlArg, {
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
method: optionsArg.method,
|
method: optionsArg.method,
|
||||||
headers: {
|
headers: {
|
||||||
@ -116,8 +116,8 @@ export class WebRequest {
|
|||||||
...(optionsArg.headers || {}),
|
...(optionsArg.headers || {}),
|
||||||
},
|
},
|
||||||
body: optionsArg.body,
|
body: optionsArg.body,
|
||||||
}).catch(async err => {
|
}).catch(async (err) => {
|
||||||
if (optionsArg.useCache && await cachedResponseDeferred.promise) {
|
if (optionsArg.useCache && (await cachedResponseDeferred.promise)) {
|
||||||
cacheUsed = true;
|
cacheUsed = true;
|
||||||
const cachedResponse = cachedResponseDeferred.promise;
|
const cachedResponse = cachedResponseDeferred.promise;
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
@ -125,6 +125,10 @@ export class WebRequest {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
if (optionsArg.useCache && (await cachedResponseDeferred.promise) && response.status === 500) {
|
||||||
|
cacheUsed = true;
|
||||||
|
response = await cachedResponseDeferred.promise;
|
||||||
|
}
|
||||||
if (!cacheUsed && optionsArg.useCache && response.status < 300) {
|
if (!cacheUsed && optionsArg.useCache && response.status < 300) {
|
||||||
const buffer = await response.clone().arrayBuffer();
|
const buffer = await response.clone().arrayBuffer();
|
||||||
await this.cacheStore.set(urlArg, buffer);
|
await this.cacheStore.set(urlArg, buffer);
|
||||||
|
Loading…
Reference in New Issue
Block a user