create-disposabl-app 0.14.18 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -1
- package/dist/disposabl.js +357 -50
- package/dist/index.js +8973 -154
- package/dist/templates/default/.github/scripts/macos-release.sh +81 -0
- package/dist/templates/default/.github/scripts/platforms.sh +1 -1
- package/dist/templates/default/.github/scripts/release.sh +1 -1
- package/dist/templates/default/.github/workflows/pipeline.yml +14 -3
- package/dist/templates/default/apps/desktop/README.md +13 -2
- package/dist/templates/default/apps/desktop/forge.config.mts +9 -3
- package/dist/templates/default/apps/docs/content/getting-started.mdx +1 -1
- package/dist/templates/default/apps/mobile/package.json +1 -0
- package/dist/templates/default/apps/web/eslint.config.ts +5 -1
- package/dist/templates/default/apps/web/public/disposabl-logo-light.svg +8 -0
- package/dist/templates/default/apps/web/public/landing/coast.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/community.jpg +0 -0
- package/dist/templates/default/apps/web/public/landing/stores/app-store.svg +46 -0
- package/dist/templates/default/apps/web/public/landing/stores/google-play.png +0 -0
- package/dist/templates/default/apps/web/public/landing/taste/LICENSE +21 -0
- package/dist/templates/default/apps/web/public/landing/taste/init.js +3 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/lib.js +189 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/moments.js +109 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.css +8 -0
- package/dist/templates/default/apps/web/public/landing/taste/moments/word-highlight/moment.js +35 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.css +321 -0
- package/dist/templates/default/apps/web/public/landing/taste/motion/motion.js +406 -0
- package/dist/templates/default/apps/web/routes.config.ts +14 -0
- package/dist/templates/default/apps/web/src/router/__root.tsx +8 -2
- package/dist/templates/default/apps/web/tsr.config.json +1 -1
- package/dist/templates/default/apps/web/vite.config.ts +10 -2
- package/dist/templates/default/bun.lock +6 -0
- package/dist/templates/default/bunfig.toml +1 -0
- package/dist/templates/default/disposabl.json +1 -1
- package/dist/templates/default/packages/backend/convex/authSetup.ts +2 -5
- package/dist/templates/default/packages/backend/convex/billing.ts +5 -0
- package/dist/templates/default/packages/backend/convex/billingHandoff.ts +6 -0
- package/dist/templates/default/packages/backend/convex/convex.config.ts +8 -0
- package/dist/templates/default/packages/backend/convex/http.ts +6 -0
- package/dist/templates/default/packages/backend/convex/revenuecat.ts +13 -0
- package/dist/templates/default/packages/backend/convex/schema.ts +44 -0
- package/dist/templates/default/packages/backend/convex/telemetry.ts +41 -5
- package/dist/templates/default/packages/backend/generate.ts +6 -0
- package/dist/templates/default/packages/cli/auth.ts +36 -3
- package/dist/templates/default/packages/cli/build.ts +6 -0
- package/dist/templates/default/packages/cli/client.ts +189 -19
- package/dist/templates/default/packages/cli/errors.ts +18 -0
- package/dist/templates/default/packages/cli/telemetry-build.ts +27 -0
- package/dist/templates/default/packages/cli/telemetry.ts +266 -0
- package/dist/templates/default/packages/disposabl-kit/PAYMENTS.md +23 -5
- package/dist/templates/default/packages/disposabl-kit/client/lib/telemetry.ts +24 -2
- package/dist/templates/default/packages/disposabl-kit/client/lib/theme.ts +1 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/billing-login.tsx +92 -0
- package/dist/templates/default/packages/disposabl-kit/client/routes/pricing.tsx +113 -43
- package/dist/templates/default/packages/disposabl-kit/client/routes/route.tsx +13 -5
- package/dist/templates/default/packages/disposabl-kit/client/vite.config.ts +3 -2
- package/dist/templates/default/packages/disposabl-kit/lib/telemetry.ts +148 -12
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/account-billing.tsx +143 -40
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/paywall.ts +83 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/policy.ts +125 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/session.ts +31 -3
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/topup.ts +99 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/use-topup.ts +116 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/billing/web-checkout.ts +190 -0
- package/dist/templates/default/packages/disposabl-kit/mobile/layout.tsx +9 -2
- package/dist/templates/default/packages/disposabl-kit/package.json +10 -1
- package/dist/templates/default/packages/disposabl-kit/payments/checkout-attempt.ts +64 -0
- package/dist/templates/default/packages/disposabl-kit/payments/config.ts +16 -3
- package/dist/templates/default/packages/disposabl-kit/server/billing-functions.ts +218 -9
- package/dist/templates/default/packages/disposabl-kit/server/billing-handoff.ts +126 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-native-capability.ts +12 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing-setup.ts +2 -0
- package/dist/templates/default/packages/disposabl-kit/server/billing.ts +31 -0
- package/dist/templates/default/packages/disposabl-kit/server/browser-session.ts +27 -0
- package/dist/templates/default/packages/disposabl-kit/server/lib/telemetry.ts +173 -59
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-contract.ts +233 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-functions.ts +554 -0
- package/dist/templates/default/packages/disposabl-kit/server/revenuecat-validator.ts +76 -0
- package/dist/templates/default/packages/reconfigure-skill/SKILL.md +36 -0
- package/dist/templates/default/packages/sdk/README.md +21 -0
- package/dist/templates/default/packages/sdk/src/analytics.ts +102 -0
- package/dist/templates/default/packages/sdk/src/index.ts +2 -0
- package/dist/templates/default/packages/skill/SKILL.md +2 -0
- package/dist/templates/default/packages/skill/references/agent.md +2 -0
- package/dist/templates/default/packages/tests/billing-attempts.test.ts +156 -0
- package/dist/templates/default/packages/tests/billing-handoff.test.ts +116 -0
- package/dist/templates/default/packages/tests/billing-setup.test.ts +4 -3
- package/dist/templates/default/packages/tests/billing.test.ts +48 -4
- package/dist/templates/default/packages/tests/ci-platform-selection.test.ts +54 -0
- package/dist/templates/default/packages/tests/desktop-macos-release.test.ts +226 -0
- package/dist/templates/default/packages/tests/desktop-packaging.test.ts +12 -0
- package/dist/templates/default/packages/tests/flows/seo.e2e.ts +3 -1
- package/dist/templates/default/packages/tests/native-billing.test.ts +417 -0
- package/dist/templates/default/packages/tests/page-telemetry.test.ts +218 -0
- package/dist/templates/default/packages/tests/revenuecat-inbox.test.ts +476 -0
- package/dist/templates/default/packages/tests/sdk-analytics.test.ts +158 -0
- package/dist/templates/default/packages/tests/telemetry.test.ts +272 -55
- package/package.json +1 -1
- package/dist/templates/default/apps/web/src/routeTree.gen.ts +0 -578
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import config from "../../../../disposabl.json";
|
|
2
2
|
import { makeFunctionReference } from "convex/server";
|
|
3
|
+
import { v } from "convex/values";
|
|
4
|
+
import { internalMutation } from "../../../backend/convex/_generated/server";
|
|
3
5
|
|
|
4
6
|
export type TelemetryEvent =
|
|
5
7
|
| "api_operation"
|
|
@@ -68,6 +70,92 @@ export function telemetryEnabled(settingsOverride: AnalyticsConfig = settings) {
|
|
|
68
70
|
);
|
|
69
71
|
}
|
|
70
72
|
|
|
73
|
+
// The UUID is a bearer capability, not browser attestation or authorization.
|
|
74
|
+
// Records expire after 30 days; an expired capability rotates without a merge.
|
|
75
|
+
const identityTtl = 30 * 24 * 60 * 60 * 1000;
|
|
76
|
+
// A single global bucket bounds anonymous issuance, regardless of input ID.
|
|
77
|
+
const issuanceBurst = 64;
|
|
78
|
+
const issuanceRefillMs = 60_000;
|
|
79
|
+
const expireIdentityRef = makeFunctionReference<
|
|
80
|
+
"mutation",
|
|
81
|
+
{ id: string },
|
|
82
|
+
null
|
|
83
|
+
>("telemetry:expireIdentity");
|
|
84
|
+
|
|
85
|
+
export const claimIdentity = internalMutation({
|
|
86
|
+
args: {
|
|
87
|
+
hash: v.optional(v.string()),
|
|
88
|
+
newHash: v.string(),
|
|
89
|
+
userId: v.optional(v.string()),
|
|
90
|
+
},
|
|
91
|
+
returns: v.union(v.boolean(), v.null()),
|
|
92
|
+
handler: async (ctx, { hash, newHash, userId }) => {
|
|
93
|
+
const row = hash
|
|
94
|
+
? await ctx.db
|
|
95
|
+
.query("telemetryIdentities")
|
|
96
|
+
.withIndex("by_hash", (q) => q.eq("hash", hash))
|
|
97
|
+
.unique()
|
|
98
|
+
: null;
|
|
99
|
+
if (
|
|
100
|
+
row &&
|
|
101
|
+
row.expiresAt > Date.now() &&
|
|
102
|
+
(!row.userId || row.userId === userId)
|
|
103
|
+
) {
|
|
104
|
+
if (userId && !row.userId)
|
|
105
|
+
await ctx.db.patch("telemetryIdentities", row._id, { userId });
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
const now = Date.now();
|
|
109
|
+
const bucket = await ctx.db
|
|
110
|
+
.query("telemetryIssuance")
|
|
111
|
+
.withIndex("by_key", (q) => q.eq("key", "global"))
|
|
112
|
+
.unique();
|
|
113
|
+
const tokens = bucket
|
|
114
|
+
? Math.min(
|
|
115
|
+
issuanceBurst,
|
|
116
|
+
bucket.tokens +
|
|
117
|
+
Math.max(0, now - bucket.updatedAt) / issuanceRefillMs,
|
|
118
|
+
)
|
|
119
|
+
: issuanceBurst;
|
|
120
|
+
if (tokens < 1) return null;
|
|
121
|
+
if (bucket) {
|
|
122
|
+
await ctx.db.patch("telemetryIssuance", bucket._id, {
|
|
123
|
+
tokens: tokens - 1,
|
|
124
|
+
updatedAt: now,
|
|
125
|
+
});
|
|
126
|
+
} else {
|
|
127
|
+
await ctx.db.insert("telemetryIssuance", {
|
|
128
|
+
key: "global",
|
|
129
|
+
tokens: tokens - 1,
|
|
130
|
+
updatedAt: now,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
const id = await ctx.db.insert("telemetryIdentities", {
|
|
134
|
+
hash: newHash,
|
|
135
|
+
expiresAt: Date.now() + identityTtl,
|
|
136
|
+
...(userId ? { userId } : {}),
|
|
137
|
+
});
|
|
138
|
+
await ctx.scheduler.runAfter(identityTtl, expireIdentityRef, { id });
|
|
139
|
+
return false;
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
export const expireIdentity = internalMutation({
|
|
144
|
+
args: { id: v.id("telemetryIdentities") },
|
|
145
|
+
returns: v.null(),
|
|
146
|
+
handler: async (ctx, { id }) => {
|
|
147
|
+
const row = await ctx.db.get("telemetryIdentities", id);
|
|
148
|
+
if (row && row.expiresAt <= Date.now())
|
|
149
|
+
await ctx.db.delete("telemetryIdentities", id);
|
|
150
|
+
return null;
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
/** Hash capabilities before persisting them; the raw value stays with its holder. */
|
|
155
|
+
export async function hashTelemetryIdentity(value: string) {
|
|
156
|
+
return opaqueId(`disposabl:anonymous-capability:${value}`);
|
|
157
|
+
}
|
|
158
|
+
|
|
71
159
|
async function opaqueId(value: string) {
|
|
72
160
|
return Array.from(
|
|
73
161
|
new Uint8Array(
|
|
@@ -83,68 +171,92 @@ export async function captureTelemetry(
|
|
|
83
171
|
properties: TelemetryProperties,
|
|
84
172
|
userId?: string,
|
|
85
173
|
settingsOverride: AnalyticsConfig = settings,
|
|
174
|
+
anonymousId?: string,
|
|
86
175
|
) {
|
|
87
176
|
if (!telemetryEnabled(settingsOverride)) return;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}),
|
|
102
|
-
...(properties.operation &&
|
|
103
|
-
/^[A-Za-z0-9_./:-]{1,100}$/.test(properties.operation) && {
|
|
104
|
-
operation: properties.operation,
|
|
177
|
+
try {
|
|
178
|
+
const token =
|
|
179
|
+
settingsOverride.analytics?.projectToken ??
|
|
180
|
+
process.env.POSTHOG_PROJECT_TOKEN!;
|
|
181
|
+
const host = settingsOverride.analytics?.host ?? process.env.POSTHOG_HOST!;
|
|
182
|
+
const identity = userId
|
|
183
|
+
? await opaqueId(`disposabl:${token}:${userId}`)
|
|
184
|
+
: anonymousId
|
|
185
|
+
? await opaqueId(`disposabl:${token}:anonymous:${anonymousId}`)
|
|
186
|
+
: crypto.randomUUID();
|
|
187
|
+
const safe = {
|
|
188
|
+
...(properties.app_id && {
|
|
189
|
+
app_id: await opaqueId(`disposabl:${token}:app:${properties.app_id}`),
|
|
105
190
|
}),
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}),
|
|
109
|
-
...([
|
|
110
|
-
"home",
|
|
111
|
-
"dashboard",
|
|
112
|
-
"connections",
|
|
113
|
-
"login",
|
|
114
|
-
"signup",
|
|
115
|
-
"account",
|
|
116
|
-
"other",
|
|
117
|
-
].includes(properties.page ?? "") && { page: properties.page }),
|
|
118
|
-
...([
|
|
119
|
-
"web",
|
|
120
|
-
"android",
|
|
121
|
-
"ios",
|
|
122
|
-
"macos",
|
|
123
|
-
"windows",
|
|
124
|
-
"linux",
|
|
125
|
-
"api",
|
|
126
|
-
"mcp",
|
|
127
|
-
"typescript-sdk",
|
|
128
|
-
"cli",
|
|
129
|
-
].includes(properties.platform ?? "") && { platform: properties.platform }),
|
|
130
|
-
...(["production", "preview"].includes(properties.channel ?? "") && {
|
|
131
|
-
channel: properties.channel,
|
|
132
|
-
}),
|
|
133
|
-
...(Number.isFinite(properties.duration_ms) &&
|
|
134
|
-
properties.duration_ms! >= 0 &&
|
|
135
|
-
properties.duration_ms! <= Number.MAX_SAFE_INTEGER && {
|
|
136
|
-
duration_ms: Math.round(properties.duration_ms!),
|
|
191
|
+
...(["api", "cli", "sdk", "mcp"].includes(properties.source ?? "") && {
|
|
192
|
+
source: properties.source,
|
|
137
193
|
}),
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
194
|
+
...(properties.operation &&
|
|
195
|
+
/^[A-Za-z0-9_./:-]{1,100}$/.test(properties.operation) && {
|
|
196
|
+
operation: properties.operation,
|
|
197
|
+
}),
|
|
198
|
+
...(["success", "error"].includes(properties.outcome ?? "") && {
|
|
199
|
+
outcome: properties.outcome,
|
|
200
|
+
}),
|
|
201
|
+
...([
|
|
202
|
+
"home",
|
|
203
|
+
"dashboard",
|
|
204
|
+
"connections",
|
|
205
|
+
"login",
|
|
206
|
+
"signup",
|
|
207
|
+
"account",
|
|
208
|
+
"other",
|
|
209
|
+
].includes(properties.page ?? "") && { page: properties.page }),
|
|
210
|
+
...([
|
|
211
|
+
"web",
|
|
212
|
+
"android",
|
|
213
|
+
"ios",
|
|
214
|
+
"macos",
|
|
215
|
+
"windows",
|
|
216
|
+
"linux",
|
|
217
|
+
"api",
|
|
218
|
+
"mcp",
|
|
219
|
+
"typescript-sdk",
|
|
220
|
+
"cli",
|
|
221
|
+
].includes(properties.platform ?? "") && {
|
|
222
|
+
platform: properties.platform,
|
|
223
|
+
}),
|
|
224
|
+
...(["production", "preview"].includes(properties.channel ?? "") && {
|
|
225
|
+
channel: properties.channel,
|
|
226
|
+
}),
|
|
227
|
+
...(Number.isFinite(properties.duration_ms) &&
|
|
228
|
+
properties.duration_ms! >= 0 &&
|
|
229
|
+
properties.duration_ms! <= Number.MAX_SAFE_INTEGER && {
|
|
230
|
+
duration_ms: Math.round(properties.duration_ms!),
|
|
231
|
+
}),
|
|
232
|
+
...Object.fromEntries(
|
|
233
|
+
(["time_since_app_creation_ms", "run_number", "run_attempt"] as const)
|
|
234
|
+
.filter(
|
|
235
|
+
(key) =>
|
|
236
|
+
Number.isSafeInteger(properties[key]) && properties[key]! >= 0,
|
|
237
|
+
)
|
|
238
|
+
.map((key) => [key, properties[key]]),
|
|
239
|
+
),
|
|
240
|
+
};
|
|
241
|
+
if (userId && anonymousId) {
|
|
242
|
+
const response = await fetch(`${host}/capture/`, {
|
|
243
|
+
method: "POST",
|
|
244
|
+
signal: AbortSignal.timeout(2000),
|
|
245
|
+
headers: { "Content-Type": "application/json" },
|
|
246
|
+
body: JSON.stringify({
|
|
247
|
+
api_key: token,
|
|
248
|
+
event: "$identify",
|
|
249
|
+
properties: {
|
|
250
|
+
distinct_id: identity,
|
|
251
|
+
$anon_distinct_id: await opaqueId(
|
|
252
|
+
`disposabl:${token}:anonymous:${anonymousId}`,
|
|
253
|
+
),
|
|
254
|
+
$ip: "0.0.0.0",
|
|
255
|
+
},
|
|
256
|
+
}),
|
|
257
|
+
});
|
|
258
|
+
if (!response.ok) return;
|
|
259
|
+
}
|
|
148
260
|
await fetch(`${host}/capture/`, {
|
|
149
261
|
method: "POST",
|
|
150
262
|
signal: AbortSignal.timeout(2000),
|
|
@@ -156,7 +268,9 @@ export async function captureTelemetry(
|
|
|
156
268
|
distinct_id: identity,
|
|
157
269
|
...safe,
|
|
158
270
|
environment: "production",
|
|
159
|
-
|
|
271
|
+
...(!userId || !anonymousId
|
|
272
|
+
? { $process_person_profile: false }
|
|
273
|
+
: {}),
|
|
160
274
|
$ip: "0.0.0.0",
|
|
161
275
|
},
|
|
162
276
|
}),
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { creditQuantity, pricing } from "../payments/config";
|
|
2
|
+
|
|
3
|
+
export type RevenueCatConfig = {
|
|
4
|
+
authorization: string;
|
|
5
|
+
project: string;
|
|
6
|
+
app: string;
|
|
7
|
+
environment: "SANDBOX" | "PRODUCTION";
|
|
8
|
+
product: string;
|
|
9
|
+
store: "APP_STORE" | "PLAY_STORE";
|
|
10
|
+
autumnKey: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type PurchaseEvent = {
|
|
14
|
+
deliveryId: string;
|
|
15
|
+
project: string;
|
|
16
|
+
app: string;
|
|
17
|
+
environment: string;
|
|
18
|
+
store: string;
|
|
19
|
+
product: string;
|
|
20
|
+
customer: string;
|
|
21
|
+
transaction: string;
|
|
22
|
+
type: "purchase" | "cancel" | "review";
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function parseTransfer(payload: unknown, config: RevenueCatConfig) {
|
|
26
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
27
|
+
return null;
|
|
28
|
+
const envelope = payload as Record<string, unknown>;
|
|
29
|
+
if (
|
|
30
|
+
envelope.api_version !== "1.0" ||
|
|
31
|
+
!envelope.event ||
|
|
32
|
+
typeof envelope.event !== "object" ||
|
|
33
|
+
Array.isArray(envelope.event)
|
|
34
|
+
)
|
|
35
|
+
return null;
|
|
36
|
+
const e = envelope.event as Record<string, unknown>;
|
|
37
|
+
const ids = (x: unknown): x is string[] =>
|
|
38
|
+
Array.isArray(x) &&
|
|
39
|
+
x.length > 0 &&
|
|
40
|
+
x.length <= 20 &&
|
|
41
|
+
x.every(
|
|
42
|
+
(id) => typeof id === "string" && id.length > 0 && id.length <= 256,
|
|
43
|
+
);
|
|
44
|
+
if (
|
|
45
|
+
e.type !== "TRANSFER" ||
|
|
46
|
+
e.app_id !== config.app ||
|
|
47
|
+
e.environment !== config.environment ||
|
|
48
|
+
(e.store !== undefined && e.store !== config.store) ||
|
|
49
|
+
(e.project_id !== undefined && e.project_id !== config.project) ||
|
|
50
|
+
typeof e.id !== "string" ||
|
|
51
|
+
!e.id ||
|
|
52
|
+
e.id.length > 256 ||
|
|
53
|
+
!ids(e.transferred_from) ||
|
|
54
|
+
!ids(e.transferred_to)
|
|
55
|
+
)
|
|
56
|
+
return null;
|
|
57
|
+
return {
|
|
58
|
+
project: config.project,
|
|
59
|
+
app: config.app,
|
|
60
|
+
environment: config.environment,
|
|
61
|
+
deliveryId: e.id,
|
|
62
|
+
from: e.transferred_from,
|
|
63
|
+
to: e.transferred_to,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** One authenticated project/environment may route only explicitly listed apps. */
|
|
68
|
+
export function revenueCatConfigs(
|
|
69
|
+
enabled: boolean,
|
|
70
|
+
env: Record<string, string | undefined>,
|
|
71
|
+
): RevenueCatConfig[] | null {
|
|
72
|
+
if (!enabled || env.REVENUECAT_CONSUMABLE_OWNER !== "custom") return null;
|
|
73
|
+
const authorization = env.REVENUECAT_WEBHOOK_AUTH?.trim();
|
|
74
|
+
const project = env.REVENUECAT_PROJECT_ID?.trim();
|
|
75
|
+
const autumnKey = env.AUTUMN_SECRET_KEY?.trim();
|
|
76
|
+
const environment = env.REVENUECAT_ENVIRONMENT;
|
|
77
|
+
if (
|
|
78
|
+
!authorization ||
|
|
79
|
+
!project ||
|
|
80
|
+
!autumnKey ||
|
|
81
|
+
(environment !== "SANDBOX" && environment !== "PRODUCTION")
|
|
82
|
+
)
|
|
83
|
+
return null;
|
|
84
|
+
let routes: unknown;
|
|
85
|
+
try {
|
|
86
|
+
routes = JSON.parse(env.REVENUECAT_ROUTES ?? "");
|
|
87
|
+
} catch {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
if (!Array.isArray(routes) || routes.length === 0 || routes.length > 20)
|
|
91
|
+
return null;
|
|
92
|
+
const result: RevenueCatConfig[] = [];
|
|
93
|
+
for (const route of routes) {
|
|
94
|
+
if (
|
|
95
|
+
!route ||
|
|
96
|
+
typeof route !== "object" ||
|
|
97
|
+
Array.isArray(route) ||
|
|
98
|
+
Object.keys(route).sort().join(",") !== "app,product,store" ||
|
|
99
|
+
typeof route.app !== "string" ||
|
|
100
|
+
!route.app ||
|
|
101
|
+
route.app.trim() !== route.app ||
|
|
102
|
+
route.app.length > 256 ||
|
|
103
|
+
typeof route.product !== "string" ||
|
|
104
|
+
!route.product ||
|
|
105
|
+
route.product.trim() !== route.product ||
|
|
106
|
+
route.product.length > 256 ||
|
|
107
|
+
(route.store !== "APP_STORE" && route.store !== "PLAY_STORE") ||
|
|
108
|
+
result.some((item) => item.app === route.app)
|
|
109
|
+
)
|
|
110
|
+
return null;
|
|
111
|
+
result.push({ authorization, project, environment, autumnKey, ...route });
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function selectRevenueCatRoute(
|
|
117
|
+
payload: unknown,
|
|
118
|
+
configs: RevenueCatConfig[],
|
|
119
|
+
) {
|
|
120
|
+
const matches = configs.filter(
|
|
121
|
+
(config) =>
|
|
122
|
+
parseTransfer(payload, config) || parseRevenueCatEvent(payload, config),
|
|
123
|
+
);
|
|
124
|
+
return matches.length === 1 ? matches[0]! : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function resolveRevenueCatRoute(
|
|
128
|
+
identity: Pick<
|
|
129
|
+
PurchaseEvent,
|
|
130
|
+
"project" | "app" | "environment" | "store" | "product"
|
|
131
|
+
>,
|
|
132
|
+
configs: RevenueCatConfig[],
|
|
133
|
+
) {
|
|
134
|
+
const matches = configs.filter(
|
|
135
|
+
(config) =>
|
|
136
|
+
config.project === identity.project &&
|
|
137
|
+
config.app === identity.app &&
|
|
138
|
+
config.environment === identity.environment &&
|
|
139
|
+
config.store === identity.store &&
|
|
140
|
+
config.product === identity.product,
|
|
141
|
+
);
|
|
142
|
+
return matches.length === 1 ? matches[0]! : null;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function parseRevenueCatEvent(
|
|
146
|
+
payload: unknown,
|
|
147
|
+
config: RevenueCatConfig,
|
|
148
|
+
): PurchaseEvent | null {
|
|
149
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload))
|
|
150
|
+
return null;
|
|
151
|
+
const envelope = payload as Record<string, unknown>;
|
|
152
|
+
if (
|
|
153
|
+
envelope.api_version !== "1.0" ||
|
|
154
|
+
!envelope.event ||
|
|
155
|
+
typeof envelope.event !== "object" ||
|
|
156
|
+
Array.isArray(envelope.event)
|
|
157
|
+
)
|
|
158
|
+
return null;
|
|
159
|
+
const e = envelope.event as Record<string, unknown>;
|
|
160
|
+
const valid = (value: unknown): value is string =>
|
|
161
|
+
typeof value === "string" &&
|
|
162
|
+
value.length > 0 &&
|
|
163
|
+
value.length <= 256 &&
|
|
164
|
+
value.trim() === value;
|
|
165
|
+
if (
|
|
166
|
+
![e.id, e.app_user_id, e.transaction_id].every(valid) ||
|
|
167
|
+
e.app_id !== config.app ||
|
|
168
|
+
e.environment !== config.environment ||
|
|
169
|
+
e.store !== config.store ||
|
|
170
|
+
e.product_id !== config.product ||
|
|
171
|
+
(e.project_id !== undefined && e.project_id !== config.project)
|
|
172
|
+
)
|
|
173
|
+
return null;
|
|
174
|
+
const customer = e.app_user_id as string;
|
|
175
|
+
if (
|
|
176
|
+
customer.startsWith("$RCAnonymousID:") ||
|
|
177
|
+
(e.original_app_user_id !== undefined &&
|
|
178
|
+
e.original_app_user_id !== customer)
|
|
179
|
+
)
|
|
180
|
+
return null;
|
|
181
|
+
const type =
|
|
182
|
+
e.type === "NON_RENEWING_PURCHASE"
|
|
183
|
+
? "purchase"
|
|
184
|
+
: e.type === "CANCELLATION"
|
|
185
|
+
? "cancel"
|
|
186
|
+
: "review";
|
|
187
|
+
return {
|
|
188
|
+
deliveryId: e.id as string,
|
|
189
|
+
project: config.project,
|
|
190
|
+
app: config.app,
|
|
191
|
+
environment: config.environment,
|
|
192
|
+
store: config.store,
|
|
193
|
+
product: config.product,
|
|
194
|
+
customer,
|
|
195
|
+
transaction: e.transaction_id as string,
|
|
196
|
+
type,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function balanceId(
|
|
201
|
+
event: Pick<
|
|
202
|
+
PurchaseEvent,
|
|
203
|
+
"project" | "app" | "environment" | "store" | "transaction"
|
|
204
|
+
>,
|
|
205
|
+
) {
|
|
206
|
+
const bytes = new TextEncoder().encode(
|
|
207
|
+
JSON.stringify([
|
|
208
|
+
"revenuecat",
|
|
209
|
+
event.project,
|
|
210
|
+
event.app,
|
|
211
|
+
event.environment,
|
|
212
|
+
event.store,
|
|
213
|
+
event.transaction,
|
|
214
|
+
]),
|
|
215
|
+
);
|
|
216
|
+
const hash = await crypto.subtle.digest("SHA-256", bytes);
|
|
217
|
+
return `rc_${Array.from(new Uint8Array(hash), (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function balanceRequest(
|
|
221
|
+
customer: string,
|
|
222
|
+
balance: string,
|
|
223
|
+
kind: "grant" | "refund",
|
|
224
|
+
) {
|
|
225
|
+
return {
|
|
226
|
+
customer_id: customer,
|
|
227
|
+
feature_id: pricing.featureId,
|
|
228
|
+
balance_id: balance,
|
|
229
|
+
...(kind === "grant"
|
|
230
|
+
? { included_grant: creditQuantity("topup") }
|
|
231
|
+
: { remaining: 0 }),
|
|
232
|
+
};
|
|
233
|
+
}
|