@hogsend/engine 0.46.0 → 0.50.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 +6 -0
- package/package.json +13 -10
- package/src/connectors/presets/index.ts +8 -2
- package/src/container.ts +149 -7
- package/src/destinations/presets/posthog.ts +7 -0
- package/src/destinations/presets/segment.ts +5 -0
- package/src/env.ts +38 -0
- package/src/flags/reconcile.ts +195 -0
- package/src/index.ts +79 -0
- package/src/journeys/authoring.ts +32 -0
- package/src/journeys/define-journey.ts +45 -40
- package/src/journeys/execute-journey-run.ts +73 -32
- package/src/journeys/journey-boundary.ts +76 -0
- package/src/journeys/journey-context.ts +128 -70
- package/src/journeys/journey-task-factory.ts +31 -0
- package/src/journeys/journey-task-runtime.ts +31 -0
- package/src/journeys/journey-version.test.ts +236 -0
- package/src/journeys/journey-version.ts +158 -0
- package/src/journeys/record-once.ts +28 -2
- package/src/lib/connector-actions.test.ts +36 -0
- package/src/lib/connector-actions.ts +42 -4
- package/src/lib/contacts.ts +49 -0
- package/src/lib/conversions.test.ts +33 -0
- package/src/lib/conversions.ts +8 -0
- package/src/lib/email.ts +23 -2
- package/src/lib/enrollment-policy.ts +51 -0
- package/src/lib/feed.ts +55 -3
- package/src/lib/flags-registry.ts +48 -0
- package/src/lib/flags.ts +498 -0
- package/src/lib/fx.ts +374 -0
- package/src/lib/global-control-readout.ts +166 -0
- package/src/lib/groups.ts +362 -0
- package/src/lib/ingestion.ts +38 -0
- package/src/lib/journey-lift.ts +142 -0
- package/src/lib/operator-settings.ts +71 -0
- package/src/lib/outbound.ts +147 -0
- package/src/lib/preferences.ts +6 -80
- package/src/lib/recipient-preferences.ts +53 -0
- package/src/lib/sms.ts +21 -2
- package/src/lib/stable-stringify.test.ts +42 -0
- package/src/lib/stable-stringify.ts +23 -0
- package/src/lib/variant.ts +99 -0
- package/src/lib/webhook-signing.ts +16 -1
- package/src/lists/registry.ts +6 -2
- package/src/lists/subscription.ts +13 -0
- package/src/routes/admin/contacts.ts +53 -4
- package/src/routes/admin/conversions.ts +137 -0
- package/src/routes/admin/flags.ts +369 -0
- package/src/routes/admin/groups.ts +866 -0
- package/src/routes/admin/impact-schemas.ts +44 -0
- package/src/routes/admin/impact-wire.test.ts +181 -0
- package/src/routes/admin/impact.ts +486 -0
- package/src/routes/admin/index.ts +12 -0
- package/src/routes/admin/journey-impact.ts +446 -0
- package/src/routes/admin/journeys.ts +70 -100
- package/src/routes/admin/readiness.ts +8 -8
- package/src/routes/admin/settings.ts +145 -0
- package/src/routes/admin/targeting.ts +256 -0
- package/src/routes/events/index.ts +11 -0
- package/src/routes/flags/index.ts +120 -0
- package/src/routes/groups/index.ts +417 -0
- package/src/routes/index.ts +23 -1
- package/src/testing.ts +25 -0
- package/src/webhook-sources/presets/index.ts +5 -1
- package/src/webhook-sources/presets/intercom.ts +230 -0
- package/src/webhook-sources/presets/segment.ts +56 -4
- package/src/worker.ts +3 -0
- package/src/workflows/bucket-backfill.ts +1 -14
- package/src/workflows/impact-digest.ts +761 -0
- package/src/workflows/journey-blueprint-interpreter.ts +18 -1
|
@@ -0,0 +1,417 @@
|
|
|
1
|
+
import type { Group } from "@hogsend/core";
|
|
2
|
+
import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
|
|
3
|
+
import type { AppEnv } from "../../app.js";
|
|
4
|
+
import {
|
|
5
|
+
addGroupMember,
|
|
6
|
+
GroupContactNotFoundError,
|
|
7
|
+
getGroup,
|
|
8
|
+
identifyGroup,
|
|
9
|
+
listGroupMembers,
|
|
10
|
+
listGroups,
|
|
11
|
+
removeGroupMember,
|
|
12
|
+
} from "../../lib/groups.js";
|
|
13
|
+
import { emitOutbound } from "../../lib/outbound.js";
|
|
14
|
+
import { errorSchema } from "../../lib/schemas.js";
|
|
15
|
+
|
|
16
|
+
// The public, serialized group shape. Internal columns (`organizationId`,
|
|
17
|
+
// `deletedAt`) are omitted; timestamps are ISO strings. Mirrors contacts'
|
|
18
|
+
// `serializeContact`/`contactSchema` pattern.
|
|
19
|
+
const groupSchema = z.object({
|
|
20
|
+
id: z.string(),
|
|
21
|
+
groupType: z.string(),
|
|
22
|
+
groupKey: z.string(),
|
|
23
|
+
displayName: z.string().nullable(),
|
|
24
|
+
properties: z.record(z.string(), z.unknown()).nullable(),
|
|
25
|
+
firstSeenAt: z.string(),
|
|
26
|
+
lastSeenAt: z.string(),
|
|
27
|
+
createdAt: z.string(),
|
|
28
|
+
updatedAt: z.string(),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
function serializeGroup(group: Group): z.infer<typeof groupSchema> {
|
|
32
|
+
return {
|
|
33
|
+
id: group.id,
|
|
34
|
+
groupType: group.groupType,
|
|
35
|
+
groupKey: group.groupKey,
|
|
36
|
+
displayName: group.displayName,
|
|
37
|
+
properties: group.properties,
|
|
38
|
+
firstSeenAt: group.firstSeenAt.toISOString(),
|
|
39
|
+
lastSeenAt: group.lastSeenAt.toISOString(),
|
|
40
|
+
createdAt: group.createdAt.toISOString(),
|
|
41
|
+
updatedAt: group.updatedAt.toISOString(),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The serialized membership shape returned by the add-member route. Internal
|
|
46
|
+
// columns (`organizationId`, `createdAt`, `updatedAt`) are omitted; `joinedAt`
|
|
47
|
+
// is an ISO string.
|
|
48
|
+
const membershipSchema = z.object({
|
|
49
|
+
id: z.string(),
|
|
50
|
+
groupId: z.string(),
|
|
51
|
+
contactId: z.string(),
|
|
52
|
+
role: z.string().nullable(),
|
|
53
|
+
joinedAt: z.string(),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// A member row: the contact behind a membership, joined to its identity.
|
|
57
|
+
const memberSchema = z.object({
|
|
58
|
+
contactId: z.string(),
|
|
59
|
+
email: z.string().nullable(),
|
|
60
|
+
externalId: z.string().nullable(),
|
|
61
|
+
role: z.string().nullable(),
|
|
62
|
+
joinedAt: z.string(),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Shared path-params schema for the `(groupType, groupKey)` natural key — the
|
|
66
|
+
// same shape the get / add-member / remove-member / list-members routes address
|
|
67
|
+
// a group by. `remove-member` extends it with the `contactId` segment.
|
|
68
|
+
const groupKeyParams = z.object({
|
|
69
|
+
groupType: z.string().min(1),
|
|
70
|
+
groupKey: z.string().min(1),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const identifyRoute = createRoute({
|
|
74
|
+
method: "post",
|
|
75
|
+
path: "/",
|
|
76
|
+
tags: ["Groups"],
|
|
77
|
+
summary: "Identify (upsert) a group",
|
|
78
|
+
description:
|
|
79
|
+
"Resolves (create / merge) a group by its (groupType, groupKey) natural key, merges `properties` onto the group's property bag (new keys win), and best-effort mirrors the property write to the active analytics provider (PostHog groupIdentify).",
|
|
80
|
+
request: {
|
|
81
|
+
body: {
|
|
82
|
+
content: {
|
|
83
|
+
"application/json": {
|
|
84
|
+
schema: z.object({
|
|
85
|
+
groupType: z.string().min(1),
|
|
86
|
+
groupKey: z.string().min(1),
|
|
87
|
+
displayName: z.string().optional(),
|
|
88
|
+
properties: z.record(z.string(), z.unknown()).optional(),
|
|
89
|
+
}),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
responses: {
|
|
95
|
+
200: {
|
|
96
|
+
content: {
|
|
97
|
+
"application/json": {
|
|
98
|
+
schema: z.object({ group: groupSchema }),
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
description: "Group identified",
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const listRoute = createRoute({
|
|
107
|
+
method: "get",
|
|
108
|
+
path: "/",
|
|
109
|
+
tags: ["Groups"],
|
|
110
|
+
summary: "List groups",
|
|
111
|
+
description:
|
|
112
|
+
"Newest-seen first. Filter with `groupType`; paginate with `limit` (default 50, max 200) + `offset`.",
|
|
113
|
+
request: {
|
|
114
|
+
query: z.object({
|
|
115
|
+
groupType: z.string().optional(),
|
|
116
|
+
limit: z.coerce.number().optional(),
|
|
117
|
+
offset: z.coerce.number().optional(),
|
|
118
|
+
}),
|
|
119
|
+
},
|
|
120
|
+
responses: {
|
|
121
|
+
200: {
|
|
122
|
+
content: {
|
|
123
|
+
"application/json": {
|
|
124
|
+
schema: z.object({ groups: z.array(groupSchema) }),
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
description: "Groups, newest-seen first",
|
|
128
|
+
},
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const getRoute = createRoute({
|
|
133
|
+
method: "get",
|
|
134
|
+
path: "/{groupType}/{groupKey}",
|
|
135
|
+
tags: ["Groups"],
|
|
136
|
+
summary: "Get a group",
|
|
137
|
+
request: {
|
|
138
|
+
params: groupKeyParams,
|
|
139
|
+
},
|
|
140
|
+
responses: {
|
|
141
|
+
200: {
|
|
142
|
+
content: {
|
|
143
|
+
"application/json": {
|
|
144
|
+
schema: z.object({ group: groupSchema }),
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
description: "The group",
|
|
148
|
+
},
|
|
149
|
+
404: {
|
|
150
|
+
content: { "application/json": { schema: errorSchema } },
|
|
151
|
+
description: "Unknown group",
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const addMemberRoute = createRoute({
|
|
157
|
+
method: "post",
|
|
158
|
+
path: "/{groupType}/{groupKey}/members",
|
|
159
|
+
tags: ["Groups"],
|
|
160
|
+
summary: "Add a member to a group",
|
|
161
|
+
description:
|
|
162
|
+
"Resolve-or-create the group, then add the contact as a member. `created` reflects whether THIS call inserted the membership (a re-add of an existing member returns `created:false`).",
|
|
163
|
+
request: {
|
|
164
|
+
params: groupKeyParams,
|
|
165
|
+
body: {
|
|
166
|
+
content: {
|
|
167
|
+
"application/json": {
|
|
168
|
+
schema: z.object({
|
|
169
|
+
// Bound to a `uuid` contacts column — validate the shape here so a
|
|
170
|
+
// malformed id is a 400 (not a 22P02 500), and a well-formed but
|
|
171
|
+
// nonexistent id is caught by the service's existence guard → 404.
|
|
172
|
+
contactId: z.string().uuid(),
|
|
173
|
+
role: z.string().optional(),
|
|
174
|
+
}),
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
responses: {
|
|
180
|
+
200: {
|
|
181
|
+
content: {
|
|
182
|
+
"application/json": {
|
|
183
|
+
schema: z.object({
|
|
184
|
+
membership: membershipSchema,
|
|
185
|
+
created: z.boolean(),
|
|
186
|
+
}),
|
|
187
|
+
},
|
|
188
|
+
},
|
|
189
|
+
description: "Membership resolved",
|
|
190
|
+
},
|
|
191
|
+
404: {
|
|
192
|
+
content: { "application/json": { schema: errorSchema } },
|
|
193
|
+
description: "Unknown contact",
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const removeMemberRoute = createRoute({
|
|
199
|
+
method: "delete",
|
|
200
|
+
path: "/{groupType}/{groupKey}/members/{contactId}",
|
|
201
|
+
tags: ["Groups"],
|
|
202
|
+
summary: "Remove a member from a group",
|
|
203
|
+
description:
|
|
204
|
+
"Hard-deletes the membership. `removed` is false when the group or membership did not exist.",
|
|
205
|
+
request: {
|
|
206
|
+
params: groupKeyParams.extend({
|
|
207
|
+
// Bound to a `uuid` contacts column — a malformed id is a 400 here rather
|
|
208
|
+
// than a 22P02 500 in the delete's WHERE clause.
|
|
209
|
+
contactId: z.string().uuid(),
|
|
210
|
+
}),
|
|
211
|
+
},
|
|
212
|
+
responses: {
|
|
213
|
+
200: {
|
|
214
|
+
content: {
|
|
215
|
+
"application/json": {
|
|
216
|
+
schema: z.object({ removed: z.boolean() }),
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
description: "Membership removal result",
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const listMembersRoute = createRoute({
|
|
225
|
+
method: "get",
|
|
226
|
+
path: "/{groupType}/{groupKey}/members",
|
|
227
|
+
tags: ["Groups"],
|
|
228
|
+
summary: "List a group's members",
|
|
229
|
+
description:
|
|
230
|
+
"Newest-joined first, joined to each member's live contact. Returns an empty list when the group does not exist.",
|
|
231
|
+
request: {
|
|
232
|
+
params: groupKeyParams,
|
|
233
|
+
query: z.object({
|
|
234
|
+
limit: z.coerce.number().optional(),
|
|
235
|
+
offset: z.coerce.number().optional(),
|
|
236
|
+
}),
|
|
237
|
+
},
|
|
238
|
+
responses: {
|
|
239
|
+
200: {
|
|
240
|
+
content: {
|
|
241
|
+
"application/json": {
|
|
242
|
+
schema: z.object({ members: z.array(memberSchema) }),
|
|
243
|
+
},
|
|
244
|
+
},
|
|
245
|
+
description: "The group's members, newest-joined first",
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// The groups router does NOT re-apply auth internally — the data-plane prefix
|
|
251
|
+
// guards in `routes/index.ts` apply `requireApiKey` + `requireScope("ingest")`
|
|
252
|
+
// to `/v1/groups` (bare + `/*`) before requests reach this router. The whole
|
|
253
|
+
// surface (group property writes, membership mutations, AND reads) is operator
|
|
254
|
+
// data, so it is secret-key only; a browser (pk_) key associates groups ONLY by
|
|
255
|
+
// attaching a `groups` map to an ingested event on `/v1/events`.
|
|
256
|
+
export const groupsRouter = new OpenAPIHono<AppEnv>()
|
|
257
|
+
.openapi(identifyRoute, async (c) => {
|
|
258
|
+
const { db, analytics, hatchet, logger } = c.get("container");
|
|
259
|
+
const body = c.req.valid("json");
|
|
260
|
+
|
|
261
|
+
const { group } = await identifyGroup({
|
|
262
|
+
db,
|
|
263
|
+
groupType: body.groupType,
|
|
264
|
+
groupKey: body.groupKey,
|
|
265
|
+
displayName: body.displayName,
|
|
266
|
+
properties: body.properties,
|
|
267
|
+
analytics,
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
const serialized = serializeGroup(group);
|
|
271
|
+
|
|
272
|
+
// INTENT-LAYER outbound emit (Phase 5): an explicit identify is intent, so
|
|
273
|
+
// fire `group.identified` on every success. NEVER emitted from the group
|
|
274
|
+
// service / ingest `associateGroups` path (a pageview-driven association
|
|
275
|
+
// would flood) — mirrors the contacts intent-layer rule.
|
|
276
|
+
void emitOutbound({
|
|
277
|
+
db,
|
|
278
|
+
hatchet,
|
|
279
|
+
logger,
|
|
280
|
+
event: "group.identified",
|
|
281
|
+
payload: serialized,
|
|
282
|
+
}).catch(logger.warn);
|
|
283
|
+
|
|
284
|
+
return c.json({ group: serialized }, 200);
|
|
285
|
+
})
|
|
286
|
+
.openapi(listRoute, async (c) => {
|
|
287
|
+
const { db } = c.get("container");
|
|
288
|
+
const { groupType, limit, offset } = c.req.valid("query");
|
|
289
|
+
|
|
290
|
+
const { groups } = await listGroups({ db, groupType, limit, offset });
|
|
291
|
+
|
|
292
|
+
return c.json({ groups: groups.map(serializeGroup) }, 200);
|
|
293
|
+
})
|
|
294
|
+
.openapi(getRoute, async (c) => {
|
|
295
|
+
const { db } = c.get("container");
|
|
296
|
+
const { groupType, groupKey } = c.req.valid("param");
|
|
297
|
+
|
|
298
|
+
const { group } = await getGroup({ db, groupType, groupKey });
|
|
299
|
+
if (!group) {
|
|
300
|
+
return c.json({ error: `Unknown group: ${groupType}/${groupKey}` }, 404);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return c.json({ group: serializeGroup(group) }, 200);
|
|
304
|
+
})
|
|
305
|
+
.openapi(addMemberRoute, async (c) => {
|
|
306
|
+
const { db, hatchet, logger } = c.get("container");
|
|
307
|
+
const { groupType, groupKey } = c.req.valid("param");
|
|
308
|
+
const { contactId, role } = c.req.valid("json");
|
|
309
|
+
|
|
310
|
+
let result: Awaited<ReturnType<typeof addGroupMember>>;
|
|
311
|
+
try {
|
|
312
|
+
result = await addGroupMember({
|
|
313
|
+
db,
|
|
314
|
+
groupType,
|
|
315
|
+
groupKey,
|
|
316
|
+
contactId,
|
|
317
|
+
role,
|
|
318
|
+
});
|
|
319
|
+
} catch (err) {
|
|
320
|
+
// A well-formed but nonexistent (or soft-deleted) contact is rejected by
|
|
321
|
+
// the service BEFORE any group is created — surface as 404, no orphan group.
|
|
322
|
+
if (err instanceof GroupContactNotFoundError) {
|
|
323
|
+
return c.json({ error: err.message }, 404);
|
|
324
|
+
}
|
|
325
|
+
throw err;
|
|
326
|
+
}
|
|
327
|
+
const { membership, created } = result;
|
|
328
|
+
|
|
329
|
+
// INTENT-LAYER outbound emit (Phase 5): fire `group.member_added` ONLY when
|
|
330
|
+
// THIS call inserted the membership — a re-add (`created:false`) is a no-op.
|
|
331
|
+
if (created) {
|
|
332
|
+
void emitOutbound({
|
|
333
|
+
db,
|
|
334
|
+
hatchet,
|
|
335
|
+
logger,
|
|
336
|
+
event: "group.member_added",
|
|
337
|
+
payload: {
|
|
338
|
+
groupType,
|
|
339
|
+
groupKey,
|
|
340
|
+
groupId: membership.groupId,
|
|
341
|
+
contactId: membership.contactId,
|
|
342
|
+
role: membership.role,
|
|
343
|
+
},
|
|
344
|
+
}).catch(logger.warn);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return c.json(
|
|
348
|
+
{
|
|
349
|
+
membership: {
|
|
350
|
+
id: membership.id,
|
|
351
|
+
groupId: membership.groupId,
|
|
352
|
+
contactId: membership.contactId,
|
|
353
|
+
role: membership.role,
|
|
354
|
+
joinedAt: membership.joinedAt.toISOString(),
|
|
355
|
+
},
|
|
356
|
+
created,
|
|
357
|
+
},
|
|
358
|
+
200,
|
|
359
|
+
);
|
|
360
|
+
})
|
|
361
|
+
.openapi(removeMemberRoute, async (c) => {
|
|
362
|
+
const { db, hatchet, logger } = c.get("container");
|
|
363
|
+
const { groupType, groupKey, contactId } = c.req.valid("param");
|
|
364
|
+
|
|
365
|
+
// Resolve the live group up-front so a successful removal's outbound payload
|
|
366
|
+
// can carry `groupId` — the service returns only `{ removed }`, and we do
|
|
367
|
+
// NOT widen its core contract. A missing group makes the remove a no-op.
|
|
368
|
+
const { group } = await getGroup({ db, groupType, groupKey });
|
|
369
|
+
|
|
370
|
+
const { removed } = await removeGroupMember({
|
|
371
|
+
db,
|
|
372
|
+
groupType,
|
|
373
|
+
groupKey,
|
|
374
|
+
contactId,
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// INTENT-LAYER outbound emit (Phase 5): fire `group.member_removed` ONLY
|
|
378
|
+
// when a membership row was actually deleted. `group` is present whenever
|
|
379
|
+
// `removed` is true (the row is deleted from an existing group).
|
|
380
|
+
if (removed && group) {
|
|
381
|
+
void emitOutbound({
|
|
382
|
+
db,
|
|
383
|
+
hatchet,
|
|
384
|
+
logger,
|
|
385
|
+
event: "group.member_removed",
|
|
386
|
+
payload: { groupType, groupKey, groupId: group.id, contactId },
|
|
387
|
+
}).catch(logger.warn);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
return c.json({ removed }, 200);
|
|
391
|
+
})
|
|
392
|
+
.openapi(listMembersRoute, async (c) => {
|
|
393
|
+
const { db } = c.get("container");
|
|
394
|
+
const { groupType, groupKey } = c.req.valid("param");
|
|
395
|
+
const { limit, offset } = c.req.valid("query");
|
|
396
|
+
|
|
397
|
+
const { members } = await listGroupMembers({
|
|
398
|
+
db,
|
|
399
|
+
groupType,
|
|
400
|
+
groupKey,
|
|
401
|
+
limit,
|
|
402
|
+
offset,
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
return c.json(
|
|
406
|
+
{
|
|
407
|
+
members: members.map((m) => ({
|
|
408
|
+
contactId: m.contactId,
|
|
409
|
+
email: m.email,
|
|
410
|
+
externalId: m.externalId,
|
|
411
|
+
role: m.role,
|
|
412
|
+
joinedAt: m.joinedAt.toISOString(),
|
|
413
|
+
})),
|
|
414
|
+
},
|
|
415
|
+
200,
|
|
416
|
+
);
|
|
417
|
+
});
|
package/src/routes/index.ts
CHANGED
|
@@ -12,6 +12,8 @@ import { emailRouter } from "./email/index.js";
|
|
|
12
12
|
import { emailsRouter } from "./emails/index.js";
|
|
13
13
|
import { eventsRouter } from "./events/index.js";
|
|
14
14
|
import { feedRouter } from "./feed/index.js";
|
|
15
|
+
import { flagsRouter } from "./flags/index.js";
|
|
16
|
+
import { groupsRouter } from "./groups/index.js";
|
|
15
17
|
import { healthRouter } from "./health.js";
|
|
16
18
|
import { listsRouter } from "./lists/index.js";
|
|
17
19
|
import { trackingRouter } from "./tracking/index.js";
|
|
@@ -77,6 +79,13 @@ export function registerRoutes(
|
|
|
77
79
|
// from the request.
|
|
78
80
|
v1.use("/feed", requirePublishableOrIngest);
|
|
79
81
|
v1.use("/feed/*", requirePublishableOrIngest);
|
|
82
|
+
// Flags: `GET /v1/flags` is the BROWSER read (publishable OR secret-ingest).
|
|
83
|
+
// Only the bare `/flags` pattern is browser-reachable; the `/flags/evaluate`
|
|
84
|
+
// subtree is secret-only (guarded in the secret section below). Because a
|
|
85
|
+
// bare `use("/flags")` matches ONLY the exact path (not the subtree), the two
|
|
86
|
+
// tiers never overlap. Recipient scoping is server-side in
|
|
87
|
+
// `resolveFeedRecipient`, never from the request.
|
|
88
|
+
v1.use("/flags", requirePublishableOrIngest);
|
|
80
89
|
// Bare `/contacts` is dual-purpose: PUT/POST = the publishable upsert;
|
|
81
90
|
// DELETE = secret-only. Hono runs ALL matching `use`s, so a single guard must
|
|
82
91
|
// branch by method rather than stacking two guards (which would 403 a valid
|
|
@@ -116,11 +125,22 @@ export function registerRoutes(
|
|
|
116
125
|
// secret-only there, PUT/POST publishable). Any FUTURE secret-only
|
|
117
126
|
// `/contacts/<x>` or `/lists/<x>` route must mount its own
|
|
118
127
|
// `requireApiKey + requireScope("ingest")` (fail-closed by construction).
|
|
119
|
-
|
|
128
|
+
//
|
|
129
|
+
// `/groups` is the same shape: the ENTIRE surface (group property writes,
|
|
130
|
+
// membership mutations, AND reads) is operator data, so it is secret-only. A
|
|
131
|
+
// browser (pk_) key never touches `/v1/groups`; it associates groups ONLY by
|
|
132
|
+
// attaching a `groups` map to an ingested event on the publishable
|
|
133
|
+
// `/v1/events` route (association-only, no property write).
|
|
134
|
+
for (const base of ["/emails", "/campaigns", "/groups"]) {
|
|
120
135
|
v1.use(base, requireApiKey, requireScope("ingest"));
|
|
121
136
|
v1.use(`${base}/*`, requireApiKey, requireScope("ingest"));
|
|
122
137
|
}
|
|
123
138
|
v1.use("/contacts/find", requireApiKey, requireScope("ingest"));
|
|
139
|
+
// `POST /v1/flags/evaluate` is the SERVER SDK read — secret-only, exactly like
|
|
140
|
+
// the other secret data-plane routes. The bare `/flags` (browser GET) is
|
|
141
|
+
// guarded separately above; guard ONLY the `/evaluate` path here so the two
|
|
142
|
+
// tiers stay disjoint. A pk_ key hitting this fails `requireScope("ingest")`.
|
|
143
|
+
v1.use("/flags/evaluate", requireApiKey, requireScope("ingest"));
|
|
124
144
|
// Register the email rate-limit ONCE. The wildcard pattern `/emails/*` matches
|
|
125
145
|
// BOTH the bare `POST /v1/emails` and any subtree, so a single registration
|
|
126
146
|
// covers the whole emails surface. Registering both bare AND wildcard with the
|
|
@@ -131,9 +151,11 @@ export function registerRoutes(
|
|
|
131
151
|
v1.route("/contacts", contactsRouter);
|
|
132
152
|
v1.route("/events", eventsRouter);
|
|
133
153
|
v1.route("/feed", feedRouter);
|
|
154
|
+
v1.route("/flags", flagsRouter);
|
|
134
155
|
v1.route("/emails", emailsRouter);
|
|
135
156
|
v1.route("/lists", listsRouter);
|
|
136
157
|
v1.route("/campaigns", campaignsRouter);
|
|
158
|
+
v1.route("/groups", groupsRouter);
|
|
137
159
|
|
|
138
160
|
app.route("/v1", v1);
|
|
139
161
|
|
package/src/testing.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Narrow, side-effect-free engine surface used by @hogsend/testing. */
|
|
2
|
+
|
|
3
|
+
export { JourneyExitedError } from "./journeys/errors.js";
|
|
4
|
+
export {
|
|
5
|
+
deriveJourneyKey,
|
|
6
|
+
getJourneyBoundary,
|
|
7
|
+
type JourneyBoundary,
|
|
8
|
+
type JourneyServiceOverrides,
|
|
9
|
+
registerKey,
|
|
10
|
+
registerRecordLabel,
|
|
11
|
+
runWithJourneyBoundary,
|
|
12
|
+
} from "./journeys/journey-boundary.js";
|
|
13
|
+
export {
|
|
14
|
+
type EnrollmentPolicyFacts,
|
|
15
|
+
type EnrollmentPolicyResult,
|
|
16
|
+
evaluateEnrollmentPolicy,
|
|
17
|
+
} from "./lib/enrollment-policy.js";
|
|
18
|
+
export { isHeldOut } from "./lib/holdout.js";
|
|
19
|
+
export {
|
|
20
|
+
pickVariant,
|
|
21
|
+
validateVariantArms,
|
|
22
|
+
validateVariantKey,
|
|
23
|
+
variantBucket,
|
|
24
|
+
} from "./lib/variant.js";
|
|
25
|
+
export { isListSubscribed } from "./lists/subscription.js";
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import type { env as engineEnv } from "../../env.js";
|
|
2
2
|
import type { DefinedWebhookSource } from "../define-webhook-source.js";
|
|
3
3
|
import { clerkSource } from "./clerk.js";
|
|
4
|
+
import { intercomSource } from "./intercom.js";
|
|
4
5
|
import { segmentSource } from "./segment.js";
|
|
5
6
|
import { stripeSource } from "./stripe.js";
|
|
6
7
|
import { supabaseSource } from "./supabase.js";
|
|
7
8
|
|
|
8
9
|
export { clerkSource } from "./clerk.js";
|
|
10
|
+
export { intercomSource } from "./intercom.js";
|
|
9
11
|
export { segmentSource } from "./segment.js";
|
|
10
12
|
export { stripeSource } from "./stripe.js";
|
|
11
13
|
export { supabaseSource } from "./supabase.js";
|
|
@@ -19,9 +21,10 @@ export const PRESET_SOURCES = {
|
|
|
19
21
|
supabase: supabaseSource,
|
|
20
22
|
stripe: stripeSource,
|
|
21
23
|
segment: segmentSource,
|
|
24
|
+
intercom: intercomSource,
|
|
22
25
|
} satisfies Record<string, DefinedWebhookSource>;
|
|
23
26
|
|
|
24
|
-
/** The stable id of a shipped preset (`"clerk" | "supabase" | "stripe" | "segment"`). */
|
|
27
|
+
/** The stable id of a shipped preset (`"clerk" | "supabase" | "stripe" | "segment" | "intercom"`). */
|
|
25
28
|
export type PresetId = keyof typeof PRESET_SOURCES;
|
|
26
29
|
|
|
27
30
|
/** The slice of the validated env `presetsFromEnv` reads (preset secrets + override). */
|
|
@@ -31,6 +34,7 @@ type PresetEnv = Pick<
|
|
|
31
34
|
| "SUPABASE_WEBHOOK_SECRET"
|
|
32
35
|
| "STRIPE_WEBHOOK_SECRET"
|
|
33
36
|
| "SEGMENT_WEBHOOK_SECRET"
|
|
37
|
+
| "INTERCOM_CLIENT_SECRET"
|
|
34
38
|
| "ENABLED_WEBHOOK_PRESETS"
|
|
35
39
|
>;
|
|
36
40
|
|