@umxr/bagsy 0.0.0-stage → 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Umar Gora
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,258 @@
1
- # Temporary Holding Version
1
+ # bagsy
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Wishlists for Shopify, built for developers. It covers Liquid themes and Hydrogen storefronts with one API, a typed client and no remotely loaded code.
4
+
5
+ ```sh
6
+ npm install @umxr/bagsy
7
+ ```
8
+
9
+ You also need the Bagsy app installed on the store. Server keys are in **Apps → Bagsy → Developers**.
10
+
11
+ | Import | For | Peer dependency |
12
+ | --- | --- | --- |
13
+ | `@umxr/bagsy` | The typed client: lists, items, saved lookups, sharing, insights | none |
14
+ | `@umxr/bagsy/react` | `useSaved`, `useWishlist`, `useWishlistCount`, `<WishlistButton>` | `react` 18 or 19 |
15
+ | `@umxr/bagsy/hydrogen` | Hydrogen 2026-10 route handlers, shopper identity, server-rendered state | `@shopify/hydrogen` ≥ 2026.10 |
16
+ | `@umxr/bagsy/server` | The same as `@umxr/bagsy/hydrogen`, for any server framework | none |
17
+ | `@umxr/bagsy/element` | `<bagsy-button>`, `<bagsy-count>`, `<bagsy-wishlist>` web components | none |
18
+ | `@umxr/bagsy/styles.css` | Default styles for the web components | none |
19
+
20
+ Everything is ESM, typed and tree-shakeable.
21
+
22
+ ## Hydrogen
23
+
24
+ Your storefront's server relays the browser's calls to Bagsy, adding your key and the shopper's identity from the session. Browser code never sees your key or a customer id. A guest's id lives in your session cookie, which is HttpOnly, so scripts can't read it. Use an encrypted session if you don't want it readable at all.
25
+
26
+ ### 1. Keys
27
+
28
+ Create a server key in **Apps → Bagsy → Developers**, then add it to your storefront's environment:
29
+
30
+ ```sh
31
+ BAGSY_API_KEY=bsk_live_…
32
+ BAGSY_API_URL=https://bagsy-api.<your-subdomain>.workers.dev/v1
33
+ ```
34
+
35
+ ### 2. A persistent session
36
+
37
+ Bagsy keeps a guest's wishlist id in your storefront session. Hydrogen's templates fall back to an **in-memory** session when customer accounts aren't configured, so a guest's saves would disappear on the next request. Use a cookie session with `SESSION_SECRET`, even for a guest-only store. Bagsy logs a warning if it writes the session and committing it sets no cookie.
38
+
39
+ ### 3. Server
40
+
41
+ Hydrogen gives you `env` per request (Oxygen and MiniOxygen), so create Bagsy in your request handler. It's cheap: caches live at module scope, not on the instance. Register its handlers with `handleShopifyRoutes`, and put it in a router context for your loaders:
42
+
43
+ ```ts
44
+ // app/lib/bagsy.server.ts
45
+ import { createContext } from 'react-router';
46
+ import { createHydrogenBagsy, type HydrogenWriteContext } from '@umxr/bagsy/hydrogen';
47
+ import type { CustomerSession } from '@shopify/hydrogen/customer-account';
48
+
49
+ export function getBagsy(env: Env, customerSession?: CustomerSession) {
50
+ return createHydrogenBagsy({
51
+ apiKey: env.BAGSY_API_KEY,
52
+ baseUrl: env.BAGSY_API_URL,
53
+ // Leave both out for a guest-only storefront.
54
+ ...(customerSession && { customerSession, shopId: env.SHOP_ID }),
55
+ });
56
+ }
57
+
58
+ export const bagsyContext = createContext<HydrogenWriteContext & { bagsy: ReturnType<typeof getBagsy> }>();
59
+ ```
60
+
61
+ ```ts
62
+ // app/root.tsx: in your existing Hydrogen middleware
63
+ const bagsy = getBagsy(env, customerSession);
64
+
65
+ const shopifyRoute = handleShopifyRoutes({
66
+ request,
67
+ requestContext,
68
+ storefrontClient,
69
+ sessionManager,
70
+ routeTemplates,
71
+ handlers: [cartHandlers, customerAccountHandlers, bagsy.handlers],
72
+ });
73
+ if (shopifyRoute) return shopifyRoute;
74
+
75
+ context.set(bagsyContext, { bagsy, sessionManager, requestContext });
76
+ ```
77
+
78
+ The relay commits the session itself when it writes one, like Hydrogen's other handlers.
79
+
80
+ ### 4. Provider and header count
81
+
82
+ ```tsx
83
+ // app/root.tsx
84
+ import { BagsyProvider, useWishlistCount } from '@umxr/bagsy/react';
85
+
86
+ export async function loader({ context }: Route.LoaderArgs) {
87
+ const { bagsy, ...ctx } = context.get(bagsyContext);
88
+ const shopper = await bagsy.shopper(ctx); // read-only: never refreshes tokens or writes the session
89
+ return { wishlistCount: await shopper.count() };
90
+ }
91
+
92
+ export default function App({ loaderData }: Route.ComponentProps) {
93
+ return (
94
+ <BagsyProvider initialCount={loaderData.wishlistCount}>
95
+ <Outlet />
96
+ </BagsyProvider>
97
+ );
98
+ }
99
+
100
+ function HeaderWishlistLink() {
101
+ const count = useWishlistCount();
102
+ return <a href="/wishlist">Wishlist {count ? `(${count})` : null}</a>;
103
+ }
104
+ ```
105
+
106
+ `initialCount` follows the root loader as it revalidates. Calling `shopper()` marks the response as personalized, so Hydrogen won't cache it publicly. To keep pages cacheable, leave out `initialCount` and the count loads in the browser.
107
+
108
+ ### 5. Hearts
109
+
110
+ ```tsx
111
+ import { WishlistButton, useSeedSaved } from '@umxr/bagsy/react';
112
+
113
+ // A collection: one Bagsy call for the whole grid, rendered on the server.
114
+ export async function loader({ context }: Route.LoaderArgs) {
115
+ const products = await getCollectionProducts(context);
116
+ const { bagsy, ...ctx } = context.get(bagsyContext);
117
+ const shopper = await bagsy.shopper(ctx);
118
+ return { products, saved: (await shopper.saved(products.map((p) => p.id))).byProduct };
119
+ }
120
+
121
+ export default function Collection({ loaderData }: Route.ComponentProps) {
122
+ useSeedSaved(loaderData.saved);
123
+ return loaderData.products.map((product) => (
124
+ <a key={product.id} href={`/products/${product.handle}`}>
125
+ {product.title}
126
+ <WishlistButton productId={product.id} aria-label="Save to wishlist">
127
+ {({ saved }) => (saved ? '♥' : '♡')}
128
+ </WishlistButton>
129
+ </a>
130
+ ));
131
+ }
132
+ ```
133
+
134
+ On a product page, pass the selected variant: `<WishlistButton productId={product.id} variantId={selectedVariant.id}>`.
135
+
136
+ Without seeding, hearts still work. Every heart that mounts in the same tick shares one lookup, and a guest who hasn't saved anything costs no API call.
137
+
138
+ **How variants work:**
139
+ - A product saved without a variant (from a card) counts as saved for every variant.
140
+ - A variant heart is filled for that variant, or for a product-level save.
141
+ - Unsaving from a variant heart removes that variant and any product-level save.
142
+ - Unsaving from a product heart removes every variant.
143
+
144
+ ### 6. Wishlist page
145
+
146
+ ```tsx
147
+ import { useWishlist } from '@umxr/bagsy/react';
148
+
149
+ export async function loader({ context }: Route.LoaderArgs) {
150
+ const { bagsy, ...ctx } = context.get(bagsyContext);
151
+ const shopper = await bagsy.shopper(ctx);
152
+ const page = await shopper.items({ limit: 24 });
153
+ // Bagsy stores ids; product data stays live from the Storefront API.
154
+ const products = await getProductsByIds(context, page.data.map((item) => item.productId));
155
+ return { page, products };
156
+ }
157
+
158
+ export default function Wishlist({ loaderData }: Route.ComponentProps) {
159
+ const { items, remove, hasNextPage, loadMore } = useWishlist({ pageSize: 24, initial: loaderData.page });
160
+ // `remove(item)` is optimistic, and hearts elsewhere update too. Items added by
161
+ // loadMore() need their product data fetched, e.g. from a resource route.
162
+ }
163
+ ```
164
+
165
+ ### Without JavaScript
166
+
167
+ In an action, `bagsy.writableShopper(ctx)` gives you a client that acts for the shopper. Commit the session with the response:
168
+
169
+ ```ts
170
+ export async function action({ request, context }: Route.ActionArgs) {
171
+ const { bagsy, ...ctx } = context.get(bagsyContext);
172
+ const shopper = await bagsy.writableShopper(ctx);
173
+ await shopper.client.items.toggle(String((await request.formData()).get('productId')));
174
+ return redirect(request.headers.get('Referer') ?? '/', { headers: await commitSession() });
175
+ }
176
+ ```
177
+
178
+ ### How identity works
179
+
180
+ | Shopper | Owner sent to Bagsy | Stored |
181
+ | --- | --- | --- |
182
+ | Guest, before saving | nothing: reads are answered with no API call | nothing |
183
+ | Guest, after a successful save | a random visitor id | `bagsy` in your session |
184
+ | Signed in | customer GID from the Customer Account API | `bagsy:customer` in the session (the id and a hash of the access token), plus an in-memory cache |
185
+
186
+ Right after sign-in, the first loader merges the guest's wishlist into the customer's on the API. Every read on that page agrees, and the merge runs once. The next relay call or action clears the guest id from the session. If the merge fails, loaders show both lists until it succeeds.
187
+
188
+ ### What browsers can call
189
+
190
+ The relay accepts same-origin `POST` requests with `Content-Type: application/json`. Hydrogen itself rejects cross-origin `POST`s before the relay runs. It forwards only these calls:
191
+
192
+ - **Lists:** create, read, rename and delete.
193
+ - **Items:** add, update and remove.
194
+ - **Sharing:** share and unshare a list.
195
+ - **Reads:** saved lookups, shared lists and health.
196
+
197
+ Insights and identity merging are never forwarded, and any `visitor_id` the browser sends is ignored. API errors pass through with their status, so `BagsyError.code` works in the browser.
198
+
199
+ ### Other frameworks
200
+
201
+ `@umxr/bagsy/server` does the same without importing Hydrogen. `createBagsyServer({ apiKey, baseUrl, customer })` returns `shopper()`, `writableShopper()`, and `handle(request, ctx)`, a plain `Request → Response | null` handler for `/api/bagsy`. `customer` is `{ read, write }`, two functions that return the signed-in customer's GID.
202
+
203
+ ## React
204
+
205
+ ```tsx
206
+ const { saved, ready, pending, toggle } = useSaved(product.id, { variantId });
207
+ const count = useWishlistCount();
208
+ const { items, status, hasNextPage, loadMore, remove } = useWishlist({ pageSize: 24 });
209
+ ```
210
+
211
+ - **Batched lookups:** every heart that mounts in the same tick shares one lookup.
212
+ - **Small:** `@umxr/bagsy/react` only bundles the relay transport, not the server, proxy or account code.
213
+ - **Optimistic toggles:** the UI updates at once and rolls back if the request fails.
214
+ - **Removing:** `remove(item)` on the wishlist page updates hearts and the count elsewhere too.
215
+ - **`<WishlistButton>` is unstyled:** style it with `[aria-pressed]`, `[data-saved]` and `[data-pending]`. It's safe inside product-card links.
216
+
217
+ ## The client
218
+
219
+ ```ts
220
+ import { createBagsy } from '@umxr/bagsy';
221
+
222
+ const bagsy = createBagsy({ mode: 'server', apiKey: process.env.BAGSY_API_KEY, baseUrl });
223
+ const shopper = bagsy.as({ customerId: 'gid://shopify/Customer/123' });
224
+
225
+ await shopper.items.toggle(8123, { variantId: 44123 }); // numeric ids or GIDs
226
+ const saved = await shopper.saved(productIds); // saved.has(id), saved.has(id, variantId)
227
+ for await (const item of shopper.items.iterate()) console.log(item.productId);
228
+ await bagsy.insights.topProducts({ since: '2026-10-01' });
229
+ ```
230
+
231
+ | Mode | Use |
232
+ | --- | --- |
233
+ | `server` | Node, Workers, Hydrogen loaders. Secret key. Throws if constructed in a browser. |
234
+ | `relay` | Browsers on headless storefronts. Calls your server's relay; `@umxr/bagsy/react` uses it. |
235
+ | `proxy` | Liquid themes, through the Shopify app proxy. Shopify identifies the customer. |
236
+ | `account` | Customer account UI extensions, with Shopify session tokens. |
237
+
238
+ Failed calls throw `BagsyError`. Use its `status` and its `code`, such as `list_not_found` or `owner_required`. Switch on `code`, not on the message.
239
+
240
+ ## Web components
241
+
242
+ In Liquid themes, the Bagsy app embed loads these for you, with no install. To use them elsewhere:
243
+
244
+ ```ts
245
+ import { define } from '@umxr/bagsy/element';
246
+ import '@umxr/bagsy/styles.css';
247
+ define();
248
+ ```
249
+
250
+ ```html
251
+ <bagsy-button product-id="8123" variant-source></bagsy-button>
252
+ <bagsy-count hide-empty></bagsy-count>
253
+ <bagsy-wishlist share></bagsy-wishlist>
254
+ ```
255
+
256
+ ## License
257
+
258
+ MIT
@@ -0,0 +1,244 @@
1
+ import {
2
+ BagsyError,
3
+ createBagsy,
4
+ createTransport,
5
+ productGid
6
+ } from "./chunk-QV3JPND6.js";
7
+
8
+ // ../hydrogen/src/server.ts
9
+ var SESSION_KEY = "bagsy";
10
+ var EMPTY_PAGE = { data: [], pageInfo: { hasNextPage: false, endCursor: null } };
11
+ function createBagsyServer(options) {
12
+ const endpoint = options.endpoint ?? "/api/bagsy";
13
+ const serverOptions = {
14
+ mode: "server",
15
+ apiKey: options.apiKey,
16
+ ...options.baseUrl && { baseUrl: options.baseUrl },
17
+ ...options.fetch && { fetch: options.fetch }
18
+ };
19
+ const base = createBagsy(serverOptions);
20
+ const transportFor = (owner) => createTransport({ ...serverOptions, ...owner });
21
+ async function mergeGuest(customerId, visitorId) {
22
+ const key = `${customerId}|${visitorId}`;
23
+ if (mergedGuests.has(key)) return true;
24
+ try {
25
+ await base.as({ customerId }).identity.merge(visitorId);
26
+ remember(mergedGuests, key);
27
+ return true;
28
+ } catch (error) {
29
+ if (!(error instanceof BagsyError)) throw error;
30
+ return false;
31
+ }
32
+ }
33
+ async function shopper(context) {
34
+ context.requestContext.markResponseAsPersonalized?.("Bagsy wishlist");
35
+ const session = await readSession(context.sessionManager);
36
+ const customerId = await options.customer?.read(context);
37
+ const owners = [];
38
+ if (customerId) owners.push(base.as({ customerId }));
39
+ if (session.visitorId && !(customerId && await mergeGuest(customerId, session.visitorId))) {
40
+ owners.push(base.as({ visitorId: session.visitorId }));
41
+ }
42
+ return {
43
+ customerId,
44
+ async saved(products) {
45
+ const list = [...products];
46
+ const lookups = await Promise.all(owners.map((o) => o.saved(list)));
47
+ return unionLookup(list, lookups);
48
+ },
49
+ async count() {
50
+ const counts = await Promise.all(
51
+ owners.map(async (o) => (await o.lists.list()).find((l) => l.isDefault)?.itemCount ?? 0)
52
+ );
53
+ return counts.reduce((a, b) => a + b, 0);
54
+ },
55
+ async items(page = {}) {
56
+ if (!owners[0]) return EMPTY_PAGE;
57
+ return owners[0].items.list("default", page);
58
+ }
59
+ };
60
+ }
61
+ async function writableShopper(context) {
62
+ context.requestContext.markResponseAsPersonalized?.("Bagsy wishlist");
63
+ const session = await readSession(context.sessionManager);
64
+ const customerId = await options.customer?.write(context);
65
+ let write = Promise.resolve();
66
+ if (customerId && session.visitorId && await mergeGuest(customerId, session.visitorId)) {
67
+ delete session.visitorId;
68
+ await writeSession(context.sessionManager, session);
69
+ }
70
+ const identityKnown = Boolean(customerId || session.visitorId);
71
+ let fresh;
72
+ const owner = () => {
73
+ if (customerId) return { customerId };
74
+ session.visitorId ??= fresh = crypto.randomUUID();
75
+ return { visitorId: session.visitorId };
76
+ };
77
+ const persist = () => {
78
+ if (fresh) {
79
+ write = writeSession(context.sessionManager, session);
80
+ fresh = void 0;
81
+ }
82
+ return write;
83
+ };
84
+ let client;
85
+ return {
86
+ customerId,
87
+ get client() {
88
+ client ??= base.as(owner());
89
+ void persist();
90
+ return client;
91
+ },
92
+ identityKnown,
93
+ owner,
94
+ persist,
95
+ settled: () => write
96
+ };
97
+ }
98
+ async function relay(request, context) {
99
+ if (request.method !== "POST") return { rejected: { status: 405, code: "method_not_allowed", message: "Use POST." } };
100
+ if (!request.headers.get("Content-Type")?.toLowerCase().startsWith("application/json")) {
101
+ return { rejected: { status: 415, code: "unsupported_media_type", message: "Send application/json." } };
102
+ }
103
+ const origin = request.headers.get("Origin");
104
+ if (origin && origin !== new URL(request.url).origin) {
105
+ return { rejected: { status: 403, code: "forbidden_origin", message: "Cross-origin requests are not allowed." } };
106
+ }
107
+ let call;
108
+ try {
109
+ call = parseCall(await request.json());
110
+ } catch (error) {
111
+ return { rejected: { status: 400, code: "invalid_call", message: error instanceof Error ? error.message : "Invalid call." } };
112
+ }
113
+ const who = await writableShopper(context);
114
+ let owner = {};
115
+ if (who.identityKnown || call.method !== "GET") owner = who.owner();
116
+ else {
117
+ const empty = emptyAnswer(call);
118
+ if (empty) return { response: empty };
119
+ if (!PUBLIC_READ.test(call.path)) owner = { visitorId: crypto.randomUUID() };
120
+ }
121
+ try {
122
+ const { status, data } = await transportFor(owner).send(call.method, call.path, {
123
+ ...call.query && { query: call.query },
124
+ ...call.body !== void 0 && { body: call.body }
125
+ });
126
+ await who.persist();
127
+ return { response: { status, data } };
128
+ } catch (error) {
129
+ if (!(error instanceof BagsyError)) throw error;
130
+ return { response: { status: error.status, data: error.problem ?? null } };
131
+ } finally {
132
+ await who.settled();
133
+ }
134
+ }
135
+ async function handle(request, context) {
136
+ if (new URL(request.url).pathname !== endpoint) return null;
137
+ const tracked = trackWrites(context.sessionManager);
138
+ const result = await relay(request, { ...context, sessionManager: tracked.sessionManager });
139
+ const headers = new Headers({ "Content-Type": "application/json", "Cache-Control": "no-store" });
140
+ if (tracked.dirty()) {
141
+ const committed = new Headers(await context.sessionManager.commit?.() ?? void 0);
142
+ warnIfNotPersisted(committed);
143
+ for (const [name, value] of committed) headers.append(name, value);
144
+ }
145
+ return "rejected" in result ? new Response(JSON.stringify({ error: { code: result.rejected.code, message: result.rejected.message } }), {
146
+ status: result.rejected.status,
147
+ headers
148
+ }) : new Response(JSON.stringify(result.response), { status: 200, headers });
149
+ }
150
+ const writable = writableShopper;
151
+ return { endpoint, shopper, writableShopper: writable, relay, handle };
152
+ }
153
+ var mergedGuests = /* @__PURE__ */ new Set();
154
+ var CACHE_LIMIT = 1e3;
155
+ function remember(cache, key, value) {
156
+ if (cache.size >= CACHE_LIMIT) cache.delete(cache.keys().next().value);
157
+ if (cache instanceof Map) cache.set(key, value);
158
+ else cache.add(key);
159
+ }
160
+ var warned = false;
161
+ function warnIfNotPersisted(headers) {
162
+ if (warned || headers.has("Set-Cookie")) return;
163
+ warned = true;
164
+ console.warn(
165
+ "Bagsy: the session was changed but committing it set no cookie, so guest wishlists will not persist. Use a persistent session (e.g. a signed cookie session with SESSION_SECRET), even without customer accounts."
166
+ );
167
+ }
168
+ var PUBLIC_READ = /^\/(?:shared\/|health$)/;
169
+ function emptyAnswer(call) {
170
+ if (call.path === "/saved") {
171
+ const ids = (call.query?.productIds ?? "").split(",").filter(Boolean);
172
+ return { status: 200, data: { data: Object.fromEntries(ids.map((id) => [id, []])) } };
173
+ }
174
+ if (call.path === "/lists") return { status: 200, data: { data: [] } };
175
+ if (call.path === "/lists/default/items") {
176
+ return { status: 200, data: { data: [], pageInfo: { hasNextPage: false, endCursor: null } } };
177
+ }
178
+ return void 0;
179
+ }
180
+ var SEGMENT = "[A-Za-z0-9_-]{1,128}";
181
+ var ALLOWED = [
182
+ [/^\/lists$/, ["GET", "POST"]],
183
+ [new RegExp(`^/lists/${SEGMENT}$`), ["GET", "PATCH", "DELETE"]],
184
+ [new RegExp(`^/lists/${SEGMENT}/items$`), ["GET", "POST", "DELETE"]],
185
+ [new RegExp(`^/lists/${SEGMENT}/items/${SEGMENT}$`), ["PATCH", "DELETE"]],
186
+ [new RegExp(`^/lists/${SEGMENT}/share$`), ["POST", "DELETE"]],
187
+ [/^\/saved$/, ["GET"]],
188
+ [new RegExp(`^/shared/${SEGMENT}$`), ["GET"]],
189
+ [/^\/health$/, ["GET"]]
190
+ ];
191
+ function parseCall(value) {
192
+ if (typeof value !== "object" || value === null) throw new TypeError("Expected a JSON object.");
193
+ const { method, path, query, body } = value;
194
+ if (typeof method !== "string" || typeof path !== "string") throw new TypeError("`method` and `path` are required.");
195
+ const rule = ALLOWED.find(([pattern]) => pattern.test(path));
196
+ if (!rule || !rule[1].includes(method)) throw new TypeError(`${method} ${path} is not available to browsers.`);
197
+ const parsed = { method, path };
198
+ if (query !== void 0) {
199
+ if (typeof query !== "object" || query === null || Object.values(query).some((v) => typeof v !== "string")) {
200
+ throw new TypeError("`query` must map names to strings.");
201
+ }
202
+ const { visitor_id: _ignored, ...rest } = query;
203
+ parsed.query = rest;
204
+ }
205
+ if (body !== void 0) parsed.body = body;
206
+ return parsed;
207
+ }
208
+ function trackWrites(sessionManager) {
209
+ let dirty = false;
210
+ const tracked = Object.create(sessionManager);
211
+ tracked.getSessionItem = (key) => sessionManager.getSessionItem(key);
212
+ tracked.setSessionItem = (key, value) => (dirty = true, sessionManager.setSessionItem(key, value));
213
+ tracked.removeSessionItem = (key) => (dirty = true, sessionManager.removeSessionItem(key));
214
+ return { sessionManager: tracked, dirty: () => dirty };
215
+ }
216
+ async function readSession(sessionManager) {
217
+ const value = await sessionManager.getSessionItem(SESSION_KEY);
218
+ return value && typeof value === "object" ? { ...value } : {};
219
+ }
220
+ async function writeSession(sessionManager, value) {
221
+ if (value.visitorId) await sessionManager.setSessionItem(SESSION_KEY, { visitorId: value.visitorId });
222
+ else await sessionManager.removeSessionItem(SESSION_KEY);
223
+ }
224
+ function unionLookup(products, lookups) {
225
+ if (lookups.length === 1) return lookups[0];
226
+ const byProduct = {};
227
+ for (const product of products) {
228
+ const id = productGid(product);
229
+ byProduct[id] = lookups.flatMap((lookup) => lookup.get(id));
230
+ }
231
+ return {
232
+ byProduct,
233
+ get: (product) => byProduct[productGid(product)] ?? [],
234
+ has: (product, variant) => lookups.some((lookup) => lookup.has(product, variant))
235
+ };
236
+ }
237
+
238
+ export {
239
+ createBagsyServer,
240
+ remember,
241
+ warnIfNotPersisted,
242
+ trackWrites
243
+ };
244
+ //# sourceMappingURL=chunk-CJV4SIV7.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../hydrogen/src/server.ts"],"sourcesContent":["import {\n BagsyError,\n createBagsy,\n createTransport,\n productGid,\n type Bagsy,\n type ItemPage,\n type Owner,\n type PageOptions,\n type ProductRef,\n type RelayRequest,\n type RelayResponse,\n type SavedEntry,\n type SavedLookup,\n type ServerOptions,\n} from '@bagsy/core';\n\ntype Awaitable<T> = T | Promise<T>;\n\n/** A session you can only read: what loaders and server components get. */\nexport interface ReadonlySessionManager {\n getSessionItem(key: string): Awaitable<unknown>;\n}\n\n/** A session you can write and commit. Same shape as Hydrogen's `ShopifyRouteSessionManager`. */\nexport interface BagsySessionManager extends ReadonlySessionManager {\n setSessionItem(key: string, value: unknown): Awaitable<void>;\n removeSessionItem(key: string): Awaitable<void>;\n /** Saves changes and returns headers (a cookie) for the response. */\n commit?(): Awaitable<HeadersInit | void>;\n}\n\n/** The parts of Hydrogen's `ShopifyRequestContext` Bagsy uses. */\nexport interface BagsyRequestContext {\n markResponseAsPersonalized?(reason: string): void;\n}\n\n/** For reads (loaders): in Hydrogen, your request's session manager and `requestContext`. */\nexport interface ReadContext {\n sessionManager: ReadonlySessionManager;\n requestContext: BagsyRequestContext;\n}\n\n/** For writes: only where the response can commit the session (route handlers, actions). */\nexport interface WriteContext extends ReadContext {\n sessionManager: BagsySessionManager;\n}\n\n/** How Bagsy learns who is signed in. `hydrogenCustomerId()` implements it for Hydrogen. */\nexport interface CustomerResolver<R extends ReadContext = ReadContext, W extends WriteContext = WriteContext> {\n /** Without refreshing tokens or writing the session. */\n read(context: R): Promise<string | undefined>;\n /** May refresh tokens and cache in the session; the caller commits. */\n write(context: W): Promise<string | undefined>;\n}\n\nexport interface CreateBagsyServerOptions<R extends ReadContext = ReadContext, W extends WriteContext = WriteContext> {\n /** A server key from the Bagsy admin (Apps → Bagsy → Developers). Keep it server-side. */\n apiKey: string;\n /** Bagsy API base URL, e.g. `https://bagsy-api.example.workers.dev/v1`. */\n baseUrl?: string;\n /** Who is signed in. Omit for a guest-only storefront. */\n customer?: CustomerResolver<R, W>;\n /** Path the browser calls; `@umxr/bagsy/react` uses the same default. @default '/api/bagsy' */\n endpoint?: string;\n fetch?: typeof globalThis.fetch;\n}\n\n/** Read-only view of the shopper, for loaders and server rendering. Never writes the session. */\nexport interface Shopper {\n /** Signed-in customer GID, if any. */\n readonly customerId: string | undefined;\n /** Saved state for products. Guests who have never saved cost no API call. Pass `.byProduct` to `@umxr/bagsy/react`. */\n saved(products: Iterable<ProductRef>): Promise<SavedLookup>;\n /** Items in the default list, for a header badge. */\n count(): Promise<number>;\n /** A page of the default list, newest first. Pass to `useWishlist({ initial })`. */\n items(page?: PageOptions): Promise<ItemPage>;\n}\n\n/** The shopper with write access, for actions and route handlers that commit the session. */\nexport interface WritableShopper {\n readonly customerId: string | undefined;\n /**\n * A Bagsy client acting for this shopper. A guest without a wishlist gets\n * an id on first use, stored in the session: commit it with the response.\n */\n readonly client: Bagsy;\n}\n\n/** What Bagsy keeps in your storefront session. */\ninterface BagsySession {\n /** Guest wishlist owner, until they sign in and it's merged. */\n visitorId?: string;\n}\n\nconst SESSION_KEY = 'bagsy';\nconst EMPTY_PAGE: ItemPage = { data: [], pageInfo: { hasNextPage: false, endCursor: null } } as ItemPage;\n\n/**\n * Bagsy for Hydrogen and other server-rendered storefronts. Cheap to create:\n * make one per request from your env, or one at module scope. Call\n * `shopper()` in loaders and `relay`/`handle` for the browser endpoint.\n */\nexport function createBagsyServer<R extends ReadContext = ReadContext, W extends WriteContext = WriteContext>(\n options: CreateBagsyServerOptions<R, W>,\n) {\n const endpoint = options.endpoint ?? '/api/bagsy';\n const serverOptions: ServerOptions = {\n mode: 'server',\n apiKey: options.apiKey,\n ...(options.baseUrl && { baseUrl: options.baseUrl }),\n ...(options.fetch && { fetch: options.fetch }),\n };\n const base = createBagsy(serverOptions);\n const transportFor = (owner: Owner) => createTransport({ ...serverOptions, ...owner });\n\n /** Fold a guest list into the customer's. Idempotent; remembered per isolate so it runs once. */\n async function mergeGuest(customerId: string, visitorId: string): Promise<boolean> {\n const key = `${customerId}|${visitorId}`;\n if (mergedGuests.has(key)) return true;\n try {\n await base.as({ customerId }).identity.merge(visitorId);\n remember(mergedGuests, key);\n return true;\n } catch (error) {\n if (!(error instanceof BagsyError)) throw error;\n return false;\n }\n }\n\n /**\n * The shopper for this request. Never refreshes tokens or writes the\n * session, so it's safe in loaders and server components. Right after\n * sign-in it merges the guest list on the API, so every read agrees.\n */\n async function shopper(context: R): Promise<Shopper> {\n context.requestContext.markResponseAsPersonalized?.('Bagsy wishlist');\n const session = await readSession(context.sessionManager);\n const customerId = await options.customer?.read(context);\n const owners: Bagsy[] = [];\n if (customerId) owners.push(base.as({ customerId }));\n // The session still holds the guest id until a write path clears it.\n // If the merge failed, show both so nothing seems to vanish.\n if (session.visitorId && !(customerId && (await mergeGuest(customerId, session.visitorId)))) {\n owners.push(base.as({ visitorId: session.visitorId }));\n }\n\n return {\n customerId,\n async saved(products) {\n const list = [...products];\n const lookups = await Promise.all(owners.map((o) => o.saved(list)));\n return unionLookup(list, lookups);\n },\n async count() {\n const counts = await Promise.all(\n owners.map(async (o) => (await o.lists.list()).find((l) => l.isDefault)?.itemCount ?? 0),\n );\n return counts.reduce((a, b) => a + b, 0);\n },\n async items(page = {}) {\n // With an unmerged guest list, the customer's comes first; the guest's\n // items appear once the merge succeeds.\n if (!owners[0]) return EMPTY_PAGE;\n return owners[0].items.list('default', page);\n },\n };\n }\n\n /**\n * The shopper with write access: merges a guest list into the customer's\n * after sign-in, and creates a guest id on first write. Use where the\n * response commits the session, e.g. a React Router action.\n */\n async function writableShopper(context: W): Promise<WritableShopper & InternalWritable> {\n context.requestContext.markResponseAsPersonalized?.('Bagsy wishlist');\n const session = await readSession(context.sessionManager);\n const customerId = await options.customer?.write(context);\n let write: Promise<void> = Promise.resolve();\n\n // Keep the guest id if the merge fails; the next write retries.\n if (customerId && session.visitorId && (await mergeGuest(customerId, session.visitorId))) {\n delete session.visitorId;\n await writeSession(context.sessionManager, session);\n }\n\n const identityKnown = Boolean(customerId || session.visitorId);\n let fresh: string | undefined;\n /** Who to act as. A guest without a wishlist gets a new id, saved by `persist()`. */\n const owner = (): Owner => {\n if (customerId) return { customerId };\n session.visitorId ??= fresh = crypto.randomUUID();\n return { visitorId: session.visitorId };\n };\n const persist = () => {\n if (fresh) {\n write = writeSession(context.sessionManager, session);\n fresh = undefined;\n }\n return write;\n };\n let client: Bagsy | undefined;\n\n return {\n customerId,\n get client() {\n // Actions can't tell us whether their call succeeded, so save the id now.\n client ??= base.as(owner());\n void persist();\n return client;\n },\n identityKnown,\n owner,\n persist,\n settled: () => write,\n };\n }\n\n /**\n * Run one call from the browser. Returns the API's status and body, or why\n * the call was rejected. Commit the session after (`handle` does).\n */\n async function relay(\n request: Request,\n context: W,\n ): Promise<{ response: RelayResponse } | { rejected: { status: number; code: string; message: string } }> {\n if (request.method !== 'POST') return { rejected: { status: 405, code: 'method_not_allowed', message: 'Use POST.' } };\n // Only same-origin scripts can send JSON: forms can't, and other origins\n // would need CORS, which this endpoint never grants.\n if (!request.headers.get('Content-Type')?.toLowerCase().startsWith('application/json')) {\n return { rejected: { status: 415, code: 'unsupported_media_type', message: 'Send application/json.' } };\n }\n const origin = request.headers.get('Origin');\n if (origin && origin !== new URL(request.url).origin) {\n return { rejected: { status: 403, code: 'forbidden_origin', message: 'Cross-origin requests are not allowed.' } };\n }\n\n let call: RelayRequest;\n try {\n call = parseCall(await request.json());\n } catch (error) {\n return { rejected: { status: 400, code: 'invalid_call', message: error instanceof Error ? error.message : 'Invalid call.' } };\n }\n\n const who = await writableShopper(context);\n let owner: Owner = {};\n if (who.identityKnown || call.method !== 'GET') owner = who.owner();\n else {\n // A guest who has never saved: answer reads here, with no API call and\n // no session. Public reads need no owner.\n const empty = emptyAnswer(call);\n if (empty) return { response: empty };\n if (!PUBLIC_READ.test(call.path)) owner = { visitorId: crypto.randomUUID() };\n }\n\n try {\n const { status, data } = await transportFor(owner).send(call.method, call.path, {\n ...(call.query && { query: call.query }),\n ...(call.body !== undefined && { body: call.body }),\n });\n // Only now is a new guest id worth keeping.\n await who.persist();\n return { response: { status, data } };\n } catch (error) {\n if (!(error instanceof BagsyError)) throw error;\n return { response: { status: error.status, data: error.problem ?? null } };\n } finally {\n await who.settled();\n }\n }\n\n /**\n * The relay endpoint as a plain `Request → Response` handler, for servers\n * without Hydrogen's route handlers. Returns null for other paths, and\n * commits the session if Bagsy changed it.\n */\n async function handle(request: Request, context: W): Promise<Response | null> {\n if (new URL(request.url).pathname !== endpoint) return null;\n const tracked = trackWrites(context.sessionManager);\n const result = await relay(request, { ...context, sessionManager: tracked.sessionManager });\n const headers = new Headers({ 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });\n if (tracked.dirty()) {\n const committed = new Headers((await context.sessionManager.commit?.()) ?? undefined);\n warnIfNotPersisted(committed);\n for (const [name, value] of committed) headers.append(name, value);\n }\n return 'rejected' in result\n ? new Response(JSON.stringify({ error: { code: result.rejected.code, message: result.rejected.message } }), {\n status: result.rejected.status,\n headers,\n })\n : new Response(JSON.stringify(result.response), { status: 200, headers });\n }\n\n const writable: (context: W) => Promise<WritableShopper> = writableShopper;\n return { endpoint, shopper, writableShopper: writable, relay, handle };\n}\n\nexport type BagsyServer<R extends ReadContext = ReadContext, W extends WriteContext = WriteContext> = ReturnType<\n typeof createBagsyServer<R, W>\n>;\n\ninterface InternalWritable {\n identityKnown: boolean;\n owner(): Owner;\n /** Save a newly created guest id to the session. */\n persist(): Promise<void>;\n /** Resolves once any session write this shopper started has finished. */\n settled(): Promise<void>;\n}\n\n// ---------------------------------------------------------------------------\n\n/** Guests merged this isolate, and customer ids by token: bounded caches that survive across requests. */\nconst mergedGuests = new Set<string>();\nconst CACHE_LIMIT = 1000;\nexport function remember<K>(cache: Set<K> | Map<K, unknown>, key: K, value?: unknown) {\n if (cache.size >= CACHE_LIMIT) cache.delete(cache.keys().next().value as K);\n if (cache instanceof Map) cache.set(key, value);\n else cache.add(key);\n}\n\nlet warned = false;\n/**\n * Warn once if Bagsy wrote the session but committing it produced no cookie.\n * Hydrogen's in-memory session (the default without customer accounts) does\n * this, and guest wishlists then vanish on the next request.\n */\nexport function warnIfNotPersisted(headers: Headers) {\n if (warned || headers.has('Set-Cookie')) return;\n warned = true;\n console.warn(\n 'Bagsy: the session was changed but committing it set no cookie, so guest wishlists will not persist. ' +\n 'Use a persistent session (e.g. a signed cookie session with SESSION_SECRET), even without customer accounts.',\n );\n}\n\nconst PUBLIC_READ = /^\\/(?:shared\\/|health$)/;\n\n/** What the API returns for an owner with nothing saved, for calls guests make on every page. */\nfunction emptyAnswer(call: RelayRequest): RelayResponse | undefined {\n if (call.path === '/saved') {\n const ids = (call.query?.productIds ?? '').split(',').filter(Boolean);\n return { status: 200, data: { data: Object.fromEntries(ids.map((id) => [id, []])) } };\n }\n if (call.path === '/lists') return { status: 200, data: { data: [] } };\n if (call.path === '/lists/default/items') {\n return { status: 200, data: { data: [], pageInfo: { hasNextPage: false, endCursor: null } } };\n }\n return undefined;\n}\n\n/** Calls browsers may make, by path and method. Insights and identity stay server-side. */\nconst SEGMENT = '[A-Za-z0-9_-]{1,128}';\nconst ALLOWED: [RegExp, string[]][] = [\n [/^\\/lists$/, ['GET', 'POST']],\n [new RegExp(`^/lists/${SEGMENT}$`), ['GET', 'PATCH', 'DELETE']],\n [new RegExp(`^/lists/${SEGMENT}/items$`), ['GET', 'POST', 'DELETE']],\n [new RegExp(`^/lists/${SEGMENT}/items/${SEGMENT}$`), ['PATCH', 'DELETE']],\n [new RegExp(`^/lists/${SEGMENT}/share$`), ['POST', 'DELETE']],\n [/^\\/saved$/, ['GET']],\n [new RegExp(`^/shared/${SEGMENT}$`), ['GET']],\n [/^\\/health$/, ['GET']],\n];\n\nfunction parseCall(value: unknown): RelayRequest {\n if (typeof value !== 'object' || value === null) throw new TypeError('Expected a JSON object.');\n const { method, path, query, body } = value as Record<string, unknown>;\n if (typeof method !== 'string' || typeof path !== 'string') throw new TypeError('`method` and `path` are required.');\n const rule = ALLOWED.find(([pattern]) => pattern.test(path));\n if (!rule || !rule[1].includes(method)) throw new TypeError(`${method} ${path} is not available to browsers.`);\n const parsed: RelayRequest = { method, path };\n if (query !== undefined) {\n if (typeof query !== 'object' || query === null || Object.values(query).some((v) => typeof v !== 'string')) {\n throw new TypeError('`query` must map names to strings.');\n }\n // Identity comes from the session, never the browser.\n const { visitor_id: _ignored, ...rest } = query as Record<string, string>;\n parsed.query = rest;\n }\n if (body !== undefined) parsed.body = body;\n return parsed;\n}\n\n/** Wraps a session manager to report whether anything was written. */\nexport function trackWrites<SM extends BagsySessionManager>(sessionManager: SM) {\n let dirty = false;\n const tracked: SM = Object.create(sessionManager);\n tracked.getSessionItem = (key) => sessionManager.getSessionItem(key);\n tracked.setSessionItem = (key, value) => ((dirty = true), sessionManager.setSessionItem(key, value));\n tracked.removeSessionItem = (key) => ((dirty = true), sessionManager.removeSessionItem(key));\n return { sessionManager: tracked, dirty: () => dirty };\n}\n\nasync function readSession(sessionManager: ReadonlySessionManager): Promise<BagsySession> {\n const value = await sessionManager.getSessionItem(SESSION_KEY);\n return value && typeof value === 'object' ? { ...(value as BagsySession) } : {};\n}\n\nasync function writeSession(sessionManager: BagsySessionManager, value: BagsySession) {\n if (value.visitorId) await sessionManager.setSessionItem(SESSION_KEY, { visitorId: value.visitorId });\n else await sessionManager.removeSessionItem(SESSION_KEY);\n}\n\nfunction unionLookup(products: ProductRef[], lookups: SavedLookup[]): SavedLookup {\n if (lookups.length === 1) return lookups[0]!;\n const byProduct: Record<string, SavedEntry[]> = {};\n for (const product of products) {\n const id = productGid(product);\n byProduct[id] = lookups.flatMap((lookup) => lookup.get(id));\n }\n return {\n byProduct,\n get: (product) => byProduct[productGid(product)] ?? [],\n has: (product, variant) => lookups.some((lookup) => lookup.has(product, variant)),\n };\n}\n"],"mappings":";;;;;;;;AAgGA,IAAM,cAAc;AACpB,IAAM,aAAuB,EAAE,MAAM,CAAC,GAAG,UAAU,EAAE,aAAa,OAAO,WAAW,KAAK,EAAE;AAOpF,SAAS,kBACd,SACA;AACA,QAAM,WAAW,QAAQ,YAAY;AACrC,QAAM,gBAA+B;AAAA,IACnC,MAAM;AAAA,IACN,QAAQ,QAAQ;AAAA,IAChB,GAAI,QAAQ,WAAW,EAAE,SAAS,QAAQ,QAAQ;AAAA,IAClD,GAAI,QAAQ,SAAS,EAAE,OAAO,QAAQ,MAAM;AAAA,EAC9C;AACA,QAAM,OA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