@zappie/sdk 0.8.0 → 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 CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 0.8.0
4
10
 
5
11
  ### Minor Changes
package/dist/debug.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/debug.ts
2
- var VERSION = true ? "0.8.0" : "0.0.0-development";
2
+ var VERSION = true ? "0.9.0" : "0.0.0-development";
3
3
  function installDebug(client) {
4
4
  if (typeof window === "undefined" || client.mode !== "test") {
5
5
  return;
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.8.0" : "0.0.0-development";
13
+ var VERSION = true ? "0.9.0" : "0.0.0-development";
14
14
  export {
15
15
  API_VERSION,
16
16
  RestTransport,
@@ -0,0 +1,6 @@
1
+ // src/internal/image-widths.ts
2
+ var IMAGE_WIDTHS = [320, 480, 640, 960, 1280, 1600];
3
+ export {
4
+ IMAGE_WIDTHS
5
+ };
6
+ //# sourceMappingURL=image-widths.js.map
@@ -0,0 +1,6 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/internal/image-widths.ts"],
4
+ "mappings": ";AACO,IAAM,eAAe,CAAC,KAAK,KAAK,KAAK,KAAK,MAAM,IAAI;",
5
+ "names": []
6
+ }
@@ -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`
@@ -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": ";AAOO,SAAS,iBACd,UACA,OACA,SACA,MACG;AACH,MAAI,SAAS,UAAU;AACrB,WAAO,EAAE,GAAG,UAAU,GAAI,WAAW,CAAC,EAAG;AAAA,EAC3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AAU,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
  }
@@ -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
+ }
@@ -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 };
@@ -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.8.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"