From 3d132927bf790ab0af9ed9044c4401ff7ed34189 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 18 Feb 2025 14:36:14 -0800 Subject: [PATCH] fix(table): fixed resolution of query params & headers --- blocks/blocks/api.ts | 1 + tools/http/request.ts | 26 ++++++++++++++------------ tools/types.ts | 8 ++++++++ tools/utils.ts | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 tools/utils.ts diff --git a/blocks/blocks/api.ts b/blocks/blocks/api.ts index 969a4ef7fb..7a3103ff61 100644 --- a/blocks/blocks/api.ts +++ b/blocks/blocks/api.ts @@ -54,6 +54,7 @@ export const ApiBlock: BlockConfig = { method: { type: 'string', required: true }, headers: { type: 'json', required: false }, body: { type: 'json', required: false }, + params: { type: 'json', required: false }, }, outputs: { response: { diff --git a/tools/http/request.ts b/tools/http/request.ts index 4ddf589219..fb02c6b5bc 100644 --- a/tools/http/request.ts +++ b/tools/http/request.ts @@ -1,11 +1,12 @@ -import { HttpMethod, ToolConfig, ToolResponse } from '../types' +import { HttpMethod, TableRow, ToolConfig, ToolResponse } from '../types' +import { transformTable } from '../utils' export interface RequestParams { url: string method?: HttpMethod - headers?: Record + headers?: TableRow[] body?: any - queryParams?: Record + params?: TableRow[] pathParams?: Record formData?: Record timeout?: number @@ -46,7 +47,7 @@ export const requestTool: ToolConfig = { type: 'object', description: 'Request body (for POST, PUT, PATCH)', }, - queryParams: { + params: { type: 'object', description: 'URL query parameters to append', }, @@ -80,11 +81,14 @@ export const requestTool: ToolConfig = { }) } - // Append query parameters - if (params.queryParams) { - const queryString = Object.entries(params.queryParams) - .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) - .join('&') + // Handle query parameters + const queryParamsObj = transformTable(params.params || null) + const queryString = Object.entries(queryParamsObj) + .filter(([_, value]) => value !== undefined && value !== null) + .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) + .join('&') + + if (queryString) { url += (url.includes('?') ? '&' : '?') + queryString } @@ -92,9 +96,7 @@ export const requestTool: ToolConfig = { }, method: 'POST' as HttpMethod, headers: (params: RequestParams) => { - const headers: Record = { - ...params.headers, - } + const headers = transformTable(params.headers || null) // Set appropriate Content-Type if (params.formData) { diff --git a/tools/types.ts b/tools/types.ts index f6ec52fd82..be6e6a5cfb 100644 --- a/tools/types.ts +++ b/tools/types.ts @@ -38,3 +38,11 @@ export interface ToolConfig

{ transformResponse: (response: Response) => Promise transformError: (error: any) => string } + +export interface TableRow { + id: string + cells: { + Key: string + Value: string + } +} diff --git a/tools/utils.ts b/tools/utils.ts new file mode 100644 index 0000000000..3d84edb667 --- /dev/null +++ b/tools/utils.ts @@ -0,0 +1,20 @@ +import { TableRow } from './types' + +/** + * Transforms a table from the store format to a key-value object + * @param table Array of table rows from the store + * @returns Record of key-value pairs + */ +export const transformTable = (table: TableRow[] | null): Record => { + if (!table) return {} + + return table.reduce( + (acc, row) => { + if (row.cells?.Key && row.cells?.Value !== undefined) { + acc[row.cells.Key] = row.cells.Value + } + return acc + }, + {} as Record + ) +}