Skip to content

Commit 85f181a

Browse files
committed
fix(revenuecat): address PR review feedback
- create_purchase: wrap JSON.parse(attributes) with try/catch and clear error - update_subscriber_attributes: drop subscriber output (endpoint returns empty body) and guard JSON.parse on attributes - grant_entitlement: throw when both duration and endTimeMs are provided, matching defer_google_subscription behavior
1 parent 6c7a18e commit 85f181a

4 files changed

Lines changed: 24 additions & 19 deletions

File tree

apps/sim/tools/revenuecat/create_purchase.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,15 @@ export const revenuecatCreatePurchaseTool: ToolConfig<
135135
body.introductory_price = params.introductoryPrice
136136
}
137137
if (params.attributes !== undefined && params.attributes !== '') {
138-
body.attributes =
139-
typeof params.attributes === 'string' ? JSON.parse(params.attributes) : params.attributes
138+
if (typeof params.attributes === 'string') {
139+
try {
140+
body.attributes = JSON.parse(params.attributes)
141+
} catch {
142+
throw new Error('attributes must be a valid JSON object')
143+
}
144+
} else {
145+
body.attributes = params.attributes
146+
}
140147
}
141148
if (params.updatedAtMs !== undefined) body.updated_at_ms = params.updatedAtMs
142149
return body

apps/sim/tools/revenuecat/grant_entitlement.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export const revenuecatGrantEntitlementTool: ToolConfig<
7070
if (!params.duration && params.endTimeMs === undefined) {
7171
throw new Error('Provide either duration or endTimeMs to grant a promotional entitlement')
7272
}
73+
if (params.duration && params.endTimeMs !== undefined) {
74+
throw new Error('Provide only one of duration or endTimeMs — they cannot be used together')
75+
}
7376
const body: Record<string, unknown> = {}
7477
if (params.endTimeMs !== undefined) body.end_time_ms = params.endTimeMs
7578
else if (params.duration) body.duration = params.duration

apps/sim/tools/revenuecat/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ export interface UpdateSubscriberAttributesResponse extends ToolResponse {
399399
output: {
400400
updated: boolean
401401
app_user_id: string
402-
subscriber: RevenueCatSubscriber
403402
}
404403
}
405404

apps/sim/tools/revenuecat/update_subscriber_attributes.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import type {
22
UpdateSubscriberAttributesParams,
33
UpdateSubscriberAttributesResponse,
44
} from '@/tools/revenuecat/types'
5-
import {
6-
extractSubscriber,
7-
SUBSCRIBER_OUTPUT,
8-
shapeSubscriber,
9-
throwIfRevenueCatError,
10-
} from '@/tools/revenuecat/types'
5+
import { throwIfRevenueCatError } from '@/tools/revenuecat/types'
116
import type { ToolConfig } from '@/tools/types'
127

138
export const revenuecatUpdateSubscriberAttributesTool: ToolConfig<
@@ -51,22 +46,27 @@ export const revenuecatUpdateSubscriberAttributesTool: ToolConfig<
5146
'Content-Type': 'application/json',
5247
}),
5348
body: (params) => {
54-
const attributes =
55-
typeof params.attributes === 'string' ? JSON.parse(params.attributes) : params.attributes
49+
let attributes: unknown
50+
if (typeof params.attributes === 'string') {
51+
try {
52+
attributes = JSON.parse(params.attributes)
53+
} catch {
54+
throw new Error('attributes must be a valid JSON object')
55+
}
56+
} else {
57+
attributes = params.attributes
58+
}
5659
return { attributes }
5760
},
5861
},
5962

6063
transformResponse: async (response, params) => {
6164
await throwIfRevenueCatError(response)
62-
const data = await response.json().catch(() => ({}))
63-
const subscriber = shapeSubscriber(extractSubscriber(data))
6465
return {
6566
success: true,
6667
output: {
6768
updated: true,
68-
app_user_id: subscriber.original_app_user_id || (params?.appUserId ?? ''),
69-
subscriber,
69+
app_user_id: params?.appUserId ?? '',
7070
},
7171
}
7272
},
@@ -80,9 +80,5 @@ export const revenuecatUpdateSubscriberAttributesTool: ToolConfig<
8080
type: 'string',
8181
description: 'The app user ID of the updated subscriber',
8282
},
83-
subscriber: {
84-
...SUBSCRIBER_OUTPUT,
85-
description: 'The updated subscriber object after applying the attribute changes',
86-
},
8783
},
8884
}

0 commit comments

Comments
 (0)