@eldrajs/sdk 0.2.5 → 0.2.7

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
@@ -9,19 +9,44 @@ platform repository.
9
9
 
10
10
  ## Unreleased
11
11
 
12
+ - `analyticsTrackerScript`'s documentation no longer says events always reach the gateway; with
13
+ `eventOrigin` set they go to that first-party proxy.
14
+
15
+ ## 0.2.6 — 2026-10-08
16
+
17
+ - Responsive images: `responsiveImage` returns `src`, `srcset`, `sizes`, `width` and `height` for
18
+ an `<img>` from any media URL or object the API returns; `imageSrcset`, `imageUrl`,
19
+ `imageVariantFor`, `isEldraImage` and `ELDRA_IMAGE_VARIANT_WIDTHS` are the parts. A bare media
20
+ URL serves 800 px, so a storefront rendering it as-is was soft on wide screens. External URLs
21
+ and formats the host does not resize come back unchanged. See `docs/images.md`.
22
+
23
+ ## 0.2.5 — 2026-09-25
24
+
25
+ - `analyticsTrackerScript` builds the analytics tracker `<script>` attributes from the same
26
+ configuration as the client, so the script comes from the gateway the site reads from. Events go
27
+ there too, unless `eventOrigin` names a first-party proxy to send them through.
28
+
29
+ ## 0.2.4 — 2026-09-23
30
+
12
31
  - `checkout.handoffUrl` defaults to the hosted checkout at `https://checkout.eldra.app` (exported
13
32
  as `DEFAULT_ELDRA_CHECKOUT_URL`) when the client uses the default API base URL, so a production
14
33
  storefront no longer needs a checkout URL setting. `checkoutUrl` and the `ELDRA_CHECKOUT_URL` env
15
34
  keys still override it; a client on any other gateway must still name its checkout.
16
35
 
36
+ ## 0.2.3 — 2026-09-23
37
+
17
38
  - `EldraHttpError.errorId` carries the problem's specific reason, such as `CART_NOT_FOUND` or
18
39
  `CART_INSUFFICIENT_STOCK`. The docs told you to branch on `code` for these, but the gateway puts
19
40
  them in `errorId`; `code` is only the category (`NOT_FOUND`, `CONFLICT`) and is unchanged.
20
41
 
42
+ ## 0.2.2 — 2026-09-20
43
+
21
44
  - Restore `previewToken` on the SDK client and Vite generator after the move from
22
45
  `vue-ui-components`. Accepts a string or callback and sends `X-Preview-Token`; the generator
23
46
  forwards it to both type endpoints without embedding it in generated files.
24
47
 
48
+ ## 0.2.1 — 2026-09-16
49
+
25
50
  - The Vite plugin writes `.eldra/web-studio/` beside the nearest `package.json` instead of Vite's
26
51
  root; on Nuxt 4 the root is `app/`, and the folder landed there. A relative `outDir` is resolved
27
52
  the same way.
package/README.md CHANGED
@@ -42,6 +42,25 @@ it as `X-Preview-Token`. Configure each separately; the generator never embeds t
42
42
  output. See [preview mode](../../docs/getting-started.md#preview-mode) for examples and header
43
43
  precedence.
44
44
 
45
+ ## Images
46
+
47
+ A media URL from the API serves an 800 px copy. Build a `srcset` over the sizes the media host
48
+ serves (400 to 2560 px) so the browser picks one that is sharp on the screen in front of it:
49
+
50
+ ```ts
51
+ import { responsiveImage } from '@eldrajs/sdk';
52
+
53
+ const image = responsiveImage(product.thumbnail, {
54
+ sizes: '(min-width: 1024px) 25vw, 50vw', // how wide the image is drawn
55
+ aspectRatio: 4 / 5, // reserves the space: width="800" height="1000"
56
+ });
57
+ // spread onto <img>: { src, srcset, sizes, width, height }
58
+ ```
59
+
60
+ `imageSrcset`, `imageUrl(source, 'xl')` and `imageVariantFor(width, density)` are there for the
61
+ parts on their own. Anything that is not an Eldra media asset comes back unchanged. The variants,
62
+ choosing `sizes`, and avoiding layout shift are in [docs/images.md](../../docs/images.md).
63
+
45
64
  ## What is in the client
46
65
 
47
66
  | Group | Methods |
package/dist/index.d.ts CHANGED
@@ -62,9 +62,85 @@ interface EldraTrackerScript {
62
62
  }
63
63
  /**
64
64
  * Builds the tracker `<script>` attributes from the same configuration the client uses, so the
65
- * tracker always reports to the gateway the site reads from. Returns `undefined` when no
66
- * organisation id is configured, which is the case in builds that should not report at all.
65
+ * script comes from the gateway the site reads from and reports there, unless `eventOrigin` names a
66
+ * first-party proxy. Returns `undefined` when no organisation id is configured, which is the case
67
+ * in builds that should not report at all.
67
68
  */
68
69
  export declare function analyticsTrackerScript(options?: EldraTrackerScriptOptions): EldraTrackerScript | undefined;
69
70
  //#endregion
70
- export type { EldraAddCartItemInput, EldraCart, EldraCartClient, EldraCartItem, EldraCartSession, EldraCartSessionOptions, EldraCartTotals, EldraCatalogClient, EldraCatalogGetProductOptions, EldraCatalogListProductsOptions, EldraCategory, EldraCheckoutClient, EldraCheckoutHandoffOptions, EldraClient, EldraClientOptions, EldraCmsClient, EldraCmsEntryList, EldraCmsEntryListDisplayMode, EldraCmsEntryListEntry, EldraCmsEntryListFilterCriterion, EldraCmsGetEntryByUniqueFieldOptions, EldraCmsGetOptions, EldraCmsListOptions, EldraCmsResolveEntryListOptions, EldraCmsResolveEntryListResponse, EldraCmsResolvedEntryList, EldraCollection, EldraCollectionList, EldraCollectionListOptions, EldraCollectionProductsOptions, EldraContract, EldraContractBody, EldraContractItem, EldraContractMethod, EldraContractPath, EldraContractPaths, EldraContractProp, EldraContractQuery, EldraContractResponse, EldraDiscountResult, EldraFeature, EldraFeatureCapabilities, EldraFeatureClient, EldraHttpClient, EldraHttpRequest, EldraInventoryClient, EldraKeyValueStorage, EldraLocaleOptions, EldraOrder, EldraOrderAccessTokenOptions, EldraOrderAccessTokens, EldraOrderLine, EldraOrderReadOptions, EldraOrdersClient, EldraOrganizationDetails, EldraOrganizationFeature, EldraOrganizationOptions, EldraPageMeta, EldraPaginated, EldraProductDetails, EldraProductList, EldraProductListItem, EldraProductMediaLink, EldraProductOption, EldraProductOptionValue, EldraProductThumbnail, EldraProductVariant, EldraRecoveredBasket, EldraRecoveredBasketItem, EldraRequestContext, EldraRequestOptions, EldraSearchOptions, EldraSearchResponse, EldraStockAvailability, EldraStockAvailabilityInput, EldraStockAvailabilityItem, EldraTrackerScript, EldraTrackerScriptOptions, RuntimeEnv, RuntimeValue };
71
+ //#region src/image.d.ts
72
+ /** A size the media host serves by path suffix; `original` is the uploaded file, untransformed. */
73
+ type EldraImageVariant = 'sm' | 'md' | 'lg' | 'xl' | 'full' | 'original';
74
+ /** The variants the media host resizes, narrowest first. */
75
+ type EldraSizedImageVariant = Exclude<EldraImageVariant, 'original'>;
76
+ /**
77
+ * The maximum width in pixels of each resized variant. The host scales down only, so an original
78
+ * narrower than a variant comes back at its own width.
79
+ */
80
+ export declare const ELDRA_IMAGE_VARIANT_WIDTHS: Readonly<Record<EldraSizedImageVariant, number>>;
81
+ /**
82
+ * A media URL as the API returns it, or any object carrying one (`thumbnail`, `mediaLinks[]`, a
83
+ * CMS media item). `contentType`, when present, keeps formats the host does not resize out of
84
+ * `srcset`.
85
+ */
86
+ type EldraImageSource = string | {
87
+ url?: string | null;
88
+ contentType?: string | null;
89
+ } | null | undefined;
90
+ /** Whether the source is an Eldra media asset the host can resize. */
91
+ export declare function isEldraImage(source: EldraImageSource): boolean;
92
+ /**
93
+ * The URL of one variant of an Eldra media asset. A URL that already names a variant is switched
94
+ * to the one asked for. Any other URL — an external image, an SVG, a GIF — comes back unchanged.
95
+ */
96
+ export declare function imageUrl(source: string, variant: EldraImageVariant): string;
97
+ export declare function imageUrl(source: EldraImageSource, variant: EldraImageVariant): string | undefined;
98
+ /**
99
+ * The narrowest variant at least `width` CSS pixels wide at the given pixel density, or `full`
100
+ * when nothing is that wide.
101
+ */
102
+ export declare function imageVariantFor(width: number, density?: number): EldraSizedImageVariant;
103
+ interface EldraImageSrcsetOptions {
104
+ /**
105
+ * The original's width in pixels, when you know it. Variants wider than it would be the same
106
+ * pixels at a new URL, so they are dropped, and the last one is described at its true width.
107
+ */
108
+ originalWidth?: number;
109
+ }
110
+ /**
111
+ * A `srcset` over the resized variants of an Eldra media asset, each described by its width. Returns
112
+ * `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
113
+ */
114
+ export declare function imageSrcset(source: EldraImageSource, options?: EldraImageSrcsetOptions): string | undefined;
115
+ interface EldraResponsiveImageOptions extends EldraImageSrcsetOptions {
116
+ /**
117
+ * How wide the image is laid out, as an `<img sizes>` value: `100vw` for a full-bleed banner,
118
+ * `(min-width: 1024px) 25vw, 50vw` for a four-up grid that is two-up on phones.
119
+ */
120
+ sizes: string;
121
+ /** The variant in `src`, for a browser that ignores `srcset`. Defaults to `md`. */
122
+ variant?: EldraSizedImageVariant;
123
+ /**
124
+ * The width-to-height ratio of the box the image fills, such as `4 / 5`. It becomes `width` and
125
+ * `height` attributes, which reserve the space before the image loads.
126
+ */
127
+ aspectRatio?: number;
128
+ /** Explicit `width` and `height` attributes; they take precedence over `aspectRatio`. */
129
+ width?: number;
130
+ height?: number;
131
+ }
132
+ /** Attributes for an `<img>`; spread them onto the tag. */
133
+ interface EldraResponsiveImage {
134
+ src: string;
135
+ srcset?: string;
136
+ sizes?: string;
137
+ width?: number;
138
+ height?: number;
139
+ }
140
+ /**
141
+ * The `src`, `srcset`, `sizes`, `width` and `height` for one image. An external image gets its URL
142
+ * as `src` and no `srcset`. Returns `undefined` when there is no URL to render.
143
+ */
144
+ export declare function responsiveImage(source: EldraImageSource, options: EldraResponsiveImageOptions): EldraResponsiveImage | undefined;
145
+ //#endregion
146
+ export type { EldraAddCartItemInput, EldraCart, EldraCartClient, EldraCartItem, EldraCartSession, EldraCartSessionOptions, EldraCartTotals, EldraCatalogClient, EldraCatalogGetProductOptions, EldraCatalogListProductsOptions, EldraCategory, EldraCheckoutClient, EldraCheckoutHandoffOptions, EldraClient, EldraClientOptions, EldraCmsClient, EldraCmsEntryList, EldraCmsEntryListDisplayMode, EldraCmsEntryListEntry, EldraCmsEntryListFilterCriterion, EldraCmsGetEntryByUniqueFieldOptions, EldraCmsGetOptions, EldraCmsListOptions, EldraCmsResolveEntryListOptions, EldraCmsResolveEntryListResponse, EldraCmsResolvedEntryList, EldraCollection, EldraCollectionList, EldraCollectionListOptions, EldraCollectionProductsOptions, EldraContract, EldraContractBody, EldraContractItem, EldraContractMethod, EldraContractPath, EldraContractPaths, EldraContractProp, EldraContractQuery, EldraContractResponse, EldraDiscountResult, EldraFeature, EldraFeatureCapabilities, EldraFeatureClient, EldraHttpClient, EldraHttpRequest, EldraImageSource, EldraImageSrcsetOptions, EldraImageVariant, EldraInventoryClient, EldraKeyValueStorage, EldraLocaleOptions, EldraOrder, EldraOrderAccessTokenOptions, EldraOrderAccessTokens, EldraOrderLine, EldraOrderReadOptions, EldraOrdersClient, EldraOrganizationDetails, EldraOrganizationFeature, EldraOrganizationOptions, EldraPageMeta, EldraPaginated, EldraProductDetails, EldraProductList, EldraProductListItem, EldraProductMediaLink, EldraProductOption, EldraProductOptionValue, EldraProductThumbnail, EldraProductVariant, EldraRecoveredBasket, EldraRecoveredBasketItem, EldraRequestContext, EldraRequestOptions, EldraResponsiveImage, EldraResponsiveImageOptions, EldraSearchOptions, EldraSearchResponse, EldraSizedImageVariant, EldraStockAvailability, EldraStockAvailabilityInput, EldraStockAvailabilityItem, EldraTrackerScript, EldraTrackerScriptOptions, RuntimeEnv, RuntimeValue };
package/dist/index.js CHANGED
@@ -50,8 +50,9 @@ function createOrderAccessTokens(options = {}) {
50
50
  //#region src/tracker.ts
51
51
  /**
52
52
  * Builds the tracker `<script>` attributes from the same configuration the client uses, so the
53
- * tracker always reports to the gateway the site reads from. Returns `undefined` when no
54
- * organisation id is configured, which is the case in builds that should not report at all.
53
+ * script comes from the gateway the site reads from and reports there, unless `eventOrigin` names a
54
+ * first-party proxy. Returns `undefined` when no organisation id is configured, which is the case
55
+ * in builds that should not report at all.
55
56
  */
56
57
  function analyticsTrackerScript(options = {}) {
57
58
  const orgId = resolveOrgId(options);
@@ -66,4 +67,98 @@ function analyticsTrackerScript(options = {}) {
66
67
  return script;
67
68
  }
68
69
  //#endregion
69
- export { DEFAULT_ELDRA_API_BASE_URL, DEFAULT_ELDRA_CHECKOUT_URL, EldraHttpError, analyticsTrackerScript, createCartSession, createEldraClient, createOrderAccessTokens, getEldraClient, initEldraClient };
70
+ //#region src/image.ts
71
+ /**
72
+ * The maximum width in pixels of each resized variant. The host scales down only, so an original
73
+ * narrower than a variant comes back at its own width.
74
+ */
75
+ const ELDRA_IMAGE_VARIANT_WIDTHS = {
76
+ sm: 400,
77
+ md: 800,
78
+ lg: 1200,
79
+ xl: 1920,
80
+ full: 2560
81
+ };
82
+ const SIZED_VARIANTS = Object.keys(ELDRA_IMAGE_VARIANT_WIDTHS);
83
+ const UUID = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
84
+ const ELDRA_ASSET_URL = new RegExp(`^((?:(?:https?:)?//[^/\\s,?#]+)?(?:/[^\\s,?#]*)?/public/${UUID}/assets/${UUID})(?:/(?:sm|md|lg|xl|full|original))?/?$`, "i");
85
+ const RESIZABLE_CONTENT_TYPES = /* @__PURE__ */ new Set([
86
+ "image/jpeg",
87
+ "image/png",
88
+ "image/webp",
89
+ "image/avif",
90
+ "image/heic",
91
+ "image/heif"
92
+ ]);
93
+ function sourceUrl(source) {
94
+ const url = typeof source === "string" ? source : source?.url;
95
+ return url ? url : void 0;
96
+ }
97
+ function resizableAssetBase(source) {
98
+ const url = sourceUrl(source);
99
+ if (!url) return void 0;
100
+ const contentType = typeof source === "object" ? source?.contentType : void 0;
101
+ if (contentType && !RESIZABLE_CONTENT_TYPES.has(contentType.toLowerCase())) return void 0;
102
+ return ELDRA_ASSET_URL.exec(url)?.[1];
103
+ }
104
+ /** Whether the source is an Eldra media asset the host can resize. */
105
+ function isEldraImage(source) {
106
+ return resizableAssetBase(source) !== void 0;
107
+ }
108
+ function imageUrl(source, variant) {
109
+ const base = resizableAssetBase(source);
110
+ return base ? `${base}/${variant}` : sourceUrl(source);
111
+ }
112
+ /**
113
+ * The narrowest variant at least `width` CSS pixels wide at the given pixel density, or `full`
114
+ * when nothing is that wide.
115
+ */
116
+ function imageVariantFor(width, density = 1) {
117
+ const needed = width * density;
118
+ if (!Number.isFinite(needed) || needed <= 0) throw new RangeError(`imageVariantFor needs a positive width, got ${width} at ${density}x`);
119
+ return SIZED_VARIANTS.find((variant) => ELDRA_IMAGE_VARIANT_WIDTHS[variant] >= needed) ?? "full";
120
+ }
121
+ /**
122
+ * A `srcset` over the resized variants of an Eldra media asset, each described by its width. Returns
123
+ * `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
124
+ */
125
+ function imageSrcset(source, options = {}) {
126
+ const base = resizableAssetBase(source);
127
+ if (!base) return void 0;
128
+ const { originalWidth } = options;
129
+ const candidates = [];
130
+ for (const variant of SIZED_VARIANTS) {
131
+ const width = ELDRA_IMAGE_VARIANT_WIDTHS[variant];
132
+ if (originalWidth !== void 0 && originalWidth > 0 && width >= originalWidth) {
133
+ candidates.push(`${base}/${variant} ${Math.round(originalWidth)}w`);
134
+ break;
135
+ }
136
+ candidates.push(`${base}/${variant} ${width}w`);
137
+ }
138
+ return candidates.join(", ");
139
+ }
140
+ /**
141
+ * The `src`, `srcset`, `sizes`, `width` and `height` for one image. An external image gets its URL
142
+ * as `src` and no `srcset`. Returns `undefined` when there is no URL to render.
143
+ */
144
+ function responsiveImage(source, options) {
145
+ const variant = options.variant ?? "md";
146
+ const src = imageUrl(source, variant);
147
+ if (!src) return void 0;
148
+ const image = { src };
149
+ const srcset = imageSrcset(source, options);
150
+ if (srcset) {
151
+ image.srcset = srcset;
152
+ image.sizes = options.sizes;
153
+ }
154
+ if (options.width !== void 0 && options.height !== void 0) {
155
+ image.width = options.width;
156
+ image.height = options.height;
157
+ } else if (options.aspectRatio !== void 0 && options.aspectRatio > 0) {
158
+ image.width = ELDRA_IMAGE_VARIANT_WIDTHS[variant];
159
+ image.height = Math.round(image.width / options.aspectRatio);
160
+ }
161
+ return image;
162
+ }
163
+ //#endregion
164
+ export { DEFAULT_ELDRA_API_BASE_URL, DEFAULT_ELDRA_CHECKOUT_URL, ELDRA_IMAGE_VARIANT_WIDTHS, EldraHttpError, analyticsTrackerScript, createCartSession, createEldraClient, createOrderAccessTokens, getEldraClient, imageSrcset, imageUrl, imageVariantFor, initEldraClient, isEldraImage, responsiveImage };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eldrajs/sdk",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Framework-free client for the Eldra Web Studio public storefront API: CMS, catalog, cart, checkout handoff, orders, inventory. Types generated from your gateway's OpenAPI document and CMS schemas at build time.",
5
5
  "keywords": [
6
6
  "cms",