Skip to content

Commit b8d8c84

Browse files
committed
fix: revert invoke function signature
what we had in v1 was good. Having an object to pass in is better cos all objects we have are optional. Also making invoke a generic type function so that you can type the value returned by your function
1 parent 81fbb72 commit b8d8c84

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/FunctionsClient.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
FunctionsHttpError,
66
FunctionsRelayError,
77
FunctionsResponse,
8+
FunctionInvokeOptions,
89
} from './types'
910

1011
export class FunctionsClient {
@@ -38,23 +39,22 @@ export class FunctionsClient {
3839
/**
3940
* Invokes a function
4041
* @param functionName - the name of the function to invoke
41-
* @param functionArgs - the arguments to the function
42-
* @param options - function invoke options
43-
* @param options.headers - headers to send with the request
42+
* @param invokeOption.headers - object representing the headers to send with the request
43+
* @param invokeOptions.body - the body of the request
4444
*/
45-
async invoke(
45+
async invoke<T = any>(
4646
functionName: string,
47-
functionArgs: any,
48-
{
49-
headers = {},
50-
}: {
51-
headers?: Record<string, string>
52-
} = {}
53-
): Promise<FunctionsResponse> {
47+
invokeOptions: FunctionInvokeOptions = {}
48+
): Promise<FunctionsResponse<T>> {
5449
try {
50+
const { headers, body: functionArgs } = invokeOptions
51+
5552
let _headers: Record<string, string> = {}
5653
let body: any
57-
if (functionArgs && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
54+
if (
55+
functionArgs &&
56+
((headers && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) || !headers)
57+
) {
5858
if (
5959
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
6060
functionArgs instanceof ArrayBuffer

src/types.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ export type Fetch = typeof fetch
44
* Response format
55
*
66
*/
7-
interface FunctionsResponseSuccess {
8-
data: any
7+
interface FunctionsResponseSuccess<T> {
8+
data: T
99
error: null
1010
}
1111
interface FunctionsResponseFailure {
1212
data: null
1313
error: any
1414
}
15-
export type FunctionsResponse = FunctionsResponseSuccess | FunctionsResponseFailure
15+
export type FunctionsResponse<T> = FunctionsResponseSuccess<T> | FunctionsResponseFailure
1616

1717
export class FunctionsError extends Error {
1818
context: any
@@ -25,13 +25,13 @@ export class FunctionsError extends Error {
2525

2626
export class FunctionsFetchError extends FunctionsError {
2727
constructor(context: any) {
28-
super('Failed to perform request to Edge Function', 'FunctionsFetchError', context)
28+
super('Failed to send a request to the Edge Function', 'FunctionsFetchError', context)
2929
}
3030
}
3131

3232
export class FunctionsRelayError extends FunctionsError {
3333
constructor(context: any) {
34-
super('Relay error communicating with deno backend', 'FunctionsRelayError', context)
34+
super('Relay Error invoking the Edge Function', 'FunctionsRelayError', context)
3535
}
3636
}
3737

@@ -40,3 +40,15 @@ export class FunctionsHttpError extends FunctionsError {
4040
super('Edge Function returned a non-2xx status code', 'FunctionsHttpError', context)
4141
}
4242
}
43+
44+
export type FunctionInvokeOptions = {
45+
headers?: { [key: string]: string }
46+
body?:
47+
| File
48+
| Blob
49+
| ArrayBuffer
50+
| FormData
51+
| ReadableStream<Uint8Array>
52+
| Record<string, any>
53+
| string
54+
}

0 commit comments

Comments
 (0)