Skip to content

Commit b719f4a

Browse files
authored
Merge pull request #93 from dsc8x/main
feat: add abort signal option to invoke function
2 parents 3ad4a60 + 1d9e19a commit b719f4a

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) {
@@ -99,7 +99,11 @@ export class FunctionsClient {
9999
// 3. default Content-Type header
100100
headers: { ..._headers, ...this.headers, ...headers },
101101
body,
102+
signal,
102103
}).catch((fetchError) => {
104+
if (fetchError.name === 'AbortError') {
105+
throw fetchError;
106+
}
103107
throw new FunctionsFetchError(fetchError)
104108
})
105109

@@ -129,6 +133,9 @@ export class FunctionsClient {
129133

130134
return { data, error: null, response }
131135
} catch (error) {
136+
if (error instanceof Error && error.name === 'AbortError') {
137+
return { data: null, error: new FunctionsFetchError(error) }
138+
}
132139
return { data: null, error, response: error instanceof FunctionsHttpError || error instanceof FunctionsRelayError ? error.context : undefined }
133140
}
134141
}

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

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

0 commit comments

Comments
 (0)