diff --git a/package.json b/package.json index 81fe831..40ff12e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fetchly", - "version": "1.8.0", + "version": "1.10.0", "description": "A lightweight and simple package for making requests using fetch", "main": "build/index.js", "types": "build/index.d.ts", diff --git a/src/helpers/createTimeoutSignal.ts b/src/helpers/createTimeoutSignal.ts new file mode 100644 index 0000000..5d01acc --- /dev/null +++ b/src/helpers/createTimeoutSignal.ts @@ -0,0 +1,11 @@ +export function createTimeoutSignal(timeoutMs: number): AbortSignal { + if (typeof AbortSignal.timeout === 'function') { + // Use native timeout if available (modern browsers) + return AbortSignal.timeout(timeoutMs) + } else { + // Fallback for React Native and older browsers + const controller = new AbortController() + setTimeout(() => controller.abort(), timeoutMs) + return controller.signal + } +} diff --git a/src/index.ts b/src/index.ts index ddacfb1..e5993db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { ErrorType } from './enums/ErrorType' import { Method } from './enums/Method' import { ResponseFormat } from './enums/ResponseFormat' import { Status } from './enums/Status' +import { createTimeoutSignal } from './helpers/createTimeoutSignal' import { stringifyParams } from './helpers/stringifyParams' import { FetchlyResult } from './types/FetchlyResult' import { NextFetchRequestConfig } from './types/NextFetchRequestConfig' @@ -181,7 +182,7 @@ export class Fetchly { redirect: options?.redirect ?? this.redirect, referrer: options?.referrer ?? this.referrer, referrerPolicy: options?.referrerPolicy ?? this.referrerPolicy, - signal: AbortSignal.timeout(options?.timeout ?? this.timeout ?? 3000), + signal: createTimeoutSignal(options?.timeout ?? this.timeout ?? 3000), proxy: options?.proxy ?? this.proxy, ...options?.additionalOptions, ...this.additionalOptions,