Skip to content

Commit 03323c6

Browse files
committed
API - added abortOnTimeout option to api instance
1 parent 726894f commit 03323c6

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

.trunk/trunk.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
version: 0.1
22
cli:
3-
version: 1.15.0
3+
version: 1.17.0
44
plugins:
55
sources:
66
- id: trunk

packages/plexus-api/src/api.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class ApiInstance {
5353
options: config.defaultOptions ?? {},
5454
optionsInit: { ...config.defaultOptions },
5555
timeout: config.timeout || undefined,
56+
abortOnTimeout: config.abortOnTimeout ?? false,
5657
baseURL:
5758
baseURL.endsWith('/') && baseURL.length > 1
5859
? baseURL.substring(0, baseURL.length - 1)
@@ -113,10 +114,13 @@ export class ApiInstance {
113114
: `${this._internalStore.baseURL}${
114115
path.startsWith('/') || path?.length === 0 ? path : `/${path}`
115116
}`
117+
118+
const controller = new AbortController()
116119
const requestObject = {
117120
...this._internalStore.options,
118121
...options,
119122
headers,
123+
signal: controller.signal,
120124
}
121125
// if we have a timeout set, call fetch and set a timeout. If the fetch takes longer than the timeout length, kill thee request and return a blank response
122126
if (this._internalStore.timeout) {
@@ -142,6 +146,8 @@ export class ApiInstance {
142146
if (raceResult) {
143147
res = raceResult
144148
} else {
149+
if (this._internalStore.abortOnTimeout) controller.abort()
150+
145151
// if we're throwing, throw an error
146152
if (this._internalStore.throws) throw new Error('Request timed out')
147153
// a 504 response status means the programmatic timeout was surpassed
@@ -306,7 +312,7 @@ export class ApiInstance {
306312
*/
307313
async post<
308314
ResponseType = any,
309-
BodyType extends Record<string, any> | string = {},
315+
BodyType extends Record<string, any> | string = {}
310316
>(
311317
path: string,
312318
body: BodyType = {} as BodyType,
@@ -459,7 +465,7 @@ export class ApiInstance {
459465
setHeaders<
460466
HeaderFunction extends () =>
461467
| Record<string, any>
462-
| Promise<Record<string, any>>,
468+
| Promise<Record<string, any>>
463469
>(inputFnOrObj: HeaderFunction | Record<string, any>) {
464470
// if (!_headers) _internalStore._options.headers = {}
465471
if (this._internalStore.noFetch) return this

packages/plexus-api/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export interface PlexusApiRes<DataType = any> {
1010
export interface PlexusApiConfig {
1111
defaultOptions?: PlexusApiOptions
1212
timeout?: number
13+
abortOnTimeout?: boolean
1314
// Deprecated
1415
silentFail?: boolean
1516

@@ -54,6 +55,7 @@ export interface ApiStore {
5455
options: PlexusApiOptions
5556
optionsInit: PlexusApiOptions
5657
timeout: number | undefined
58+
abortOnTimeout: boolean
5759
baseURL: string
5860
noFetch: boolean
5961
authToken: string

0 commit comments

Comments
 (0)