@growth-labs/store 0.2.0 → 0.2.1

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/health.d.ts CHANGED
@@ -20,11 +20,14 @@ export interface StoreHealthCheckOptions {
20
20
  }
21
21
  /**
22
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.
23
+ * common case: `.check()` resolves `telemetry.{d1Binding,siteId,
24
+ * staleOrderAlertDays}` from the integration's config at request time (via
25
+ * `getConfigOrNull()` — see state.ts for why this must happen per-request
26
+ * rather than once when `createStoreHealthCheck()` itself is called, which
27
+ * for most sites happens while building `astro.config.mjs`'s options object,
28
+ * before the config singleton a built Worker actually serves from exists).
29
+ * Explicit options here only override that default — useful for tests, never
30
+ * required in a real site.
28
31
  */
29
32
  export declare function createStoreHealthCheck(options?: StoreHealthCheckOptions): {
30
33
  name: string;
@@ -1 +1 @@
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"}
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;;;;;;;;;;GAUG;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
@@ -52,17 +52,16 @@ async function checkOrderStaleness(env, options) {
52
52
  }
53
53
  /**
54
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.
55
+ * common case: `.check()` resolves `telemetry.{d1Binding,siteId,
56
+ * staleOrderAlertDays}` from the integration's config at request time (via
57
+ * `getConfigOrNull()` — see state.ts for why this must happen per-request
58
+ * rather than once when `createStoreHealthCheck()` itself is called, which
59
+ * for most sites happens while building `astro.config.mjs`'s options object,
60
+ * before the config singleton a built Worker actually serves from exists).
61
+ * Explicit options here only override that default — useful for tests, never
62
+ * required in a real site.
60
63
  */
61
64
  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;
66
65
  return {
67
66
  name: 'store',
68
67
  async check() {
@@ -76,6 +75,10 @@ export function createStoreHealthCheck(options = {}) {
76
75
  error: 'FW_WEBHOOK_SECRET is not set — order telemetry is silently dropping orders',
77
76
  };
78
77
  }
78
+ const resolvedConfig = options.d1Binding && options.siteId ? null : await getConfigOrNull();
79
+ const d1Binding = options.d1Binding ?? resolvedConfig?.telemetry.d1Binding ?? 'DB';
80
+ const siteId = options.siteId ?? resolvedConfig?.telemetry.siteId;
81
+ const staleOrderAlertDays = options.staleOrderAlertDays ?? resolvedConfig?.telemetry.staleOrderAlertDays ?? 7;
79
82
  if (env && siteId) {
80
83
  const staleReason = await checkOrderStaleness(env, {
81
84
  d1Binding,
@@ -1 +1 @@
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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,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"}
@@ -1 +1 @@
1
- {"version":3,"file":"cart-bootstrap.d.ts","sourceRoot":"","sources":["../../src/routes/cart-bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAGvC,wBAAsB,GAAG,CAAC,QAAQ,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAEjE"}
1
+ {"version":3,"file":"cart-bootstrap.d.ts","sourceRoot":"","sources":["../../src/routes/cart-bootstrap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAGvC,wBAAsB,GAAG,CAAC,QAAQ,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAGjE"}
@@ -1,5 +1,6 @@
1
1
  import { getConfig } from '../state.js';
2
2
  export async function GET(_context) {
3
- return Response.json({ currency: getConfig().defaultCurrency });
3
+ const config = await getConfig();
4
+ return Response.json({ currency: config.defaultCurrency });
4
5
  }
5
6
  //# sourceMappingURL=cart-bootstrap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"cart-bootstrap.js","sourceRoot":"","sources":["../../src/routes/cart-bootstrap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,GAAG,CAAC,QAAoB;IAC7C,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAC,eAAe,EAAE,CAAC,CAAA;AAChE,CAAC"}
1
+ {"version":3,"file":"cart-bootstrap.js","sourceRoot":"","sources":["../../src/routes/cart-bootstrap.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,GAAG,CAAC,QAAoB;IAC7C,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAA;IAChC,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,eAAe,EAAE,CAAC,CAAA;AAC3D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../../src/routes/checkout.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAIvC,wBAAsB,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAMjE;AAED,wBAAgB,GAAG,IAAI,QAAQ,CAE9B"}
1
+ {"version":3,"file":"checkout.d.ts","sourceRoot":"","sources":["../../src/routes/checkout.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAIvC,wBAAsB,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAOjE;AAED,wBAAgB,GAAG,IAAI,QAAQ,CAE9B"}
@@ -2,10 +2,11 @@ import { env as cloudflareEnv } from 'cloudflare:workers';
2
2
  import { createCheckoutResponse } from '../api.js';
3
3
  import { getConfig } from '../state.js';
4
4
  export async function POST(context) {
5
+ const config = await getConfig();
5
6
  return createCheckoutResponse({
6
7
  request: context.request,
7
8
  env: cloudflareEnv,
8
- cartOrigin: getConfig().cartOrigin,
9
+ cartOrigin: config.cartOrigin,
9
10
  });
10
11
  }
11
12
  export function ALL() {
@@ -1 +1 @@
1
- {"version":3,"file":"checkout.js","sourceRoot":"","sources":["../../src/routes/checkout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAEzD,OAAO,EAAoB,sBAAsB,EAAE,MAAM,WAAW,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,OAAmB;IAC7C,OAAO,sBAAsB,CAAC;QAC7B,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,GAAG,EAAE,aAA4B;QACjC,UAAU,EAAE,SAAS,EAAE,CAAC,UAAU;KAClC,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,GAAG;IAClB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;AACvE,CAAC"}
1
+ {"version":3,"file":"checkout.js","sourceRoot":"","sources":["../../src/routes/checkout.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAEzD,OAAO,EAAoB,sBAAsB,EAAE,MAAM,WAAW,CAAA;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAEvC,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,OAAmB;IAC7C,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAA;IAChC,OAAO,sBAAsB,CAAC;QAC7B,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,GAAG,EAAE,aAA4B;QACjC,UAAU,EAAE,MAAM,CAAC,UAAU;KAC7B,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,GAAG;IAClB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;AACvE,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"webhook.d.ts","sourceRoot":"","sources":["../../src/routes/webhook.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAGvC,wBAAsB,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAKjE;AAED,wBAAgB,GAAG,IAAI,QAAQ,CAE9B"}
1
+ {"version":3,"file":"webhook.d.ts","sourceRoot":"","sources":["../../src/routes/webhook.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AAgCvC,wBAAsB,IAAI,CAAC,OAAO,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAMjE;AAED,wBAAgB,GAAG,IAAI,QAAQ,CAE9B"}
@@ -1,6 +1,34 @@
1
1
  import { env as cloudflareEnv } from 'cloudflare:workers';
2
- import { createWebhookResponse } from '../api.js';
2
+ import { createWebhookResponse, setOrderPlacedHandler } from '../api.js';
3
+ import { createD1OrderHandler } from '../orders.js';
4
+ import { getConfigOrNull } from '../state.js';
5
+ let orderHandlerRegistered = false;
6
+ /**
7
+ * `store()`'s `astro:config:setup` hook also calls `setOrderPlacedHandler`,
8
+ * but that call runs in the astro CLI's Node process while loading
9
+ * astro.config.mjs — a different module instantiation than the one a built
10
+ * Worker ships (see state.ts). This route module IS part of the Worker's own
11
+ * runtime bundle (it's `injectRoute`-registered), so it registers the D1
12
+ * order handler itself, once, against the exact `api.js` instance this
13
+ * route's `createWebhookResponse` call also uses. Idempotent with the
14
+ * config-setup-time call — whichever runs last in a given process wins, and
15
+ * both install the same handler shape.
16
+ */
17
+ async function ensureOrderHandlerRegistered() {
18
+ if (orderHandlerRegistered)
19
+ return;
20
+ const config = await getConfigOrNull();
21
+ if (!config)
22
+ return;
23
+ setOrderPlacedHandler(createD1OrderHandler({
24
+ d1Binding: config.telemetry.d1Binding,
25
+ waeBinding: config.telemetry.waeBinding,
26
+ siteId: config.telemetry.siteId,
27
+ }));
28
+ orderHandlerRegistered = true;
29
+ }
3
30
  export async function POST(context) {
31
+ await ensureOrderHandlerRegistered();
4
32
  return createWebhookResponse({
5
33
  request: context.request,
6
34
  env: cloudflareEnv,
@@ -1 +1 @@
1
- {"version":3,"file":"webhook.js","sourceRoot":"","sources":["../../src/routes/webhook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAEzD,OAAO,EAAE,qBAAqB,EAAmB,MAAM,WAAW,CAAA;AAElE,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,OAAmB;IAC7C,OAAO,qBAAqB,CAAC;QAC5B,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,GAAG,EAAE,aAA2B;KAChC,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,GAAG;IAClB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;AACvE,CAAC"}
1
+ {"version":3,"file":"webhook.js","sourceRoot":"","sources":["../../src/routes/webhook.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,IAAI,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAEzD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAmB,MAAM,WAAW,CAAA;AACzF,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE7C,IAAI,sBAAsB,GAAG,KAAK,CAAA;AAElC;;;;;;;;;;GAUG;AACH,KAAK,UAAU,4BAA4B;IAC1C,IAAI,sBAAsB;QAAE,OAAM;IAClC,MAAM,MAAM,GAAG,MAAM,eAAe,EAAE,CAAA;IACtC,IAAI,CAAC,MAAM;QAAE,OAAM;IACnB,qBAAqB,CACpB,oBAAoB,CAAC;QACpB,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,SAAS;QACrC,UAAU,EAAE,MAAM,CAAC,SAAS,CAAC,UAAU;QACvC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,MAAM;KAC/B,CAAC,CACF,CAAA;IACD,sBAAsB,GAAG,IAAI,CAAA;AAC9B,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,OAAmB;IAC7C,MAAM,4BAA4B,EAAE,CAAA;IACpC,OAAO,qBAAqB,CAAC;QAC5B,OAAO,EAAE,OAAO,CAAC,OAAO;QACxB,GAAG,EAAE,aAA2B;KAChC,CAAC,CAAA;AACH,CAAC;AAED,MAAM,UAAU,GAAG;IAClB,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAA;AACvE,CAAC"}
package/dist/state.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ResolvedStoreOptions } from './options.js';
2
2
  export declare function _setConfig(next: ResolvedStoreOptions): void;
3
- export declare function getConfig(): ResolvedStoreOptions;
4
- export declare function getConfigOrNull(): ResolvedStoreOptions | null;
3
+ export declare function getConfig(): Promise<ResolvedStoreOptions>;
4
+ export declare function getConfigOrNull(): Promise<ResolvedStoreOptions | null>;
5
5
  //# sourceMappingURL=state.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAIxD,wBAAgB,UAAU,CAAC,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAE3D;AAED,wBAAgB,SAAS,IAAI,oBAAoB,CAOhD;AAED,wBAAgB,eAAe,IAAI,oBAAoB,GAAG,IAAI,CAE7D"}
1
+ {"version":3,"file":"state.d.ts","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAIxD,wBAAgB,UAAU,CAAC,IAAI,EAAE,oBAAoB,GAAG,IAAI,CAE3D;AA4BD,wBAAsB,SAAS,IAAI,OAAO,CAAC,oBAAoB,CAAC,CAU/D;AAED,wBAAsB,eAAe,IAAI,OAAO,CAAC,oBAAoB,GAAG,IAAI,CAAC,CAK5E"}
package/dist/state.js CHANGED
@@ -2,13 +2,43 @@ let config = null;
2
2
  export function _setConfig(next) {
3
3
  config = next;
4
4
  }
5
- export function getConfig() {
5
+ /**
6
+ * `_setConfig` is called both by `store()`'s `astro:config:setup` hook — which
7
+ * runs in the astro CLI's Node process while *loading* astro.config.mjs, a
8
+ * different module instantiation than the one the built Worker ships — and by
9
+ * the vite-plugin-generated `virtual:growth-labs/store/config` module, which
10
+ * bakes the resolved options in as literal data and calls `_setConfig` as
11
+ * part of its own module body. The virtual module is the only one of the two
12
+ * that is guaranteed to execute inside the actual bundled Worker's runtime
13
+ * module graph, so it is the fallback here.
14
+ *
15
+ * A dynamic import keeps the `virtual:` specifier off every caller's static
16
+ * import graph (the pure-Node import gate, and any non-Vite tooling, would
17
+ * otherwise choke on the scheme) — see scripts/verify-pure-node-import.mjs
18
+ * and health.ts's identical `cloudflare:workers` pattern.
19
+ */
20
+ async function resolveViaVirtualModule() {
21
+ try {
22
+ const mod = (await import('virtual:growth-labs/store/config'));
23
+ return mod?.config ?? null;
24
+ }
25
+ catch {
26
+ return null;
27
+ }
28
+ }
29
+ export async function getConfig() {
30
+ if (!config) {
31
+ config = await resolveViaVirtualModule();
32
+ }
6
33
  if (!config) {
7
34
  throw new Error('@growth-labs/store: config accessed before the integration ran. Add store() to astro.config.mjs.');
8
35
  }
9
36
  return config;
10
37
  }
11
- export function getConfigOrNull() {
38
+ export async function getConfigOrNull() {
39
+ if (!config) {
40
+ config = await resolveViaVirtualModule();
41
+ }
12
42
  return config;
13
43
  }
14
44
  //# sourceMappingURL=state.js.map
package/dist/state.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAEA,IAAI,MAAM,GAAgC,IAAI,CAAA;AAE9C,MAAM,UAAU,UAAU,CAAC,IAA0B;IACpD,MAAM,GAAG,IAAI,CAAA;AACd,CAAC;AAED,MAAM,UAAU,SAAS;IACxB,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACd,kGAAkG,CAClG,CAAA;IACF,CAAC;IACD,OAAO,MAAM,CAAA;AACd,CAAC;AAED,MAAM,UAAU,eAAe;IAC9B,OAAO,MAAM,CAAA;AACd,CAAC"}
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["../src/state.ts"],"names":[],"mappings":"AAEA,IAAI,MAAM,GAAgC,IAAI,CAAA;AAE9C,MAAM,UAAU,UAAU,CAAC,IAA0B;IACpD,MAAM,GAAG,IAAI,CAAA;AACd,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,KAAK,UAAU,uBAAuB;IACrC,IAAI,CAAC;QACJ,MAAM,GAAG,GAAG,CAAC,MAAM,MAAM,CAAC,kCAAkC,CAAC,CAE5D,CAAA;QACD,OAAO,GAAG,EAAE,MAAM,IAAI,IAAI,CAAA;IAC3B,CAAC;IAAC,MAAM,CAAC;QACR,OAAO,IAAI,CAAA;IACZ,CAAC;AACF,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,SAAS;IAC9B,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,GAAG,MAAM,uBAAuB,EAAE,CAAA;IACzC,CAAC;IACD,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACd,kGAAkG,CAClG,CAAA;IACF,CAAC;IACD,OAAO,MAAM,CAAA;AACd,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,eAAe;IACpC,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,MAAM,GAAG,MAAM,uBAAuB,EAAE,CAAA;IACzC,CAAC;IACD,OAAO,MAAM,CAAA;AACd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@growth-labs/store",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "types": "./dist/index.d.ts",
6
6
  "exports": {
@@ -14,7 +14,7 @@ interface Props {
14
14
  }
15
15
 
16
16
  const { storePath = '/store/', fourthwallShopUrl } = Astro.props
17
- const config = getConfig()
17
+ const config = await getConfig()
18
18
  const env = cloudflareEnv as FwEnv
19
19
  const token = env?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
20
20
  const currency = env?.PUBLIC_FW_DEFAULT_CURRENCY ?? config.defaultCurrency
@@ -18,7 +18,7 @@ interface Props {
18
18
  }
19
19
 
20
20
  const { handle, basePath = '/store', notFoundPath = '/404/' } = Astro.props
21
- const config = getConfig()
21
+ const config = await getConfig()
22
22
 
23
23
  const collection = getStoreCollection(config.collections, handle)
24
24
  if (!collection) {
@@ -19,7 +19,7 @@ interface Props {
19
19
  }
20
20
 
21
21
  const { heading, basePath = '/store' } = Astro.props
22
- const config = getConfig()
22
+ const config = await getConfig()
23
23
  const env = cloudflareEnv as FwEnv
24
24
  const token = env?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
25
25
  const cookieCurrency = Astro.cookies.get('fw_currency')?.value
@@ -36,7 +36,7 @@ interface Props {
36
36
  }
37
37
 
38
38
  const { slug, basePath = '/store', notFoundResponseStatus = 404, fourthwallShopBaseUrl, kind = 'merch', kindModule: KindModule } = Astro.props
39
- const config = getConfig()
39
+ const config = await getConfig()
40
40
  const env = cloudflareEnv as FwEnv
41
41
  const token = env?.PUBLIC_FW_STOREFRONT_TOKEN ?? ''
42
42
  const cookieCurrency = Astro.cookies.get('fw_currency')?.value
package/src/health.ts CHANGED
@@ -85,22 +85,19 @@ async function checkOrderStaleness(
85
85
 
86
86
  /**
87
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.
88
+ * common case: `.check()` resolves `telemetry.{d1Binding,siteId,
89
+ * staleOrderAlertDays}` from the integration's config at request time (via
90
+ * `getConfigOrNull()` — see state.ts for why this must happen per-request
91
+ * rather than once when `createStoreHealthCheck()` itself is called, which
92
+ * for most sites happens while building `astro.config.mjs`'s options object,
93
+ * before the config singleton a built Worker actually serves from exists).
94
+ * Explicit options here only override that default — useful for tests, never
95
+ * required in a real site.
93
96
  */
94
97
  export function createStoreHealthCheck(options: StoreHealthCheckOptions = {}): {
95
98
  name: string
96
99
  check: () => Promise<StoreHealthCheckResult>
97
100
  } {
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
-
104
101
  return {
105
102
  name: 'store',
106
103
  async check() {
@@ -115,6 +112,12 @@ export function createStoreHealthCheck(options: StoreHealthCheckOptions = {}): {
115
112
  }
116
113
  }
117
114
 
115
+ const resolvedConfig = options.d1Binding && options.siteId ? null : await getConfigOrNull()
116
+ const d1Binding = options.d1Binding ?? resolvedConfig?.telemetry.d1Binding ?? 'DB'
117
+ const siteId = options.siteId ?? resolvedConfig?.telemetry.siteId
118
+ const staleOrderAlertDays =
119
+ options.staleOrderAlertDays ?? resolvedConfig?.telemetry.staleOrderAlertDays ?? 7
120
+
118
121
  if (env && siteId) {
119
122
  const staleReason = await checkOrderStaleness(env, {
120
123
  d1Binding,
@@ -2,5 +2,6 @@ import type { APIContext } from 'astro'
2
2
  import { getConfig } from '../state.js'
3
3
 
4
4
  export async function GET(_context: APIContext): Promise<Response> {
5
- return Response.json({ currency: getConfig().defaultCurrency })
5
+ const config = await getConfig()
6
+ return Response.json({ currency: config.defaultCurrency })
6
7
  }
@@ -4,10 +4,11 @@ import { type CheckoutEnv, createCheckoutResponse } from '../api.js'
4
4
  import { getConfig } from '../state.js'
5
5
 
6
6
  export async function POST(context: APIContext): Promise<Response> {
7
+ const config = await getConfig()
7
8
  return createCheckoutResponse({
8
9
  request: context.request,
9
10
  env: cloudflareEnv as CheckoutEnv,
10
- cartOrigin: getConfig().cartOrigin,
11
+ cartOrigin: config.cartOrigin,
11
12
  })
12
13
  }
13
14
 
@@ -1,8 +1,38 @@
1
1
  import { env as cloudflareEnv } from 'cloudflare:workers'
2
2
  import type { APIContext } from 'astro'
3
- import { createWebhookResponse, type WebhookEnv } from '../api.js'
3
+ import { createWebhookResponse, setOrderPlacedHandler, type WebhookEnv } from '../api.js'
4
+ import { createD1OrderHandler } from '../orders.js'
5
+ import { getConfigOrNull } from '../state.js'
6
+
7
+ let orderHandlerRegistered = false
8
+
9
+ /**
10
+ * `store()`'s `astro:config:setup` hook also calls `setOrderPlacedHandler`,
11
+ * but that call runs in the astro CLI's Node process while loading
12
+ * astro.config.mjs — a different module instantiation than the one a built
13
+ * Worker ships (see state.ts). This route module IS part of the Worker's own
14
+ * runtime bundle (it's `injectRoute`-registered), so it registers the D1
15
+ * order handler itself, once, against the exact `api.js` instance this
16
+ * route's `createWebhookResponse` call also uses. Idempotent with the
17
+ * config-setup-time call — whichever runs last in a given process wins, and
18
+ * both install the same handler shape.
19
+ */
20
+ async function ensureOrderHandlerRegistered(): Promise<void> {
21
+ if (orderHandlerRegistered) return
22
+ const config = await getConfigOrNull()
23
+ if (!config) return
24
+ setOrderPlacedHandler(
25
+ createD1OrderHandler({
26
+ d1Binding: config.telemetry.d1Binding,
27
+ waeBinding: config.telemetry.waeBinding,
28
+ siteId: config.telemetry.siteId,
29
+ }),
30
+ )
31
+ orderHandlerRegistered = true
32
+ }
4
33
 
5
34
  export async function POST(context: APIContext): Promise<Response> {
35
+ await ensureOrderHandlerRegistered()
6
36
  return createWebhookResponse({
7
37
  request: context.request,
8
38
  env: cloudflareEnv as WebhookEnv,
package/src/state.ts CHANGED
@@ -6,7 +6,36 @@ export function _setConfig(next: ResolvedStoreOptions): void {
6
6
  config = next
7
7
  }
8
8
 
9
- export function getConfig(): ResolvedStoreOptions {
9
+ /**
10
+ * `_setConfig` is called both by `store()`'s `astro:config:setup` hook — which
11
+ * runs in the astro CLI's Node process while *loading* astro.config.mjs, a
12
+ * different module instantiation than the one the built Worker ships — and by
13
+ * the vite-plugin-generated `virtual:growth-labs/store/config` module, which
14
+ * bakes the resolved options in as literal data and calls `_setConfig` as
15
+ * part of its own module body. The virtual module is the only one of the two
16
+ * that is guaranteed to execute inside the actual bundled Worker's runtime
17
+ * module graph, so it is the fallback here.
18
+ *
19
+ * A dynamic import keeps the `virtual:` specifier off every caller's static
20
+ * import graph (the pure-Node import gate, and any non-Vite tooling, would
21
+ * otherwise choke on the scheme) — see scripts/verify-pure-node-import.mjs
22
+ * and health.ts's identical `cloudflare:workers` pattern.
23
+ */
24
+ async function resolveViaVirtualModule(): Promise<ResolvedStoreOptions | null> {
25
+ try {
26
+ const mod = (await import('virtual:growth-labs/store/config')) as {
27
+ config?: ResolvedStoreOptions
28
+ }
29
+ return mod?.config ?? null
30
+ } catch {
31
+ return null
32
+ }
33
+ }
34
+
35
+ export async function getConfig(): Promise<ResolvedStoreOptions> {
36
+ if (!config) {
37
+ config = await resolveViaVirtualModule()
38
+ }
10
39
  if (!config) {
11
40
  throw new Error(
12
41
  '@growth-labs/store: config accessed before the integration ran. Add store() to astro.config.mjs.',
@@ -15,6 +44,9 @@ export function getConfig(): ResolvedStoreOptions {
15
44
  return config
16
45
  }
17
46
 
18
- export function getConfigOrNull(): ResolvedStoreOptions | null {
47
+ export async function getConfigOrNull(): Promise<ResolvedStoreOptions | null> {
48
+ if (!config) {
49
+ config = await resolveViaVirtualModule()
50
+ }
19
51
  return config
20
52
  }
package/src/virtual.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  declare module 'virtual:growth-labs/store/config' {
2
2
  import type { ResolvedStoreOptions } from '@growth-labs/store'
3
3
  export const config: ResolvedStoreOptions
4
- export function getConfig(): ResolvedStoreOptions
5
- export function getConfigOrNull(): ResolvedStoreOptions | null
4
+ export function getConfig(): Promise<ResolvedStoreOptions>
5
+ export function getConfigOrNull(): Promise<ResolvedStoreOptions | null>
6
6
  }