Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add abort signal option to invoke function #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/FunctionsClient.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolveFetch } from './helper'
import {
Fetch,
FunctionInvokeOptions,
FunctionRegion,
FunctionsFetchError,
FunctionsHttpError,
FunctionsRelayError,
FunctionsResponse,
FunctionInvokeOptions,
FunctionRegion,
} from './types'

export class FunctionsClient {
Expand Down Expand Up @@ -51,7 +51,7 @@ export class FunctionsClient {
options: FunctionInvokeOptions = {}
): Promise<FunctionsResponse<T>> {
try {
const { headers, method, body: functionArgs } = options
const { headers, method, body: functionArgs, signal } = options
let _headers: Record<string, string> = {}
let { region } = options
if (!region) {
Expand Down Expand Up @@ -96,7 +96,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)
})

Expand Down Expand Up @@ -126,6 +130,9 @@ export class FunctionsClient {

return { data, error: null }
} catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
return { data: null, error: new FunctionsFetchError(error) }
}
return { data: null, error }
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ export type FunctionInvokeOptions = {
| ReadableStream<Uint8Array>
| Record<string, any>
| string
/**
* The AbortSignal to use for the request.
* */
signal?: AbortSignal
}