From a3968b32b09097eb481681c4801a0a0ac78afa9c Mon Sep 17 00:00:00 2001 From: Julio Sansossio Date: Thu, 23 May 2024 08:36:26 -0400 Subject: [PATCH] feat/Function query params --- src/FunctionsClient.ts | 8 ++++++++ src/types.ts | 4 ++++ test/spec/params.spec.ts | 8 ++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/FunctionsClient.ts b/src/FunctionsClient.ts index ed1849f..ad6c9df 100644 --- a/src/FunctionsClient.ts +++ b/src/FunctionsClient.ts @@ -88,6 +88,14 @@ export class FunctionsClient { } } + if (options.params) { + const searchParams = new URLSearchParams() + for (const key in options.params) { + searchParams.set(key, options.params[key]) + } + functionName = `${functionName}?${searchParams.toString()}` + } + const response = await this.fetch(`${this.url}/${functionName}`, { method: method || 'POST', // headers priority is (high to low): diff --git a/src/types.ts b/src/types.ts index 93eb496..3fd51db 100644 --- a/src/types.ts +++ b/src/types.ts @@ -83,4 +83,8 @@ export type FunctionInvokeOptions = { | ReadableStream | Record | string + /** + * The query parameters to send with the request. + */ + params?: Record } diff --git a/test/spec/params.spec.ts b/test/spec/params.spec.ts index 1383d48..1065174 100644 --- a/test/spec/params.spec.ts +++ b/test/spec/params.spec.ts @@ -38,13 +38,17 @@ describe('params reached to function', () => { }) log('invoke mirror') - const { data, error } = await fclient.invoke('mirror', {}) + const { data, error } = await fclient.invoke('mirror', { + params: { + hello: 'world', + } + }) log('assert no error') expect(error).toBeNull() const expected = { - url: 'http://localhost:8000/mirror', + url: 'http://localhost:8000/mirror?hello=world', method: 'POST', headers: data?.headers ?? [], body: '',