@eldrajs/sdk 0.3.1 → 0.3.2
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 +14 -0
- package/README.md +2 -1
- package/dist/index.d.ts +9 -7
- package/dist/index.js +22 -4
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,20 @@ platform repository.
|
|
|
9
9
|
|
|
10
10
|
## Unreleased
|
|
11
11
|
|
|
12
|
+
- **Images use the size the API gives them (contract 4.14.0).** `EldraImageSource` accepts the
|
|
13
|
+
`width` and `height` an image object now carries (its pixels as displayed). `imageSrcset` stops at
|
|
14
|
+
that width unless `originalWidth` is passed, and `responsiveImage` sets `width`/`height` to the
|
|
15
|
+
`src` variant's own size (the original scaled to the variant, never enlarged) when neither
|
|
16
|
+
`width`/`height` nor `aspectRatio` is given; both of those still win. An image without them
|
|
17
|
+
behaves as before.
|
|
18
|
+
|
|
19
|
+
- **`imageSrcset` lists the original when it is wider than `full`.** When the original's width is
|
|
20
|
+
known (the API's `width`, or `originalWidth`) and above 2560 px, the `srcset` ends with
|
|
21
|
+
`…/original <width>w`, so a zoomed crop that needs more than 2560 px is sharp. The original is
|
|
22
|
+
served as uploaded, so a browser picks it only when no variant is wide enough. Never for HEIC,
|
|
23
|
+
which most browsers cannot show as uploaded. A narrower original still ends the list at its own
|
|
24
|
+
width.
|
|
25
|
+
|
|
12
26
|
- **`buildToken` client option (WS-403).** When set (a string or a function returning one), every
|
|
13
27
|
request carries `X-Eldra-Build-Token`, a read-only build token (`eldra_bld_…`, created in Studio → Settings → Developers → Build tokens),
|
|
14
28
|
so the gateway can give a theme build's reads their own rate-limit bucket. Server and build only;
|
package/README.md
CHANGED
|
@@ -45,7 +45,8 @@ precedence.
|
|
|
45
45
|
## Images
|
|
46
46
|
|
|
47
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
|
|
48
|
+
serves (400 to 2560 px, then the original when it is wider) so the browser picks one that is sharp
|
|
49
|
+
on the screen in front of it:
|
|
49
50
|
|
|
50
51
|
```ts
|
|
51
52
|
import { responsiveImage } from '@eldrajs/sdk';
|
package/dist/index.d.ts
CHANGED
|
@@ -87,11 +87,13 @@ export declare const ELDRA_IMAGE_VARIANT_WIDTHS: Readonly<Record<EldraSizedImage
|
|
|
87
87
|
/**
|
|
88
88
|
* A media URL as the API returns it, or any object carrying one (`thumbnail`, `mediaLinks[]`, a
|
|
89
89
|
* CMS media item). `contentType`, when present, keeps formats the host does not resize out of
|
|
90
|
-
* `srcset
|
|
90
|
+
* `srcset`; `width` and `height`, when present, are the displayed pixel size (docs/images.md).
|
|
91
91
|
*/
|
|
92
92
|
type EldraImageSource = string | {
|
|
93
93
|
url?: string | null;
|
|
94
94
|
contentType?: string | null;
|
|
95
|
+
width?: number | null;
|
|
96
|
+
height?: number | null;
|
|
95
97
|
} | null | undefined;
|
|
96
98
|
/** Whether the source is an Eldra media asset the host can resize. */
|
|
97
99
|
export declare function isEldraImage(source: EldraImageSource): boolean;
|
|
@@ -108,14 +110,14 @@ export declare function imageUrl(source: EldraImageSource, variant: EldraImageVa
|
|
|
108
110
|
export declare function imageVariantFor(width: number, density?: number): EldraSizedImageVariant;
|
|
109
111
|
interface EldraImageSrcsetOptions {
|
|
110
112
|
/**
|
|
111
|
-
* The original's width in pixels,
|
|
112
|
-
*
|
|
113
|
+
* The original's width in pixels, by default the source's `width`. Wider variants are dropped and
|
|
114
|
+
* the last is described at its true width; an original wider than `full` is listed itself, last.
|
|
113
115
|
*/
|
|
114
116
|
originalWidth?: number;
|
|
115
117
|
}
|
|
116
118
|
/**
|
|
117
|
-
* A `srcset` over the resized variants of an Eldra media asset,
|
|
118
|
-
* `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
|
|
119
|
+
* A `srcset` over the resized variants of an Eldra media asset, and the original when it is wider.
|
|
120
|
+
* Returns `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
|
|
119
121
|
*/
|
|
120
122
|
export declare function imageSrcset(source: EldraImageSource, options?: EldraImageSrcsetOptions): string | undefined;
|
|
121
123
|
interface EldraResponsiveImageOptions extends EldraImageSrcsetOptions {
|
|
@@ -128,10 +130,10 @@ interface EldraResponsiveImageOptions extends EldraImageSrcsetOptions {
|
|
|
128
130
|
variant?: EldraSizedImageVariant;
|
|
129
131
|
/**
|
|
130
132
|
* The width-to-height ratio of the box the image fills, such as `4 / 5`. It becomes `width` and
|
|
131
|
-
* `height` attributes, which reserve the space before the image loads.
|
|
133
|
+
* `height` attributes, which reserve the space before the image loads, in place of the source's size.
|
|
132
134
|
*/
|
|
133
135
|
aspectRatio?: number;
|
|
134
|
-
/** Explicit `width` and `height` attributes; they take precedence over
|
|
136
|
+
/** Explicit `width` and `height` attributes; they take precedence over everything else. */
|
|
135
137
|
width?: number;
|
|
136
138
|
height?: number;
|
|
137
139
|
}
|
package/dist/index.js
CHANGED
|
@@ -94,6 +94,15 @@ function sourceUrl(source) {
|
|
|
94
94
|
const url = typeof source === "string" ? source : source?.url;
|
|
95
95
|
return url ? url : void 0;
|
|
96
96
|
}
|
|
97
|
+
const isSide = (n) => typeof n === "number" && n > 0 && n < Infinity;
|
|
98
|
+
/** The size the API gave the image, when it gave both sides. */
|
|
99
|
+
function sourceSize(source) {
|
|
100
|
+
const s = typeof source === "object" ? source : void 0;
|
|
101
|
+
return s && isSide(s.width) && isSide(s.height) ? {
|
|
102
|
+
width: s.width,
|
|
103
|
+
height: s.height
|
|
104
|
+
} : void 0;
|
|
105
|
+
}
|
|
97
106
|
function resizableAssetBase(source) {
|
|
98
107
|
const url = sourceUrl(source);
|
|
99
108
|
if (!url) return void 0;
|
|
@@ -118,23 +127,28 @@ function imageVariantFor(width, density = 1) {
|
|
|
118
127
|
if (!Number.isFinite(needed) || needed <= 0) throw new RangeError(`imageVariantFor needs a positive width, got ${width} at ${density}x`);
|
|
119
128
|
return SIZED_VARIANTS.find((variant) => ELDRA_IMAGE_VARIANT_WIDTHS[variant] >= needed) ?? "full";
|
|
120
129
|
}
|
|
130
|
+
function originalDisplays(source) {
|
|
131
|
+
const contentType = typeof source === "object" ? source?.contentType?.toLowerCase() : void 0;
|
|
132
|
+
return contentType !== "image/heic" && contentType !== "image/heif";
|
|
133
|
+
}
|
|
121
134
|
/**
|
|
122
|
-
* A `srcset` over the resized variants of an Eldra media asset,
|
|
123
|
-
* `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
|
|
135
|
+
* A `srcset` over the resized variants of an Eldra media asset, and the original when it is wider.
|
|
136
|
+
* Returns `undefined` for anything else, so `<img :srcset>` simply drops the attribute.
|
|
124
137
|
*/
|
|
125
138
|
function imageSrcset(source, options = {}) {
|
|
126
139
|
const base = resizableAssetBase(source);
|
|
127
140
|
if (!base) return void 0;
|
|
128
|
-
const
|
|
141
|
+
const originalWidth = options.originalWidth ?? sourceSize(source)?.width;
|
|
129
142
|
const candidates = [];
|
|
130
143
|
for (const variant of SIZED_VARIANTS) {
|
|
131
144
|
const width = ELDRA_IMAGE_VARIANT_WIDTHS[variant];
|
|
132
145
|
if (originalWidth !== void 0 && originalWidth > 0 && width >= originalWidth) {
|
|
133
146
|
candidates.push(`${base}/${variant} ${Math.round(originalWidth)}w`);
|
|
134
|
-
|
|
147
|
+
return candidates.join(", ");
|
|
135
148
|
}
|
|
136
149
|
candidates.push(`${base}/${variant} ${width}w`);
|
|
137
150
|
}
|
|
151
|
+
if (isSide(originalWidth) && Math.round(originalWidth) > ELDRA_IMAGE_VARIANT_WIDTHS.full && originalDisplays(source)) candidates.push(`${base}/original ${Math.round(originalWidth)}w`);
|
|
138
152
|
return candidates.join(", ");
|
|
139
153
|
}
|
|
140
154
|
/**
|
|
@@ -151,12 +165,16 @@ function responsiveImage(source, options) {
|
|
|
151
165
|
image.srcset = srcset;
|
|
152
166
|
image.sizes = options.sizes;
|
|
153
167
|
}
|
|
168
|
+
const size = sourceSize(source);
|
|
154
169
|
if (options.width !== void 0 && options.height !== void 0) {
|
|
155
170
|
image.width = options.width;
|
|
156
171
|
image.height = options.height;
|
|
157
172
|
} else if (options.aspectRatio !== void 0 && options.aspectRatio > 0) {
|
|
158
173
|
image.width = ELDRA_IMAGE_VARIANT_WIDTHS[variant];
|
|
159
174
|
image.height = Math.round(image.width / options.aspectRatio);
|
|
175
|
+
} else if (size) {
|
|
176
|
+
image.width = srcset ? Math.min(ELDRA_IMAGE_VARIANT_WIDTHS[variant], size.width) : size.width;
|
|
177
|
+
image.height = Math.round(image.width * size.height / size.width) || 1;
|
|
160
178
|
}
|
|
161
179
|
return image;
|
|
162
180
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eldrajs/sdk",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.2",
|
|
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",
|