Skip to content

Commit c221aac

Browse files
committed
improvement(supabase): response handling
1 parent bfd1997 commit c221aac

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

sim/blocks/blocks/supabase.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { ToolResponse } from '@/tools/types'
33
import { BlockConfig } from '../types'
44

55
interface SupabaseResponse extends ToolResponse {
6-
data: any
7-
error: any
6+
output: {
7+
message: string
8+
results: any
9+
}
10+
error?: string
811
}
912

1013
export const SupabaseBlock: BlockConfig<SupabaseResponse> = {
@@ -107,12 +110,9 @@ export const SupabaseBlock: BlockConfig<SupabaseResponse> = {
107110
outputs: {
108111
response: {
109112
type: {
110-
success: 'boolean',
111-
output: 'json',
112-
severity: 'string',
113-
data: 'json',
114-
error: 'json',
115-
},
113+
message: 'string',
114+
results: 'json'
115+
}
116116
},
117117
},
118118
}

sim/tools/supabase/insert.ts

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ export const insertTool: ToolConfig<SupabaseInsertParams, SupabaseInsertResponse
1919
},
2020
request: {
2121
url: (params) =>
22-
`https://${params.projectId}.supabase.co/rest/v1/${params.table}`,
22+
`https://${params.projectId}.supabase.co/rest/v1/${params.table}?select=*`,
2323
method: 'POST',
2424
headers: (params) => ({
2525
'apikey': params.apiKey,
2626
'Authorization': `Bearer ${params.apiKey}`,
2727
'Content-Type': 'application/json',
28+
'Prefer': 'return=representation'
2829
}),
2930
body: (params) => {
3031
// If data is an object but not an array, wrap it in an array
@@ -37,8 +38,8 @@ export const insertTool: ToolConfig<SupabaseInsertParams, SupabaseInsertResponse
3738
},
3839
directExecution: async (params: SupabaseInsertParams) => {
3940
try {
40-
// Construct the URL for the Supabase REST API
41-
const url = `https://${params.projectId}.supabase.co/rest/v1/${params.table}`;
41+
// Construct the URL for the Supabase REST API with select=* to return inserted data
42+
const url = `https://${params.projectId}.supabase.co/rest/v1/${params.table}?select=*`;
4243

4344
// Prepare the data - if it's an object but not an array, wrap it in an array
4445
const dataToSend = typeof params.data === 'object' && !Array.isArray(params.data)
@@ -52,6 +53,7 @@ export const insertTool: ToolConfig<SupabaseInsertParams, SupabaseInsertResponse
5253
'apikey': params.apiKey,
5354
'Authorization': `Bearer ${params.apiKey}`,
5455
'Content-Type': 'application/json',
56+
'Prefer': 'return=representation'
5557
},
5658
body: JSON.stringify(dataToSend),
5759
});
@@ -69,16 +71,15 @@ export const insertTool: ToolConfig<SupabaseInsertParams, SupabaseInsertResponse
6971
message: `Successfully inserted data into ${params.table}`,
7072
results: data,
7173
},
72-
data: data,
73-
error: null,
74+
error: undefined,
7475
}
7576
} catch (error) {
7677
return {
7778
success: false,
7879
output: {
7980
message: `Error inserting into Supabase: ${error instanceof Error ? error.message : String(error)}`,
81+
results: [],
8082
},
81-
data: [],
8283
error: error instanceof Error ? error.message : String(error),
8384
}
8485
}
@@ -89,17 +90,28 @@ export const insertTool: ToolConfig<SupabaseInsertParams, SupabaseInsertResponse
8990
throw new Error(error.message || 'Failed to insert data into Supabase');
9091
}
9192

92-
const data = await response.json();
93+
// Handle empty response case
94+
const text = await response.text();
95+
if (!text || text.trim() === '') {
96+
return {
97+
success: true,
98+
output: {
99+
message: 'Successfully inserted data into Supabase (no data returned)',
100+
results: [],
101+
},
102+
error: undefined,
103+
}
104+
}
105+
106+
const data = JSON.parse(text);
93107

94108
return {
95109
success: true,
96110
output: {
97111
message: 'Successfully inserted data into Supabase',
98112
results: data,
99113
},
100-
severity: 'info',
101-
data: data,
102-
error: null,
114+
error: undefined,
103115
}
104116
},
105117
transformError: (error: any) => {

sim/tools/supabase/query.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,15 @@ export const queryTool: ToolConfig<SupabaseQueryParams, SupabaseQueryResponse> =
5353
message: `Successfully queried data from ${params.table}`,
5454
results: data,
5555
},
56-
data: data,
57-
error: null,
56+
error: undefined,
5857
}
5958
} catch (error) {
6059
return {
6160
success: false,
6261
output: {
6362
message: `Error querying Supabase: ${error instanceof Error ? error.message : String(error)}`,
63+
results: [],
6464
},
65-
data: [],
6665
error: error instanceof Error ? error.message : String(error),
6766
}
6867
}
@@ -81,9 +80,7 @@ export const queryTool: ToolConfig<SupabaseQueryParams, SupabaseQueryResponse> =
8180
message: 'Successfully queried data from Supabase',
8281
results: data,
8382
},
84-
severity: 'info',
85-
data: data,
86-
error: null,
83+
error: undefined,
8784
}
8885
},
8986
transformError: (error: any) => {

sim/tools/supabase/types.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ export interface SupabaseInsertParams {
1414
}
1515

1616
export interface SupabaseQueryResponse extends ToolResponse {
17-
data: any[]
18-
error: any
17+
error?: string
18+
output: {
19+
message: string
20+
results: any
21+
}
1922
}
2023

2124
export interface SupabaseInsertResponse extends ToolResponse {
22-
data: any[]
23-
error: any
25+
error?: string
26+
output: {
27+
message: string
28+
results: any
29+
}
2430
}

0 commit comments

Comments
 (0)