Skip to content

Commit 45bcc64

Browse files
committed
feat: add abort signal option to invoke function
1 parent c262698 commit 45bcc64

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

packages/core/functions-js/src/FunctionsClient.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { resolveFetch } from './helper'
22
import {
33
Fetch,
4+
FunctionInvokeOptions,
5+
FunctionRegion,
46
FunctionsFetchError,
57
FunctionsHttpError,
68
FunctionsRelayError,
79
FunctionsResponse,
8-
FunctionInvokeOptions,
9-
FunctionRegion,
1010
} from './types'
1111

1212
export class FunctionsClient {
@@ -51,7 +51,7 @@ export class FunctionsClient {
5151
options: FunctionInvokeOptions = {}
5252
): Promise<FunctionsResponse<T>> {
5353
try {
54-
const { headers, method, body: functionArgs } = options
54+
const { headers, method, body: functionArgs, signal } = options
5555
let _headers: Record<string, string> = {}
5656
let { region } = options
5757
if (!region) {
@@ -96,7 +96,11 @@ export class FunctionsClient {
9696
// 3. default Content-Type header
9797
headers: { ..._headers, ...this.headers, ...headers },
9898
body,
99+
signal,
99100
}).catch((fetchError) => {
101+
if (fetchError.name === 'AbortError') {
102+
throw fetchError;
103+
}
100104
throw new FunctionsFetchError(fetchError)
101105
})
102106

@@ -126,6 +130,9 @@ export class FunctionsClient {
126130

127131
return { data, error: null }
128132
} catch (error) {
133+
if (error instanceof Error && error.name === 'AbortError') {
134+
return { data: null, error: new FunctionsFetchError(error) }
135+
}
129136
return { data: null, error }
130137
}
131138
}

packages/core/functions-js/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ export type FunctionInvokeOptions = {
8383
| ReadableStream<Uint8Array>
8484
| Record<string, any>
8585
| string
86+
/**
87+
* The AbortSignal to use for the request.
88+
* */
89+
signal?: AbortSignal
8690
}

0 commit comments

Comments
 (0)