mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
fix(table): fixed resolution of query params & headers
This commit is contained in:
@@ -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
@@ -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) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user