@growth-labs/store 0.2.0 → 0.2.2

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.
Files changed (53) hide show
  1. package/dist/client.d.ts +16 -0
  2. package/dist/client.d.ts.map +1 -1
  3. package/dist/client.js +7 -1
  4. package/dist/client.js.map +1 -1
  5. package/dist/collections.d.ts +35 -0
  6. package/dist/collections.d.ts.map +1 -1
  7. package/dist/collections.js +77 -0
  8. package/dist/collections.js.map +1 -1
  9. package/dist/health.d.ts +8 -5
  10. package/dist/health.d.ts.map +1 -1
  11. package/dist/health.js +12 -9
  12. package/dist/health.js.map +1 -1
  13. package/dist/index.d.ts +13 -2
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +12 -1
  16. package/dist/index.js.map +1 -1
  17. package/dist/options.d.ts +31 -26
  18. package/dist/options.d.ts.map +1 -1
  19. package/dist/options.js +11 -7
  20. package/dist/options.js.map +1 -1
  21. package/dist/routes/cart-bootstrap.d.ts.map +1 -1
  22. package/dist/routes/cart-bootstrap.js +2 -1
  23. package/dist/routes/cart-bootstrap.js.map +1 -1
  24. package/dist/routes/checkout.d.ts.map +1 -1
  25. package/dist/routes/checkout.js +2 -1
  26. package/dist/routes/checkout.js.map +1 -1
  27. package/dist/routes/webhook.d.ts.map +1 -1
  28. package/dist/routes/webhook.js +29 -1
  29. package/dist/routes/webhook.js.map +1 -1
  30. package/dist/state.d.ts +2 -2
  31. package/dist/state.d.ts.map +1 -1
  32. package/dist/state.js +32 -2
  33. package/dist/state.js.map +1 -1
  34. package/dist/types.d.ts +51 -0
  35. package/dist/types.d.ts.map +1 -1
  36. package/dist/types.js +4 -0
  37. package/dist/types.js.map +1 -1
  38. package/package.json +1 -1
  39. package/src/client.ts +8 -0
  40. package/src/collections.ts +95 -0
  41. package/src/components/StoreCartPage.astro +1 -1
  42. package/src/components/StoreCollectionPage.astro +35 -15
  43. package/src/components/StoreIndexPage.astro +10 -4
  44. package/src/components/StoreProductPage.astro +1 -1
  45. package/src/health.ts +14 -11
  46. package/src/index.ts +17 -1
  47. package/src/options.ts +11 -7
  48. package/src/routes/cart-bootstrap.ts +2 -1
  49. package/src/routes/checkout.ts +2 -1
  50. package/src/routes/webhook.ts +31 -1
  51. package/src/state.ts +34 -2
  52. package/src/types.ts +6 -0
  53. package/src/virtual.d.ts +2 -2
package/src/options.ts CHANGED
@@ -2,12 +2,13 @@ import { z } from 'zod'
2
2
 
3
3
  const storeCollectionSchema = z.object({
4
4
  handle: z.string().min(1),
5
- name: z.string().min(1),
5
+ /** Overrides the shop's own collection name when set; otherwise the live Fourthwall name is used. */
6
+ name: z.string().min(1).optional(),
6
7
  description: z.string().optional(),
7
8
  coverImagePath: z.string().optional(),
8
9
  grouping: z.enum(['division', 'type']).optional(),
9
- hidden: z.boolean().default(false),
10
- productKind: z.string().default('merch'),
10
+ hidden: z.boolean().optional(),
11
+ productKind: z.string().optional(),
11
12
  })
12
13
 
13
14
  const merchantReturnPolicySchema = z.object({
@@ -20,10 +21,13 @@ const baseStoreOptionsSchema = z.object({
20
21
  brandName: z.string().min(1),
21
22
 
22
23
  /**
23
- * The Fourthwall catalogue for this site, reconciled into config instead
24
- * of a per-site fork. An empty array is valid — a site without a
25
- * Fourthwall shop yet (bigbrain, pre-launch) still gets the health check
26
- * and routes; checkout stays disabled until `checkoutEnabled` is true.
24
+ * Collection OVERRIDES, not the exhaustive list — the live Fourthwall shop
25
+ * is the source of truth for which collections exist (see
26
+ * `resolveShopCollections`). A site only sets ordering, `grouping`,
27
+ * `hidden`, `coverImagePath`, or a label override here. An empty array is
28
+ * valid — a site without a Fourthwall shop yet (bigbrain, pre-launch)
29
+ * still gets the health check and routes; checkout stays disabled until
30
+ * `checkoutEnabled` is true.
27
31
  */
28
32
  collections: z.array(storeCollectionSchema).default([]),
29
33
 
@@ -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/types.ts CHANGED
@@ -81,6 +81,12 @@ export const FwCollectionProductsSchema = z.object({
81
81
  })
82
82
  export type FwCollectionProducts = z.infer<typeof FwCollectionProductsSchema>
83
83
 
84
+ export const FwCollectionListSchema = z.object({
85
+ results: z.array(FwCollectionSchema),
86
+ paging: FwPagingSchema,
87
+ })
88
+ export type FwCollectionList = z.infer<typeof FwCollectionListSchema>
89
+
84
90
  export const FwCartItemSchema = z.object({
85
91
  variant: FwVariantSchema,
86
92
  quantity: z.number(),
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
  }