* Fix fetch

Fix timeout in fetch.
Fix errors when response answer has a status code that don't have a body.
Fix FetchOptions missing timeout property.

* Fix fetch

Fix FetchOptions missing timeout property
This commit is contained in:
2023-11-03 23:06:52 +03:00
parent 26f3e86877
commit 82cf49191b
2 changed files with 5 additions and 2 deletions
+1
View File
@@ -12,6 +12,7 @@ const redirectCodes = new Set([301, 302, 307, 308]);
* @property {number} [maxRedirects] - Maximum amount of redirects to be followed.
* @property {AbortSignal} [signal] - Signal to abruptly cancel the request
* @property {Uint8Array | string} [body] - Defines a request body. Data must be serializable.
* @property {number} [timeout] - Request timeout time.
*/
/**
+4 -2
View File
@@ -1,10 +1,11 @@
import Remote from "../../polyfill/remote";
const methods = new Set(["GET", "PUT", "POST", "DELETE"]);
const bodylessStatusCodes = new Set([101, 204, 205, 304]);
class FetchResponse extends Response {
constructor(options) {
super(options.content, {
super(bodylessStatusCodes.has(options.status) ? null : options.content, {
headers: new Headers(options.headers),
method: options.method ?? "GET",
body: options.content,
@@ -40,6 +41,7 @@ const convertSignal = signal => {
* @property {number} [maxRedirects] - Maximum amount of redirects to be followed.
* @property {AbortSignal} [signal] - Signal to abruptly cancel the request
* @property {Uint8Array | string} [body] - Defines a request body. Data must be serializable.
* @property {number} [timeout] - Request timeout time.
*/
/**
@@ -58,7 +60,7 @@ export default function fetch(url, options = {}) {
if (typeof options.body === "string" || options.body instanceof Uint8Array) data.body = options.body;
if (typeof options.method === "string" && methods.has(options.method)) data.method = options.method;
if (options.signal instanceof AbortSignal) data.signal = convertSignal(options.signal);
if (typeof options.timeout === 'number') data.timeout = options.timeout;
let ctx;
try {
ctx = Remote.nativeFetch(url, data);