Skip to content

Commit

Permalink
fixed: AbortSignal to work with react native and old browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
3adel-bassiony committed Nov 19, 2024
1 parent 9e88a3c commit f1c7e3c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/helpers/createTimeoutSignal.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit f1c7e3c

Please sign in to comment.