@asteby/storefront 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/mock.cjs CHANGED
@@ -311,9 +311,9 @@ function decodeCursor(cursor) {
311
311
  throw new HttpError(400, "validation", "Invalid cursor", "after");
312
312
  }
313
313
  }
314
- function paginate(items, search, defaultFirst = 24) {
314
+ function paginate(items, search, defaultFirst = 24, maxFirst = 100) {
315
315
  const first = search.has("first") ? Number(search.get("first")) : defaultFirst;
316
- if (!Number.isInteger(first) || first < 1 || first > 100) throw new HttpError(400, "validation", "`first` must be between 1 and 100", "first");
316
+ if (!Number.isInteger(first) || first < 1 || first > maxFirst) throw new HttpError(400, "validation", `\`first\` must be between 1 and ${maxFirst}`, "first");
317
317
  const start = decodeCursor(search.get("after"));
318
318
  const nodes = items.slice(start, start + first);
319
319
  const end = start + nodes.length;
@@ -362,6 +362,7 @@ function createMockStorefront(options = {}) {
362
362
  otps: /* @__PURE__ */ new Map(),
363
363
  sessions: /* @__PURE__ */ new Map(),
364
364
  idempotency: /* @__PURE__ */ new Map(),
365
+ checkouts: /* @__PURE__ */ new Map(),
365
366
  seq: 0
366
367
  };
367
368
  }
@@ -636,7 +637,7 @@ function createMockStorefront(options = {}) {
636
637
  const nodes = skus.flatMap((sku) => {
637
638
  for (const p of state.products) {
638
639
  const v = p.variants.find((x) => x.sku === sku);
639
- if (v) return [{ sku, quantity_available: v.quantity_available ?? 0 }];
640
+ if (v) return [{ sku, quantity_available: v.quantity_available }];
640
641
  }
641
642
  return [];
642
643
  });
@@ -713,10 +714,14 @@ function createMockStorefront(options = {}) {
713
714
  const cart = renderCart(getCartRecord(params[0]));
714
715
  const net = toCents(cart.cost.subtotal.amount) - toCents(cart.cost.discount.amount);
715
716
  return {
716
- body: { rates: shippingRates(net), pickup: state.shop.branches.map((b) => ({ branch_id: b.id, name: b.name })) },
717
+ body: { rates: shippingRates(net), pickup: state.shop.branches.map((b) => ({ branch_id: b.id, name: b.name })), partial: false, unavailable: [] },
717
718
  cache: "private"
718
719
  };
719
720
  });
721
+ route("GET", "/carts/:id/payment-methods", "cart:write", ({ params }) => {
722
+ getCartRecord(params[0]);
723
+ return { body: { methods: [{ key: "mock", kind: "builtin", default: true }], market: null }, cache: "private" };
724
+ });
720
725
  route("PUT", "/carts/:id/delivery", "cart:write", ({ params, body }) => {
721
726
  const cart = getCartRecord(params[0]);
722
727
  if (body.method === "shipping") {
@@ -784,13 +789,27 @@ function createMockStorefront(options = {}) {
784
789
  });
785
790
  const redirect = new URL(`https://mock.local/pay/${orderId}`);
786
791
  redirect.searchParams.set("return_url", body.return_url);
792
+ const checkoutId = nextId("chk");
787
793
  const result = {
788
794
  order: { id: orderId, number },
789
- payment: { provider: "mock", status: "pending", redirect_url: redirect.toString(), client_secret: null }
795
+ payment: { provider: "mock", status: "pending", redirect_url: redirect.toString(), client_secret: null, instructions: null },
796
+ checkout: {
797
+ id: checkoutId,
798
+ status: "pending",
799
+ external_order_id: orderId,
800
+ total: cart.cost.total,
801
+ stock_hold_expires_at: new Date(Date.now() + 30 * 6e4).toISOString()
802
+ }
790
803
  };
804
+ state.checkouts.set(checkoutId, result);
791
805
  if (key) state.idempotency.set(`${params[0]}:${key}`, { status: 201, body: result });
792
806
  return { status: 201, body: result, cache: "private" };
793
807
  });
808
+ route("GET", "/checkouts/:id", "checkout:write", ({ params }) => {
809
+ const found = state.checkouts.get(params[0]);
810
+ if (!found) throw new HttpError(404, "not_found", "Checkout not found");
811
+ return { body: found, cache: "private" };
812
+ });
794
813
  route("POST", "/customer/otp", "customer:write", ({ body }) => {
795
814
  state.otps.set(identifierOf(body), otpCode);
796
815
  return { status: 204 };
@@ -889,7 +908,7 @@ function createMockStorefront(options = {}) {
889
908
  if (tag) items = items.filter((a) => a.tags.includes(tag));
890
909
  const q = search.get("q")?.toLowerCase();
891
910
  if (q) items = items.filter((a) => `${a.title} ${a.excerpt ?? ""}`.toLowerCase().includes(q));
892
- return { body: paginate(items, search, 10), cache: "public" };
911
+ return { body: paginate(items, search, 10, 50), cache: "public" };
893
912
  });
894
913
  route("GET", "/content/articles/:handle", "content:read", ({ params, search }) => ({
895
914
  body: requireNode(state.articles.find((a) => a.handle === params[0]), `Article ${params[0]}`),