@zappie/sdk 0.7.2 → 0.9.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/CHANGELOG.md +20 -0
- package/dist/cart/item.d.ts +1 -1
- package/dist/{client-DInn0X0F.d.ts → client-75aFOuQi.d.ts} +1 -1
- package/dist/debug.js +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/integrations/index.d.ts +1 -1
- package/dist/internal/image-widths.js +6 -0
- package/dist/internal/image-widths.js.map +6 -0
- package/dist/internal.d.ts +1 -1
- package/dist/persist/catalogue.d.ts +2 -2
- package/dist/react/cart.js +13 -2
- package/dist/react/cart.js.map +1 -1
- package/dist/react/changes.js +94 -0
- package/dist/react/changes.js.map +6 -0
- package/dist/react/index.d.ts +53 -4
- package/dist/react/index.js +2 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react/rollback.js +88 -0
- package/dist/react/rollback.js.map +6 -0
- package/dist/server/index.d.ts +1 -1
- package/dist/studio/index.js +1 -0
- package/dist/template/index.d.ts +3 -2
- package/dist/template/index.js +1 -1
- package/dist/template/index.js.map +1 -1
- package/dist/testing/index.d.ts +1 -1
- package/dist/transport/search.d.ts +1 -1
- package/dist/transport/selected-menu.d.ts +2 -2
- package/dist/{types-CxY5C6AN.d.ts → types-BzyzhqYz.d.ts} +1 -1
- package/dist/uploads/index.d.ts +22 -0
- package/dist/uploads/index.js +37 -0
- package/dist/uploads/index.js.map +6 -0
- package/dist/vite/index.d.ts +30 -2
- package/dist/vite/index.js +64 -1
- package/package.json +5 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @zappie/sdk
|
|
2
2
|
|
|
3
|
+
## 0.9.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 93c8cd4: Resize a site's uploads by their path on the site. The new `@zappie/sdk/uploads` entry names the host's `/_zappie/assets/` route: `uploadPath`, `isUpload`, `canResizeUpload`, `uploadResized` and `uploadSrcSet` ask it for an upload at the nearest of `IMAGE_WIDTHS` and `UPLOAD_QUALITIES`. Foundations' `Image` serves an upload path resized, with the same direct mode and fallback to the original. `zappieUploads({ host })` from `@zappie/sdk/vite` serves uploads on a local dev server from the site's published address.
|
|
8
|
+
|
|
9
|
+
## 0.8.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- a444bdf: A cart change shows at once, and the cart puts it back when the server refuses it. Before, a quantity the server wouldn't sell stayed in the bag with an issue, and a site that wanted the change undone wrote its own optimistic state.
|
|
14
|
+
|
|
15
|
+
`useCart().actions` from `@zappie/sdk/react` now tracks each `add`, `update` and `remove`. When the next quote refuses a line the shopper just changed, with `quantity_limit`, `item_unavailable`, `item_not_found` or an option issue, the cart puts that line back: a new line is removed, and a changed line returns to what the last quote accepted, or to the server's nearest allowed quantity when that sits between the two. Only that line goes back. A newer change to the same line is never undone, because a quote for an older cart is never applied. Rapid changes still go to the server as one quote. A line restored from storage keeps its issue and fixes, as before.
|
|
16
|
+
|
|
17
|
+
`useCartNotice()` returns `{ notice, dismiss }`: one notice per quote, with the server's `message` (such as "Only 2 left"), its `code`, the `lineId` and `count`, the number of lines put back. The SDK also writes the message to one hidden polite live region, `[data-zappie-announcer]`, so screen readers hear the rollback whether or not you show a toast. `actions.add(item, input, { key })` takes a key for one shopper action: a second add with the same key returns the first line and adds nothing. Make the key with `crypto.randomUUID()` when the shopper acts, and reuse it only to retry that action. Never build it from the line. `CartActions`, `CartChangeOptions` and `CartNotice` are exported.
|
|
18
|
+
|
|
19
|
+
`@zappie/foundations`: `Cart.Line` gives its render function `total`, the line total from the current quote, and `pending`, true while a change waits for the server, and sets `aria-busy` while it waits. `ItemForm` adds through the tracked actions, so a refused add from the item sheet goes back too.
|
|
20
|
+
|
|
21
|
+
`eslint-plugin-zappie`: `cart-via-sdk` reports `useOptimistic`, `useState` or `useReducer` seeded from `useCart()` or `useCartLine()` data, and a `useOptimistic` whose value is named for a cart, bag or basket. Change the cart through `useCart().actions` and read refusals with `useCartNotice()`.
|
|
22
|
+
|
|
3
23
|
## 0.7.2
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/dist/cart/item.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Zappie } from '../index.js';
|
|
2
2
|
import { Item } from '../index.js';
|
|
3
|
-
import '../types-
|
|
3
|
+
import '../types-BzyzhqYz.js';
|
|
4
4
|
|
|
5
5
|
/** Register an item already read by a partial/selected catalogue, without fetching it again. */
|
|
6
6
|
declare function prepareItem(client: Zappie, item: Item, options: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { a as Menu, C as CategoryPage, S as Store, I as Item, M as Money, F as Formatters } from './models-C-kEg1Id.js';
|
|
2
|
-
import { Q as QuoteRequest,
|
|
2
|
+
import { Q as QuoteRequest, h as WireQuote, e as CartIssueCode, d as CartModule, b as CartLineInput } from './types-BzyzhqYz.js';
|
|
3
3
|
|
|
4
4
|
/** Stable public error codes. */
|
|
5
5
|
type ZappieErrorCode = "network_error" | "timeout" | "rate_limited" | "unauthorized" | "forbidden" | "forbidden_origin" | "scope_required" | "not_found" | "store_not_found" | "branch_not_found" | "item_not_found" | "cart_invalid" | "invalid_request" | "version_unsupported" | "server_error";
|
package/dist/debug.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { a as ZappieError, T as Transport, R as RequestCtx, f as MenuFulfilment, g as MenuResponse, j as SearchRequest, k as SearchPage, l as MenuVersion } from './client-
|
|
1
|
+
import { a as ZappieError, T as Transport, R as RequestCtx, f as MenuFulfilment, g as MenuResponse, j as SearchRequest, k as SearchPage, l as MenuVersion } from './client-75aFOuQi.js';
|
|
2
2
|
import { SelectedMenuRequest, SelectedMenuResponse } from './transport/selected-menu.js';
|
|
3
|
-
export { A as API_VERSION, E as ErrorDetails, m as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, e as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-
|
|
4
|
-
import {
|
|
5
|
-
export {
|
|
3
|
+
export { A as API_VERSION, E as ErrorDetails, m as MenuReadOptions, M as MenuResource, n as ReadOptions, S as StorageLike, c as StoreResource, Z as Zappie, b as ZappieClientOptions, b as ZappieConfig, e as ZappieErrorCode, d as ZappieEvents, o as createZappie, p as isZappieError } from './client-75aFOuQi.js';
|
|
4
|
+
import { g as CartIssue, b as CartLineInput, C as CartLine, Q as QuoteRequest, h as WireQuote } from './types-BzyzhqYz.js';
|
|
5
|
+
export { i as CartFix, c as CartFulfilment, e as CartIssueCode, d as CartModule, j as CartSnapshot, f as CartState, k as CartSyncAdapter, l as CartSyncDocument, D as DeliveryAddress, a as Quote, m as WireFix, W as WireFulfilment, n as WireIssue, o as WireLine, p as WireMoney } from './types-BzyzhqYz.js';
|
|
6
6
|
import { I as Item, O as OptionGroup, M as Money, a as Menu, B as Branch, S as Store, C as CategoryPage } from './models-C-kEg1Id.js';
|
|
7
7
|
export { b as Category, c as Choice, d as Currency, F as Formatters, e as Fulfilment, f as MenuSchedule, g as MenuSection, h as MoneyFormatOptions, i as OpeningTime, j as OptionCondition, P as PaymentMethodKind, k as SpecialHours, V as Variant, l as format, m as money } from './models-C-kEg1Id.js';
|
|
8
8
|
|
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ import { ZappieError, isZappieError } from "./errors.js";
|
|
|
10
10
|
export * from "./models.js";
|
|
11
11
|
import { format, money } from "./money.js";
|
|
12
12
|
import { RestTransport } from "./transport/rest.js";
|
|
13
|
-
var VERSION = true ? "0.
|
|
13
|
+
var VERSION = true ? "0.9.0" : "0.0.0-development";
|
|
14
14
|
export {
|
|
15
15
|
API_VERSION,
|
|
16
16
|
RestTransport,
|
package/dist/internal.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { q as CATALOGUE_MAX_AGE_MS, r as CATALOGUE_MAX_BYTES, s as CatalogueScope, t as SavedCatalogue, u as catalogueDigest, w as catalogueKey, x as createCatalogueStorage, y as createSavedCatalogue } from '../client-
|
|
1
|
+
export { q as CATALOGUE_MAX_AGE_MS, r as CATALOGUE_MAX_BYTES, s as CatalogueScope, t as SavedCatalogue, u as catalogueDigest, w as catalogueKey, x as createCatalogueStorage, y as createSavedCatalogue } from '../client-75aFOuQi.js';
|
|
2
2
|
import '../models-C-kEg1Id.js';
|
|
3
|
-
import '../types-
|
|
3
|
+
import '../types-BzyzhqYz.js';
|
package/dist/react/cart.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
// src/react/cart.ts
|
|
4
4
|
import { useEffect, useSyncExternalStore } from "react";
|
|
5
5
|
import { EMPTY_CART } from "../cart/index.js";
|
|
6
|
+
import { cartActions } from "./changes.js";
|
|
6
7
|
import { useZappie } from "./provider.js";
|
|
7
8
|
function useCart() {
|
|
8
9
|
const { cart } = useZappie();
|
|
@@ -12,7 +13,7 @@ function useCart() {
|
|
|
12
13
|
() => EMPTY_CART
|
|
13
14
|
);
|
|
14
15
|
useEffect(() => cart.mount(), [cart]);
|
|
15
|
-
return { ...snapshot, actions: cart };
|
|
16
|
+
return { ...snapshot, actions: cartActions(cart).actions };
|
|
16
17
|
}
|
|
17
18
|
function useCartLine(lineId) {
|
|
18
19
|
return useCart().lines.find((line) => line.id === lineId);
|
|
@@ -22,9 +23,19 @@ function useCartCatalogue() {
|
|
|
22
23
|
useCart();
|
|
23
24
|
return client._internal.cartCatalogue();
|
|
24
25
|
}
|
|
26
|
+
function useCartNotice() {
|
|
27
|
+
const tracker = cartActions(useZappie().cart);
|
|
28
|
+
const notice = useSyncExternalStore(
|
|
29
|
+
tracker.subscribe,
|
|
30
|
+
tracker.notice,
|
|
31
|
+
() => null
|
|
32
|
+
);
|
|
33
|
+
return { notice, dismiss: tracker.dismiss };
|
|
34
|
+
}
|
|
25
35
|
export {
|
|
26
36
|
useCart,
|
|
27
37
|
useCartCatalogue,
|
|
28
|
-
useCartLine
|
|
38
|
+
useCartLine,
|
|
39
|
+
useCartNotice
|
|
29
40
|
};
|
|
30
41
|
//# sourceMappingURL=cart.js.map
|
package/dist/react/cart.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/react/cart.ts"],
|
|
4
|
-
"mappings": ";;;AACA,SAAS,WAAW,4BAA4B;AAChD,SAAS,kBAAkB;AAC3B,SAAS,iBAAiB;
|
|
4
|
+
"mappings": ";;;AACA,SAAS,WAAW,4BAA4B;AAChD,SAAS,kBAAkB;AAC3B,SAAS,mBAAmB;AAC5B,SAAS,iBAAiB;AAMnB,SAAS,UAAU;AACxB,QAAM,EAAE,KAAK,IAAI,UAAU;AAC3B,QAAM,WAAW;AAAA,IACf,KAAK;AAAA,IACL,KAAK;AAAA,IACL,MAAM;AAAA,EACR;AACA,YAAU,MAAM,KAAK,MAAM,GAAG,CAAC,IAAI,CAAC;AACpC,SAAO,EAAE,GAAG,UAAU,SAAS,YAAY,IAAI,EAAE,QAAQ;AAC3D;AAKO,SAAS,YAAY,QAAgB;AAC1C,SAAO,QAAQ,EAAE,MAAM,KAAK,CAAC,SAAS,KAAK,OAAO,MAAM;AAC1D;AAEO,SAAS,mBAAmB;AACjC,QAAM,SAAS,UAAU;AACzB,UAAQ;AACR,SAAO,OAAO,UAAU,cAAc;AACxC;AAKO,SAAS,gBAAgB;AAC9B,QAAM,UAAU,YAAY,UAAU,EAAE,IAAI;AAC5C,QAAM,SAAS;AAAA,IACb,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,MAAM;AAAA,EACR;AACA,SAAO,EAAE,QAAQ,SAAS,QAAQ,QAAQ;AAC5C;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// src/react/changes.ts
|
|
2
|
+
var trackers = /* @__PURE__ */ new WeakMap();
|
|
3
|
+
function cartActions(cart) {
|
|
4
|
+
let tracker = trackers.get(cart);
|
|
5
|
+
if (tracker) {
|
|
6
|
+
return tracker;
|
|
7
|
+
}
|
|
8
|
+
const confirmed = /* @__PURE__ */ new Map();
|
|
9
|
+
const keyed = /* @__PURE__ */ new Map();
|
|
10
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
11
|
+
let notice = null;
|
|
12
|
+
let watching = false;
|
|
13
|
+
const emit = () => {
|
|
14
|
+
for (const fn of [...listeners]) {
|
|
15
|
+
fn();
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
const find = (lineId) => cart.lines.find((candidate) => candidate.id === lineId);
|
|
19
|
+
const remember = (lineId, prior) => {
|
|
20
|
+
if (!confirmed.has(lineId)) {
|
|
21
|
+
confirmed.set(lineId, prior ?? null);
|
|
22
|
+
}
|
|
23
|
+
if (!watching) {
|
|
24
|
+
watching = true;
|
|
25
|
+
void import("./rollback.js").then(
|
|
26
|
+
({ watch }) => watch(cart, confirmed, (value) => {
|
|
27
|
+
notice = value;
|
|
28
|
+
emit();
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const actions = Object.freeze(
|
|
34
|
+
Object.create(cart, {
|
|
35
|
+
add: {
|
|
36
|
+
value(item, input, { key } = {}) {
|
|
37
|
+
const done = key ? keyed.get(key) : void 0;
|
|
38
|
+
if (done) {
|
|
39
|
+
return find(done.id) ?? done;
|
|
40
|
+
}
|
|
41
|
+
const before = cart.lines;
|
|
42
|
+
const line = cart.add(item, input);
|
|
43
|
+
remember(
|
|
44
|
+
line.id,
|
|
45
|
+
before.find((candidate) => candidate.id === line.id)
|
|
46
|
+
);
|
|
47
|
+
if (key) {
|
|
48
|
+
keyed.set(key, line);
|
|
49
|
+
if (keyed.size > 100) {
|
|
50
|
+
keyed.delete(keyed.keys().next().value);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return line;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
update: {
|
|
57
|
+
value(lineId, patch) {
|
|
58
|
+
const prior = find(lineId);
|
|
59
|
+
const line = cart.update(lineId, patch);
|
|
60
|
+
remember(lineId, prior);
|
|
61
|
+
return line;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
remove: {
|
|
65
|
+
value(lineId) {
|
|
66
|
+
confirmed.delete(lineId);
|
|
67
|
+
cart.remove(lineId);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
tracker = {
|
|
73
|
+
actions,
|
|
74
|
+
notice: () => notice,
|
|
75
|
+
dismiss() {
|
|
76
|
+
if (notice) {
|
|
77
|
+
notice = null;
|
|
78
|
+
emit();
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
subscribe(fn) {
|
|
82
|
+
listeners.add(fn);
|
|
83
|
+
return () => {
|
|
84
|
+
listeners.delete(fn);
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
trackers.set(cart, tracker);
|
|
89
|
+
return tracker;
|
|
90
|
+
}
|
|
91
|
+
export {
|
|
92
|
+
cartActions
|
|
93
|
+
};
|
|
94
|
+
//# sourceMappingURL=changes.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/react/changes.ts"],
|
|
4
|
+
"mappings": ";AAmDA,IAAM,WAAW,oBAAI,QAA6B;AAE3C,SAAS,YAAY,MAA2B;AACrD,MAAI,UAAU,SAAS,IAAI,IAAI;AAC/B,MAAI,SAAS;AACX,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,oBAAI,IAA6B;AACnD,QAAM,QAAQ,oBAAI,IAAsB;AACxC,QAAM,YAAY,oBAAI,IAAgB;AACtC,MAAI,SAA4B;AAChC,MAAI,WAAW;AACf,QAAM,OAAO,MAAM;AACjB,eAA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tB,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,MACA,QAAQ;AAAA,QACN,MAAM,QAAgB;AACpB,oBAAU,OAAO,MAAM;AACvB,eAAK,OAAO,MAAM;AAAA,QACpB;AAAA,MACF;AAAA,IACF,CAAC;AAAA,EACH;AACA,YAAU;AAAA,IACR;AAAA,IACA,QAAQ,MAAM;AAAA,IACd,UAAU;AACR,UAAI,QAAQ;AACV,iBAAS;AACT,aAAK;AAAA,MACP;AAAA,IACF;AAAA,IACA,UAAU,IAAI;AACZ,gBAAU,IAAI,EAAE;AAChB,aAAO,MAAM;AACX,kBAAU,OAAO,EAAE;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AACA,WAAS,IAAI,MAAM,OAAO;AAC1B,SAAO;AACT;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/react/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ZappieError, Zappie } from '../index.js';
|
|
|
3
3
|
import { Item, Menu, Store, Money } from '../index.js';
|
|
4
4
|
import * as react from 'react';
|
|
5
5
|
import { ReactNode } from 'react';
|
|
6
|
-
import { CartModule, CartLine, CartFulfilment, CartState, CartIssue } from '../index.js';
|
|
6
|
+
import { CartModule, CartLineInput, CartLine, CartIssueCode, CartFulfilment, CartState, CartIssue } from '../index.js';
|
|
7
7
|
|
|
8
8
|
/** Subscribe to storefront identity and branch-directory data. */
|
|
9
9
|
declare function useStore(): {
|
|
@@ -39,9 +39,43 @@ declare function ZappieProvider({ client, initialData, children, }: {
|
|
|
39
39
|
/** Read your storefront client from the nearest ZappieProvider. */
|
|
40
40
|
declare function useZappie(): Zappie;
|
|
41
41
|
|
|
42
|
-
/**
|
|
42
|
+
/**
|
|
43
|
+
* Options for one cart change.
|
|
44
|
+
*/
|
|
45
|
+
type CartChangeOptions = {
|
|
46
|
+
/**
|
|
47
|
+
* A key for one shopper action. A second add with the same key returns the first line and adds nothing,
|
|
48
|
+
* so a retried add never adds twice. Make it with `crypto.randomUUID()` when the shopper acts and reuse it
|
|
49
|
+
* only to retry that action. Never build it from the line's contents.
|
|
50
|
+
*/
|
|
51
|
+
key?: string;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* The cart's operations as `useCart().actions` returns them. Each add, update and remove shows at once.
|
|
55
|
+
* When the next quote refuses the change, the cart puts that line back and raises one notice.
|
|
56
|
+
*/
|
|
57
|
+
type CartActions = Omit<CartModule, "add"> & {
|
|
58
|
+
add(item: Item | string, input?: Omit<CartLineInput, "itemId">, options?: CartChangeOptions): CartLine;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* The one message for changes the server refused and the cart undid. `message` is the server's reason.
|
|
62
|
+
* `count` says how many lines the cart put back. Each new notice gets a new `id`.
|
|
63
|
+
*/
|
|
64
|
+
type CartNotice = Readonly<{
|
|
65
|
+
id: number;
|
|
66
|
+
code: CartIssueCode;
|
|
67
|
+
message: string;
|
|
68
|
+
lineId?: string;
|
|
69
|
+
count: number;
|
|
70
|
+
}>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Load saved cart state after mount. Server and initial hydration snapshots are empty.
|
|
74
|
+
* `actions` shows each change at once and puts a line back when the next quote refuses it.
|
|
75
|
+
* Read the refusal with `useCartNotice()`.
|
|
76
|
+
*/
|
|
43
77
|
declare function useCart(): {
|
|
44
|
-
actions:
|
|
78
|
+
actions: CartActions;
|
|
45
79
|
branchId?: string | undefined;
|
|
46
80
|
lines: readonly CartLine[];
|
|
47
81
|
count: number;
|
|
@@ -76,5 +110,20 @@ declare function useCartLine(lineId: string): Readonly<{
|
|
|
76
110
|
}> | undefined;
|
|
77
111
|
/** Read the cart validation catalogue, including held items, without fetching it. */
|
|
78
112
|
declare function useCartCatalogue(): Menu | undefined;
|
|
113
|
+
/**
|
|
114
|
+
* Read the one notice for cart changes the server refused. The cart has already put those lines back
|
|
115
|
+
* and told screen readers. Show `notice.message` once, as a toast, and call `dismiss` to clear it.
|
|
116
|
+
*/
|
|
117
|
+
declare function useCartNotice(): {
|
|
118
|
+
notice: Readonly<{
|
|
119
|
+
id: number;
|
|
120
|
+
code: CartIssueCode;
|
|
121
|
+
message: string;
|
|
122
|
+
lineId?: string;
|
|
123
|
+
count: number;
|
|
124
|
+
}> | null;
|
|
125
|
+
dismiss: () => void;
|
|
126
|
+
};
|
|
79
127
|
|
|
80
|
-
export { ZappieProvider, useCart, useCartCatalogue, useCartLine, useItem, useMenu, useStore, useZappie };
|
|
128
|
+
export { ZappieProvider, useCart, useCartCatalogue, useCartLine, useCartNotice, useItem, useMenu, useStore, useZappie };
|
|
129
|
+
export type { CartActions, CartChangeOptions, CartNotice };
|
package/dist/react/index.js
CHANGED
|
@@ -4,13 +4,14 @@
|
|
|
4
4
|
import { VERSION } from "../index.js";
|
|
5
5
|
import { useItem, useMenu, useStore } from "./hooks.js";
|
|
6
6
|
import { ZappieProvider, useZappie } from "./provider.js";
|
|
7
|
-
import { useCart, useCartCatalogue, useCartLine } from "./cart.js";
|
|
7
|
+
import { useCart, useCartCatalogue, useCartLine, useCartNotice } from "./cart.js";
|
|
8
8
|
export {
|
|
9
9
|
VERSION,
|
|
10
10
|
ZappieProvider,
|
|
11
11
|
useCart,
|
|
12
12
|
useCartCatalogue,
|
|
13
13
|
useCartLine,
|
|
14
|
+
useCartNotice,
|
|
14
15
|
useItem,
|
|
15
16
|
useMenu,
|
|
16
17
|
useStore,
|
package/dist/react/index.js.map
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/react/index.ts"],
|
|
4
|
-
"mappings": ";;;AACA,SAAS,eAAe;AACxB,SAAS,SAAS,SAAS,gBAAgB;AAC3C,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,SAAS,kBAAkB,
|
|
4
|
+
"mappings": ";;;AACA,SAAS,eAAe;AACxB,SAAS,SAAS,SAAS,gBAAgB;AAC3C,SAAS,gBAAgB,iBAAiB;AAE1C,SAAS,SAAS,kBAAkB,aAAa,qBAAqB;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// src/react/rollback.ts
|
|
2
|
+
var refusals = /^(quantity_limit|item_unavailable|item_not_found|option_)/;
|
|
3
|
+
var region;
|
|
4
|
+
function announce(message) {
|
|
5
|
+
if (typeof document === "undefined") {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if (!region?.isConnected) {
|
|
9
|
+
region = document.createElement("div");
|
|
10
|
+
region.setAttribute("role", "status");
|
|
11
|
+
region.setAttribute("aria-live", "polite");
|
|
12
|
+
region.setAttribute("data-zappie-announcer", "");
|
|
13
|
+
region.style.cssText = "position:absolute;width:1px;height:1px;overflow:hidden;clip-path:inset(50%)";
|
|
14
|
+
document.body.appendChild(region);
|
|
15
|
+
}
|
|
16
|
+
const target = region;
|
|
17
|
+
target.textContent = "";
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
target.textContent = message;
|
|
20
|
+
}, 50);
|
|
21
|
+
}
|
|
22
|
+
function watch(cart, confirmed, show) {
|
|
23
|
+
let seen;
|
|
24
|
+
let sequence = 0;
|
|
25
|
+
const settle = () => {
|
|
26
|
+
const snapshot = cart.getSnapshot();
|
|
27
|
+
const quote = "quote" in snapshot.state ? snapshot.state.quote : void 0;
|
|
28
|
+
if (!quote || quote === seen) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
seen = quote;
|
|
32
|
+
let first;
|
|
33
|
+
let count = 0;
|
|
34
|
+
for (const [lineId, prior] of [...confirmed]) {
|
|
35
|
+
const line = snapshot.lines.find((candidate) => candidate.id === lineId);
|
|
36
|
+
const quoted = quote.lines.find(
|
|
37
|
+
(candidate) => candidate.lineId === lineId
|
|
38
|
+
)?.quantity;
|
|
39
|
+
if (line && quoted !== void 0 && quoted !== line.quantity) {
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
confirmed.delete(lineId);
|
|
43
|
+
const problem = quote.issues.find(
|
|
44
|
+
(issue) => issue.lineId === lineId && issue.severity === "blocking" && refusals.test(issue.code)
|
|
45
|
+
);
|
|
46
|
+
if (!line || !problem) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
const fix = problem.fixes.find(
|
|
50
|
+
(candidate) => candidate.action === "set_quantity" && candidate.quantity > (prior?.quantity ?? 0) && candidate.quantity < line.quantity
|
|
51
|
+
);
|
|
52
|
+
try {
|
|
53
|
+
if (fix?.action === "set_quantity") {
|
|
54
|
+
cart.update(lineId, { quantity: fix.quantity });
|
|
55
|
+
} else if (prior) {
|
|
56
|
+
cart.update(lineId, {
|
|
57
|
+
quantity: prior.quantity,
|
|
58
|
+
choices: prior.choices,
|
|
59
|
+
inputs: prior.inputs,
|
|
60
|
+
variantId: prior.variantId,
|
|
61
|
+
note: prior.note
|
|
62
|
+
});
|
|
63
|
+
} else {
|
|
64
|
+
cart.remove(lineId);
|
|
65
|
+
}
|
|
66
|
+
first ?? (first = problem);
|
|
67
|
+
count++;
|
|
68
|
+
} catch {
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
if (first) {
|
|
72
|
+
announce(first.message);
|
|
73
|
+
show({
|
|
74
|
+
id: ++sequence,
|
|
75
|
+
code: first.code,
|
|
76
|
+
message: first.message,
|
|
77
|
+
...first.lineId ? { lineId: first.lineId } : {},
|
|
78
|
+
count
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
cart.subscribe(settle);
|
|
83
|
+
settle();
|
|
84
|
+
}
|
|
85
|
+
export {
|
|
86
|
+
watch
|
|
87
|
+
};
|
|
88
|
+
//# sourceMappingURL=rollback.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/react/rollback.ts"],
|
|
4
|
+
"mappings": ";AAKA,IAAM,WAAW;AACjB,IAAI;AAIJ,SAAS,SAAS,SAAiB;AACjC,MAAI,OAAO,aA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A,UAAI;AACF,YAAI,KAAK,WAAW,gBAAgB;AAClC,eAAK,OAAO,QAAQ,EAAE,UAAU,IAAI,SAAS,CAAC;AAAA,QAChD,WAAW,OAAO;AAEhB,eAAK,OAAO,QAAQ;AAAA,YAClB,UAAU,MAAM;AAAA,YAChB,SAAS,MAAM;AAAA,YACf,QAAQ,MAAM;AAAA,YACd,WAAW,MAAM;AAAA,YACjB,MAAM,MAAM;AAAA,UACd,CAAwC;AAAA,QAC1C,OAAO;AACL,eAAK,OAAO,MAAM;AAAA,QACpB;AACA,0BAAU;AACV;AAAA,MACF,QAAQ;AAAA,MAER;AAAA,IACF;AACA,QAAI,OAAO;AACT,eAAS,MAAM,OAAO;AACtB,WAAK;AAAA,QACH,IAAI,EAAE;AAAA,QACN,MAAM,MAAM;AAAA,QACZ,SAAS,MAAM;AAAA,QACf,GAAI,MAAM,SAAS,EAAE,QAAQ,MAAM,OAAO,IAAI,CAAC;AAAA,QAC/C;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AACA,OAAK,UAAU,MAAM;AACrB,SAAO;AACT;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Menu, Store, Formatters } from '../index.js';
|
|
2
|
-
import { C as Cache } from '../client-
|
|
2
|
+
import { C as Cache } from '../client-75aFOuQi.js';
|
|
3
3
|
import { ZappieError, ZappieConfig, Transport, StorageLike, StoreResource, MenuResource, ZappieEvents } from '../index.js';
|
|
4
4
|
export { VERSION } from '../index.js';
|
|
5
5
|
import { CartModule, CartLineInput } from '../index.js';
|
package/dist/studio/index.js
CHANGED
package/dist/template/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
/** Cloudflare image widths shared by template sites. */
|
|
2
|
+
declare const IMAGE_WIDTHS: readonly [320, 480, 640, 960, 1280, 1600];
|
|
3
|
+
|
|
1
4
|
/** A value accepted by a template manifest and the live preview bridge. */
|
|
2
5
|
type TemplateSettingValue = string | number | boolean;
|
|
3
6
|
/** Where a template is running. Live sites never accept preview messages. */
|
|
@@ -17,8 +20,6 @@ declare const EDITOR_ORIGINS: readonly string[];
|
|
|
17
20
|
declare function editorOrigin(ancestorOrigins: ArrayLike<string> | null | undefined, referrer: string, allowed?: readonly string[]): string | null;
|
|
18
21
|
/** Read primitive setting values from an editor message. Oversized messages and values are dropped. */
|
|
19
22
|
declare function settingsFromMessage(data: unknown): Record<string, TemplateSettingValue> | null;
|
|
20
|
-
/** Cloudflare image widths shared by template sites. */
|
|
21
|
-
declare const IMAGE_WIDTHS: readonly [320, 480, 640, 960, 1280, 1600];
|
|
22
23
|
/**
|
|
23
24
|
* The image hosts that same-origin resizing accepts by default: Zappie's production photo bucket.
|
|
24
25
|
* Pass your own lowercase host list as `resizeHosts` to `resized`, `srcSet` or foundations' `Image`
|
package/dist/template/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/template/index.ts
|
|
2
|
+
import { IMAGE_WIDTHS } from "../internal/image-widths.js";
|
|
2
3
|
function templateSettings(defaults, saved, preview, mode) {
|
|
3
4
|
if (mode === "review") {
|
|
4
5
|
return { ...defaults, ...preview ?? {} };
|
|
@@ -55,7 +56,6 @@ function settingsFromMessage(data) {
|
|
|
55
56
|
}
|
|
56
57
|
return values;
|
|
57
58
|
}
|
|
58
|
-
var IMAGE_WIDTHS = [320, 480, 640, 960, 1280, 1600];
|
|
59
59
|
var RESIZE_HOSTS = Object.freeze([
|
|
60
60
|
"zappie-public-prod.s3.eu-west-2.amazonaws.com"
|
|
61
61
|
]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/template/index.ts"],
|
|
4
|
-
"mappings": ";
|
|
4
|
+
"mappings": ";AAAA,SAAS,oBAAoB;AAWtB,SAAS,iBACd,UACA,OACA,SACA,MACG;AACH,MAAI,SAAS,UAAU;AACrB,W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nD,OAAO,UAAU,WACjB;AACA,aAAO,GAAG,IAAI;AAAA,IAChB;AAAA,EACF;AACA,SAAO;AACT;AAOO,IAAM,eAAkC,OAAO,OAAO;AAAA,EAC3D;AACF,CAAC;AAOM,SAAS,UACd,KACA,cAAiC,cACxB;AACT,MAAI;AACF,UAAM,MAAM,IAAI,IAAI,GAAG;AACvB,WACE,YAAY,KAAK,IAAI,QAAQ,KAC7B,YAAY,SAAS,IAAI,QAAQ,KACjC,CAAC,qBAAqB,KAAK,IAAI,QAAQ;AAAA,EAE3C,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAMO,SAAS,QACd,KACA,OACA,UAAU,IACV,aACQ;AACR,SAAO,UAAU,KAAK,WAAW,IAC7B,wBAAwB,KAAK,YAAY,OAAO,+BAA+B,IAAI,IAAI,GAAG,EAAE,IAAI,KAChG;AACN;AAMO,SAAS,OACd,KACA,SACA,aACQ;AACR,SAAO,UAAU,KAAK,WAAW,IAC7B,aAAa;AAAA,IACX,CAAC,UAAU,GAAG,QAAQ,KAAK,OAAO,SAAS,WAAW,CAAC,IAAI,KAAK;AAAA,EAClE,EAAE,KAAK,IAAI,IACX;AACN;",
|
|
5
5
|
"names": []
|
|
6
6
|
}
|
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as Cache } from '../client-
|
|
1
|
+
import { C as Cache } from '../client-75aFOuQi.js';
|
|
2
2
|
import { Transport, ZappieErrorCode, StorageLike, StoreResource, MenuResource, ZappieEvents } from '../index.js';
|
|
3
3
|
import { Store, Menu, Formatters } from '../index.js';
|
|
4
4
|
export { VERSION } from '../index.js';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Menu } from '../index.js';
|
|
2
2
|
import { SearchRequest, SearchPage } from '../index.js';
|
|
3
|
-
import '../types-
|
|
3
|
+
import '../types-BzyzhqYz.js';
|
|
4
4
|
|
|
5
5
|
/** Loaded only when search is used; request authentication/retries stay in REST. */
|
|
6
6
|
declare function searchPage(ctx: SearchRequest, read: (path: string) => Promise<unknown>, mapMenu: (value: unknown, branchId: string) => Menu, invalid: () => Error): Promise<SearchPage>;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { z as SelectedMenu, h as SelectedMenuRequest, i as SelectedMenuResponse, B as SelectedMenuTransport, D as createSelectedMenuReader, F as selectedMenu, G as selectedMenuKey } from '../client-
|
|
1
|
+
export { z as SelectedMenu, h as SelectedMenuRequest, i as SelectedMenuResponse, B as SelectedMenuTransport, D as createSelectedMenuReader, F as selectedMenu, G as selectedMenuKey } from '../client-75aFOuQi.js';
|
|
2
2
|
import '../models-C-kEg1Id.js';
|
|
3
|
-
import '../types-
|
|
3
|
+
import '../types-BzyzhqYz.js';
|
|
@@ -379,4 +379,4 @@ interface CartSyncAdapter {
|
|
|
379
379
|
}>;
|
|
380
380
|
}
|
|
381
381
|
|
|
382
|
-
export type { CartLine as C, DeliveryAddress as D, QuoteRequest as Q, WireFulfilment as W, Quote as a, CartLineInput as b, CartFulfilment as c, CartModule as d,
|
|
382
|
+
export type { CartLine as C, DeliveryAddress as D, QuoteRequest as Q, WireFulfilment as W, Quote as a, CartLineInput as b, CartFulfilment as c, CartModule as d, CartIssueCode as e, CartState as f, CartIssue as g, WireQuote as h, CartFix as i, CartSnapshot as j, CartSyncAdapter as k, CartSyncDocument as l, WireFix as m, WireIssue as n, WireLine as o, WireMoney as p };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Where the host serves a site's uploads, on the site's own address. */
|
|
2
|
+
declare const UPLOADS_PATH = "/_zappie/assets/";
|
|
3
|
+
/** The qualities the uploads route accepts; {@link uploadResized} asks for the nearest. */
|
|
4
|
+
declare const UPLOAD_QUALITIES: readonly [60, 75, 80, 90];
|
|
5
|
+
/** The path the host serves an upload at, from its name. */
|
|
6
|
+
declare function uploadPath(name: string): string;
|
|
7
|
+
/** Whether `src` is one of the site's uploads, served by the host at {@link UPLOADS_PATH}. */
|
|
8
|
+
declare function isUpload(src: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Whether the host resizes the upload at `src`: a png, jpg, gif or webp upload. SVG and AVIF
|
|
11
|
+
* uploads, and anything that is not an upload, are served as they are.
|
|
12
|
+
*/
|
|
13
|
+
declare function canResizeUpload(src: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* The host's resized version of an upload, at the nearest of `IMAGE_WIDTHS` and
|
|
16
|
+
* {@link UPLOAD_QUALITIES}. An upload the host does not resize is returned unchanged.
|
|
17
|
+
*/
|
|
18
|
+
declare function uploadResized(src: string, width: number, quality?: number): string;
|
|
19
|
+
/** The responsive candidates for an upload, or an empty string when the host does not resize it. */
|
|
20
|
+
declare function uploadSrcSet(src: string, quality?: number): string;
|
|
21
|
+
|
|
22
|
+
export { UPLOADS_PATH, UPLOAD_QUALITIES, canResizeUpload, isUpload, uploadPath, uploadResized, uploadSrcSet };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/uploads/index.ts
|
|
2
|
+
import { IMAGE_WIDTHS } from "../internal/image-widths.js";
|
|
3
|
+
var UPLOADS_PATH = "/_zappie/assets/";
|
|
4
|
+
var UPLOAD_QUALITIES = [60, 75, 80, 90];
|
|
5
|
+
var UPLOAD = /^\/_zappie\/assets\/[a-z0-9][a-z0-9-]*\.([a-z0-9]+)$/;
|
|
6
|
+
function uploadPath(name) {
|
|
7
|
+
return `${UPLOADS_PATH}${name}`;
|
|
8
|
+
}
|
|
9
|
+
function isUpload(src) {
|
|
10
|
+
return UPLOAD.test(src);
|
|
11
|
+
}
|
|
12
|
+
function canResizeUpload(src) {
|
|
13
|
+
return /^(png|jpe?g|gif|webp)$/.test(UPLOAD.exec(src)?.[1] ?? "");
|
|
14
|
+
}
|
|
15
|
+
function nearest(values, wanted) {
|
|
16
|
+
return values.reduce(
|
|
17
|
+
(best, value) => Math.abs(value - wanted) < Math.abs(best - wanted) ? value : best
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
function uploadResized(src, width, quality = 80) {
|
|
21
|
+
return canResizeUpload(src) ? `${src}?w=${nearest(IMAGE_WIDTHS, width)}&q=${nearest(UPLOAD_QUALITIES, quality)}` : src;
|
|
22
|
+
}
|
|
23
|
+
function uploadSrcSet(src, quality) {
|
|
24
|
+
return canResizeUpload(src) ? IMAGE_WIDTHS.map(
|
|
25
|
+
(width) => `${uploadResized(src, width, quality)} ${width}w`
|
|
26
|
+
).join(", ") : "";
|
|
27
|
+
}
|
|
28
|
+
export {
|
|
29
|
+
UPLOADS_PATH,
|
|
30
|
+
UPLOAD_QUALITIES,
|
|
31
|
+
canResizeUpload,
|
|
32
|
+
isUpload,
|
|
33
|
+
uploadPath,
|
|
34
|
+
uploadResized,
|
|
35
|
+
uploadSrcSet
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/uploads/index.ts"],
|
|
4
|
+
"mappings": ";AAAA,SAAS,oBAAoB;AAGtB,IAAM,eAAe;AAGrB,IAAM,mBAAmB,CAAC,IAAI,IAAI,IAAI,EAAE;AAE/C,IAAM,SAAS;AAGR,SAAS,WAAW,MAAsB;AAC/C,SAAO,GAAG,YAAY,GAAG,IAAI;AAC/B;AAGO,SAAS,SAAS,KAAsB;AAC7C,SAAO,OAAO,KAAK,GAAG;AACxB;AAMO,SAAS,gBAAgB,KAAsB;AACpD,SAAO,yBAAyB,KAAK,OAAO,KAAK,GAAG,IAAI,CAAC,KAAK,EAAE;AAClE;AAEA,SAAS,QAAQ,QAA2B,QAAwB;AAClE,SAAO,OAAO;AAAA,IAAO,CAAC,MAAM,UAC1B,KAAK,IAAI,QAAQ,MAAM,IAAI,KAAK,IAAI,OAAO,MAAM,IAAI,QAAQ;AAAA,EAC/D;AACF;AAMO,SAAS,cACd,KACA,OACA,UAAU,IACF;AACR,SAAO,gBAAgB,GAAG,IACtB,GAAG,GAAG,MAAM,QAAQ,cAAc,KAAK,CAAC,MAAM,QAAQ,kBAAkB,OAAO,CAAC,KAChF;AACN;AAGO,SAAS,aAAa,KAAa,SAA0B;AAClE,SAAO,gBAAgB,GAAG,IACtB,aAAa;AAAA,IACX,CAAC,UAAU,GAAG,cAAc,KAAK,OAAO,OAAO,CAAC,IAAI,KAAK;AAAA,EAC3D,EAAE,KAAK,IAAI,IACX;AACN;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
package/dist/vite/index.d.ts
CHANGED
|
@@ -55,6 +55,34 @@ declare function markSource(code: string, file: string): MarkedSource | null;
|
|
|
55
55
|
* ```
|
|
56
56
|
*/
|
|
57
57
|
declare function zappieHoistImports(): Plugin;
|
|
58
|
+
/** The options {@link zappieUploads} takes. */
|
|
59
|
+
type ZappieUploadsOptions = {
|
|
60
|
+
/** The site's published address, such as `https://shop.zappie.shop`, which serves its uploads. Without it the plugin is off. */
|
|
61
|
+
host: string | undefined;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Serve the site's uploads on the local dev server by asking its published address for each
|
|
65
|
+
* `/_zappie/assets/` request, query included, so `Image` shows the same resized versions as the
|
|
66
|
+
* live site. The Studio preview needs nothing: its draft host answers uploads itself.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* import { zappieUploads } from "@zappie/sdk/vite";
|
|
71
|
+
*
|
|
72
|
+
* export default defineConfig({
|
|
73
|
+
* plugins: [zappieUploads({ host: process.env.ZAPPIE_SITE_HOST }), reactRouter()],
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare function zappieUploads({ host }: ZappieUploadsOptions): Plugin;
|
|
78
|
+
/** The https origin of `host`, or undefined when it is not one. */
|
|
79
|
+
declare function uploadsOrigin(host: string | undefined): string | undefined;
|
|
80
|
+
/** Ask the published site for one upload and answer what it answered. */
|
|
81
|
+
declare function forwardUpload(origin: string, path: string, method?: string, accept?: string): Promise<{
|
|
82
|
+
status: number;
|
|
83
|
+
headers: Record<string, string>;
|
|
84
|
+
body: Buffer;
|
|
85
|
+
}>;
|
|
58
86
|
|
|
59
|
-
export { COMPONENT_ATTRIBUTE, SOURCE_ATTRIBUTE, markSource, zappieHoistImports, zappieSource };
|
|
60
|
-
export type { MarkedSource, ZappieSourceOptions };
|
|
87
|
+
export { COMPONENT_ATTRIBUTE, SOURCE_ATTRIBUTE, forwardUpload, markSource, uploadsOrigin, zappieHoistImports, zappieSource, zappieUploads };
|
|
88
|
+
export type { MarkedSource, ZappieSourceOptions, ZappieUploadsOptions };
|
package/dist/vite/index.js
CHANGED
|
@@ -196,10 +196,73 @@ function relativeImport(from, file) {
|
|
|
196
196
|
const rest = to.slice(shared).join("/");
|
|
197
197
|
return up === 0 ? `./${rest}` : `${"../".repeat(up)}${rest}`;
|
|
198
198
|
}
|
|
199
|
+
var UPLOADS_PREFIX = "/_zappie/assets/";
|
|
200
|
+
function zappieUploads({ host }) {
|
|
201
|
+
const origin = uploadsOrigin(host);
|
|
202
|
+
return {
|
|
203
|
+
name: "zappie-uploads",
|
|
204
|
+
apply: (_config, { command }) => command === "serve" && Boolean(origin),
|
|
205
|
+
configureServer(server) {
|
|
206
|
+
server.middlewares.use((request, response, next) => {
|
|
207
|
+
const path = request.url ?? "";
|
|
208
|
+
if (!origin || !path.startsWith(UPLOADS_PREFIX)) {
|
|
209
|
+
next();
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
void forwardUpload(origin, path, request.method, request.headers.accept).then(({ status, headers, body }) => {
|
|
213
|
+
response.writeHead(status, headers);
|
|
214
|
+
response.end(body);
|
|
215
|
+
}).catch(() => {
|
|
216
|
+
response.writeHead(502, { "cache-control": "no-store" });
|
|
217
|
+
response.end();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
function uploadsOrigin(host) {
|
|
224
|
+
if (!host) {
|
|
225
|
+
return void 0;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const url = new URL(host.includes("://") ? host : `https://${host}`);
|
|
229
|
+
return url.protocol === "https:" ? url.origin : void 0;
|
|
230
|
+
} catch {
|
|
231
|
+
return void 0;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function forwardUpload(origin, path, method = "GET", accept) {
|
|
235
|
+
if (method !== "GET" && method !== "HEAD") {
|
|
236
|
+
return {
|
|
237
|
+
status: 405,
|
|
238
|
+
headers: { allow: "GET, HEAD" },
|
|
239
|
+
body: Buffer.alloc(0)
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
const upstream = await fetch(`${origin}${path}`, {
|
|
243
|
+
method,
|
|
244
|
+
headers: accept ? { accept } : {}
|
|
245
|
+
});
|
|
246
|
+
const headers = {};
|
|
247
|
+
for (const key of ["content-type", "cache-control", "vary"]) {
|
|
248
|
+
const value = upstream.headers.get(key);
|
|
249
|
+
if (value) {
|
|
250
|
+
headers[key] = value;
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return {
|
|
254
|
+
status: upstream.status,
|
|
255
|
+
headers,
|
|
256
|
+
body: Buffer.from(await upstream.arrayBuffer())
|
|
257
|
+
};
|
|
258
|
+
}
|
|
199
259
|
export {
|
|
200
260
|
COMPONENT_ATTRIBUTE,
|
|
201
261
|
SOURCE_ATTRIBUTE,
|
|
262
|
+
forwardUpload,
|
|
202
263
|
markSource,
|
|
264
|
+
uploadsOrigin,
|
|
203
265
|
zappieHoistImports,
|
|
204
|
-
zappieSource
|
|
266
|
+
zappieSource,
|
|
267
|
+
zappieUploads
|
|
205
268
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zappie/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.0",
|
|
4
4
|
"description": "Typed, framework-agnostic client for Zappie menus, carts, checkout, auth and events.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -93,6 +93,10 @@
|
|
|
93
93
|
"types": "./dist/template/index.d.ts",
|
|
94
94
|
"import": "./dist/template/index.js"
|
|
95
95
|
},
|
|
96
|
+
"./uploads": {
|
|
97
|
+
"types": "./dist/uploads/index.d.ts",
|
|
98
|
+
"import": "./dist/uploads/index.js"
|
|
99
|
+
},
|
|
96
100
|
"./template/react": {
|
|
97
101
|
"types": "./dist/template/react.d.ts",
|
|
98
102
|
"import": "./dist/template/react.js"
|