diff --git a/src/FunctionsClient.ts b/src/FunctionsClient.ts index 7cc8c3e..e1910d6 100644 --- a/src/FunctionsClient.ts +++ b/src/FunctionsClient.ts @@ -1,12 +1,12 @@ import { resolveFetch } from './helper' import { Fetch, + FunctionInvokeOptions, + FunctionRegion, FunctionsFetchError, FunctionsHttpError, FunctionsRelayError, FunctionsResponse, - FunctionInvokeOptions, - FunctionRegion, } from './types' export class FunctionsClient { @@ -51,7 +51,7 @@ export class FunctionsClient { options: FunctionInvokeOptions = {} ): Promise> { try { - const { headers, method, body: functionArgs } = options + const { headers, method, body: functionArgs, signal } = options let _headers: Record = {} let { region } = options if (!region) { @@ -99,7 +99,11 @@ export class FunctionsClient { // 3. default Content-Type header headers: { ..._headers, ...this.headers, ...headers }, body, + signal, }).catch((fetchError) => { + if (fetchError.name === 'AbortError') { + throw fetchError; + } throw new FunctionsFetchError(fetchError) }) @@ -129,6 +133,9 @@ export class FunctionsClient { return { data, error: null, response } } catch (error) { + if (error instanceof Error && error.name === 'AbortError') { + return { data: null, error: new FunctionsFetchError(error) } + } return { data: null, error, response: error instanceof FunctionsHttpError || error instanceof FunctionsRelayError ? error.context : undefined } } } diff --git a/src/types.ts b/src/types.ts index ffcc425..208c433 100644 --- a/src/types.ts +++ b/src/types.ts @@ -84,4 +84,8 @@ export type FunctionInvokeOptions = { | ReadableStream | Record | string + /** + * The AbortSignal to use for the request. + * */ + signal?: AbortSignal }