@ikeboy003/cart 0.1.0 → 0.3.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/engine.d.ts.map +1 -1
- package/dist/engine.js +5 -0
- package/dist/engine.js.map +1 -1
- package/dist/server.d.ts +86 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +227 -0
- package/dist/server.js.map +1 -0
- package/dist/types.d.ts +10 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +26 -5
- package/src/engine.ts +7 -0
- package/src/server.ts +333 -0
- package/src/types.ts +11 -1
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAEZ,iBAAiB,EAClB,MAAM,YAAY,CAAC;AAuBpB,qBAAa,UAAU,CAAC,KAAK,SAAS,QAAQ,GAAG,QAAQ;IACvD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAE3B,OAAO,CAAC,KAAK,CAAe;IAC5B,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAyB;IACnD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAiB;IACzC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAyB;IAC/C,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,UAAU,CAA8C;IAChE,OAAO,CAAC,WAAW,CAAC,CAAa;IACjC,8EAA8E;IAC9E,OAAO,CAAC,WAAW,CAAe;gBAEtB,IAAI,EAAE,iBAAiB,CAAC,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACV,QAAQ,EACR,YAAY,EAEZ,iBAAi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+E;IACzE,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAc9B,OAAO,CAAC,YAAY;YASN,IAAI;IASlB;;;OAGG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAQ5B,sEAAsE;IACtE,OAAO,IAAI,IAAI;CAQhB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,UAAU,CAAC,KAAK,SAAS,QAAQ,GAAG,QAAQ,EAC1D,IAAI,EAAE,iBAAiB,CAAC,KAAK,CAAC,GAC7B,UAAU,CAAC,KAAK,CAAC,CAEnB"}
|
package/dist/engine.js
CHANGED
|
@@ -38,6 +38,11 @@ export class CartEngine {
|
|
|
38
38
|
};
|
|
39
39
|
/** Stable identity between mutations — safe for `useSyncExternalStore`. */
|
|
40
40
|
this.getSnapshot = () => this.snapshotRef;
|
|
41
|
+
// Production must persist: refuse to silently run as a local-only cart that
|
|
42
|
+
// drops every order. Local dev can omit sync (sqlite / no backend wired).
|
|
43
|
+
if (opts.env === "production" && !opts.sync) {
|
|
44
|
+
throw new Error('[cart] env "production" requires a `sync` adapter — without it the cart never persists. Pass sync, or use env "local".');
|
|
45
|
+
}
|
|
41
46
|
this.namespace = opts.namespace;
|
|
42
47
|
this.storage = opts.storage === undefined ? safeLocalStorage() : opts.storage;
|
|
43
48
|
this.sync = opts.sync;
|
package/dist/engine.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,2EAA2E;AAC3E,yEAAyE;AACzE,oEAAoE;AACpE,sDAAsD;AAStD,MAAM,QAAQ,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,QAAQ,EAAE,QAAQ,CAAC;AACpD,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC;AAE9C,SAAS,gBAAgB;IACvB,IAAI,CAAC;QACH,OAAO,OAAO,YAAY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AC,WAAW,IAAI,OAAO,QAAQ,KAAK,WAAW,EAAE,CAAC;YACxD,QAAQ,CAAC,mBAAmB,CAAC,kBAAkB,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;YACnE,MAAM,CAAC,mBAAmB,CAAC,UAAU,EAAE,IAAI,CAAC,WAAW,CAAC,CAAC;QAC3D,CAAC;QACD,IAAI,IAAI,CAAC,UAAU;YAAE,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QACnD,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAC;IACzB,CAAC;CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,UAAU,CACxB,IAA8B;IAE9B,OAAO,IAAI,UAAU,CAAQ,IAAI,CAAC,CAAC;AACrC,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { CartItem, CartSnapshot } from "./types.js";
|
|
2
|
+
/** The durable store the cart route talks to. Two methods, both per (id, namespace). */
|
|
3
|
+
export interface DurableCartStore<TItem extends CartItem = CartItem> {
|
|
4
|
+
read(id: string, namespace: string): Promise<TItem[] | null>;
|
|
5
|
+
write(snapshot: CartSnapshot<TItem>): Promise<void>;
|
|
6
|
+
}
|
|
7
|
+
/** Minimal structural shape of a D1 binding — your `env.CART_DB` satisfies it. */
|
|
8
|
+
export interface D1Like {
|
|
9
|
+
prepare(query: string): D1StmtLike;
|
|
10
|
+
}
|
|
11
|
+
export interface D1StmtLike {
|
|
12
|
+
bind(...values: unknown[]): D1StmtLike;
|
|
13
|
+
run(): Promise<unknown>;
|
|
14
|
+
first<T = unknown>(colName?: string): Promise<T | null>;
|
|
15
|
+
}
|
|
16
|
+
export type CartStoreEnv = "local" | "production";
|
|
17
|
+
/** Carts table DDL — apply once (D1 migration, or auto on the SQLite fallback). */
|
|
18
|
+
export declare const CARTS_TABLE_SQL = "CREATE TABLE IF NOT EXISTS carts (\n id TEXT PRIMARY KEY,\n namespace TEXT NOT NULL,\n items TEXT NOT NULL DEFAULT '[]',\n customer_ref TEXT,\n status TEXT NOT NULL DEFAULT 'active',\n created_at INTEGER,\n updated_at INTEGER\n)";
|
|
19
|
+
export interface CreateCartStoreOptions {
|
|
20
|
+
/** The D1 binding (e.g. `env.CART_DB`). Present → used. Absent in production → throws. */
|
|
21
|
+
d1?: D1Like | null;
|
|
22
|
+
/** "local" (D1 optional, SQLite fallback) vs "production" (D1 required). Default "local". */
|
|
23
|
+
env?: CartStoreEnv;
|
|
24
|
+
/** Local SQLite file when no binding (default "cart.sqlite"; ":memory:" for ephemeral). */
|
|
25
|
+
sqlitePath?: string;
|
|
26
|
+
/** Table name (default "carts"). */
|
|
27
|
+
table?: string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Build the durable cart store. Resolves a `DurableCartStore` — pass it to
|
|
31
|
+
* {@link cartHandlers}. The promise lets the local path lazy-load `node:sqlite`
|
|
32
|
+
* without forcing it into a Workers bundle.
|
|
33
|
+
*/
|
|
34
|
+
export declare function createCartStore<TItem extends CartItem = CartItem>(opts?: CreateCartStoreOptions): Promise<DurableCartStore<TItem>>;
|
|
35
|
+
/** `order_intents` DDL — the immutable per-checkout order snapshot. `createOrderStore`
|
|
36
|
+
* self-applies it (idempotent), so no separate migration step. */
|
|
37
|
+
export declare const ORDER_INTENTS_SQL = "CREATE TABLE IF NOT EXISTS order_intents (\n cart_id TEXT PRIMARY KEY,\n site TEXT NOT NULL,\n items TEXT NOT NULL DEFAULT '[]',\n subtotal_cents INTEGER NOT NULL DEFAULT 0,\n customer_name TEXT,\n customer_email TEXT,\n customer_phone TEXT,\n created_at INTEGER\n)";
|
|
38
|
+
/** A checkout snapshot: the cart's lines + the guest customer, frozen at PaymentIntent
|
|
39
|
+
* time. The payment webhook reads this (not the mutable cart) to build the order. */
|
|
40
|
+
export interface OrderSnapshot<TItem extends CartItem = CartItem> {
|
|
41
|
+
/** Cart id — the PaymentIntent carries it in metadata; the webhook joins on it. */
|
|
42
|
+
cartId: string;
|
|
43
|
+
/** Owning site / namespace (becomes the order's `site`). */
|
|
44
|
+
site: string;
|
|
45
|
+
/** Cart lines, stored VERBATIM — the order pipeline projects them to its own shape. */
|
|
46
|
+
items: TItem[];
|
|
47
|
+
/** Guest contact — only known at checkout, never on the cart. */
|
|
48
|
+
customer?: {
|
|
49
|
+
name?: string;
|
|
50
|
+
email?: string;
|
|
51
|
+
phone?: string;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Writes {@link OrderSnapshot}s. One method — the snapshot is write-once-per-checkout. */
|
|
55
|
+
export interface OrderSnapshotStore<TItem extends CartItem = CartItem> {
|
|
56
|
+
save(snapshot: OrderSnapshot<TItem>): Promise<void>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Build the order-snapshot store. Same env gating as {@link createCartStore}: a D1 binding
|
|
60
|
+
* is used when present (and self-applies the `order_intents` DDL); production without one
|
|
61
|
+
* throws; local without one falls back to `node:sqlite`.
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const orders = await createOrderStore({ d1: env.CART_DB, env: "production" });
|
|
65
|
+
* await orders.save({ cartId, site: "fourth-street", items, customer });
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function createOrderStore<TItem extends CartItem = CartItem>(opts?: CreateCartStoreOptions): Promise<OrderSnapshotStore<TItem>>;
|
|
69
|
+
/**
|
|
70
|
+
* Web-standard `GET`/`PUT` handlers for `/api/cart`, matching the `httpSync`
|
|
71
|
+
* wire contract. Framework-agnostic (Request → Response) — re-export them from a
|
|
72
|
+
* Next route handler, a Hono route, a raw Worker, etc. Accepts the store or a
|
|
73
|
+
* promise of it (what {@link createCartStore} returns).
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { env } from "cloudflare:workers";
|
|
77
|
+
* import { createCartStore, cartHandlers } from "@ikeboy003/cart/server";
|
|
78
|
+
* const store = createCartStore({ d1: env.CART_DB, env: env.APP_ENV });
|
|
79
|
+
* export const { GET, PUT } = cartHandlers(store);
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
export declare function cartHandlers<TItem extends CartItem = CartItem>(store: DurableCartStore<TItem> | Promise<DurableCartStore<TItem>>): {
|
|
83
|
+
GET: (req: Request) => Promise<Response>;
|
|
84
|
+
PUT: (req: Request) => Promise<Response>;
|
|
85
|
+
};
|
|
86
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEzD,wFAAwF;AACxF,MAAM,WAAW,gBAAgB,CAAC,KAAK,SAAS,QAAQ,GAAG,QAAQ;IACjE,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;IAC7D,KAAK,CAAC,QAAQ,EAAE,YAAY,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACrD;AAED,kFAAkF;AAClF,MAAM,WAAW,MAAM;IACrB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAAC;CACpC;AACD,MAAM,WAAW,UAAU;IACzB,IAAI,CAAC,GAAG,MAAM,EAAE,OAAO,EAAE,GAAG,UAAU,CAAC;IACvC,GAAG,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IACxB,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;CACzD;AAED,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,YAAY,CAAC;AAElD,mFAAmF;AACnF,eAAO,MAAM,eAAe,8QAQ1B,CAAC;AAEH,MAAM,WAAW,sBAAsB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AAG,QAAQ,EAC5D,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,GAAG,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,GAChE;IACD,GAAG,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IACzC,GAAG,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;CAC1C,CA0BA"}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
// Server half (separate entry `@ikeboy003/cart/server`) — the durable cart
|
|
2
|
+
// store the app's `/api/cart` route reads/writes, plus ready-made web-standard
|
|
3
|
+
// route handlers. Env-gated so D1 keys aren't required to set up locally, but
|
|
4
|
+
// are required once deployed:
|
|
5
|
+
//
|
|
6
|
+
// env "local" → uses the D1 binding if present (wrangler/workerd local D1
|
|
7
|
+
// IS sqlite, no keys); otherwise falls back to a local
|
|
8
|
+
// SQLite file via Node's built-in `node:sqlite`.
|
|
9
|
+
// env "production" → REQUIRES the D1 binding; throws if it's missing, so you
|
|
10
|
+
// can't deploy a cart that drops orders.
|
|
11
|
+
//
|
|
12
|
+
// Imports no Cloudflare types — `D1Like` is a structural shape your real binding
|
|
13
|
+
// satisfies. `node:sqlite` is lazy-imported only on the local fallback path, so
|
|
14
|
+
// a Workers build never pulls it in.
|
|
15
|
+
/** Carts table DDL — apply once (D1 migration, or auto on the SQLite fallback). */
|
|
16
|
+
export const CARTS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS carts (
|
|
17
|
+
id TEXT PRIMARY KEY,
|
|
18
|
+
namespace TEXT NOT NULL,
|
|
19
|
+
items TEXT NOT NULL DEFAULT '[]',
|
|
20
|
+
customer_ref TEXT,
|
|
21
|
+
status TEXT NOT NULL DEFAULT 'active',
|
|
22
|
+
created_at INTEGER,
|
|
23
|
+
updated_at INTEGER
|
|
24
|
+
)`;
|
|
25
|
+
function d1Store(d1, table) {
|
|
26
|
+
return {
|
|
27
|
+
async read(id, namespace) {
|
|
28
|
+
const items = await d1
|
|
29
|
+
.prepare(`SELECT items FROM ${table} WHERE id = ?1 AND namespace = ?2`)
|
|
30
|
+
.bind(id, namespace)
|
|
31
|
+
.first("items");
|
|
32
|
+
if (items == null)
|
|
33
|
+
return null;
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(items);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
async write(s) {
|
|
42
|
+
await d1
|
|
43
|
+
.prepare(`INSERT INTO ${table} (id, namespace, items, status, created_at, updated_at)
|
|
44
|
+
VALUES (?1, ?2, ?3, 'active', ?4, ?4)
|
|
45
|
+
ON CONFLICT(id) DO UPDATE SET items = ?3, updated_at = ?4`)
|
|
46
|
+
.bind(s.id, s.namespace, JSON.stringify(s.items), s.updatedAt)
|
|
47
|
+
.run();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async function sqliteStore(path, table) {
|
|
52
|
+
let DatabaseSync;
|
|
53
|
+
try {
|
|
54
|
+
// Non-literal specifier: `node:sqlite` is experimental and absent from
|
|
55
|
+
// @types/node, so a literal import fails type resolution. Resolved at
|
|
56
|
+
// runtime on Node >= 22.5 (stable in 24).
|
|
57
|
+
const spec = "node:sqlite";
|
|
58
|
+
const mod = (await import(spec));
|
|
59
|
+
DatabaseSync = mod.DatabaseSync;
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
throw new Error("[cart] local SQLite store needs Node's built-in node:sqlite (Node >= 22.5; stable in 24). Upgrade Node, or pass a d1 binding.");
|
|
63
|
+
}
|
|
64
|
+
const db = new DatabaseSync(path);
|
|
65
|
+
db.exec(CARTS_TABLE_SQL.replace("carts", table));
|
|
66
|
+
const ins = db.prepare(`INSERT INTO ${table} (id, namespace, items, status, created_at, updated_at)
|
|
67
|
+
VALUES (?, ?, ?, 'active', ?, ?)
|
|
68
|
+
ON CONFLICT(id) DO UPDATE SET items = excluded.items, updated_at = excluded.updated_at`);
|
|
69
|
+
const sel = db.prepare(`SELECT items FROM ${table} WHERE id = ? AND namespace = ?`);
|
|
70
|
+
return {
|
|
71
|
+
async read(id, namespace) {
|
|
72
|
+
const row = sel.get(id, namespace);
|
|
73
|
+
if (!row?.items)
|
|
74
|
+
return null;
|
|
75
|
+
try {
|
|
76
|
+
return JSON.parse(row.items);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
async write(s) {
|
|
83
|
+
ins.run(s.id, s.namespace, JSON.stringify(s.items), s.updatedAt, s.updatedAt);
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Build the durable cart store. Resolves a `DurableCartStore` — pass it to
|
|
89
|
+
* {@link cartHandlers}. The promise lets the local path lazy-load `node:sqlite`
|
|
90
|
+
* without forcing it into a Workers bundle.
|
|
91
|
+
*/
|
|
92
|
+
export async function createCartStore(opts = {}) {
|
|
93
|
+
const env = opts.env ?? "local";
|
|
94
|
+
const table = opts.table ?? "carts";
|
|
95
|
+
if (opts.d1)
|
|
96
|
+
return d1Store(opts.d1, table);
|
|
97
|
+
if (env === "production") {
|
|
98
|
+
throw new Error("[cart] production requires a D1 binding (pass `d1: env.CART_DB`). Local dev can omit it and uses SQLite.");
|
|
99
|
+
}
|
|
100
|
+
return sqliteStore(opts.sqlitePath ?? "cart.sqlite", table);
|
|
101
|
+
}
|
|
102
|
+
// ── order snapshot ─────────────────────────────────────────────────────────────
|
|
103
|
+
//
|
|
104
|
+
// The live cart is MUTABLE — it's emptied on checkout success. A payment webhook that
|
|
105
|
+
// rebuilt the order by reading the cart therefore raced the clear and got empty/0 orders.
|
|
106
|
+
// So at PaymentIntent time the checkout freezes an IMMUTABLE snapshot of the cart's lines
|
|
107
|
+
// (+ the guest customer, which only exists at checkout, never on the cart) into its own
|
|
108
|
+
// `order_intents` table, keyed by cart_id. The order pipeline reads THIS; carts stay carts.
|
|
109
|
+
/** `order_intents` DDL — the immutable per-checkout order snapshot. `createOrderStore`
|
|
110
|
+
* self-applies it (idempotent), so no separate migration step. */
|
|
111
|
+
export const ORDER_INTENTS_SQL = `CREATE TABLE IF NOT EXISTS order_intents (
|
|
112
|
+
cart_id TEXT PRIMARY KEY,
|
|
113
|
+
site TEXT NOT NULL,
|
|
114
|
+
items TEXT NOT NULL DEFAULT '[]',
|
|
115
|
+
subtotal_cents INTEGER NOT NULL DEFAULT 0,
|
|
116
|
+
customer_name TEXT,
|
|
117
|
+
customer_email TEXT,
|
|
118
|
+
customer_phone TEXT,
|
|
119
|
+
created_at INTEGER
|
|
120
|
+
)`;
|
|
121
|
+
/** Σ priceCents × quantity over the cart lines — the order subtotal, frozen here so the
|
|
122
|
+
* webhook never has to recompute it off a cart that may already be gone. */
|
|
123
|
+
function lineSubtotalCents(items) {
|
|
124
|
+
return items.reduce((n, i) => n + Math.round(i.priceCents) * i.quantity, 0);
|
|
125
|
+
}
|
|
126
|
+
function d1OrderStore(d1) {
|
|
127
|
+
const ready = d1.prepare(ORDER_INTENTS_SQL).run(); // self-bootstrap; idempotent
|
|
128
|
+
return {
|
|
129
|
+
async save(s) {
|
|
130
|
+
await ready;
|
|
131
|
+
await d1
|
|
132
|
+
.prepare(`INSERT INTO order_intents
|
|
133
|
+
(cart_id, site, items, subtotal_cents, customer_name, customer_email, customer_phone, created_at)
|
|
134
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
|
|
135
|
+
ON CONFLICT(cart_id) DO UPDATE SET
|
|
136
|
+
items=?3, subtotal_cents=?4, customer_name=?5, customer_email=?6, customer_phone=?7`)
|
|
137
|
+
.bind(s.cartId, s.site, JSON.stringify(s.items), lineSubtotalCents(s.items), s.customer?.name ?? "", s.customer?.email ?? "", s.customer?.phone ?? "", Date.now())
|
|
138
|
+
.run();
|
|
139
|
+
},
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
async function sqliteOrderStore(path) {
|
|
143
|
+
let DatabaseSync;
|
|
144
|
+
try {
|
|
145
|
+
const spec = "node:sqlite";
|
|
146
|
+
const mod = (await import(spec));
|
|
147
|
+
DatabaseSync = mod.DatabaseSync;
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
throw new Error("[cart] local order store needs Node's built-in node:sqlite (Node >= 22.5; stable in 24). Upgrade Node, or pass a d1 binding.");
|
|
151
|
+
}
|
|
152
|
+
const db = new DatabaseSync(path);
|
|
153
|
+
db.exec(ORDER_INTENTS_SQL);
|
|
154
|
+
const ins = db.prepare(`INSERT INTO order_intents
|
|
155
|
+
(cart_id, site, items, subtotal_cents, customer_name, customer_email, customer_phone, created_at)
|
|
156
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
157
|
+
ON CONFLICT(cart_id) DO UPDATE SET
|
|
158
|
+
items=excluded.items, subtotal_cents=excluded.subtotal_cents, customer_name=excluded.customer_name,
|
|
159
|
+
customer_email=excluded.customer_email, customer_phone=excluded.customer_phone`);
|
|
160
|
+
return {
|
|
161
|
+
async save(s) {
|
|
162
|
+
ins.run(s.cartId, s.site, JSON.stringify(s.items), lineSubtotalCents(s.items), s.customer?.name ?? "", s.customer?.email ?? "", s.customer?.phone ?? "", Date.now());
|
|
163
|
+
},
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Build the order-snapshot store. Same env gating as {@link createCartStore}: a D1 binding
|
|
168
|
+
* is used when present (and self-applies the `order_intents` DDL); production without one
|
|
169
|
+
* throws; local without one falls back to `node:sqlite`.
|
|
170
|
+
*
|
|
171
|
+
* ```ts
|
|
172
|
+
* const orders = await createOrderStore({ d1: env.CART_DB, env: "production" });
|
|
173
|
+
* await orders.save({ cartId, site: "fourth-street", items, customer });
|
|
174
|
+
* ```
|
|
175
|
+
*/
|
|
176
|
+
export async function createOrderStore(opts = {}) {
|
|
177
|
+
if (opts.d1)
|
|
178
|
+
return d1OrderStore(opts.d1);
|
|
179
|
+
if ((opts.env ?? "local") === "production") {
|
|
180
|
+
throw new Error("[cart] production order store requires a D1 binding (pass `d1: env.CART_DB`).");
|
|
181
|
+
}
|
|
182
|
+
return sqliteOrderStore(opts.sqlitePath ?? "cart.sqlite");
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Web-standard `GET`/`PUT` handlers for `/api/cart`, matching the `httpSync`
|
|
186
|
+
* wire contract. Framework-agnostic (Request → Response) — re-export them from a
|
|
187
|
+
* Next route handler, a Hono route, a raw Worker, etc. Accepts the store or a
|
|
188
|
+
* promise of it (what {@link createCartStore} returns).
|
|
189
|
+
*
|
|
190
|
+
* ```ts
|
|
191
|
+
* import { env } from "cloudflare:workers";
|
|
192
|
+
* import { createCartStore, cartHandlers } from "@ikeboy003/cart/server";
|
|
193
|
+
* const store = createCartStore({ d1: env.CART_DB, env: env.APP_ENV });
|
|
194
|
+
* export const { GET, PUT } = cartHandlers(store);
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
export function cartHandlers(store) {
|
|
198
|
+
const resolved = Promise.resolve(store);
|
|
199
|
+
return {
|
|
200
|
+
async GET(req) {
|
|
201
|
+
const url = new URL(req.url);
|
|
202
|
+
const id = url.searchParams.get("id");
|
|
203
|
+
const namespace = url.searchParams.get("namespace") ?? "";
|
|
204
|
+
if (!id)
|
|
205
|
+
return new Response("missing id", { status: 400 });
|
|
206
|
+
const items = await (await resolved).read(id, namespace);
|
|
207
|
+
if (!items)
|
|
208
|
+
return new Response(null, { status: 404 });
|
|
209
|
+
return Response.json({ items });
|
|
210
|
+
},
|
|
211
|
+
async PUT(req) {
|
|
212
|
+
let snap;
|
|
213
|
+
try {
|
|
214
|
+
snap = (await req.json());
|
|
215
|
+
}
|
|
216
|
+
catch {
|
|
217
|
+
return new Response("invalid json", { status: 400 });
|
|
218
|
+
}
|
|
219
|
+
if (!snap?.id || !Array.isArray(snap.items)) {
|
|
220
|
+
return new Response("invalid snapshot", { status: 400 });
|
|
221
|
+
}
|
|
222
|
+
await (await resolved).write(snap);
|
|
223
|
+
return new Response(null, { status: 204 });
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,+EAA+E;AAC/E,8EAA8E;AAC9E,8BAA8B;AAC9B,EAAE;AACF,iFAAiF;AACjF,4EAA4E;AAC5E,sEAAsE;AACtE,+EAA+E;AAC/E,8DAA8D;AAC9D,EAAE;AACF,iFAAiF;AACjF,gFAAgF;AAChF,qCAAqC;AAsBrC,mFAAmF;AACnF,MAAM,CAAC,MAAM,eAAe,GAAG;;;;;;;;EAQ7B,CAAC;AAaH,SAAS,OAAO,CAAyB,EAAU,EAAE,KAAa;IAChE,OAAO;QACL,KAAK,CAAC,IAAI,CAAC,EAAE,EAAE,SAAS;YACtB,MAAM,KAAK,GAAG,MAAM,EAAE;iBACnB,OAAO,CAAC,qBAAqB,KAAK,mCAAmC,CAAC;iBACtE,IAAI,CAAC,EAAE,EAAE,SAAS,CAAC;iBACnB,KAAK,CAAS,OAAO,CAAC,CAAC;YAC1B,IAAI,KAAK,IAAI,IAAI;gBAAE,OAAO,IAAI,CAAC;YAC/B,IAAI,CAAC;gBACH,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAY,CAAC;YACtC,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QACD,KAAK,CAAC,KAAK,CAAC,CAAC;YACX,MAAM,EAAE;iBACL,OAAO,CACN,eAAe,KAAK;;qEAEuC,CAC5D;iBACA,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,C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,EAAE,SAAS,CAAC,CAAC;YACzD,IAAI,CAAC,KAAK;gBAAE,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;YACvD,OAAO,QAAQ,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAClC,CAAC;QACD,KAAK,CAAC,GAAG,CAAC,GAAG;YACX,IAAI,IAAyB,CAAC;YAC9B,IAAI,CAAC;gBACH,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,IAAI,EAAE,CAAwB,CAAC;YACnD,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,IAAI,QAAQ,CAAC,cAAc,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;YACvD,CAAC;YACD,IAAI,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC;gBAC5C,OAAO,IAAI,QAAQ,CAAC,kBAAkB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;YAC3D,CAAC;YACD,MAAM,CAAC,MAAM,QAAQ,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACnC,OAAO,IAAI,QAAQ,CAAC,IAAI,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC;QAC7C,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -35,13 +35,22 @@ export interface CartSyncAdapter<TItem extends CartItem = CartItem> {
|
|
|
35
35
|
push(snapshot: CartSnapshot<TItem>): Promise<void>;
|
|
36
36
|
pull(id: string, namespace: string): Promise<TItem[] | null>;
|
|
37
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Deployment env. `"local"` makes the durable mirror optional — a site sets up
|
|
40
|
+
* and develops with NO sync/D1 wired (the cart runs on localStorage alone).
|
|
41
|
+
* `"production"` REQUIRES `sync` (throws at init if missing) so you can't ship a
|
|
42
|
+
* cart that silently never persists. Default `"local"`.
|
|
43
|
+
*/
|
|
44
|
+
export type CartEnv = "local" | "production";
|
|
38
45
|
/** Config passed at import — nothing brand-specific is baked in. */
|
|
39
46
|
export interface CreateCartOptions<TItem extends CartItem = CartItem> {
|
|
40
47
|
/** Scopes the storage keys + cart id, and is carried in the snapshot. */
|
|
41
48
|
namespace: string;
|
|
49
|
+
/** local (sync optional) vs production (sync required). Default "local". */
|
|
50
|
+
env?: CartEnv;
|
|
42
51
|
/** Persistence. Defaults to localStorage; pass `null` for in-memory only. */
|
|
43
52
|
storage?: Storage | null;
|
|
44
|
-
/** Durable mirror. Omit for local-only. */
|
|
53
|
+
/** Durable mirror. Omit for local-only (allowed in "local", not "production"). */
|
|
45
54
|
sync?: CartSyncAdapter<TItem>;
|
|
46
55
|
/** Coalesce window for durable pushes (ms). Default 400. */
|
|
47
56
|
debounceMs?: number;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAiBA;;;;;;GAMG;AACH,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kFAAkF;IAClF,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAED,yEAAyE;AACzE,MAAM,WAAW,YAAY,CAAC,KAAK,SAAS,QAAQ,GAAG,QAAQ;IAC7D,0EAA0E;IAC1E,EAAE,EAAE,MAAM,CAAC;IACX,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,KAAK,EAAE,CAAC;IACf,mFAAmF;IACnF,SAAS,EAAE,MAAM,CAAC;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,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACnD,IAAI,CAAC,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAAC;CAC9D;AAED;;;;;GAKG;AACH,MAAM,MAAM,OAAO,GAAG,OAAO,GAAG,YAAY,CAAC;AAE7C,oEAAoE;AACpE,MAAM,WAAW,iBAAiB,CAAC,KAAK,SAAS,QAAQ,GAAG,QAAQ;IAClE,yEAAyE;IACzE,SAAS,EAAE,MAAM,CAAC;IAClB,4EAA4E;IAC5E,GAAG,CAAC,EAAE,OAAO,CAAC;IACd,6EAA6E;IAC7E,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAC;IACzB,kFAAkF;IAClF,IAAI,CAAC,EAAE,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9B,4DAA4D;IAC5D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,8CAA8C;IAC9C,KAAK,CAAC,EAAE,MAAM,MAAM,CAAC;CACtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ikeboy003/cart",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Headless cart primitive: local-first state + a configurable durable sync path. Framework-agnostic core, optional React binding. No POS/payment/store logic baked in.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -11,26 +11,47 @@
|
|
|
11
11
|
"./react": {
|
|
12
12
|
"types": "./dist/react.d.ts",
|
|
13
13
|
"import": "./dist/react.js"
|
|
14
|
+
},
|
|
15
|
+
"./server": {
|
|
16
|
+
"types": "./dist/server.d.ts",
|
|
17
|
+
"import": "./dist/server.js"
|
|
14
18
|
}
|
|
15
19
|
},
|
|
16
|
-
"files": [
|
|
20
|
+
"files": [
|
|
21
|
+
"dist",
|
|
22
|
+
"src"
|
|
23
|
+
],
|
|
17
24
|
"sideEffects": false,
|
|
18
25
|
"scripts": {
|
|
19
26
|
"build": "tsc -p tsconfig.json",
|
|
20
27
|
"dev": "tsc -p tsconfig.json --watch",
|
|
21
28
|
"test": "node --test test/*.test.mjs"
|
|
22
29
|
},
|
|
23
|
-
"keywords": [
|
|
30
|
+
"keywords": [
|
|
31
|
+
"cart",
|
|
32
|
+
"ecommerce",
|
|
33
|
+
"headless",
|
|
34
|
+
"local-first"
|
|
35
|
+
],
|
|
24
36
|
"license": "MIT",
|
|
25
|
-
"publishConfig": {
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
26
40
|
"peerDependencies": {
|
|
27
41
|
"react": ">=18"
|
|
28
42
|
},
|
|
29
43
|
"peerDependenciesMeta": {
|
|
30
|
-
"react": {
|
|
44
|
+
"react": {
|
|
45
|
+
"optional": true
|
|
46
|
+
}
|
|
31
47
|
},
|
|
32
48
|
"devDependencies": {
|
|
33
49
|
"@types/react": "^19",
|
|
34
50
|
"typescript": "^5.5.0"
|
|
51
|
+
},
|
|
52
|
+
"repository": {
|
|
53
|
+
"type": "git",
|
|
54
|
+
"url": "git+https://github.com/ikeboy003/ikeboy-npm-packages.git",
|
|
55
|
+
"directory": "packages/cart"
|
|
35
56
|
}
|
|
36
57
|
}
|
package/src/engine.ts
CHANGED
|
@@ -47,6 +47,13 @@ export class CartEngine<TItem extends CartItem = CartItem> {
|
|
|
47
47
|
private snapshotRef: TItem[] = [];
|
|
48
48
|
|
|
49
49
|
constructor(opts: CreateCartOptions<TItem>) {
|
|
50
|
+
// Production must persist: refuse to silently run as a local-only cart that
|
|
51
|
+
// drops every order. Local dev can omit sync (sqlite / no backend wired).
|
|
52
|
+
if (opts.env === "production" && !opts.sync) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
'[cart] env "production" requires a `sync` adapter — without it the cart never persists. Pass sync, or use env "local".',
|
|
55
|
+
);
|
|
56
|
+
}
|
|
50
57
|
this.namespace = opts.namespace;
|
|
51
58
|
this.storage = opts.storage === undefined ? safeLocalStorage() : opts.storage;
|
|
52
59
|
this.sync = opts.sync;
|
package/src/server.ts
ADDED
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
// Server half (separate entry `@ikeboy003/cart/server`) — the durable cart
|
|
2
|
+
// store the app's `/api/cart` route reads/writes, plus ready-made web-standard
|
|
3
|
+
// route handlers. Env-gated so D1 keys aren't required to set up locally, but
|
|
4
|
+
// are required once deployed:
|
|
5
|
+
//
|
|
6
|
+
// env "local" → uses the D1 binding if present (wrangler/workerd local D1
|
|
7
|
+
// IS sqlite, no keys); otherwise falls back to a local
|
|
8
|
+
// SQLite file via Node's built-in `node:sqlite`.
|
|
9
|
+
// env "production" → REQUIRES the D1 binding; throws if it's missing, so you
|
|
10
|
+
// can't deploy a cart that drops orders.
|
|
11
|
+
//
|
|
12
|
+
// Imports no Cloudflare types — `D1Like` is a structural shape your real binding
|
|
13
|
+
// satisfies. `node:sqlite` is lazy-imported only on the local fallback path, so
|
|
14
|
+
// a Workers build never pulls it in.
|
|
15
|
+
|
|
16
|
+
import type { CartItem, CartSnapshot } from "./types.js";
|
|
17
|
+
|
|
18
|
+
/** The durable store the cart route talks to. Two methods, both per (id, namespace). */
|
|
19
|
+
export interface DurableCartStore<TItem extends CartItem = CartItem> {
|
|
20
|
+
read(id: string, namespace: string): Promise<TItem[] | null>;
|
|
21
|
+
write(snapshot: CartSnapshot<TItem>): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Minimal structural shape of a D1 binding — your `env.CART_DB` satisfies it. */
|
|
25
|
+
export interface D1Like {
|
|
26
|
+
prepare(query: string): D1StmtLike;
|
|
27
|
+
}
|
|
28
|
+
export interface D1StmtLike {
|
|
29
|
+
bind(...values: unknown[]): D1StmtLike;
|
|
30
|
+
run(): Promise<unknown>;
|
|
31
|
+
first<T = unknown>(colName?: string): Promise<T | null>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export type CartStoreEnv = "local" | "production";
|
|
35
|
+
|
|
36
|
+
/** Carts table DDL — apply once (D1 migration, or auto on the SQLite fallback). */
|
|
37
|
+
export const CARTS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS carts (
|
|
38
|
+
id TEXT PRIMARY KEY,
|
|
39
|
+
namespace TEXT NOT NULL,
|
|
40
|
+
items TEXT NOT NULL DEFAULT '[]',
|
|
41
|
+
customer_ref TEXT,
|
|
42
|
+
status TEXT NOT NULL DEFAULT 'active',
|
|
43
|
+
created_at INTEGER,
|
|
44
|
+
updated_at INTEGER
|
|
45
|
+
)`;
|
|
46
|
+
|
|
47
|
+
export interface CreateCartStoreOptions {
|
|
48
|
+
/** The D1 binding (e.g. `env.CART_DB`). Present → used. Absent in production → throws. */
|
|
49
|
+
d1?: D1Like | null;
|
|
50
|
+
/** "local" (D1 optional, SQLite fallback) vs "production" (D1 required). Default "local". */
|
|
51
|
+
env?: CartStoreEnv;
|
|
52
|
+
/** Local SQLite file when no binding (default "cart.sqlite"; ":memory:" for ephemeral). */
|
|
53
|
+
sqlitePath?: string;
|
|
54
|
+
/** Table name (default "carts"). */
|
|
55
|
+
table?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function d1Store<TItem extends CartItem>(d1: D1Like, table: string): DurableCartStore<TItem> {
|
|
59
|
+
return {
|
|
60
|
+
async read(id, namespace) {
|
|
61
|
+
const items = await d1
|
|
62
|
+
.prepare(`SELECT items FROM ${table} WHERE id = ?1 AND namespace = ?2`)
|
|
63
|
+
.bind(id, namespace)
|
|
64
|
+
.first<string>("items");
|
|
65
|
+
if (items == null) return null;
|
|
66
|
+
try {
|
|
67
|
+
return JSON.parse(items) as TItem[];
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
async write(s) {
|
|
73
|
+
await d1
|
|
74
|
+
.prepare(
|
|
75
|
+
`INSERT INTO ${table} (id, namespace, items, status, created_at, updated_at)
|
|
76
|
+
VALUES (?1, ?2, ?3, 'active', ?4, ?4)
|
|
77
|
+
ON CONFLICT(id) DO UPDATE SET items = ?3, updated_at = ?4`,
|
|
78
|
+
)
|
|
79
|
+
.bind(s.id, s.namespace, JSON.stringify(s.items), s.updatedAt)
|
|
80
|
+
.run();
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function sqliteStore<TItem extends CartItem>(
|
|
86
|
+
path: string,
|
|
87
|
+
table: string,
|
|
88
|
+
): Promise<DurableCartStore<TItem>> {
|
|
89
|
+
type SqliteDb = {
|
|
90
|
+
exec(sql: string): void;
|
|
91
|
+
prepare(sql: string): { run(...a: unknown[]): unknown; get(...a: unknown[]): unknown };
|
|
92
|
+
};
|
|
93
|
+
let DatabaseSync: new (p: string) => SqliteDb;
|
|
94
|
+
try {
|
|
95
|
+
// Non-literal specifier: `node:sqlite` is experimental and absent from
|
|
96
|
+
// @types/node, so a literal import fails type resolution. Resolved at
|
|
97
|
+
// runtime on Node >= 22.5 (stable in 24).
|
|
98
|
+
const spec = "node:sqlite";
|
|
99
|
+
const mod = (await import(spec)) as unknown as {
|
|
100
|
+
DatabaseSync: new (p: string) => SqliteDb;
|
|
101
|
+
};
|
|
102
|
+
DatabaseSync = mod.DatabaseSync;
|
|
103
|
+
} catch {
|
|
104
|
+
throw new Error(
|
|
105
|
+
"[cart] local SQLite store needs Node's built-in node:sqlite (Node >= 22.5; stable in 24). Upgrade Node, or pass a d1 binding.",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
const db = new DatabaseSync(path);
|
|
109
|
+
db.exec(CARTS_TABLE_SQL.replace("carts", table));
|
|
110
|
+
const ins = db.prepare(
|
|
111
|
+
`INSERT INTO ${table} (id, namespace, items, status, created_at, updated_at)
|
|
112
|
+
VALUES (?, ?, ?, 'active', ?, ?)
|
|
113
|
+
ON CONFLICT(id) DO UPDATE SET items = excluded.items, updated_at = excluded.updated_at`,
|
|
114
|
+
);
|
|
115
|
+
const sel = db.prepare(`SELECT items FROM ${table} WHERE id = ? AND namespace = ?`);
|
|
116
|
+
return {
|
|
117
|
+
async read(id, namespace) {
|
|
118
|
+
const row = sel.get(id, namespace) as { items?: string } | undefined;
|
|
119
|
+
if (!row?.items) return null;
|
|
120
|
+
try {
|
|
121
|
+
return JSON.parse(row.items) as TItem[];
|
|
122
|
+
} catch {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
async write(s) {
|
|
127
|
+
ins.run(s.id, s.namespace, JSON.stringify(s.items), s.updatedAt, s.updatedAt);
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Build the durable cart store. Resolves a `DurableCartStore` — pass it to
|
|
134
|
+
* {@link cartHandlers}. The promise lets the local path lazy-load `node:sqlite`
|
|
135
|
+
* without forcing it into a Workers bundle.
|
|
136
|
+
*/
|
|
137
|
+
export async function createCartStore<TItem extends CartItem = CartItem>(
|
|
138
|
+
opts: CreateCartStoreOptions = {},
|
|
139
|
+
): Promise<DurableCartStore<TItem>> {
|
|
140
|
+
const env = opts.env ?? "local";
|
|
141
|
+
const table = opts.table ?? "carts";
|
|
142
|
+
if (opts.d1) return d1Store<TItem>(opts.d1, table);
|
|
143
|
+
if (env === "production") {
|
|
144
|
+
throw new Error(
|
|
145
|
+
"[cart] production requires a D1 binding (pass `d1: env.CART_DB`). Local dev can omit it and uses SQLite.",
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return sqliteStore<TItem>(opts.sqlitePath ?? "cart.sqlite", table);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ── order snapshot ─────────────────────────────────────────────────────────────
|
|
152
|
+
//
|
|
153
|
+
// The live cart is MUTABLE — it's emptied on checkout success. A payment webhook that
|
|
154
|
+
// rebuilt the order by reading the cart therefore raced the clear and got empty/0 orders.
|
|
155
|
+
// So at PaymentIntent time the checkout freezes an IMMUTABLE snapshot of the cart's lines
|
|
156
|
+
// (+ the guest customer, which only exists at checkout, never on the cart) into its own
|
|
157
|
+
// `order_intents` table, keyed by cart_id. The order pipeline reads THIS; carts stay carts.
|
|
158
|
+
|
|
159
|
+
/** `order_intents` DDL — the immutable per-checkout order snapshot. `createOrderStore`
|
|
160
|
+
* self-applies it (idempotent), so no separate migration step. */
|
|
161
|
+
export const ORDER_INTENTS_SQL = `CREATE TABLE IF NOT EXISTS order_intents (
|
|
162
|
+
cart_id TEXT PRIMARY KEY,
|
|
163
|
+
site TEXT NOT NULL,
|
|
164
|
+
items TEXT NOT NULL DEFAULT '[]',
|
|
165
|
+
subtotal_cents INTEGER NOT NULL DEFAULT 0,
|
|
166
|
+
customer_name TEXT,
|
|
167
|
+
customer_email TEXT,
|
|
168
|
+
customer_phone TEXT,
|
|
169
|
+
created_at INTEGER
|
|
170
|
+
)`;
|
|
171
|
+
|
|
172
|
+
/** A checkout snapshot: the cart's lines + the guest customer, frozen at PaymentIntent
|
|
173
|
+
* time. The payment webhook reads this (not the mutable cart) to build the order. */
|
|
174
|
+
export interface OrderSnapshot<TItem extends CartItem = CartItem> {
|
|
175
|
+
/** Cart id — the PaymentIntent carries it in metadata; the webhook joins on it. */
|
|
176
|
+
cartId: string;
|
|
177
|
+
/** Owning site / namespace (becomes the order's `site`). */
|
|
178
|
+
site: string;
|
|
179
|
+
/** Cart lines, stored VERBATIM — the order pipeline projects them to its own shape. */
|
|
180
|
+
items: TItem[];
|
|
181
|
+
/** Guest contact — only known at checkout, never on the cart. */
|
|
182
|
+
customer?: { name?: string; email?: string; phone?: string };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** Writes {@link OrderSnapshot}s. One method — the snapshot is write-once-per-checkout. */
|
|
186
|
+
export interface OrderSnapshotStore<TItem extends CartItem = CartItem> {
|
|
187
|
+
save(snapshot: OrderSnapshot<TItem>): Promise<void>;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Σ priceCents × quantity over the cart lines — the order subtotal, frozen here so the
|
|
191
|
+
* webhook never has to recompute it off a cart that may already be gone. */
|
|
192
|
+
function lineSubtotalCents<TItem extends CartItem>(items: TItem[]): number {
|
|
193
|
+
return items.reduce((n, i) => n + Math.round(i.priceCents) * i.quantity, 0);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function d1OrderStore<TItem extends CartItem>(d1: D1Like): OrderSnapshotStore<TItem> {
|
|
197
|
+
const ready = d1.prepare(ORDER_INTENTS_SQL).run(); // self-bootstrap; idempotent
|
|
198
|
+
return {
|
|
199
|
+
async save(s) {
|
|
200
|
+
await ready;
|
|
201
|
+
await d1
|
|
202
|
+
.prepare(
|
|
203
|
+
`INSERT INTO order_intents
|
|
204
|
+
(cart_id, site, items, subtotal_cents, customer_name, customer_email, customer_phone, created_at)
|
|
205
|
+
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)
|
|
206
|
+
ON CONFLICT(cart_id) DO UPDATE SET
|
|
207
|
+
items=?3, subtotal_cents=?4, customer_name=?5, customer_email=?6, customer_phone=?7`,
|
|
208
|
+
)
|
|
209
|
+
.bind(
|
|
210
|
+
s.cartId,
|
|
211
|
+
s.site,
|
|
212
|
+
JSON.stringify(s.items),
|
|
213
|
+
lineSubtotalCents(s.items),
|
|
214
|
+
s.customer?.name ?? "",
|
|
215
|
+
s.customer?.email ?? "",
|
|
216
|
+
s.customer?.phone ?? "",
|
|
217
|
+
Date.now(),
|
|
218
|
+
)
|
|
219
|
+
.run();
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async function sqliteOrderStore<TItem extends CartItem>(
|
|
225
|
+
path: string,
|
|
226
|
+
): Promise<OrderSnapshotStore<TItem>> {
|
|
227
|
+
type SqliteDb = {
|
|
228
|
+
exec(sql: string): void;
|
|
229
|
+
prepare(sql: string): { run(...a: unknown[]): unknown };
|
|
230
|
+
};
|
|
231
|
+
let DatabaseSync: new (p: string) => SqliteDb;
|
|
232
|
+
try {
|
|
233
|
+
const spec = "node:sqlite";
|
|
234
|
+
const mod = (await import(spec)) as unknown as { DatabaseSync: new (p: string) => SqliteDb };
|
|
235
|
+
DatabaseSync = mod.DatabaseSync;
|
|
236
|
+
} catch {
|
|
237
|
+
throw new Error(
|
|
238
|
+
"[cart] local order store needs Node's built-in node:sqlite (Node >= 22.5; stable in 24). Upgrade Node, or pass a d1 binding.",
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
const db = new DatabaseSync(path);
|
|
242
|
+
db.exec(ORDER_INTENTS_SQL);
|
|
243
|
+
const ins = db.prepare(
|
|
244
|
+
`INSERT INTO order_intents
|
|
245
|
+
(cart_id, site, items, subtotal_cents, customer_name, customer_email, customer_phone, created_at)
|
|
246
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
247
|
+
ON CONFLICT(cart_id) DO UPDATE SET
|
|
248
|
+
items=excluded.items, subtotal_cents=excluded.subtotal_cents, customer_name=excluded.customer_name,
|
|
249
|
+
customer_email=excluded.customer_email, customer_phone=excluded.customer_phone`,
|
|
250
|
+
);
|
|
251
|
+
return {
|
|
252
|
+
async save(s) {
|
|
253
|
+
ins.run(
|
|
254
|
+
s.cartId,
|
|
255
|
+
s.site,
|
|
256
|
+
JSON.stringify(s.items),
|
|
257
|
+
lineSubtotalCents(s.items),
|
|
258
|
+
s.customer?.name ?? "",
|
|
259
|
+
s.customer?.email ?? "",
|
|
260
|
+
s.customer?.phone ?? "",
|
|
261
|
+
Date.now(),
|
|
262
|
+
);
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Build the order-snapshot store. Same env gating as {@link createCartStore}: a D1 binding
|
|
269
|
+
* is used when present (and self-applies the `order_intents` DDL); production without one
|
|
270
|
+
* throws; local without one falls back to `node:sqlite`.
|
|
271
|
+
*
|
|
272
|
+
* ```ts
|
|
273
|
+
* const orders = await createOrderStore({ d1: env.CART_DB, env: "production" });
|
|
274
|
+
* await orders.save({ cartId, site: "fourth-street", items, customer });
|
|
275
|
+
* ```
|
|
276
|
+
*/
|
|
277
|
+
export async function createOrderStore<TItem extends CartItem = CartItem>(
|
|
278
|
+
opts: CreateCartStoreOptions = {},
|
|
279
|
+
): Promise<OrderSnapshotStore<TItem>> {
|
|
280
|
+
if (opts.d1) return d1OrderStore<TItem>(opts.d1);
|
|
281
|
+
if ((opts.env ?? "local") === "production") {
|
|
282
|
+
throw new Error(
|
|
283
|
+
"[cart] production order store requires a D1 binding (pass `d1: env.CART_DB`).",
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return sqliteOrderStore<TItem>(opts.sqlitePath ?? "cart.sqlite");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Web-standard `GET`/`PUT` handlers for `/api/cart`, matching the `httpSync`
|
|
291
|
+
* wire contract. Framework-agnostic (Request → Response) — re-export them from a
|
|
292
|
+
* Next route handler, a Hono route, a raw Worker, etc. Accepts the store or a
|
|
293
|
+
* promise of it (what {@link createCartStore} returns).
|
|
294
|
+
*
|
|
295
|
+
* ```ts
|
|
296
|
+
* import { env } from "cloudflare:workers";
|
|
297
|
+
* import { createCartStore, cartHandlers } from "@ikeboy003/cart/server";
|
|
298
|
+
* const store = createCartStore({ d1: env.CART_DB, env: env.APP_ENV });
|
|
299
|
+
* export const { GET, PUT } = cartHandlers(store);
|
|
300
|
+
* ```
|
|
301
|
+
*/
|
|
302
|
+
export function cartHandlers<TItem extends CartItem = CartItem>(
|
|
303
|
+
store: DurableCartStore<TItem> | Promise<DurableCartStore<TItem>>,
|
|
304
|
+
): {
|
|
305
|
+
GET: (req: Request) => Promise<Response>;
|
|
306
|
+
PUT: (req: Request) => Promise<Response>;
|
|
307
|
+
} {
|
|
308
|
+
const resolved = Promise.resolve(store);
|
|
309
|
+
return {
|
|
310
|
+
async GET(req) {
|
|
311
|
+
const url = new URL(req.url);
|
|
312
|
+
const id = url.searchParams.get("id");
|
|
313
|
+
const namespace = url.searchParams.get("namespace") ?? "";
|
|
314
|
+
if (!id) return new Response("missing id", { status: 400 });
|
|
315
|
+
const items = await (await resolved).read(id, namespace);
|
|
316
|
+
if (!items) return new Response(null, { status: 404 });
|
|
317
|
+
return Response.json({ items });
|
|
318
|
+
},
|
|
319
|
+
async PUT(req) {
|
|
320
|
+
let snap: CartSnapshot<TItem>;
|
|
321
|
+
try {
|
|
322
|
+
snap = (await req.json()) as CartSnapshot<TItem>;
|
|
323
|
+
} catch {
|
|
324
|
+
return new Response("invalid json", { status: 400 });
|
|
325
|
+
}
|
|
326
|
+
if (!snap?.id || !Array.isArray(snap.items)) {
|
|
327
|
+
return new Response("invalid snapshot", { status: 400 });
|
|
328
|
+
}
|
|
329
|
+
await (await resolved).write(snap);
|
|
330
|
+
return new Response(null, { status: 204 });
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -55,13 +55,23 @@ export interface CartSyncAdapter<TItem extends CartItem = CartItem> {
|
|
|
55
55
|
pull(id: string, namespace: string): Promise<TItem[] | null>;
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Deployment env. `"local"` makes the durable mirror optional — a site sets up
|
|
60
|
+
* and develops with NO sync/D1 wired (the cart runs on localStorage alone).
|
|
61
|
+
* `"production"` REQUIRES `sync` (throws at init if missing) so you can't ship a
|
|
62
|
+
* cart that silently never persists. Default `"local"`.
|
|
63
|
+
*/
|
|
64
|
+
export type CartEnv = "local" | "production";
|
|
65
|
+
|
|
58
66
|
/** Config passed at import — nothing brand-specific is baked in. */
|
|
59
67
|
export interface CreateCartOptions<TItem extends CartItem = CartItem> {
|
|
60
68
|
/** Scopes the storage keys + cart id, and is carried in the snapshot. */
|
|
61
69
|
namespace: string;
|
|
70
|
+
/** local (sync optional) vs production (sync required). Default "local". */
|
|
71
|
+
env?: CartEnv;
|
|
62
72
|
/** Persistence. Defaults to localStorage; pass `null` for in-memory only. */
|
|
63
73
|
storage?: Storage | null;
|
|
64
|
-
/** Durable mirror. Omit for local-only. */
|
|
74
|
+
/** Durable mirror. Omit for local-only (allowed in "local", not "production"). */
|
|
65
75
|
sync?: CartSyncAdapter<TItem>;
|
|
66
76
|
/** Coalesce window for durable pushes (ms). Default 400. */
|
|
67
77
|
debounceMs?: number;
|