From 5662942eed940c57d76fb1a9ea7748acc1143ee3 Mon Sep 17 00:00:00 2001 From: Lakshan Perera Date: Wed, 2 Jul 2025 15:25:23 +1000 Subject: [PATCH] fix: include response object in FunctionsClient invoke method return - Updated invoke method to include response object in both success and error cases - Added test cases to verify response object is present in function invocations - Ensures compliance with FunctionsResponse type definition --- src/FunctionsClient.ts | 4 ++-- src/types.ts | 5 +++-- test/spec/hello.spec.ts | 9 +++++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/FunctionsClient.ts b/src/FunctionsClient.ts index be2d44b..7cc8c3e 100644 --- a/src/FunctionsClient.ts +++ b/src/FunctionsClient.ts @@ -127,9 +127,9 @@ export class FunctionsClient { data = await response.text() } - return { data, error: null } + return { data, error: null, response } } catch (error) { - return { data: null, 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 93eb496..ffcc425 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,15 +2,16 @@ export type Fetch = typeof fetch /** * Response format - * */ export interface FunctionsResponseSuccess { data: T error: null + response?: Response } export interface FunctionsResponseFailure { data: null error: any + response?: Response } export type FunctionsResponse = FunctionsResponseSuccess | FunctionsResponseFailure @@ -62,7 +63,7 @@ export enum FunctionRegion { export type FunctionInvokeOptions = { /** * Object representing the headers to send with the request. - * */ + */ headers?: { [key: string]: string } /** * The HTTP verb of the request diff --git a/test/spec/hello.spec.ts b/test/spec/hello.spec.ts index 280c1b7..64e6ced 100644 --- a/test/spec/hello.spec.ts +++ b/test/spec/hello.spec.ts @@ -34,12 +34,15 @@ describe('basic tests (hello function)', () => { }) log('invoke hello') - const { data, error } = await fclient.invoke('hello', {}) + const { data, error, response } = await fclient.invoke('hello', {}) log('assert no error') expect(error).toBeNull() log(`assert ${data} is equal to 'Hello World'`) expect(data).toEqual('Hello World') + log('assert response object is present') + expect(response).toBeDefined() + expect(response).toBeInstanceOf(Response) }) test('invoke hello with setAuth', async () => { @@ -71,12 +74,14 @@ describe('basic tests (hello function)', () => { fclient.setAuth(wrongKey) log('invoke hello') - const { data, error } = await fclient.invoke('hello', {}) + const { data, error, response } = await fclient.invoke('hello', {}) log('check error') expect(error).not.toBeNull() expect(error?.message).toEqual('Relay Error invoking the Edge Function') expect(data).toBeNull() + log('assert response object is present in error case') + expect(response).toBeDefined() }) test('invoke hello: auth override by setAuth wrong key', async () => {