@asteby/storefront 0.1.0 → 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/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;
@@ -636,7 +636,7 @@ function createMockStorefront(options = {}) {
636
636
  const nodes = skus.flatMap((sku) => {
637
637
  for (const p of state.products) {
638
638
  const v = p.variants.find((x) => x.sku === sku);
639
- if (v) return [{ sku, quantity_available: v.quantity_available ?? 0 }];
639
+ if (v) return [{ sku, quantity_available: v.quantity_available }];
640
640
  }
641
641
  return [];
642
642
  });
@@ -713,10 +713,14 @@ function createMockStorefront(options = {}) {
713
713
  const cart = renderCart(getCartRecord(params[0]));
714
714
  const net = toCents(cart.cost.subtotal.amount) - toCents(cart.cost.discount.amount);
715
715
  return {
716
- body: { rates: shippingRates(net), pickup: state.shop.branches.map((b) => ({ branch_id: b.id, name: b.name })) },
716
+ body: { rates: shippingRates(net), pickup: state.shop.branches.map((b) => ({ branch_id: b.id, name: b.name })), partial: false, unavailable: [] },
717
717
  cache: "private"
718
718
  };
719
719
  });
720
+ route("GET", "/carts/:id/payment-methods", "cart:write", ({ params }) => {
721
+ getCartRecord(params[0]);
722
+ return { body: { methods: [{ key: "mock", kind: "builtin", default: true }], market: null }, cache: "private" };
723
+ });
720
724
  route("PUT", "/carts/:id/delivery", "cart:write", ({ params, body }) => {
721
725
  const cart = getCartRecord(params[0]);
722
726
  if (body.method === "shipping") {
@@ -784,9 +788,17 @@ function createMockStorefront(options = {}) {
784
788
  });
785
789
  const redirect = new URL(`https://mock.local/pay/${orderId}`);
786
790
  redirect.searchParams.set("return_url", body.return_url);
791
+ const checkoutId = nextId("chk");
787
792
  const result = {
788
793
  order: { id: orderId, number },
789
- payment: { provider: "mock", status: "pending", redirect_url: redirect.toString(), client_secret: null }
794
+ payment: { provider: "mock", status: "pending", redirect_url: redirect.toString(), client_secret: null, instructions: null },
795
+ checkout: {
796
+ id: checkoutId,
797
+ status: "pending",
798
+ external_order_id: orderId,
799
+ total: cart.cost.total,
800
+ stock_hold_expires_at: new Date(Date.now() + 30 * 6e4).toISOString()
801
+ }
790
802
  };
791
803
  if (key) state.idempotency.set(`${params[0]}:${key}`, { status: 201, body: result });
792
804
  return { status: 201, body: result, cache: "private" };
@@ -889,7 +901,7 @@ function createMockStorefront(options = {}) {
889
901
  if (tag) items = items.filter((a) => a.tags.includes(tag));
890
902
  const q = search.get("q")?.toLowerCase();
891
903
  if (q) items = items.filter((a) => `${a.title} ${a.excerpt ?? ""}`.toLowerCase().includes(q));
892
- return { body: paginate(items, search, 10), cache: "public" };
904
+ return { body: paginate(items, search, 10, 50), cache: "public" };
893
905
  });
894
906
  route("GET", "/content/articles/:handle", "content:read", ({ params, search }) => ({
895
907
  body: requireNode(state.articles.find((a) => a.handle === params[0]), `Article ${params[0]}`),