fix(table): fixed resolution of query params & headers

This commit is contained in:
Waleed Latif
2025-02-18 14:36:14 -08:00
parent 2de52416d4
commit 3d132927bf
4 changed files with 43 additions and 12 deletions
+1
View File
@@ -54,6 +54,7 @@ export const ApiBlock: BlockConfig<RequestResponse> = {
method: { type: 'string', required: true },
headers: { type: 'json', required: false },
body: { type: 'json', required: false },
params: { type: 'json', required: false },
},
outputs: {
response: {
+14 -12
View File
@@ -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<string, string>
headers?: TableRow[]
body?: any
queryParams?: Record<string, string>
params?: TableRow[]
pathParams?: Record<string, string>
formData?: Record<string, string | Blob>
timeout?: number
@@ -46,7 +47,7 @@ export const requestTool: ToolConfig<RequestParams, RequestResponse> = {
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<RequestParams, RequestResponse> = {
})
}
// 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<RequestParams, RequestResponse> = {
},
method: 'POST' as HttpMethod,
headers: (params: RequestParams) => {
const headers: Record<string, string> = {
...params.headers,
}
const headers = transformTable(params.headers || null)
// Set appropriate Content-Type
if (params.formData) {
+8
View File
@@ -38,3 +38,11 @@ export interface ToolConfig<P = any, R extends ToolResponse = ToolResponse> {
transformResponse: (response: Response) => Promise<R>
transformError: (error: any) => string
}
export interface TableRow {
id: string
cells: {
Key: string
Value: string
}
}
+20
View File
@@ -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<string, string> => {
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<string, string>
)
}