|
| 1 | +import { getErrorMessage } from '@sim/utils/errors' |
1 | 2 | import { getPostHogIngestBaseUrl } from '@/tools/posthog/utils' |
2 | 3 | import type { ToolConfig } from '@/tools/types' |
3 | 4 |
|
@@ -63,33 +64,35 @@ export const batchEventsTool: ToolConfig<PostHogBatchEventsParams, PostHogBatchE |
63 | 64 | 'Content-Type': 'application/json', |
64 | 65 | }), |
65 | 66 | body: (params) => { |
66 | | - let batch: any[] |
| 67 | + let batch: unknown |
67 | 68 | try { |
68 | 69 | batch = JSON.parse(params.batch) |
69 | | - } catch (e) { |
70 | | - batch = [] |
| 70 | + } catch (error) { |
| 71 | + throw new Error(`Invalid batch JSON: ${getErrorMessage(error)}`) |
| 72 | + } |
| 73 | + if (!Array.isArray(batch)) { |
| 74 | + throw new Error('batch must be a JSON array of events') |
71 | 75 | } |
72 | 76 |
|
73 | 77 | return { |
74 | 78 | api_key: params.projectApiKey, |
75 | | - batch: batch, |
| 79 | + batch, |
76 | 80 | } |
77 | 81 | }, |
78 | 82 | }, |
79 | 83 |
|
80 | 84 | transformResponse: async (response: Response, params) => { |
81 | 85 | const data = await response.json() |
82 | | - let eventsProcessed = 0 |
83 | | - try { |
84 | | - eventsProcessed = params ? JSON.parse(params.batch).length : 0 |
85 | | - } catch { |
86 | | - eventsProcessed = 0 |
87 | | - } |
| 86 | + const eventsProcessed = params ? (JSON.parse(params.batch) as unknown[]).length : 0 |
| 87 | + const success = data.status === 1 |
| 88 | + |
88 | 89 | return { |
89 | | - success: true, |
| 90 | + success, |
90 | 91 | output: { |
91 | | - status: 'Batch events captured successfully', |
92 | | - events_processed: data.status === 1 ? eventsProcessed : 0, |
| 92 | + status: success |
| 93 | + ? 'Batch events captured successfully' |
| 94 | + : `Batch events capture failed (status: ${data.status})`, |
| 95 | + events_processed: success ? eventsProcessed : 0, |
93 | 96 | }, |
94 | 97 | } |
95 | 98 | }, |
|
0 commit comments