@growth-labs/store 0.1.0 → 0.2.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/dist/api.d.ts.map +1 -1
- package/dist/api.js +5 -0
- package/dist/api.js.map +1 -1
- package/dist/health.d.ts +17 -1
- package/dist/health.d.ts.map +1 -1
- package/dist/health.js +65 -10
- package/dist/health.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -1
- package/dist/options.d.ts +107 -1
- package/dist/options.d.ts.map +1 -1
- package/dist/options.js +24 -1
- package/dist/options.js.map +1 -1
- package/dist/orders.d.ts +34 -0
- package/dist/orders.d.ts.map +1 -0
- package/dist/orders.js +169 -0
- package/dist/orders.js.map +1 -0
- package/migrations/0001_gl_store_orders.sql +23 -0
- package/package.json +2 -1
- package/src/api.ts +5 -0
- package/src/health.ts +90 -12
- package/src/index.ts +18 -0
- package/src/options.ts +26 -1
- package/src/orders.ts +223 -0
package/dist/api.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,kBAAkB,EAA4B,MAAM,YAAY,CAAA;AAE9E,eAAO,MAAM,2BAA2B,6BAA6B,CAAA;AAErE,MAAM,WAAW,WAAW;IAC3B,yBAAyB,CAAC,EAAE,MAAM,CAAA;IAClC,0BAA0B,CAAC,EAAE,MAAM,CAAA;CACnC;AAED,MAAM,WAAW,UAAU;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAA;CAC1B;AASD,wBAAsB,sBAAsB,CAAC,KAAK,EAAE;IACnD,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,EAAE,WAAW,GAAG,IAAI,GAAG,SAAS,CAAA;IACnC,UAAU,EAAE,MAAM,CAAA;CAClB,GAAG,OAAO,CAAC,QAAQ,C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AAE,UAAU,GAAG,IAAI,GAAG,SAAS,CAAA;IAClC,UAAU,CAAC,EAAE,MAAM,CAAA;CACnB,GAAG,OAAO,CAAC,QAAQ,CAAC,CA+CpB;AAED,qFAAqF;AACrF,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAE/E"}
|
package/dist/api.js
CHANGED
|
@@ -77,6 +77,7 @@ export async function createWebhookResponse(input) {
|
|
|
77
77
|
}
|
|
78
78
|
const signature = input.request.headers.get(FOURTHWALL_SIGNATURE_HEADER);
|
|
79
79
|
if (!signature) {
|
|
80
|
+
console.error('store.webhook.invalid_signature', { reason: 'missing' });
|
|
80
81
|
return Response.json({ error: 'Missing signature' }, { status: 401 });
|
|
81
82
|
}
|
|
82
83
|
let rawBody;
|
|
@@ -88,6 +89,10 @@ export async function createWebhookResponse(input) {
|
|
|
88
89
|
}
|
|
89
90
|
const isValid = await verifySignature(rawBody, signature, secret, input.cryptoImpl ?? globalThis.crypto).catch(() => false);
|
|
90
91
|
if (!isValid) {
|
|
92
|
+
// Tripwire (channel-store-recovery B2): a signature failure is either an
|
|
93
|
+
// attacker probing the endpoint or the wrong secret configured after a
|
|
94
|
+
// rotation — both are worth alerting on, unlike the routine 500 above.
|
|
95
|
+
console.error('store.webhook.invalid_signature', { reason: 'mismatch' });
|
|
91
96
|
return Response.json({ error: 'Invalid signature' }, { status: 401 });
|
|
92
97
|
}
|
|
93
98
|
let body;
|
package/dist/api.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"api.js","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,EAA2B,wBAAwB,EAAE,MAAM,YAAY,CAAA;AAE9E,MAAM,CAAC,MAAM,2BAA2B,GAAG,0BAA0B,C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AiB,EACjB,MAAc,EACd,UAAkB;IAElB,MAAM,GAAG,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,SAAS,CAC5C,KAAK,EACL,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,EAChC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,EACjC,KAAK,EACL,CAAC,MAAM,CAAC,CACR,CAAA;IACD,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAA;IAC3F,MAAM,QAAQ,GAAG,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAA;IACrE,OAAO,eAAe,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAA;AAC5C,CAAC"}
|
package/dist/health.d.ts
CHANGED
|
@@ -10,7 +10,23 @@ export interface StoreHealthCheckResult {
|
|
|
10
10
|
ms: number;
|
|
11
11
|
error?: string;
|
|
12
12
|
}
|
|
13
|
-
export
|
|
13
|
+
export interface StoreHealthCheckOptions {
|
|
14
|
+
/** D1 binding to query for the order-staleness tripwire. Defaults to `telemetry.d1Binding`'s default, `"DB"`. */
|
|
15
|
+
d1Binding?: string;
|
|
16
|
+
/** Site identity to scope the staleness query to. Defaults to `telemetry.siteId`. Pass it from the resolved store options. */
|
|
17
|
+
siteId?: string;
|
|
18
|
+
/** Alert once a site with prior orders has gone this many days without a new one. Default 7. */
|
|
19
|
+
staleOrderAlertDays?: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* `health({ checks: [createStoreHealthCheck()] })` needs no arguments in the
|
|
23
|
+
* common case: the integration already resolved `telemetry.{d1Binding,
|
|
24
|
+
* siteId,staleOrderAlertDays}` at `astro:config:setup` time and stashed it in
|
|
25
|
+
* `state.ts`'s module singleton (the same mechanism the `.astro` page
|
|
26
|
+
* components use for `getConfig()`). Explicit options here only override
|
|
27
|
+
* that default — useful for tests, never required in a real site.
|
|
28
|
+
*/
|
|
29
|
+
export declare function createStoreHealthCheck(options?: StoreHealthCheckOptions): {
|
|
14
30
|
name: string;
|
|
15
31
|
check: () => Promise<StoreHealthCheckResult>;
|
|
16
32
|
};
|
package/dist/health.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../src/health.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../src/health.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,MAAM,WAAW,sBAAsB;IACtC,EAAE,EAAE,OAAO,CAAA;IACX,EAAE,EAAE,MAAM,CAAA;IACV,KAAK,CAAC,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,uBAAuB;IACvC,iHAAiH;IACjH,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,8HAA8H;IAC9H,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,gGAAgG;IAChG,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC5B;AA8DD;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,GAAE,uBAA4B,GAAG;IAC9E,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,EAAE,MAAM,OAAO,CAAC,sBAAsB,CAAC,CAAA;CAC5C,CAmCA"}
|
package/dist/health.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isWebhookConfigured } from './api.js';
|
|
2
|
+
import { getConfigOrNull } from './state.js';
|
|
2
3
|
/**
|
|
3
4
|
* Resolves `cloudflare:workers` via a dynamic import wrapped in try/catch,
|
|
4
5
|
* keeping the specifier off this module's static import graph — this file
|
|
@@ -16,22 +17,76 @@ async function resolveCloudflareEnv() {
|
|
|
16
17
|
return null;
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
|
-
|
|
20
|
+
/**
|
|
21
|
+
* Tripwire (channel-store-recovery B2): a site that has ever recorded an
|
|
22
|
+
* order but has gone quiet for `staleOrderAlertDays` is almost certainly
|
|
23
|
+
* broken (a silently-failing webhook, a revoked secret, a Fourthwall-side
|
|
24
|
+
* change) rather than genuinely out of sales — the same silent-500 failure
|
|
25
|
+
* mode that started this program. A site with zero orders ever (bigbrain
|
|
26
|
+
* pre-launch, or any site before its first sale) is not alerted on: there is
|
|
27
|
+
* no prior signal to regress from.
|
|
28
|
+
*/
|
|
29
|
+
async function checkOrderStaleness(env, options) {
|
|
30
|
+
const db = env[options.d1Binding];
|
|
31
|
+
if (!db)
|
|
32
|
+
return undefined; // covered by the webhook-secret check above; avoid a duplicate red reason
|
|
33
|
+
let lastReceivedAt;
|
|
34
|
+
try {
|
|
35
|
+
lastReceivedAt = await db
|
|
36
|
+
.prepare('SELECT MAX(received_at) as last_received_at FROM gl_store_orders WHERE site_id = ?')
|
|
37
|
+
.bind(options.siteId)
|
|
38
|
+
.first('last_received_at');
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
return `gl_store_orders query failed: ${error instanceof Error ? error.message : String(error)}`;
|
|
42
|
+
}
|
|
43
|
+
if (lastReceivedAt === null || lastReceivedAt === undefined)
|
|
44
|
+
return undefined;
|
|
45
|
+
const staleMs = options.staleOrderAlertDays * 24 * 60 * 60 * 1000;
|
|
46
|
+
const ageMs = Date.now() - lastReceivedAt;
|
|
47
|
+
if (ageMs > staleMs) {
|
|
48
|
+
const days = Math.floor(ageMs / (24 * 60 * 60 * 1000));
|
|
49
|
+
return `no order ingested for ${days}d (alert threshold ${options.staleOrderAlertDays}d) — last order at ${new Date(lastReceivedAt).toISOString()}`;
|
|
50
|
+
}
|
|
51
|
+
return undefined;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* `health({ checks: [createStoreHealthCheck()] })` needs no arguments in the
|
|
55
|
+
* common case: the integration already resolved `telemetry.{d1Binding,
|
|
56
|
+
* siteId,staleOrderAlertDays}` at `astro:config:setup` time and stashed it in
|
|
57
|
+
* `state.ts`'s module singleton (the same mechanism the `.astro` page
|
|
58
|
+
* components use for `getConfig()`). Explicit options here only override
|
|
59
|
+
* that default — useful for tests, never required in a real site.
|
|
60
|
+
*/
|
|
61
|
+
export function createStoreHealthCheck(options = {}) {
|
|
62
|
+
const resolvedConfig = getConfigOrNull();
|
|
63
|
+
const d1Binding = options.d1Binding ?? resolvedConfig?.telemetry.d1Binding ?? 'DB';
|
|
64
|
+
const siteId = options.siteId ?? resolvedConfig?.telemetry.siteId;
|
|
65
|
+
const staleOrderAlertDays = options.staleOrderAlertDays ?? resolvedConfig?.telemetry.staleOrderAlertDays ?? 7;
|
|
20
66
|
return {
|
|
21
67
|
name: 'store',
|
|
22
68
|
async check() {
|
|
23
69
|
const start = Date.now();
|
|
24
70
|
const env = await resolveCloudflareEnv();
|
|
25
71
|
const configured = isWebhookConfigured(env);
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
72
|
+
if (!configured) {
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
ms: Date.now() - start,
|
|
76
|
+
error: 'FW_WEBHOOK_SECRET is not set — order telemetry is silently dropping orders',
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (env && siteId) {
|
|
80
|
+
const staleReason = await checkOrderStaleness(env, {
|
|
81
|
+
d1Binding,
|
|
82
|
+
siteId,
|
|
83
|
+
staleOrderAlertDays,
|
|
84
|
+
});
|
|
85
|
+
if (staleReason) {
|
|
86
|
+
return { ok: false, ms: Date.now() - start, error: staleReason };
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return { ok: true, ms: Date.now() - start };
|
|
35
90
|
},
|
|
36
91
|
};
|
|
37
92
|
}
|
package/dist/health.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"health.js","sourceRoot":"","sources":["../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"health.js","sourceRoot":"","sources":["../src/health.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAA;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AA8B5C;;;;;;;GAOG;AACH,KAAK,UAAU,oBAAoB;IAClC,IAAI,CAAC;QACJ,MAAM,GAAG,GAAG,CAAC,MAAM,MAAM,CAAC,oBAAoB,CAAC,CAAsC,CAAA;QACrF,OAAO,GAAG,EAAE,GAAG,IAAI,IAAI,CAAA;IACxB,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAA;IACZ,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,KAAK,UAAU,mBAAmB,CACjC,GAA4B,EAC5B,OAAgG;IAEhG,MAAM,EAAE,GAAG,GAAG,CAAC,OAAO,CAAC,SAAS,CAA+B,CAAA;IAC/D,IAAI,CAAC,EAAE;QAAE,OAAO,SAAS,CAAA,CAAC,0EAA0E;IAEpG,IAAI,cAA6B,CAAA;IACjC,IAAI,CAAC;QACJ,cAAc,GAAG,MAAM,EAAE;aACvB,OAAO,CAAC,oFAAoF,CAAC;aAC7F,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC;aACpB,KAAK,CAAgB,kBAAkB,CAAC,CAAA;IAC3C,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,iCAAiC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAA;IACjG,CAAC;IAED,IAAI,cAAc,KAAK,IAAI,IAAI,cAAc,KAAK,SAAS;QAAE,OAAO,SAAS,CAAA;IAE7E,MA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;oBACtB,KAAK,EAAE,4EAA4E;iBACnF,CAAA;YACF,CAAC;YAED,IAAI,GAAG,IAAI,MAAM,EAAE,CAAC;gBACnB,MAAM,WAAW,GAAG,MAAM,mBAAmB,CAAC,GAAG,EAAE;oBAClD,SAAS;oBACT,MAAM;oBACN,mBAAmB;iBACnB,CAAC,CAAA;gBACF,IAAI,WAAW,EAAE,CAAC;oBACjB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,CAAA;gBACjE,CAAC;YACF,CAAC;YAED,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,EAAE,CAAA;QAC5C,CAAC;KACD,CAAA;AACF,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -11,10 +11,13 @@ export type { FourthwallClient, FourthwallClientConfig } from './client.js';
|
|
|
11
11
|
export { createFourthwallClient } from './client.js';
|
|
12
12
|
export type { StoreCollection, StoreCollectionGrouping } from './collections.js';
|
|
13
13
|
export { filterPublishedCollections, getGroupedCollections, getStoreCollection, resolveProductKind, } from './collections.js';
|
|
14
|
+
export type { StoreHealthCheckOptions } from './health.js';
|
|
14
15
|
export { createStoreHealthCheck } from './health.js';
|
|
15
16
|
export { hasProductImages, optimizeFwImage } from './image.js';
|
|
16
17
|
export type { ResolvedStoreOptions, StoreOptions } from './options.js';
|
|
17
18
|
export { storeOptionsSchema } from './options.js';
|
|
19
|
+
export type { OrderTelemetryOptions } from './orders.js';
|
|
20
|
+
export { buildStoreOrderWAEDataPoint, createD1OrderHandler } from './orders.js';
|
|
18
21
|
export type * from './types.js';
|
|
19
22
|
export type { MerchantReturnPolicy, StoreVariantClientData } from './variant.js';
|
|
20
23
|
export { buildStoreProductJsonLd, buildStoreVariantClientData, createStoreAddToCartAvailabilityState, isStoreVariantAvailable, normalizeStoreVariantSku, selectStoreVariant, } from './variant.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,OAAO,CAAA;AAE7C,OAAO,EAAE,KAAK,YAAY,EAAsB,MAAM,cAAc,CAAA;AAepE,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,WAAW,EAAE,YAAY,GAAG,gBAAgB,CAyCzE;AAED,YAAY,EAAE,WAAW,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAC3E,OAAO,EAAE,2BAA2B,EAAE,mBAAmB,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAA;AAClG,YAAY,EAAE,sBAAsB,EAAE,4BAA4B,EAAE,MAAM,mBAAmB,CAAA;AAC7F,OAAO,EAAE,6BAA6B,EAAE,MAAM,mBAAmB,CAAA;AACjE,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAC/F,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAA;AACpD,YAAY,EAAE,gBAAgB,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACpD,YAAY,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAA;AAChF,OAAO,EACN,0BAA0B,EAC1B,qBAAqB,EACrB,kBAAkB,EAClB,kBAAkB,GAClB,MAAM,kBAAkB,CAAA;AACzB,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAA;AAC1D,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAC9D,YAAY,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACjD,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAA;AACxD,OAAO,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAC/E,mBAAmB,YAAY,CAAA;AAC/B,YAAY,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAA;AAChF,OAAO,EACN,uBAAuB,EACvB,2BAA2B,EAC3B,qCAAqC,EACrC,uBAAuB,EACvB,wBAAwB,EACxB,kBAAkB,GAClB,MAAM,cAAc,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url';
|
|
2
|
+
import { setOrderPlacedHandler } from './api.js';
|
|
2
3
|
import { storeOptionsSchema } from './options.js';
|
|
4
|
+
import { createD1OrderHandler } from './orders.js';
|
|
3
5
|
import { _setConfig } from './state.js';
|
|
4
6
|
import { growthLabsStorePlugin } from './vite-plugin.js';
|
|
5
7
|
function isSourceEntrypoint() {
|
|
@@ -12,6 +14,16 @@ function resolveEntrypoint(path) {
|
|
|
12
14
|
export default function store(userOptions) {
|
|
13
15
|
const options = storeOptionsSchema.parse(userOptions);
|
|
14
16
|
_setConfig(options);
|
|
17
|
+
// B2 (channel-store-recovery): every consuming site gets D1 order
|
|
18
|
+
// persistence + the store_order_placed WAE event automatically — the
|
|
19
|
+
// package owns this now, sites never wire it by hand. A site with its own
|
|
20
|
+
// order pipeline can still override via `setOrderPlacedHandler()` after
|
|
21
|
+
// `store()` runs.
|
|
22
|
+
setOrderPlacedHandler(createD1OrderHandler({
|
|
23
|
+
d1Binding: options.telemetry.d1Binding,
|
|
24
|
+
waeBinding: options.telemetry.waeBinding,
|
|
25
|
+
siteId: options.telemetry.siteId,
|
|
26
|
+
}));
|
|
15
27
|
return {
|
|
16
28
|
name: '@growth-labs/store',
|
|
17
29
|
hooks: {
|
|
@@ -44,5 +56,6 @@ export { filterPublishedCollections, getGroupedCollections, getStoreCollection,
|
|
|
44
56
|
export { createStoreHealthCheck } from './health.js';
|
|
45
57
|
export { hasProductImages, optimizeFwImage } from './image.js';
|
|
46
58
|
export { storeOptionsSchema } from './options.js';
|
|
59
|
+
export { buildStoreOrderWAEDataPoint, createD1OrderHandler } from './orders.js';
|
|
47
60
|
export { buildStoreProductJsonLd, buildStoreVariantClientData, createStoreAddToCartAvailabilityState, isStoreVariantAvailable, normalizeStoreVariantSku, selectStoreVariant, } from './variant.js';
|
|
48
61
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC,OAAO,EAAqB,kBAAkB,EAAE,MAAM,cAAc,CAAA;AACpE,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAA;AACvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAA;AAExD,SAAS,kBAAkB;IAC1B,OAAO,OAAO,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAA;AAC9E,CAAC;AAED,MAAM,oBAAoB,GAAG,kBAAkB,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAA;AAEjE,SAAS,iBAAiB,CAAC,IAAY;IACtC,OAAO,aAAa,CAAC,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,oBAAoB,EAAE,EAAE,MAAM,CAAC,IAAI,CAAC,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AAA;AACpD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAE9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAA;AAEjD,OAAO,EAAE,2BAA2B,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAG/E,OAAO,EACN,uBAAuB,EACvB,2BAA2B,EAC3B,qCAAqC,EACrC,uBAAuB,EACvB,wBAAwB,EACxB,kBAAkB,GAClB,MAAM,cAAc,CAAA"}
|
package/dist/options.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ declare const storeCollectionSchema: z.ZodObject<{
|
|
|
24
24
|
hidden?: boolean | undefined;
|
|
25
25
|
productKind?: string | undefined;
|
|
26
26
|
}>;
|
|
27
|
-
export declare const storeOptionsSchema: z.ZodObject<{
|
|
27
|
+
export declare const storeOptionsSchema: z.ZodEffects<z.ZodObject<{
|
|
28
28
|
/** Shown in the store JSON-LD `brand.name` and page copy. */
|
|
29
29
|
brandName: z.ZodString;
|
|
30
30
|
/**
|
|
@@ -108,7 +108,107 @@ export declare const storeOptionsSchema: z.ZodObject<{
|
|
|
108
108
|
webhookPath?: string | undefined;
|
|
109
109
|
cartBootstrapPath?: string | undefined;
|
|
110
110
|
}>>;
|
|
111
|
+
/**
|
|
112
|
+
* B2 order telemetry: every verified `ORDER_PLACED` webhook is persisted to
|
|
113
|
+
* the package-owned `gl_store_orders` D1 table and mirrored to WAE as
|
|
114
|
+
* `store_order_placed`. `siteId` defaults to `cartOrigin` — the site
|
|
115
|
+
* already has exactly one identity for attribution, so this avoids asking
|
|
116
|
+
* for the same value twice.
|
|
117
|
+
*/
|
|
118
|
+
telemetry: z.ZodDefault<z.ZodObject<{
|
|
119
|
+
d1Binding: z.ZodDefault<z.ZodString>;
|
|
120
|
+
waeBinding: z.ZodDefault<z.ZodString>;
|
|
121
|
+
siteId: z.ZodOptional<z.ZodString>;
|
|
122
|
+
/** Health check goes red when a site with prior orders has ingested none for this many days. */
|
|
123
|
+
staleOrderAlertDays: z.ZodDefault<z.ZodNumber>;
|
|
124
|
+
}, "strip", z.ZodTypeAny, {
|
|
125
|
+
d1Binding: string;
|
|
126
|
+
waeBinding: string;
|
|
127
|
+
staleOrderAlertDays: number;
|
|
128
|
+
siteId?: string | undefined;
|
|
129
|
+
}, {
|
|
130
|
+
d1Binding?: string | undefined;
|
|
131
|
+
waeBinding?: string | undefined;
|
|
132
|
+
siteId?: string | undefined;
|
|
133
|
+
staleOrderAlertDays?: number | undefined;
|
|
134
|
+
}>>;
|
|
111
135
|
}, "strip", z.ZodTypeAny, {
|
|
136
|
+
brandName: string;
|
|
137
|
+
collections: {
|
|
138
|
+
name: string;
|
|
139
|
+
handle: string;
|
|
140
|
+
hidden: boolean;
|
|
141
|
+
productKind: string;
|
|
142
|
+
description?: string | undefined;
|
|
143
|
+
coverImagePath?: string | undefined;
|
|
144
|
+
grouping?: "type" | "division" | undefined;
|
|
145
|
+
}[];
|
|
146
|
+
catalogueHandle: string;
|
|
147
|
+
landingProductCount: number | "all";
|
|
148
|
+
defaultCurrency: string;
|
|
149
|
+
supportedCurrencies: string[];
|
|
150
|
+
cartOrigin: string;
|
|
151
|
+
checkoutEnabled: boolean;
|
|
152
|
+
imageTransform: {
|
|
153
|
+
enabled: boolean;
|
|
154
|
+
};
|
|
155
|
+
routes: {
|
|
156
|
+
checkoutPath: string;
|
|
157
|
+
webhookPath: string;
|
|
158
|
+
cartBootstrapPath: string;
|
|
159
|
+
};
|
|
160
|
+
telemetry: {
|
|
161
|
+
d1Binding: string;
|
|
162
|
+
waeBinding: string;
|
|
163
|
+
staleOrderAlertDays: number;
|
|
164
|
+
siteId?: string | undefined;
|
|
165
|
+
};
|
|
166
|
+
merchantReturnPolicy?: {
|
|
167
|
+
applicableCountry: string;
|
|
168
|
+
returnPolicyCategory: string;
|
|
169
|
+
} | undefined;
|
|
170
|
+
}, {
|
|
171
|
+
brandName: string;
|
|
172
|
+
cartOrigin: string;
|
|
173
|
+
collections?: {
|
|
174
|
+
name: string;
|
|
175
|
+
handle: string;
|
|
176
|
+
description?: string | undefined;
|
|
177
|
+
coverImagePath?: string | undefined;
|
|
178
|
+
grouping?: "type" | "division" | undefined;
|
|
179
|
+
hidden?: boolean | undefined;
|
|
180
|
+
productKind?: string | undefined;
|
|
181
|
+
}[] | undefined;
|
|
182
|
+
catalogueHandle?: string | undefined;
|
|
183
|
+
landingProductCount?: number | "all" | undefined;
|
|
184
|
+
defaultCurrency?: string | undefined;
|
|
185
|
+
supportedCurrencies?: string[] | undefined;
|
|
186
|
+
checkoutEnabled?: boolean | undefined;
|
|
187
|
+
merchantReturnPolicy?: {
|
|
188
|
+
applicableCountry: string;
|
|
189
|
+
returnPolicyCategory: string;
|
|
190
|
+
} | undefined;
|
|
191
|
+
imageTransform?: {
|
|
192
|
+
enabled?: boolean | undefined;
|
|
193
|
+
} | undefined;
|
|
194
|
+
routes?: {
|
|
195
|
+
checkoutPath?: string | undefined;
|
|
196
|
+
webhookPath?: string | undefined;
|
|
197
|
+
cartBootstrapPath?: string | undefined;
|
|
198
|
+
} | undefined;
|
|
199
|
+
telemetry?: {
|
|
200
|
+
d1Binding?: string | undefined;
|
|
201
|
+
waeBinding?: string | undefined;
|
|
202
|
+
siteId?: string | undefined;
|
|
203
|
+
staleOrderAlertDays?: number | undefined;
|
|
204
|
+
} | undefined;
|
|
205
|
+
}>, {
|
|
206
|
+
telemetry: {
|
|
207
|
+
siteId: string;
|
|
208
|
+
d1Binding: string;
|
|
209
|
+
waeBinding: string;
|
|
210
|
+
staleOrderAlertDays: number;
|
|
211
|
+
};
|
|
112
212
|
brandName: string;
|
|
113
213
|
collections: {
|
|
114
214
|
name: string;
|
|
@@ -166,6 +266,12 @@ export declare const storeOptionsSchema: z.ZodObject<{
|
|
|
166
266
|
webhookPath?: string | undefined;
|
|
167
267
|
cartBootstrapPath?: string | undefined;
|
|
168
268
|
} | undefined;
|
|
269
|
+
telemetry?: {
|
|
270
|
+
d1Binding?: string | undefined;
|
|
271
|
+
waeBinding?: string | undefined;
|
|
272
|
+
siteId?: string | undefined;
|
|
273
|
+
staleOrderAlertDays?: number | undefined;
|
|
274
|
+
} | undefined;
|
|
169
275
|
}>;
|
|
170
276
|
export type StoreOptions = z.input<typeof storeOptionsSchema>;
|
|
171
277
|
export type ResolvedStoreOptions = z.output<typeof storeOptionsSchema>;
|
package/dist/options.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,QAAA,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;EAQzB,CAAA;
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,QAAA,MAAM,qBAAqB;;;;;;;;;;;;;;;;;;;;;;;;EAQzB,CAAA;AA8EF,eAAO,MAAM,kBAAkB;IAtE9B,6DAA6D;;IAG7D;;;;;OAKG;;;;;;;;;;;;;;;;;;;;;;;;;;IAGH,8EAA8E;;IAG9E;;;;;OAKG;;;;IAMH,2FAA2F;;IAG3F;;;;;OAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAmBH;;;;;;OAMG;;;;;QAMD,gGAAgG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAYhG,CAAA;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAC7D,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,kBAAkB,CAAC,CAAA;AACtE,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,qBAAqB,CAAC,CAAA"}
|
package/dist/options.js
CHANGED
|
@@ -12,7 +12,7 @@ const merchantReturnPolicySchema = z.object({
|
|
|
12
12
|
applicableCountry: z.string().min(1),
|
|
13
13
|
returnPolicyCategory: z.string().min(1),
|
|
14
14
|
});
|
|
15
|
-
|
|
15
|
+
const baseStoreOptionsSchema = z.object({
|
|
16
16
|
/** Shown in the store JSON-LD `brand.name` and page copy. */
|
|
17
17
|
brandName: z.string().min(1),
|
|
18
18
|
/**
|
|
@@ -55,5 +55,28 @@ export const storeOptionsSchema = z.object({
|
|
|
55
55
|
cartBootstrapPath: z.string().default('/api/store/cart-bootstrap'),
|
|
56
56
|
})
|
|
57
57
|
.default({}),
|
|
58
|
+
/**
|
|
59
|
+
* B2 order telemetry: every verified `ORDER_PLACED` webhook is persisted to
|
|
60
|
+
* the package-owned `gl_store_orders` D1 table and mirrored to WAE as
|
|
61
|
+
* `store_order_placed`. `siteId` defaults to `cartOrigin` — the site
|
|
62
|
+
* already has exactly one identity for attribution, so this avoids asking
|
|
63
|
+
* for the same value twice.
|
|
64
|
+
*/
|
|
65
|
+
telemetry: z
|
|
66
|
+
.object({
|
|
67
|
+
d1Binding: z.string().default('DB'),
|
|
68
|
+
waeBinding: z.string().default('ANALYTICS'),
|
|
69
|
+
siteId: z.string().min(1).optional(),
|
|
70
|
+
/** Health check goes red when a site with prior orders has ingested none for this many days. */
|
|
71
|
+
staleOrderAlertDays: z.number().int().positive().default(7),
|
|
72
|
+
})
|
|
73
|
+
.default({}),
|
|
58
74
|
});
|
|
75
|
+
export const storeOptionsSchema = baseStoreOptionsSchema.transform((options) => ({
|
|
76
|
+
...options,
|
|
77
|
+
telemetry: {
|
|
78
|
+
...options.telemetry,
|
|
79
|
+
siteId: options.telemetry.siteId ?? options.cartOrigin,
|
|
80
|
+
},
|
|
81
|
+
}));
|
|
59
82
|
//# sourceMappingURL=options.js.map
|
package/dist/options.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"options.js","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC;IACtC,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACzB,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACvB,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IAClC,cAAc,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;IACrC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,QAAQ,EAAE;IACjD,MAAM,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC;IAClC,WAAW,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC;CACxC,CAAC,CAAA;AAEF,MAAM,0BAA0B,GAAG,CAAC,CAAC,MAAM,CAAC;IAC3C,iBAAiB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IACpC,oBAAoB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;CACvC,CAAC,CAAA;AAEF,MAAM,
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,MAAM,qBAAqB,GAAG,CAAC,CAAC,MAAM,CAAC;IACtC,MAAM,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CA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BAAmB,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;KAC3D,CAAC;SACD,OAAO,CAAC,EAAE,CAAC;CACb,CAAC,CAAA;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG,sBAAsB,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;IAChF,GAAG,OAAO;IACV,SAAS,EAAE;QACV,GAAG,OAAO,CAAC,SAAS;QACpB,MAAM,EAAE,OAAO,CAAC,SAAS,CAAC,MAAM,IAAI,OAAO,CAAC,UAAU;KACtD;CACD,CAAC,CAAC,CAAA"}
|
package/dist/orders.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { OrderPlacedHandler } from './api.js';
|
|
2
|
+
import type { FwOrderPlacedEvent } from './types.js';
|
|
3
|
+
export interface OrderTelemetryOptions {
|
|
4
|
+
d1Binding: string;
|
|
5
|
+
waeBinding: string;
|
|
6
|
+
siteId: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Mirrors `@growth-labs/analytics`'s `store_order_placed` blob/double layout
|
|
10
|
+
* (packages don't import each other — PLATFORM_PATTERNS.md — so this
|
|
11
|
+
* replicates the slot mapping from `buildWAEDataPoint`/`buildStorefrontWAEParams`
|
|
12
|
+
* rather than calling into that package).
|
|
13
|
+
*/
|
|
14
|
+
export declare function buildStoreOrderWAEDataPoint(options: OrderTelemetryOptions, event: FwOrderPlacedEvent, rawPayloadHash: string): {
|
|
15
|
+
blobs: string[];
|
|
16
|
+
doubles: number[];
|
|
17
|
+
indexes: string[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* B2's order-telemetry handler, wired via `setOrderPlacedHandler` from
|
|
21
|
+
* `index.ts` for every consuming site automatically: persists every verified
|
|
22
|
+
* `ORDER_PLACED` webhook to the package-owned `gl_store_orders` D1 table
|
|
23
|
+
* (migrations/0001_gl_store_orders.sql) and emits the mirrored WAE point.
|
|
24
|
+
*
|
|
25
|
+
* The D1 write and the WAE write are independent best-effort operations —
|
|
26
|
+
* a WAE outage must not drop the durable order row, and vice versa. Both
|
|
27
|
+
* failure paths log a structured, greppable event rather than throwing:
|
|
28
|
+
* `createWebhookResponse` has already validated the HMAC signature and is
|
|
29
|
+
* about to return 200 to Fourthwall by the time this handler runs, so
|
|
30
|
+
* throwing here would not trigger a useful retry, only an unhandled
|
|
31
|
+
* rejection after the response was already sent.
|
|
32
|
+
*/
|
|
33
|
+
export declare function createD1OrderHandler(options: OrderTelemetryOptions): OrderPlacedHandler;
|
|
34
|
+
//# sourceMappingURL=orders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orders.d.ts","sourceRoot":"","sources":["../src/orders.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAA;AAClD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEpD,MAAM,WAAW,qBAAqB;IACrC,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,EAAE,MAAM,CAAA;CACd;AAmED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CAC1C,OAAO,EAAE,qBAAqB,EAC9B,KAAK,EAAE,kBAAkB,EACzB,cAAc,EAAE,MAAM,GACpB;IAAE,KAAK,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,CAwD3D;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,qBAAqB,GAAG,kBAAkB,CAkEvF"}
|
package/dist/orders.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves `cloudflare:workers` via a dynamic import wrapped in try/catch —
|
|
3
|
+
* `createD1OrderHandler` is wired from `index.ts`'s `astro:config:setup`
|
|
4
|
+
* hook, which runs under plain Node during `astro build`/`astro check`.
|
|
5
|
+
* See health.ts and scripts/verify-pure-node-import.mjs for the same pattern.
|
|
6
|
+
*/
|
|
7
|
+
async function resolveTelemetryEnv() {
|
|
8
|
+
try {
|
|
9
|
+
const mod = (await import('cloudflare:workers'));
|
|
10
|
+
return mod?.env ?? null;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
async function sha256Hex(input) {
|
|
17
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input));
|
|
18
|
+
return Array.from(new Uint8Array(digest))
|
|
19
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
20
|
+
.join('');
|
|
21
|
+
}
|
|
22
|
+
function orderItemCount(event) {
|
|
23
|
+
return event.data.offers.reduce((total, offer) => total + (offer.variant.quantity ?? 0), 0);
|
|
24
|
+
}
|
|
25
|
+
function buildItemsJson(event) {
|
|
26
|
+
return JSON.stringify(event.data.offers.map((offer) => ({
|
|
27
|
+
offerId: offer.id,
|
|
28
|
+
name: offer.name ?? null,
|
|
29
|
+
variantId: offer.variant.id,
|
|
30
|
+
sku: offer.variant.sku ?? null,
|
|
31
|
+
quantity: offer.variant.quantity ?? 0,
|
|
32
|
+
unitPrice: (offer.variant.unitPrice ?? offer.variant.price)?.value ?? null,
|
|
33
|
+
})));
|
|
34
|
+
}
|
|
35
|
+
function cartOriginFor(event, fallback) {
|
|
36
|
+
const metadata = event.data.metadata;
|
|
37
|
+
const fromMetadata = metadata && typeof metadata.cart_origin === 'string'
|
|
38
|
+
? metadata.cart_origin
|
|
39
|
+
: undefined;
|
|
40
|
+
return fromMetadata ?? fallback;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Mirrors `@growth-labs/analytics`'s `store_order_placed` blob/double layout
|
|
44
|
+
* (packages don't import each other — PLATFORM_PATTERNS.md — so this
|
|
45
|
+
* replicates the slot mapping from `buildWAEDataPoint`/`buildStorefrontWAEParams`
|
|
46
|
+
* rather than calling into that package).
|
|
47
|
+
*/
|
|
48
|
+
export function buildStoreOrderWAEDataPoint(options, event, rawPayloadHash) {
|
|
49
|
+
const utm = event.data.trackingParams ?? {};
|
|
50
|
+
const labelExtras = {
|
|
51
|
+
cartOrigin: cartOriginFor(event, options.siteId),
|
|
52
|
+
rawPayloadHash,
|
|
53
|
+
itemCount: orderItemCount(event),
|
|
54
|
+
};
|
|
55
|
+
if (event.data.email)
|
|
56
|
+
labelExtras.customerEmail = event.data.email;
|
|
57
|
+
return {
|
|
58
|
+
blobs: [
|
|
59
|
+
'store_order_placed', // blob1 eventName
|
|
60
|
+
options.siteId, // blob2 siteId
|
|
61
|
+
'', // blob3 visitorId
|
|
62
|
+
'', // blob4 sessionId
|
|
63
|
+
'', // blob5 pageUrl
|
|
64
|
+
'', // blob6 pagePath
|
|
65
|
+
'', // blob7 referrer
|
|
66
|
+
utm.utm_source ?? '', // blob8 utmSource
|
|
67
|
+
utm.utm_medium ?? '', // blob9 utmMedium
|
|
68
|
+
utm.utm_campaign ?? '', // blob10 utmCampaign
|
|
69
|
+
'', // blob11 derivedSource
|
|
70
|
+
'', // blob12 country
|
|
71
|
+
'', // blob13 deviceType
|
|
72
|
+
'', // blob14 browserName
|
|
73
|
+
'', // blob15 osName
|
|
74
|
+
event.data.id, // blob16 contentSlug = orderId
|
|
75
|
+
'order', // blob17 contentType
|
|
76
|
+
'storefront', // blob18 eventCategory
|
|
77
|
+
JSON.stringify(labelExtras), // blob19 eventLabel
|
|
78
|
+
'false', // blob20 isNewVisitor
|
|
79
|
+
],
|
|
80
|
+
doubles: [
|
|
81
|
+
Date.now(), // double1 timestamp
|
|
82
|
+
orderItemCount(event), // double2 eventValue
|
|
83
|
+
0,
|
|
84
|
+
0,
|
|
85
|
+
0,
|
|
86
|
+
0,
|
|
87
|
+
0,
|
|
88
|
+
0,
|
|
89
|
+
0, // double3-9
|
|
90
|
+
0, // double10 productPrice (not applicable to a whole order)
|
|
91
|
+
event.data.amounts.total.value, // double11 cartValue
|
|
92
|
+
0,
|
|
93
|
+
0,
|
|
94
|
+
0,
|
|
95
|
+
0,
|
|
96
|
+
0,
|
|
97
|
+
0,
|
|
98
|
+
0,
|
|
99
|
+
0,
|
|
100
|
+
0, // double12-20
|
|
101
|
+
],
|
|
102
|
+
indexes: ['store_order_placed'],
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* B2's order-telemetry handler, wired via `setOrderPlacedHandler` from
|
|
107
|
+
* `index.ts` for every consuming site automatically: persists every verified
|
|
108
|
+
* `ORDER_PLACED` webhook to the package-owned `gl_store_orders` D1 table
|
|
109
|
+
* (migrations/0001_gl_store_orders.sql) and emits the mirrored WAE point.
|
|
110
|
+
*
|
|
111
|
+
* The D1 write and the WAE write are independent best-effort operations —
|
|
112
|
+
* a WAE outage must not drop the durable order row, and vice versa. Both
|
|
113
|
+
* failure paths log a structured, greppable event rather than throwing:
|
|
114
|
+
* `createWebhookResponse` has already validated the HMAC signature and is
|
|
115
|
+
* about to return 200 to Fourthwall by the time this handler runs, so
|
|
116
|
+
* throwing here would not trigger a useful retry, only an unhandled
|
|
117
|
+
* rejection after the response was already sent.
|
|
118
|
+
*/
|
|
119
|
+
export function createD1OrderHandler(options) {
|
|
120
|
+
return async (event) => {
|
|
121
|
+
const env = await resolveTelemetryEnv();
|
|
122
|
+
if (!env) {
|
|
123
|
+
console.error('store.order_telemetry.env_unavailable', { orderId: event.data.id });
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const rawPayloadHash = await sha256Hex(JSON.stringify(event));
|
|
127
|
+
const db = env[options.d1Binding];
|
|
128
|
+
if (!db) {
|
|
129
|
+
console.error('store.order_telemetry.d1_binding_missing', {
|
|
130
|
+
binding: options.d1Binding,
|
|
131
|
+
orderId: event.data.id,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
try {
|
|
136
|
+
await db
|
|
137
|
+
.prepare(`INSERT OR IGNORE INTO gl_store_orders
|
|
138
|
+
(id, shop_id, site_id, amount, currency, items_json, cart_origin, utm_source, utm_medium, utm_campaign, utm_term, utm_content, raw_payload_hash, received_at)
|
|
139
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
140
|
+
.bind(event.data.id, event.data.shopId ?? event.shopId ?? null, options.siteId, event.data.amounts.total.value, event.data.amounts.total.currency, buildItemsJson(event), cartOriginFor(event, options.siteId), event.data.trackingParams?.utm_source ?? null, event.data.trackingParams?.utm_medium ?? null, event.data.trackingParams?.utm_campaign ?? null, event.data.trackingParams?.utm_term ?? null, event.data.trackingParams?.utm_content ?? null, rawPayloadHash, Date.now())
|
|
141
|
+
.run();
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error('store.order_telemetry.d1_write_failed', {
|
|
145
|
+
orderId: event.data.id,
|
|
146
|
+
error: error instanceof Error ? error.message : String(error),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const wae = env[options.waeBinding];
|
|
151
|
+
if (!wae?.writeDataPoint) {
|
|
152
|
+
console.error('store.order_telemetry.wae_binding_missing', {
|
|
153
|
+
binding: options.waeBinding,
|
|
154
|
+
orderId: event.data.id,
|
|
155
|
+
});
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
await wae.writeDataPoint(buildStoreOrderWAEDataPoint(options, event, rawPayloadHash));
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
console.error('store.order_telemetry.wae_write_failed', {
|
|
163
|
+
orderId: event.data.id,
|
|
164
|
+
error: error instanceof Error ? error.message : String(error),
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=orders.js.map
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XISTS).
|
|
5
|
+
|
|
6
|
+
CREATE TABLE IF NOT EXISTS gl_store_orders (
|
|
7
|
+
id TEXT PRIMARY KEY, -- Fourthwall order id (ORDER_PLACED event's data.id)
|
|
8
|
+
shop_id TEXT,
|
|
9
|
+
site_id TEXT NOT NULL, -- this site's `cartOrigin`/`telemetry.siteId` config value
|
|
10
|
+
amount REAL NOT NULL,
|
|
11
|
+
currency TEXT NOT NULL,
|
|
12
|
+
items_json TEXT NOT NULL,
|
|
13
|
+
cart_origin TEXT,
|
|
14
|
+
utm_source TEXT,
|
|
15
|
+
utm_medium TEXT,
|
|
16
|
+
utm_campaign TEXT,
|
|
17
|
+
utm_term TEXT,
|
|
18
|
+
utm_content TEXT,
|
|
19
|
+
raw_payload_hash TEXT NOT NULL,
|
|
20
|
+
received_at INTEGER NOT NULL
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
CREATE INDEX IF NOT EXISTS gl_store_orders_site_received_idx ON gl_store_orders (site_id, received_at);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@growth-labs/store",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
"dist",
|
|
32
32
|
"src/**/*.ts",
|
|
33
33
|
"src/**/*.astro",
|
|
34
|
+
"migrations",
|
|
34
35
|
"README.md",
|
|
35
36
|
"CHANGELOG.md"
|
|
36
37
|
],
|
package/src/api.ts
CHANGED
|
@@ -115,6 +115,7 @@ export async function createWebhookResponse(input: {
|
|
|
115
115
|
|
|
116
116
|
const signature = input.request.headers.get(FOURTHWALL_SIGNATURE_HEADER)
|
|
117
117
|
if (!signature) {
|
|
118
|
+
console.error('store.webhook.invalid_signature', { reason: 'missing' })
|
|
118
119
|
return Response.json({ error: 'Missing signature' }, { status: 401 })
|
|
119
120
|
}
|
|
120
121
|
|
|
@@ -132,6 +133,10 @@ export async function createWebhookResponse(input: {
|
|
|
132
133
|
input.cryptoImpl ?? globalThis.crypto,
|
|
133
134
|
).catch(() => false)
|
|
134
135
|
if (!isValid) {
|
|
136
|
+
// Tripwire (channel-store-recovery B2): a signature failure is either an
|
|
137
|
+
// attacker probing the endpoint or the wrong secret configured after a
|
|
138
|
+
// rotation — both are worth alerting on, unlike the routine 500 above.
|
|
139
|
+
console.error('store.webhook.invalid_signature', { reason: 'mismatch' })
|
|
135
140
|
return Response.json({ error: 'Invalid signature' }, { status: 401 })
|
|
136
141
|
}
|
|
137
142
|
|
package/src/health.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isWebhookConfigured } from './api.js'
|
|
2
|
+
import { getConfigOrNull } from './state.js'
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Structurally matches `@growth-labs/health`'s `CustomCheck` without
|
|
@@ -13,6 +14,21 @@ export interface StoreHealthCheckResult {
|
|
|
13
14
|
error?: string
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
export interface StoreHealthCheckOptions {
|
|
18
|
+
/** D1 binding to query for the order-staleness tripwire. Defaults to `telemetry.d1Binding`'s default, `"DB"`. */
|
|
19
|
+
d1Binding?: string
|
|
20
|
+
/** Site identity to scope the staleness query to. Defaults to `telemetry.siteId`. Pass it from the resolved store options. */
|
|
21
|
+
siteId?: string
|
|
22
|
+
/** Alert once a site with prior orders has gone this many days without a new one. Default 7. */
|
|
23
|
+
staleOrderAlertDays?: number
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface D1LikeDatabase {
|
|
27
|
+
prepare(query: string): {
|
|
28
|
+
bind(...values: unknown[]): { first<T>(column: string): Promise<T | null> }
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
16
32
|
/**
|
|
17
33
|
* Resolves `cloudflare:workers` via a dynamic import wrapped in try/catch,
|
|
18
34
|
* keeping the specifier off this module's static import graph — this file
|
|
@@ -21,34 +37,96 @@ export interface StoreHealthCheckResult {
|
|
|
21
37
|
* the module being unresolvable at runtime, the loader refuses the scheme
|
|
22
38
|
* before resolution even starts). See scripts/verify-pure-node-import.mjs.
|
|
23
39
|
*/
|
|
24
|
-
async function resolveCloudflareEnv(): Promise<
|
|
40
|
+
async function resolveCloudflareEnv(): Promise<Record<string, unknown> | null> {
|
|
25
41
|
try {
|
|
26
42
|
const mod = (await import('cloudflare:workers')) as { env?: Record<string, unknown> }
|
|
27
|
-
return
|
|
43
|
+
return mod?.env ?? null
|
|
28
44
|
} catch {
|
|
29
45
|
return null
|
|
30
46
|
}
|
|
31
47
|
}
|
|
32
48
|
|
|
33
|
-
|
|
49
|
+
/**
|
|
50
|
+
* Tripwire (channel-store-recovery B2): a site that has ever recorded an
|
|
51
|
+
* order but has gone quiet for `staleOrderAlertDays` is almost certainly
|
|
52
|
+
* broken (a silently-failing webhook, a revoked secret, a Fourthwall-side
|
|
53
|
+
* change) rather than genuinely out of sales — the same silent-500 failure
|
|
54
|
+
* mode that started this program. A site with zero orders ever (bigbrain
|
|
55
|
+
* pre-launch, or any site before its first sale) is not alerted on: there is
|
|
56
|
+
* no prior signal to regress from.
|
|
57
|
+
*/
|
|
58
|
+
async function checkOrderStaleness(
|
|
59
|
+
env: Record<string, unknown>,
|
|
60
|
+
options: Required<Pick<StoreHealthCheckOptions, 'd1Binding' | 'siteId' | 'staleOrderAlertDays'>>,
|
|
61
|
+
): Promise<string | undefined> {
|
|
62
|
+
const db = env[options.d1Binding] as D1LikeDatabase | undefined
|
|
63
|
+
if (!db) return undefined // covered by the webhook-secret check above; avoid a duplicate red reason
|
|
64
|
+
|
|
65
|
+
let lastReceivedAt: number | null
|
|
66
|
+
try {
|
|
67
|
+
lastReceivedAt = await db
|
|
68
|
+
.prepare('SELECT MAX(received_at) as last_received_at FROM gl_store_orders WHERE site_id = ?')
|
|
69
|
+
.bind(options.siteId)
|
|
70
|
+
.first<number | null>('last_received_at')
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return `gl_store_orders query failed: ${error instanceof Error ? error.message : String(error)}`
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (lastReceivedAt === null || lastReceivedAt === undefined) return undefined
|
|
76
|
+
|
|
77
|
+
const staleMs = options.staleOrderAlertDays * 24 * 60 * 60 * 1000
|
|
78
|
+
const ageMs = Date.now() - lastReceivedAt
|
|
79
|
+
if (ageMs > staleMs) {
|
|
80
|
+
const days = Math.floor(ageMs / (24 * 60 * 60 * 1000))
|
|
81
|
+
return `no order ingested for ${days}d (alert threshold ${options.staleOrderAlertDays}d) — last order at ${new Date(lastReceivedAt).toISOString()}`
|
|
82
|
+
}
|
|
83
|
+
return undefined
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* `health({ checks: [createStoreHealthCheck()] })` needs no arguments in the
|
|
88
|
+
* common case: the integration already resolved `telemetry.{d1Binding,
|
|
89
|
+
* siteId,staleOrderAlertDays}` at `astro:config:setup` time and stashed it in
|
|
90
|
+
* `state.ts`'s module singleton (the same mechanism the `.astro` page
|
|
91
|
+
* components use for `getConfig()`). Explicit options here only override
|
|
92
|
+
* that default — useful for tests, never required in a real site.
|
|
93
|
+
*/
|
|
94
|
+
export function createStoreHealthCheck(options: StoreHealthCheckOptions = {}): {
|
|
34
95
|
name: string
|
|
35
96
|
check: () => Promise<StoreHealthCheckResult>
|
|
36
97
|
} {
|
|
98
|
+
const resolvedConfig = getConfigOrNull()
|
|
99
|
+
const d1Binding = options.d1Binding ?? resolvedConfig?.telemetry.d1Binding ?? 'DB'
|
|
100
|
+
const siteId = options.siteId ?? resolvedConfig?.telemetry.siteId
|
|
101
|
+
const staleOrderAlertDays =
|
|
102
|
+
options.staleOrderAlertDays ?? resolvedConfig?.telemetry.staleOrderAlertDays ?? 7
|
|
103
|
+
|
|
37
104
|
return {
|
|
38
105
|
name: 'store',
|
|
39
106
|
async check() {
|
|
40
107
|
const start = Date.now()
|
|
41
108
|
const env = await resolveCloudflareEnv()
|
|
42
|
-
const configured = isWebhookConfigured(env)
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
error: 'FW_WEBHOOK_SECRET is not set — order telemetry is silently dropping orders',
|
|
50
|
-
}),
|
|
109
|
+
const configured = isWebhookConfigured(env as { FW_WEBHOOK_SECRET?: string } | null)
|
|
110
|
+
if (!configured) {
|
|
111
|
+
return {
|
|
112
|
+
ok: false,
|
|
113
|
+
ms: Date.now() - start,
|
|
114
|
+
error: 'FW_WEBHOOK_SECRET is not set — order telemetry is silently dropping orders',
|
|
115
|
+
}
|
|
51
116
|
}
|
|
117
|
+
|
|
118
|
+
if (env && siteId) {
|
|
119
|
+
const staleReason = await checkOrderStaleness(env, {
|
|
120
|
+
d1Binding,
|
|
121
|
+
siteId,
|
|
122
|
+
staleOrderAlertDays,
|
|
123
|
+
})
|
|
124
|
+
if (staleReason) {
|
|
125
|
+
return { ok: false, ms: Date.now() - start, error: staleReason }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { ok: true, ms: Date.now() - start }
|
|
52
130
|
},
|
|
53
131
|
}
|
|
54
132
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url'
|
|
2
2
|
import type { AstroIntegration } from 'astro'
|
|
3
|
+
import { setOrderPlacedHandler } from './api.js'
|
|
3
4
|
import { type StoreOptions, storeOptionsSchema } from './options.js'
|
|
5
|
+
import { createD1OrderHandler } from './orders.js'
|
|
4
6
|
import { _setConfig } from './state.js'
|
|
5
7
|
import { growthLabsStorePlugin } from './vite-plugin.js'
|
|
6
8
|
|
|
@@ -19,6 +21,19 @@ export default function store(userOptions: StoreOptions): AstroIntegration {
|
|
|
19
21
|
|
|
20
22
|
_setConfig(options)
|
|
21
23
|
|
|
24
|
+
// B2 (channel-store-recovery): every consuming site gets D1 order
|
|
25
|
+
// persistence + the store_order_placed WAE event automatically — the
|
|
26
|
+
// package owns this now, sites never wire it by hand. A site with its own
|
|
27
|
+
// order pipeline can still override via `setOrderPlacedHandler()` after
|
|
28
|
+
// `store()` runs.
|
|
29
|
+
setOrderPlacedHandler(
|
|
30
|
+
createD1OrderHandler({
|
|
31
|
+
d1Binding: options.telemetry.d1Binding,
|
|
32
|
+
waeBinding: options.telemetry.waeBinding,
|
|
33
|
+
siteId: options.telemetry.siteId,
|
|
34
|
+
}),
|
|
35
|
+
)
|
|
36
|
+
|
|
22
37
|
return {
|
|
23
38
|
name: '@growth-labs/store',
|
|
24
39
|
hooks: {
|
|
@@ -59,10 +74,13 @@ export {
|
|
|
59
74
|
getStoreCollection,
|
|
60
75
|
resolveProductKind,
|
|
61
76
|
} from './collections.js'
|
|
77
|
+
export type { StoreHealthCheckOptions } from './health.js'
|
|
62
78
|
export { createStoreHealthCheck } from './health.js'
|
|
63
79
|
export { hasProductImages, optimizeFwImage } from './image.js'
|
|
64
80
|
export type { ResolvedStoreOptions, StoreOptions } from './options.js'
|
|
65
81
|
export { storeOptionsSchema } from './options.js'
|
|
82
|
+
export type { OrderTelemetryOptions } from './orders.js'
|
|
83
|
+
export { buildStoreOrderWAEDataPoint, createD1OrderHandler } from './orders.js'
|
|
66
84
|
export type * from './types.js'
|
|
67
85
|
export type { MerchantReturnPolicy, StoreVariantClientData } from './variant.js'
|
|
68
86
|
export {
|
package/src/options.ts
CHANGED
|
@@ -15,7 +15,7 @@ const merchantReturnPolicySchema = z.object({
|
|
|
15
15
|
returnPolicyCategory: z.string().min(1),
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
const baseStoreOptionsSchema = z.object({
|
|
19
19
|
/** Shown in the store JSON-LD `brand.name` and page copy. */
|
|
20
20
|
brandName: z.string().min(1),
|
|
21
21
|
|
|
@@ -67,8 +67,33 @@ export const storeOptionsSchema = z.object({
|
|
|
67
67
|
cartBootstrapPath: z.string().default('/api/store/cart-bootstrap'),
|
|
68
68
|
})
|
|
69
69
|
.default({}),
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* B2 order telemetry: every verified `ORDER_PLACED` webhook is persisted to
|
|
73
|
+
* the package-owned `gl_store_orders` D1 table and mirrored to WAE as
|
|
74
|
+
* `store_order_placed`. `siteId` defaults to `cartOrigin` — the site
|
|
75
|
+
* already has exactly one identity for attribution, so this avoids asking
|
|
76
|
+
* for the same value twice.
|
|
77
|
+
*/
|
|
78
|
+
telemetry: z
|
|
79
|
+
.object({
|
|
80
|
+
d1Binding: z.string().default('DB'),
|
|
81
|
+
waeBinding: z.string().default('ANALYTICS'),
|
|
82
|
+
siteId: z.string().min(1).optional(),
|
|
83
|
+
/** Health check goes red when a site with prior orders has ingested none for this many days. */
|
|
84
|
+
staleOrderAlertDays: z.number().int().positive().default(7),
|
|
85
|
+
})
|
|
86
|
+
.default({}),
|
|
70
87
|
})
|
|
71
88
|
|
|
89
|
+
export const storeOptionsSchema = baseStoreOptionsSchema.transform((options) => ({
|
|
90
|
+
...options,
|
|
91
|
+
telemetry: {
|
|
92
|
+
...options.telemetry,
|
|
93
|
+
siteId: options.telemetry.siteId ?? options.cartOrigin,
|
|
94
|
+
},
|
|
95
|
+
}))
|
|
96
|
+
|
|
72
97
|
export type StoreOptions = z.input<typeof storeOptionsSchema>
|
|
73
98
|
export type ResolvedStoreOptions = z.output<typeof storeOptionsSchema>
|
|
74
99
|
export type StoreCollectionInput = z.input<typeof storeCollectionSchema>
|
package/src/orders.ts
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type { OrderPlacedHandler } from './api.js'
|
|
2
|
+
import type { FwOrderPlacedEvent } from './types.js'
|
|
3
|
+
|
|
4
|
+
export interface OrderTelemetryOptions {
|
|
5
|
+
d1Binding: string
|
|
6
|
+
waeBinding: string
|
|
7
|
+
siteId: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
interface D1PreparedStatement {
|
|
11
|
+
bind(...values: unknown[]): D1PreparedStatement
|
|
12
|
+
run(): Promise<unknown>
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface D1LikeDatabase {
|
|
16
|
+
prepare(query: string): D1PreparedStatement
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface WaeLikeBinding {
|
|
20
|
+
writeDataPoint(dataPoint: {
|
|
21
|
+
blobs: string[]
|
|
22
|
+
doubles: number[]
|
|
23
|
+
indexes: string[]
|
|
24
|
+
}): unknown | Promise<unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Resolves `cloudflare:workers` via a dynamic import wrapped in try/catch —
|
|
29
|
+
* `createD1OrderHandler` is wired from `index.ts`'s `astro:config:setup`
|
|
30
|
+
* hook, which runs under plain Node during `astro build`/`astro check`.
|
|
31
|
+
* See health.ts and scripts/verify-pure-node-import.mjs for the same pattern.
|
|
32
|
+
*/
|
|
33
|
+
async function resolveTelemetryEnv(): Promise<Record<string, unknown> | null> {
|
|
34
|
+
try {
|
|
35
|
+
const mod = (await import('cloudflare:workers')) as { env?: Record<string, unknown> }
|
|
36
|
+
return mod?.env ?? null
|
|
37
|
+
} catch {
|
|
38
|
+
return null
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function sha256Hex(input: string): Promise<string> {
|
|
43
|
+
const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(input))
|
|
44
|
+
return Array.from(new Uint8Array(digest))
|
|
45
|
+
.map((byte) => byte.toString(16).padStart(2, '0'))
|
|
46
|
+
.join('')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function orderItemCount(event: FwOrderPlacedEvent): number {
|
|
50
|
+
return event.data.offers.reduce((total, offer) => total + (offer.variant.quantity ?? 0), 0)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function buildItemsJson(event: FwOrderPlacedEvent): string {
|
|
54
|
+
return JSON.stringify(
|
|
55
|
+
event.data.offers.map((offer) => ({
|
|
56
|
+
offerId: offer.id,
|
|
57
|
+
name: offer.name ?? null,
|
|
58
|
+
variantId: offer.variant.id,
|
|
59
|
+
sku: offer.variant.sku ?? null,
|
|
60
|
+
quantity: offer.variant.quantity ?? 0,
|
|
61
|
+
unitPrice: (offer.variant.unitPrice ?? offer.variant.price)?.value ?? null,
|
|
62
|
+
})),
|
|
63
|
+
)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function cartOriginFor(event: FwOrderPlacedEvent, fallback: string): string {
|
|
67
|
+
const metadata = event.data.metadata
|
|
68
|
+
const fromMetadata =
|
|
69
|
+
metadata && typeof metadata.cart_origin === 'string'
|
|
70
|
+
? (metadata.cart_origin as string)
|
|
71
|
+
: undefined
|
|
72
|
+
return fromMetadata ?? fallback
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Mirrors `@growth-labs/analytics`'s `store_order_placed` blob/double layout
|
|
77
|
+
* (packages don't import each other — PLATFORM_PATTERNS.md — so this
|
|
78
|
+
* replicates the slot mapping from `buildWAEDataPoint`/`buildStorefrontWAEParams`
|
|
79
|
+
* rather than calling into that package).
|
|
80
|
+
*/
|
|
81
|
+
export function buildStoreOrderWAEDataPoint(
|
|
82
|
+
options: OrderTelemetryOptions,
|
|
83
|
+
event: FwOrderPlacedEvent,
|
|
84
|
+
rawPayloadHash: string,
|
|
85
|
+
): { blobs: string[]; doubles: number[]; indexes: string[] } {
|
|
86
|
+
const utm = event.data.trackingParams ?? {}
|
|
87
|
+
const labelExtras: Record<string, string | number> = {
|
|
88
|
+
cartOrigin: cartOriginFor(event, options.siteId),
|
|
89
|
+
rawPayloadHash,
|
|
90
|
+
itemCount: orderItemCount(event),
|
|
91
|
+
}
|
|
92
|
+
if (event.data.email) labelExtras.customerEmail = event.data.email
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
blobs: [
|
|
96
|
+
'store_order_placed', // blob1 eventName
|
|
97
|
+
options.siteId, // blob2 siteId
|
|
98
|
+
'', // blob3 visitorId
|
|
99
|
+
'', // blob4 sessionId
|
|
100
|
+
'', // blob5 pageUrl
|
|
101
|
+
'', // blob6 pagePath
|
|
102
|
+
'', // blob7 referrer
|
|
103
|
+
utm.utm_source ?? '', // blob8 utmSource
|
|
104
|
+
utm.utm_medium ?? '', // blob9 utmMedium
|
|
105
|
+
utm.utm_campaign ?? '', // blob10 utmCampaign
|
|
106
|
+
'', // blob11 derivedSource
|
|
107
|
+
'', // blob12 country
|
|
108
|
+
'', // blob13 deviceType
|
|
109
|
+
'', // blob14 browserName
|
|
110
|
+
'', // blob15 osName
|
|
111
|
+
event.data.id, // blob16 contentSlug = orderId
|
|
112
|
+
'order', // blob17 contentType
|
|
113
|
+
'storefront', // blob18 eventCategory
|
|
114
|
+
JSON.stringify(labelExtras), // blob19 eventLabel
|
|
115
|
+
'false', // blob20 isNewVisitor
|
|
116
|
+
],
|
|
117
|
+
doubles: [
|
|
118
|
+
Date.now(), // double1 timestamp
|
|
119
|
+
orderItemCount(event), // double2 eventValue
|
|
120
|
+
0,
|
|
121
|
+
0,
|
|
122
|
+
0,
|
|
123
|
+
0,
|
|
124
|
+
0,
|
|
125
|
+
0,
|
|
126
|
+
0, // double3-9
|
|
127
|
+
0, // double10 productPrice (not applicable to a whole order)
|
|
128
|
+
event.data.amounts.total.value, // double11 cartValue
|
|
129
|
+
0,
|
|
130
|
+
0,
|
|
131
|
+
0,
|
|
132
|
+
0,
|
|
133
|
+
0,
|
|
134
|
+
0,
|
|
135
|
+
0,
|
|
136
|
+
0,
|
|
137
|
+
0, // double12-20
|
|
138
|
+
],
|
|
139
|
+
indexes: ['store_order_placed'],
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* B2's order-telemetry handler, wired via `setOrderPlacedHandler` from
|
|
145
|
+
* `index.ts` for every consuming site automatically: persists every verified
|
|
146
|
+
* `ORDER_PLACED` webhook to the package-owned `gl_store_orders` D1 table
|
|
147
|
+
* (migrations/0001_gl_store_orders.sql) and emits the mirrored WAE point.
|
|
148
|
+
*
|
|
149
|
+
* The D1 write and the WAE write are independent best-effort operations —
|
|
150
|
+
* a WAE outage must not drop the durable order row, and vice versa. Both
|
|
151
|
+
* failure paths log a structured, greppable event rather than throwing:
|
|
152
|
+
* `createWebhookResponse` has already validated the HMAC signature and is
|
|
153
|
+
* about to return 200 to Fourthwall by the time this handler runs, so
|
|
154
|
+
* throwing here would not trigger a useful retry, only an unhandled
|
|
155
|
+
* rejection after the response was already sent.
|
|
156
|
+
*/
|
|
157
|
+
export function createD1OrderHandler(options: OrderTelemetryOptions): OrderPlacedHandler {
|
|
158
|
+
return async (event: FwOrderPlacedEvent) => {
|
|
159
|
+
const env = await resolveTelemetryEnv()
|
|
160
|
+
if (!env) {
|
|
161
|
+
console.error('store.order_telemetry.env_unavailable', { orderId: event.data.id })
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const rawPayloadHash = await sha256Hex(JSON.stringify(event))
|
|
166
|
+
|
|
167
|
+
const db = env[options.d1Binding] as D1LikeDatabase | undefined
|
|
168
|
+
if (!db) {
|
|
169
|
+
console.error('store.order_telemetry.d1_binding_missing', {
|
|
170
|
+
binding: options.d1Binding,
|
|
171
|
+
orderId: event.data.id,
|
|
172
|
+
})
|
|
173
|
+
} else {
|
|
174
|
+
try {
|
|
175
|
+
await db
|
|
176
|
+
.prepare(
|
|
177
|
+
`INSERT OR IGNORE INTO gl_store_orders
|
|
178
|
+
(id, shop_id, site_id, amount, currency, items_json, cart_origin, utm_source, utm_medium, utm_campaign, utm_term, utm_content, raw_payload_hash, received_at)
|
|
179
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
180
|
+
)
|
|
181
|
+
.bind(
|
|
182
|
+
event.data.id,
|
|
183
|
+
event.data.shopId ?? event.shopId ?? null,
|
|
184
|
+
options.siteId,
|
|
185
|
+
event.data.amounts.total.value,
|
|
186
|
+
event.data.amounts.total.currency,
|
|
187
|
+
buildItemsJson(event),
|
|
188
|
+
cartOriginFor(event, options.siteId),
|
|
189
|
+
event.data.trackingParams?.utm_source ?? null,
|
|
190
|
+
event.data.trackingParams?.utm_medium ?? null,
|
|
191
|
+
event.data.trackingParams?.utm_campaign ?? null,
|
|
192
|
+
event.data.trackingParams?.utm_term ?? null,
|
|
193
|
+
event.data.trackingParams?.utm_content ?? null,
|
|
194
|
+
rawPayloadHash,
|
|
195
|
+
Date.now(),
|
|
196
|
+
)
|
|
197
|
+
.run()
|
|
198
|
+
} catch (error) {
|
|
199
|
+
console.error('store.order_telemetry.d1_write_failed', {
|
|
200
|
+
orderId: event.data.id,
|
|
201
|
+
error: error instanceof Error ? error.message : String(error),
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const wae = env[options.waeBinding] as WaeLikeBinding | undefined
|
|
207
|
+
if (!wae?.writeDataPoint) {
|
|
208
|
+
console.error('store.order_telemetry.wae_binding_missing', {
|
|
209
|
+
binding: options.waeBinding,
|
|
210
|
+
orderId: event.data.id,
|
|
211
|
+
})
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
try {
|
|
215
|
+
await wae.writeDataPoint(buildStoreOrderWAEDataPoint(options, event, rawPayloadHash))
|
|
216
|
+
} catch (error) {
|
|
217
|
+
console.error('store.order_telemetry.wae_write_failed', {
|
|
218
|
+
orderId: event.data.id,
|
|
219
|
+
error: error instanceof Error ? error.message : String(error),
|
|
220
|
+
})
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|