@nonphoto/sanity-image 2.1.0 → 3.0.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/index.d.ts CHANGED
@@ -1,208 +1,63 @@
1
- type ImageUrlBuilderOptions = Partial<SanityProjectDetails> & {
2
- baseUrl?: string;
3
- source?: SanityImageSource;
4
- bg?: string;
5
- dpr?: number;
6
- width?: number;
7
- height?: number;
1
+ type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
2
+ type FitMode = "clip" | "crop" | "fill" | "fillmax" | "max" | "scale" | "min";
3
+ type CropMode = "top" | "bottom" | "left" | "right" | "center" | "focalpoint" | "entropy";
4
+ type AutoMode = "format";
5
+ type Orientation = 0 | 90 | 180 | 270;
6
+ type Dpr = 1 | 2 | 3;
7
+ type SanityImageParams = {
8
+ auto?: AutoMode;
9
+ background?: string;
10
+ blur?: number;
11
+ crop?: CropMode;
12
+ download?: boolean | string;
13
+ dpr?: Dpr;
14
+ fit?: FitMode;
15
+ flipHorizontal?: boolean;
16
+ flipVertical?: boolean;
8
17
  focalPoint?: {
9
18
  x: number;
10
19
  y: number;
11
20
  };
12
- maxWidth?: number;
21
+ format?: ImageFormat;
22
+ frame?: number;
23
+ height?: number;
24
+ invert?: boolean;
13
25
  maxHeight?: number;
14
- minWidth?: number;
26
+ maxWidth?: number;
15
27
  minHeight?: number;
16
- blur?: number;
17
- sharpen?: number;
28
+ minWidth?: number;
29
+ orientation?: Orientation;
30
+ pad?: number;
31
+ quality?: number;
18
32
  rect?: {
19
33
  left: number;
20
34
  top: number;
21
35
  width: number;
22
36
  height: number;
23
37
  };
24
- format?: ImageFormat;
25
- invert?: boolean;
26
- orientation?: Orientation;
27
- quality?: number;
28
- download?: boolean | string;
29
- flipHorizontal?: boolean;
30
- flipVertical?: boolean;
31
- ignoreImageParams?: boolean;
32
- fit?: FitMode;
33
- crop?: CropMode;
34
38
  saturation?: number;
35
- auto?: AutoMode;
36
- pad?: number;
37
- vanityName?: string;
38
- frame?: number;
39
- };
40
- type ImageUrlBuilderOptionsWithAliases = ImageUrlBuilderOptions & {
41
- w?: number;
42
- h?: number;
43
- q?: number;
44
- fm?: number;
45
- dl?: boolean | string;
46
- or?: Orientation;
47
- sharp?: number;
48
- "min-h"?: number;
49
- "max-h"?: number;
50
- "min-w"?: number;
51
- "max-w"?: number;
52
- sat?: number;
53
- [key: string]: any;
54
- };
55
- type ImageUrlBuilderOptionsWithAsset = ImageUrlBuilderOptions & {
56
- asset: {
57
- id: string;
58
- width: number;
59
- height: number;
60
- format: string;
61
- };
62
- [key: string]: any;
63
- };
64
- type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
65
- type FitMode = "clip" | "crop" | "fill" | "fillmax" | "max" | "scale" | "min";
66
- type CropMode = "top" | "bottom" | "left" | "right" | "center" | "focalpoint" | "entropy";
67
- type AutoMode = "format";
68
- type Orientation = 0 | 90 | 180 | 270;
69
- interface SanityClientLike {
70
- clientConfig: {
71
- dataset?: string;
72
- projectId?: string;
73
- apiHost?: string;
74
- };
75
- }
76
- type SanityModernClientLike = {
77
- config(): {
78
- dataset?: string;
79
- projectId?: string;
80
- apiHost?: string;
81
- };
82
- };
83
- declare type SanityImageSource = string | SanityReference | SanityAsset | SanityImageObject | SanityImageWithAssetStub;
84
- interface SanityProjectDetails {
85
- baseUrl?: string;
86
- projectId: string;
87
- dataset: string;
88
- }
89
- interface SanityReference {
90
- _ref: string;
91
- }
92
- interface SanityImageWithAssetStub {
93
- asset: {
94
- url: string;
95
- };
96
- }
97
- interface SanityAsset {
98
- _id?: string;
99
- url?: string;
100
- path?: string;
101
- assetId?: string;
102
- extension?: string;
103
- [key: string]: any;
104
- }
105
- interface SanityImageDimensions {
106
- aspectRatio: number;
107
- height: number;
108
- width: number;
109
- }
110
- interface SanityImageFitResult {
39
+ sharpen?: number;
111
40
  width?: number;
112
- height?: number;
113
- rect: SanityImageRect;
114
- }
115
- interface SanityImageRect {
116
- left: number;
117
- top: number;
118
- width: number;
119
- height: number;
120
- }
121
- interface SanityImageCrop {
122
- _type?: string;
123
- left: number;
124
- bottom: number;
125
- right: number;
126
- top: number;
127
- }
128
- interface SanityImageHotspot {
129
- _type?: string;
130
- width: number;
131
- height: number;
132
- x: number;
133
- y: number;
134
- }
135
- interface SanityImageObject {
136
- asset: SanityReference | SanityAsset;
137
- crop?: SanityImageCrop;
138
- hotspot?: SanityImageHotspot;
139
- }
140
- interface CropSpec {
141
- left: number;
142
- top: number;
143
- width: number;
144
- height: number;
145
- }
146
- interface HotspotSpec {
147
- left: number;
148
- top: number;
149
- right: number;
150
- bottom: number;
151
- }
152
- interface SanityImageObjectLike extends Pick<SanityImageObject, "asset"> {
153
- [key: string]: any;
154
- }
155
- interface SanityImageMetadata {
156
- lqip?: string;
157
- dimensions?: SanityImageDimensions;
158
- [key: string]: any;
159
- }
160
- interface Size {
161
- width: number;
162
- height: number;
163
- }
164
-
165
- declare const defaultWidths: number[];
166
- declare const defaultWidth = 1280;
167
- declare const defaultQuality = 90;
168
- declare const fitComparators: {
169
- cover: (...values: number[]) => number;
170
- contain: (...values: number[]) => number;
171
41
  };
172
- declare function fit(containee: Size, container: Size, mode: keyof typeof fitComparators): {
42
+ declare function sanityImageParamsToSearchParamEntries({ auto, background, blur, crop, download, dpr, fit, flipHorizontal, flipVertical, focalPoint, format, frame, height, invert, maxHeight, maxWidth, minHeight, minWidth, orientation, pad, quality, rect, saturation, sharpen, width, }: SanityImageParams): string[][];
43
+
44
+ interface SanityImageAssetStub {
45
+ id: string;
173
46
  width: number;
174
47
  height: number;
175
- };
176
- declare function isCrop(x: unknown): x is SanityImageCrop;
177
- declare function isHotspot(x: unknown): x is SanityImageHotspot;
178
- declare function isImageObjectLike(x: unknown): x is SanityImageObjectLike;
179
- declare function isImageObject(x: unknown): x is SanityImageObject;
180
- declare function isReference(x: unknown): x is SanityReference;
181
- declare function isAsset(x: unknown): x is SanityAsset;
182
- declare function isDimensions(x: unknown): x is SanityImageDimensions;
183
- declare function isSize(x: unknown): x is Size;
184
- declare function isMetadata(x: unknown): x is SanityImageMetadata;
185
- interface ImageSrcOptions {
186
- client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
187
- source: SanityImageSource;
188
- width: number;
189
- quality?: number;
190
- aspectRatio?: number;
48
+ format: string;
49
+ vanityName?: string;
191
50
  }
192
- declare function imageSrc({ client, source, width, quality, aspectRatio, }: ImageSrcOptions): string;
193
- interface ImageSrcsetOptions {
194
- client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
195
- source: SanityImageSource;
196
- widths?: number[];
197
- quality?: number;
198
- aspectRatio?: number;
51
+ declare function parseSanityImageAssetId(assetId: string): SanityImageAssetStub | undefined;
52
+ declare function sanityImageAssetStub(source: unknown): SanityImageAssetStub | undefined;
53
+ declare function sanityImageAssetId(source: unknown): string | undefined;
54
+
55
+ declare const defaultSrcsetWidths: number[];
56
+ interface SanityClientLike {
57
+ projectId: string;
58
+ dataset: string;
199
59
  }
200
- declare function imageSrcset({ client, source, widths, quality, aspectRatio, }: ImageSrcsetOptions): string;
201
- declare function imageAlt(source: SanityImageSource): string | undefined;
202
- declare function croppedSize(intrinsicSize: Size, crop: SanityImageCrop): Size;
203
- declare function aspectRatio(size: Size): number;
204
- declare function imageMetadata(source: SanityImageSource): SanityImageMetadata | undefined;
205
- declare function imageCroppedSize(source: SanityImageSource): Size | undefined;
206
- declare function imageAspectRatio(source: SanityImageSource): number | undefined;
60
+ declare function sanityImageUrl(client: SanityClientLike, image: SanityImageAssetStub, params?: SanityImageParams): string;
61
+ declare function sanityImageSrcset(client: SanityClientLike, image: SanityImageAssetStub, params?: Omit<SanityImageParams, "width">, widths?: number[]): string;
207
62
 
208
- export { type AutoMode, type CropMode, type CropSpec, type FitMode, type HotspotSpec, type ImageFormat, type ImageSrcOptions, type ImageSrcsetOptions, type ImageUrlBuilderOptions, type ImageUrlBuilderOptionsWithAliases, type ImageUrlBuilderOptionsWithAsset, type Orientation, type SanityAsset, type SanityClientLike, type SanityImageCrop, type SanityImageDimensions, type SanityImageFitResult, type SanityImageHotspot, type SanityImageMetadata, type SanityImageObject, type SanityImageObjectLike, type SanityImageRect, type SanityImageSource, type SanityImageWithAssetStub, type SanityModernClientLike, type SanityProjectDetails, type SanityReference, type Size, aspectRatio, croppedSize, defaultQuality, defaultWidth, defaultWidths, fit, fitComparators, imageAlt, imageAspectRatio, imageCroppedSize, imageMetadata, imageSrc, imageSrcset, isAsset, isCrop, isDimensions, isHotspot, isImageObject, isImageObjectLike, isMetadata, isReference, isSize };
63
+ export { type AutoMode, type CropMode, type Dpr, type FitMode, type ImageFormat, type Orientation, type SanityClientLike, type SanityImageAssetStub, type SanityImageParams, defaultSrcsetWidths, parseSanityImageAssetId, sanityImageAssetId, sanityImageAssetStub, sanityImageParamsToSearchParamEntries, sanityImageSrcset, sanityImageUrl };
package/dist/index.js CHANGED
@@ -1,6 +1,83 @@
1
- // src/index.ts
2
- import imageUrlBuilder from "@sanity/image-url";
3
- var defaultWidths = [
1
+ // src/params.ts
2
+ function sanityImageParamsToSearchParamEntries({
3
+ auto,
4
+ background,
5
+ blur,
6
+ crop,
7
+ download,
8
+ dpr,
9
+ fit,
10
+ flipHorizontal,
11
+ flipVertical,
12
+ focalPoint,
13
+ format,
14
+ frame,
15
+ height,
16
+ invert,
17
+ maxHeight,
18
+ maxWidth,
19
+ minHeight,
20
+ minWidth,
21
+ orientation,
22
+ pad,
23
+ quality,
24
+ rect,
25
+ saturation,
26
+ sharpen,
27
+ width
28
+ }) {
29
+ return Object.entries({
30
+ auto,
31
+ bg: background,
32
+ blur,
33
+ crop,
34
+ dl: download,
35
+ dpr,
36
+ fit,
37
+ flip: [flipHorizontal && "h", flipVertical && "v"].filter(Boolean).join(""),
38
+ fm: format,
39
+ "fp-x": focalPoint?.x,
40
+ "fp-y": focalPoint?.y,
41
+ frame,
42
+ h: height,
43
+ invert,
44
+ "max-h": maxHeight,
45
+ "max-w": maxWidth,
46
+ "min-h": minHeight,
47
+ "min-w": minWidth,
48
+ or: orientation,
49
+ pad,
50
+ q: quality,
51
+ rect: rect ? [rect.left, rect.top, rect.width, rect.height].map(Math.round).join(",") : void 0,
52
+ sat: saturation,
53
+ sharp: sharpen,
54
+ w: width
55
+ }).filter(([, value]) => typeof value === "number" || Boolean(value)).map(([key, value]) => [
56
+ key,
57
+ encodeURIComponent(
58
+ typeof value === "number" ? Math.round(value) : value
59
+ )
60
+ ]);
61
+ }
62
+
63
+ // src/stub.ts
64
+ function parseSanityImageAssetId(assetId) {
65
+ const matches = assetId.match(/^image-(\w+)-(\d+)x(\d+)-(\w+)$/);
66
+ if (matches) {
67
+ const [, id, width, height, format] = matches;
68
+ return { id, width: Number(width), height: Number(height), format };
69
+ }
70
+ }
71
+ function sanityImageAssetStub(source) {
72
+ const id = sanityImageAssetId(source);
73
+ return id ? parseSanityImageAssetId(id) : void 0;
74
+ }
75
+ function sanityImageAssetId(source) {
76
+ return typeof source === "string" ? source : typeof source === "object" && source != null ? "_ref" in source && typeof source._ref === "string" ? source._ref : "_id" in source && typeof source._id === "string" ? source._id : "asset" in source ? sanityImageAssetId(source.asset) : void 0 : void 0;
77
+ }
78
+
79
+ // src/url.ts
80
+ var defaultSrcsetWidths = [
4
81
  6016,
5
82
  // 6K
6
83
  5120,
@@ -32,116 +109,43 @@ var defaultWidths = [
32
109
  360,
33
110
  240
34
111
  ];
35
- var defaultWidth = 1280;
36
- var defaultQuality = 90;
37
- var fitComparators = {
38
- cover: Math.max,
39
- contain: Math.min
40
- };
41
- function fit(containee, container, mode) {
42
- const sx = container.width / containee.width;
43
- const sy = container.height / containee.height;
44
- const s = fitComparators[mode](sx, sy);
45
- return {
46
- width: containee.width * s,
47
- height: containee.height * s
48
- };
49
- }
50
- function isCrop(x) {
51
- return x != null && typeof x == "object" && "top" in x && typeof x.top === "number" && "right" in x && typeof x.right === "number" && "bottom" in x && typeof x.bottom === "number" && "left" in x && typeof x.left === "number" && ("_type" in x ? typeof x._type === "undefined" || typeof x._type === "string" : true);
52
- }
53
- function isHotspot(x) {
54
- return x != null && typeof x == "object" && "width" in x && typeof x.width === "number" && "height" in x && typeof x.height === "number" && "x" in x && typeof x.x === "number" && "y" in x && typeof x.y === "number" && ("_type" in x ? typeof x._type === "undefined" || typeof x._type === "string" : true);
55
- }
56
- function isImageObjectLike(x) {
57
- return x != null && typeof x === "object" && "asset" in x && (isAsset(x.asset) || isReference(x.asset));
58
- }
59
- function isImageObject(x) {
60
- return isImageObjectLike(x) && ("crop" in x ? typeof x.crop === "undefined" || isCrop(x.crop) : true) && ("hotspot" in x ? typeof x.hotspot === "undefined" || isHotspot(x.hotspot) : true);
61
- }
62
- function isReference(x) {
63
- return x != null && typeof x === "object" && "_ref" in x && typeof x._ref === "string";
64
- }
65
- function isAsset(x) {
66
- return x != null && typeof x === "object" && "_type" in x && x._type === "sanity.asset";
67
- }
68
- function isDimensions(x) {
69
- return x != null && typeof x === "object" && "aspectRatio" in x && typeof x.aspectRatio === "number" && "width" in x && typeof x.width === "number" && "height" in x && typeof x.height === "number";
70
- }
71
- function isSize(x) {
72
- return x != null && typeof x === "object" && "width" in x && typeof x.width === "number" && "height" in x && typeof x.height === "number";
73
- }
74
- function isMetadata(x) {
75
- return x != null && typeof x === "object" && "lqip" in x && typeof x.lqip === "string" && "dimensions" in x && isDimensions(x.dimensions);
76
- }
77
- function imageSrc({
78
- client,
79
- source,
80
- width,
81
- quality = defaultQuality,
82
- aspectRatio: aspectRatio2
83
- }) {
84
- const builder = imageUrlBuilder(client).image(source).quality(quality).auto("format").width(width);
85
- return (aspectRatio2 ? builder.height(width * aspectRatio2) : builder).url();
86
- }
87
- function imageSrcset({
88
- client,
89
- source,
90
- widths = defaultWidths,
91
- quality = defaultQuality,
92
- aspectRatio: aspectRatio2
93
- }) {
94
- const builder = imageUrlBuilder(client).image(source).quality(quality).auto("format");
95
- return widths.sort((a, b) => a - b).map((width) => {
96
- const url = (aspectRatio2 ? builder.height(width * aspectRatio2) : builder).width(width).url();
112
+ function sanityImageUrl(client, image, params) {
113
+ const url = new URL(
114
+ [
115
+ `https://cdn.sanity.io/images`,
116
+ client.projectId,
117
+ client.dataset,
118
+ `${image.id}-${image.width}x${image.height}.${image.format}`,
119
+ image.vanityName
120
+ ].filter(Boolean).join("/")
121
+ );
122
+ if (params) {
123
+ url.search = new URLSearchParams(
124
+ sanityImageParamsToSearchParamEntries(params)
125
+ ).toString();
126
+ }
127
+ return url.href;
128
+ }
129
+ function sanityImageSrcset(client, image, params, widths = defaultSrcsetWidths) {
130
+ const aspectRatio = image.height / image.width;
131
+ return [
132
+ ...widths.sort((a, b) => a - b).filter((width) => width < image.width),
133
+ image.width
134
+ ].map((width) => {
135
+ const url = sanityImageUrl(client, image, {
136
+ ...params,
137
+ width,
138
+ height: width * aspectRatio
139
+ });
97
140
  return `${url} ${width}w`;
98
141
  }).join(",");
99
142
  }
100
- function imageAlt(source) {
101
- return isImageObjectLike(source) && "alt" in source && typeof source.alt === "string" ? source.alt : void 0;
102
- }
103
- function croppedSize(intrinsicSize, crop) {
104
- return {
105
- width: intrinsicSize.width - crop.left - crop.right,
106
- height: intrinsicSize.height - crop.top - crop.bottom
107
- };
108
- }
109
- function aspectRatio(size) {
110
- return size.height / size.width;
111
- }
112
- function imageMetadata(source) {
113
- const asset = isImageObjectLike(source) ? source.asset : source;
114
- return isAsset(asset) && isMetadata(asset.metadata) ? asset.metadata : void 0;
115
- }
116
- function imageCroppedSize(source) {
117
- const metadata = imageMetadata(source);
118
- return metadata?.dimensions ? isImageObject(source) && source.crop != null ? croppedSize(metadata.dimensions, source.crop) : metadata.dimensions : void 0;
119
- }
120
- function imageAspectRatio(source) {
121
- const size = imageCroppedSize(source);
122
- return size ? aspectRatio(size) : void 0;
123
- }
124
143
  export {
125
- aspectRatio,
126
- croppedSize,
127
- defaultQuality,
128
- defaultWidth,
129
- defaultWidths,
130
- fit,
131
- fitComparators,
132
- imageAlt,
133
- imageAspectRatio,
134
- imageCroppedSize,
135
- imageMetadata,
136
- imageSrc,
137
- imageSrcset,
138
- isAsset,
139
- isCrop,
140
- isDimensions,
141
- isHotspot,
142
- isImageObject,
143
- isImageObjectLike,
144
- isMetadata,
145
- isReference,
146
- isSize
144
+ defaultSrcsetWidths,
145
+ parseSanityImageAssetId,
146
+ sanityImageAssetId,
147
+ sanityImageAssetStub,
148
+ sanityImageParamsToSearchParamEntries,
149
+ sanityImageSrcset,
150
+ sanityImageUrl
147
151
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nonphoto/sanity-image",
3
- "version": "2.1.0",
3
+ "version": "3.0.0",
4
4
  "author": "Jonas Luebbers <jonas@jonasluebbers.com> (https://www.jonasluebbers.com)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -28,9 +28,6 @@
28
28
  "build": "tsup ./src/index.ts --dts --format esm --clean",
29
29
  "release": "semantic-release"
30
30
  },
31
- "dependencies": {
32
- "@sanity/image-url": "^1.1.0"
33
- },
34
31
  "devDependencies": {
35
32
  "semantic-release": "24.2.7",
36
33
  "tsup": "8.5.0",
package/src/index.ts CHANGED
@@ -1,267 +1,3 @@
1
- import imageUrlBuilder from "@sanity/image-url";
2
- import {
3
- SanityAsset,
4
- SanityClientLike,
5
- SanityImageCrop,
6
- SanityImageDimensions,
7
- SanityImageHotspot,
8
- SanityImageMetadata,
9
- SanityImageObject,
10
- SanityImageObjectLike,
11
- SanityImageSource,
12
- SanityModernClientLike,
13
- SanityProjectDetails,
14
- SanityReference,
15
- Size,
16
- } from "./types";
17
-
18
- export * from "./types";
19
-
20
- export const defaultWidths = [
21
- 6016, // 6K
22
- 5120, // 5K
23
- 4480, // 4.5K
24
- 3840, // 4K
25
- 3200, // QHD+
26
- 2560, // WQXGA
27
- 2048, // QXGA
28
- 1920, // 1080p
29
- 1668, // iPad
30
- 1280, // 720p
31
- 1080, // iPhone 6-8 Plus
32
- 960,
33
- 720, // iPhone 6-8
34
- 640, // 480p
35
- 480,
36
- 360,
37
- 240,
38
- ];
39
-
40
- export const defaultWidth = 1280;
41
-
42
- export const defaultQuality = 90;
43
-
44
- export const fitComparators = {
45
- cover: Math.max,
46
- contain: Math.min,
47
- };
48
-
49
- // TODO: impement comparators for every FitMode
50
- export function fit(
51
- containee: Size,
52
- container: Size,
53
- mode: keyof typeof fitComparators
54
- ) {
55
- const sx = container.width / containee.width;
56
- const sy = container.height / containee.height;
57
- const s = fitComparators[mode](sx, sy);
58
- return {
59
- width: containee.width * s,
60
- height: containee.height * s,
61
- };
62
- }
63
-
64
- export function isCrop(x: unknown): x is SanityImageCrop {
65
- return (
66
- x != null &&
67
- typeof x == "object" &&
68
- "top" in x &&
69
- typeof x.top === "number" &&
70
- "right" in x &&
71
- typeof x.right === "number" &&
72
- "bottom" in x &&
73
- typeof x.bottom === "number" &&
74
- "left" in x &&
75
- typeof x.left === "number" &&
76
- ("_type" in x
77
- ? typeof x._type === "undefined" || typeof x._type === "string"
78
- : true)
79
- );
80
- }
81
-
82
- export function isHotspot(x: unknown): x is SanityImageHotspot {
83
- return (
84
- x != null &&
85
- typeof x == "object" &&
86
- "width" in x &&
87
- typeof x.width === "number" &&
88
- "height" in x &&
89
- typeof x.height === "number" &&
90
- "x" in x &&
91
- typeof x.x === "number" &&
92
- "y" in x &&
93
- typeof x.y === "number" &&
94
- ("_type" in x
95
- ? typeof x._type === "undefined" || typeof x._type === "string"
96
- : true)
97
- );
98
- }
99
-
100
- export function isImageObjectLike(x: unknown): x is SanityImageObjectLike {
101
- return (
102
- x != null &&
103
- typeof x === "object" &&
104
- "asset" in x &&
105
- (isAsset(x.asset) || isReference(x.asset))
106
- );
107
- }
108
-
109
- export function isImageObject(x: unknown): x is SanityImageObject {
110
- return (
111
- isImageObjectLike(x) &&
112
- ("crop" in x ? typeof x.crop === "undefined" || isCrop(x.crop) : true) &&
113
- ("hotspot" in x
114
- ? typeof x.hotspot === "undefined" || isHotspot(x.hotspot)
115
- : true)
116
- );
117
- }
118
-
119
- export function isReference(x: unknown): x is SanityReference {
120
- return (
121
- x != null &&
122
- typeof x === "object" &&
123
- "_ref" in x &&
124
- typeof x._ref === "string"
125
- );
126
- }
127
-
128
- export function isAsset(x: unknown): x is SanityAsset {
129
- return (
130
- x != null &&
131
- typeof x === "object" &&
132
- "_type" in x &&
133
- x._type === "sanity.asset"
134
- );
135
- }
136
-
137
- export function isDimensions(x: unknown): x is SanityImageDimensions {
138
- return (
139
- x != null &&
140
- typeof x === "object" &&
141
- "aspectRatio" in x &&
142
- typeof x.aspectRatio === "number" &&
143
- "width" in x &&
144
- typeof x.width === "number" &&
145
- "height" in x &&
146
- typeof x.height === "number"
147
- );
148
- }
149
-
150
- export function isSize(x: unknown): x is Size {
151
- return (
152
- x != null &&
153
- typeof x === "object" &&
154
- "width" in x &&
155
- typeof x.width === "number" &&
156
- "height" in x &&
157
- typeof x.height === "number"
158
- );
159
- }
160
-
161
- export function isMetadata(x: unknown): x is SanityImageMetadata {
162
- return (
163
- x != null &&
164
- typeof x === "object" &&
165
- "lqip" in x &&
166
- typeof x.lqip === "string" &&
167
- "dimensions" in x &&
168
- isDimensions(x.dimensions)
169
- );
170
- }
171
-
172
- export interface ImageSrcOptions {
173
- client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
174
- source: SanityImageSource;
175
- width: number;
176
- quality?: number;
177
- aspectRatio?: number;
178
- }
179
-
180
- export function imageSrc({
181
- client,
182
- source,
183
- width,
184
- quality = defaultQuality,
185
- aspectRatio,
186
- }: ImageSrcOptions): string {
187
- const builder = imageUrlBuilder(client)
188
- .image(source)
189
- .quality(quality)
190
- .auto("format")
191
- .width(width);
192
- return (aspectRatio ? builder.height(width * aspectRatio) : builder).url();
193
- }
194
-
195
- export interface ImageSrcsetOptions {
196
- client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
197
- source: SanityImageSource;
198
- widths?: number[];
199
- quality?: number;
200
- aspectRatio?: number;
201
- }
202
-
203
- export function imageSrcset({
204
- client,
205
- source,
206
- widths = defaultWidths,
207
- quality = defaultQuality,
208
- aspectRatio,
209
- }: ImageSrcsetOptions): string {
210
- const builder = imageUrlBuilder(client)
211
- .image(source)
212
- .quality(quality)
213
- .auto("format");
214
- return widths
215
- .sort((a, b) => a - b)
216
- .map((width) => {
217
- const url = (aspectRatio ? builder.height(width * aspectRatio) : builder)
218
- .width(width)
219
- .url();
220
- return `${url} ${width}w`;
221
- })
222
- .join(",");
223
- }
224
-
225
- export function imageAlt(source: SanityImageSource): string | undefined {
226
- return isImageObjectLike(source) &&
227
- "alt" in source &&
228
- typeof source.alt === "string"
229
- ? source.alt
230
- : undefined;
231
- }
232
-
233
- export function croppedSize(intrinsicSize: Size, crop: SanityImageCrop): Size {
234
- return {
235
- width: intrinsicSize.width - crop.left - crop.right,
236
- height: intrinsicSize.height - crop.top - crop.bottom,
237
- };
238
- }
239
-
240
- export function aspectRatio(size: Size): number {
241
- return size.height / size.width;
242
- }
243
-
244
- export function imageMetadata(
245
- source: SanityImageSource
246
- ): SanityImageMetadata | undefined {
247
- const asset = isImageObjectLike(source) ? source.asset : source;
248
- return isAsset(asset) && isMetadata(asset.metadata)
249
- ? asset.metadata
250
- : undefined;
251
- }
252
-
253
- export function imageCroppedSize(source: SanityImageSource): Size | undefined {
254
- const metadata = imageMetadata(source);
255
- return metadata?.dimensions
256
- ? isImageObject(source) && source.crop != null
257
- ? croppedSize(metadata.dimensions, source.crop)
258
- : metadata.dimensions
259
- : undefined;
260
- }
261
-
262
- export function imageAspectRatio(
263
- source: SanityImageSource
264
- ): number | undefined {
265
- const size = imageCroppedSize(source);
266
- return size ? aspectRatio(size) : undefined;
267
- }
1
+ export * from "./params";
2
+ export * from "./stub";
3
+ export * from "./url";
package/src/params.ts ADDED
@@ -0,0 +1,118 @@
1
+ export type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
2
+
3
+ export type FitMode =
4
+ | "clip"
5
+ | "crop"
6
+ | "fill"
7
+ | "fillmax"
8
+ | "max"
9
+ | "scale"
10
+ | "min";
11
+
12
+ export type CropMode =
13
+ | "top"
14
+ | "bottom"
15
+ | "left"
16
+ | "right"
17
+ | "center"
18
+ | "focalpoint"
19
+ | "entropy";
20
+
21
+ export type AutoMode = "format";
22
+
23
+ export type Orientation = 0 | 90 | 180 | 270;
24
+
25
+ export type Dpr = 1 | 2 | 3;
26
+
27
+ export type SanityImageParams = {
28
+ auto?: AutoMode;
29
+ background?: string;
30
+ blur?: number;
31
+ crop?: CropMode;
32
+ download?: boolean | string;
33
+ dpr?: Dpr;
34
+ fit?: FitMode;
35
+ flipHorizontal?: boolean;
36
+ flipVertical?: boolean;
37
+ focalPoint?: { x: number; y: number };
38
+ format?: ImageFormat;
39
+ frame?: number;
40
+ height?: number;
41
+ invert?: boolean;
42
+ maxHeight?: number;
43
+ maxWidth?: number;
44
+ minHeight?: number;
45
+ minWidth?: number;
46
+ orientation?: Orientation;
47
+ pad?: number;
48
+ quality?: number;
49
+ rect?: { left: number; top: number; width: number; height: number };
50
+ saturation?: number;
51
+ sharpen?: number;
52
+ width?: number;
53
+ };
54
+
55
+ export function sanityImageParamsToSearchParamEntries({
56
+ auto,
57
+ background,
58
+ blur,
59
+ crop,
60
+ download,
61
+ dpr,
62
+ fit,
63
+ flipHorizontal,
64
+ flipVertical,
65
+ focalPoint,
66
+ format,
67
+ frame,
68
+ height,
69
+ invert,
70
+ maxHeight,
71
+ maxWidth,
72
+ minHeight,
73
+ minWidth,
74
+ orientation,
75
+ pad,
76
+ quality,
77
+ rect,
78
+ saturation,
79
+ sharpen,
80
+ width,
81
+ }: SanityImageParams): string[][] {
82
+ return Object.entries({
83
+ auto,
84
+ bg: background,
85
+ blur,
86
+ crop,
87
+ dl: download,
88
+ dpr,
89
+ fit,
90
+ flip: [flipHorizontal && "h", flipVertical && "v"].filter(Boolean).join(""),
91
+ fm: format,
92
+ "fp-x": focalPoint?.x,
93
+ "fp-y": focalPoint?.y,
94
+ frame,
95
+ h: height,
96
+ invert,
97
+ "max-h": maxHeight,
98
+ "max-w": maxWidth,
99
+ "min-h": minHeight,
100
+ "min-w": minWidth,
101
+ or: orientation,
102
+ pad,
103
+ q: quality,
104
+ rect: rect
105
+ ? [rect.left, rect.top, rect.width, rect.height].map(Math.round).join(",")
106
+ : undefined,
107
+ sat: saturation,
108
+ sharp: sharpen,
109
+ w: width,
110
+ })
111
+ .filter(([, value]) => typeof value === "number" || Boolean(value))
112
+ .map(([key, value]) => [
113
+ key,
114
+ encodeURIComponent(
115
+ typeof value === "number" ? Math.round(value) : (value as string)
116
+ ),
117
+ ]);
118
+ }
package/src/stub.ts ADDED
@@ -0,0 +1,38 @@
1
+ export interface SanityImageAssetStub {
2
+ id: string;
3
+ width: number;
4
+ height: number;
5
+ format: string;
6
+ vanityName?: string;
7
+ }
8
+
9
+ export function parseSanityImageAssetId(
10
+ assetId: string
11
+ ): SanityImageAssetStub | undefined {
12
+ const matches = assetId.match(/^image-(\w+)-(\d+)x(\d+)-(\w+)$/);
13
+ if (matches) {
14
+ const [, id, width, height, format] = matches;
15
+ return { id, width: Number(width), height: Number(height), format };
16
+ }
17
+ }
18
+
19
+ export function sanityImageAssetStub(
20
+ source: unknown
21
+ ): SanityImageAssetStub | undefined {
22
+ const id = sanityImageAssetId(source);
23
+ return id ? parseSanityImageAssetId(id) : undefined;
24
+ }
25
+
26
+ export function sanityImageAssetId(source: unknown): string | undefined {
27
+ return typeof source === "string"
28
+ ? source
29
+ : typeof source === "object" && source != null
30
+ ? "_ref" in source && typeof source._ref === "string"
31
+ ? source._ref
32
+ : "_id" in source && typeof source._id === "string"
33
+ ? source._id
34
+ : "asset" in source
35
+ ? sanityImageAssetId(source.asset)
36
+ : undefined
37
+ : undefined;
38
+ }
package/src/url.ts ADDED
@@ -0,0 +1,76 @@
1
+ import {
2
+ SanityImageParams,
3
+ sanityImageParamsToSearchParamEntries,
4
+ } from "./params";
5
+ import { SanityImageAssetStub } from "./stub";
6
+
7
+ export const defaultSrcsetWidths = [
8
+ 6016, // 6K
9
+ 5120, // 5K
10
+ 4480, // 4.5K
11
+ 3840, // 4K
12
+ 3200, // QHD+
13
+ 2560, // WQXGA
14
+ 2048, // QXGA
15
+ 1920, // 1080p
16
+ 1668, // iPad
17
+ 1280, // 720p
18
+ 1080, // iPhone 6-8 Plus
19
+ 960,
20
+ 720, // iPhone 6-8
21
+ 640, // 480p
22
+ 480,
23
+ 360,
24
+ 240,
25
+ ];
26
+
27
+ export interface SanityClientLike {
28
+ projectId: string;
29
+ dataset: string;
30
+ }
31
+
32
+ export function sanityImageUrl(
33
+ client: SanityClientLike,
34
+ image: SanityImageAssetStub,
35
+ params?: SanityImageParams
36
+ ): string {
37
+ const url = new URL(
38
+ [
39
+ `https://cdn.sanity.io/images`,
40
+ client.projectId,
41
+ client.dataset,
42
+ `${image.id}-${image.width}x${image.height}.${image.format}`,
43
+ image.vanityName,
44
+ ]
45
+ .filter(Boolean)
46
+ .join("/")
47
+ );
48
+ if (params) {
49
+ url.search = new URLSearchParams(
50
+ sanityImageParamsToSearchParamEntries(params)
51
+ ).toString();
52
+ }
53
+ return url.href;
54
+ }
55
+
56
+ export function sanityImageSrcset(
57
+ client: SanityClientLike,
58
+ image: SanityImageAssetStub,
59
+ params?: Omit<SanityImageParams, "width">,
60
+ widths: number[] = defaultSrcsetWidths
61
+ ): string {
62
+ const aspectRatio = image.height / image.width;
63
+ return [
64
+ ...widths.sort((a, b) => a - b).filter((width) => width < image.width),
65
+ image.width,
66
+ ]
67
+ .map((width) => {
68
+ const url = sanityImageUrl(client, image, {
69
+ ...params,
70
+ width,
71
+ height: width * aspectRatio,
72
+ });
73
+ return `${url} ${width}w`;
74
+ })
75
+ .join(",");
76
+ }
package/src/types.ts DELETED
@@ -1,208 +0,0 @@
1
- export type ImageUrlBuilderOptions = Partial<SanityProjectDetails> & {
2
- baseUrl?: string;
3
- source?: SanityImageSource;
4
- bg?: string;
5
- dpr?: number;
6
- width?: number;
7
- height?: number;
8
- focalPoint?: {
9
- x: number;
10
- y: number;
11
- };
12
- maxWidth?: number;
13
- maxHeight?: number;
14
- minWidth?: number;
15
- minHeight?: number;
16
- blur?: number;
17
- sharpen?: number;
18
- rect?: {
19
- left: number;
20
- top: number;
21
- width: number;
22
- height: number;
23
- };
24
- format?: ImageFormat;
25
- invert?: boolean;
26
- orientation?: Orientation;
27
- quality?: number;
28
- download?: boolean | string;
29
- flipHorizontal?: boolean;
30
- flipVertical?: boolean;
31
- ignoreImageParams?: boolean;
32
- fit?: FitMode;
33
- crop?: CropMode;
34
- saturation?: number;
35
- auto?: AutoMode;
36
- pad?: number;
37
- vanityName?: string;
38
- frame?: number;
39
- };
40
-
41
- export type ImageUrlBuilderOptionsWithAliases = ImageUrlBuilderOptions & {
42
- w?: number;
43
- h?: number;
44
- q?: number;
45
- fm?: number;
46
- dl?: boolean | string;
47
- or?: Orientation;
48
- sharp?: number;
49
- "min-h"?: number;
50
- "max-h"?: number;
51
- "min-w"?: number;
52
- "max-w"?: number;
53
- sat?: number;
54
- [key: string]: any;
55
- };
56
-
57
- export type ImageUrlBuilderOptionsWithAsset = ImageUrlBuilderOptions & {
58
- asset: {
59
- id: string;
60
- width: number;
61
- height: number;
62
- format: string;
63
- };
64
- [key: string]: any;
65
- };
66
-
67
- export type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
68
-
69
- export type FitMode =
70
- | "clip"
71
- | "crop"
72
- | "fill"
73
- | "fillmax"
74
- | "max"
75
- | "scale"
76
- | "min";
77
-
78
- export type CropMode =
79
- | "top"
80
- | "bottom"
81
- | "left"
82
- | "right"
83
- | "center"
84
- | "focalpoint"
85
- | "entropy";
86
-
87
- export type AutoMode = "format";
88
-
89
- export type Orientation = 0 | 90 | 180 | 270;
90
-
91
- export interface SanityClientLike {
92
- clientConfig: {
93
- dataset?: string;
94
- projectId?: string;
95
- apiHost?: string;
96
- };
97
- }
98
-
99
- export type SanityModernClientLike = {
100
- config(): {
101
- dataset?: string;
102
- projectId?: string;
103
- apiHost?: string;
104
- };
105
- };
106
-
107
- export declare type SanityImageSource =
108
- | string
109
- | SanityReference
110
- | SanityAsset
111
- | SanityImageObject
112
- | SanityImageWithAssetStub;
113
-
114
- export interface SanityProjectDetails {
115
- baseUrl?: string;
116
- projectId: string;
117
- dataset: string;
118
- }
119
-
120
- export interface SanityReference {
121
- _ref: string;
122
- }
123
-
124
- export interface SanityImageWithAssetStub {
125
- asset: {
126
- url: string;
127
- };
128
- }
129
-
130
- export interface SanityAsset {
131
- _id?: string;
132
- url?: string;
133
- path?: string;
134
- assetId?: string;
135
- extension?: string;
136
- [key: string]: any;
137
- }
138
-
139
- export interface SanityImageDimensions {
140
- aspectRatio: number;
141
- height: number;
142
- width: number;
143
- }
144
-
145
- export interface SanityImageFitResult {
146
- width?: number;
147
- height?: number;
148
- rect: SanityImageRect;
149
- }
150
-
151
- export interface SanityImageRect {
152
- left: number;
153
- top: number;
154
- width: number;
155
- height: number;
156
- }
157
-
158
- export interface SanityImageCrop {
159
- _type?: string;
160
- left: number;
161
- bottom: number;
162
- right: number;
163
- top: number;
164
- }
165
-
166
- export interface SanityImageHotspot {
167
- _type?: string;
168
- width: number;
169
- height: number;
170
- x: number;
171
- y: number;
172
- }
173
-
174
- export interface SanityImageObject {
175
- asset: SanityReference | SanityAsset;
176
- crop?: SanityImageCrop;
177
- hotspot?: SanityImageHotspot;
178
- }
179
-
180
- export interface CropSpec {
181
- left: number;
182
- top: number;
183
- width: number;
184
- height: number;
185
- }
186
-
187
- export interface HotspotSpec {
188
- left: number;
189
- top: number;
190
- right: number;
191
- bottom: number;
192
- }
193
-
194
- export interface SanityImageObjectLike
195
- extends Pick<SanityImageObject, "asset"> {
196
- [key: string]: any;
197
- }
198
-
199
- export interface SanityImageMetadata {
200
- lqip?: string;
201
- dimensions?: SanityImageDimensions;
202
- [key: string]: any;
203
- }
204
-
205
- export interface Size {
206
- width: number;
207
- height: number;
208
- }