@nonphoto/sanity-image 3.0.0 → 3.1.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 +52 -3
- package/dist/index.js +21 -3
- package/package.json +1 -1
- package/src/asset.ts +12 -0
- package/src/imageObject.ts +15 -0
- package/src/index.ts +4 -0
- package/src/metadata.ts +35 -0
- package/src/reference.ts +12 -0
- package/src/stub.ts +21 -15
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,51 @@
|
|
|
1
|
+
interface SanityImageAssetLike {
|
|
2
|
+
_id: string;
|
|
3
|
+
}
|
|
4
|
+
declare function isSanityImageAssetLike(x: any): x is SanityImageAssetLike;
|
|
5
|
+
|
|
6
|
+
interface SanityReference {
|
|
7
|
+
_ref: string;
|
|
8
|
+
}
|
|
9
|
+
declare function isSanityReference(x: any): x is SanityReference;
|
|
10
|
+
|
|
11
|
+
interface SanityImageObject {
|
|
12
|
+
asset: SanityImageAssetLike | SanityReference;
|
|
13
|
+
}
|
|
14
|
+
declare function isSanityImageObject(x: any): x is SanityImageObject;
|
|
15
|
+
|
|
16
|
+
interface SanityImagePaletteSwatch {
|
|
17
|
+
_type?: "sanity.imagePaletteSwatch" | null;
|
|
18
|
+
background?: string | null;
|
|
19
|
+
foreground?: string | null;
|
|
20
|
+
population?: number | null;
|
|
21
|
+
title?: string | null;
|
|
22
|
+
}
|
|
23
|
+
interface SanityImagePalette {
|
|
24
|
+
_type?: "sanity.imagePalette" | null;
|
|
25
|
+
darkMuted?: SanityImagePaletteSwatch | null;
|
|
26
|
+
darkVibrant?: SanityImagePaletteSwatch | null;
|
|
27
|
+
dominant?: SanityImagePaletteSwatch | null;
|
|
28
|
+
lightMuted?: SanityImagePaletteSwatch | null;
|
|
29
|
+
lightVibrant?: SanityImagePaletteSwatch | null;
|
|
30
|
+
muted?: SanityImagePaletteSwatch | null;
|
|
31
|
+
vibrant?: SanityImagePaletteSwatch | null;
|
|
32
|
+
}
|
|
33
|
+
interface SanityImageDimensions {
|
|
34
|
+
_type?: "sanity.imageDimensions" | null;
|
|
35
|
+
aspectRatio?: number | null;
|
|
36
|
+
width?: number | null;
|
|
37
|
+
height?: number | null;
|
|
38
|
+
}
|
|
39
|
+
interface SanityImageMetadata {
|
|
40
|
+
_type: "sanity.imageMetadata";
|
|
41
|
+
blurHash?: string | null;
|
|
42
|
+
dimensions?: SanityImageDimensions | null;
|
|
43
|
+
hasAlpha?: boolean | null;
|
|
44
|
+
isOpaque?: boolean | null;
|
|
45
|
+
lqip?: string | null;
|
|
46
|
+
palette?: SanityImagePalette | null;
|
|
47
|
+
}
|
|
48
|
+
|
|
1
49
|
type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
|
|
2
50
|
type FitMode = "clip" | "crop" | "fill" | "fillmax" | "max" | "scale" | "min";
|
|
3
51
|
type CropMode = "top" | "bottom" | "left" | "right" | "center" | "focalpoint" | "entropy";
|
|
@@ -41,6 +89,7 @@ type SanityImageParams = {
|
|
|
41
89
|
};
|
|
42
90
|
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
91
|
|
|
92
|
+
type SanityImageSource = SanityImageObject | SanityReference | SanityImageAssetLike | string;
|
|
44
93
|
interface SanityImageAssetStub {
|
|
45
94
|
id: string;
|
|
46
95
|
width: number;
|
|
@@ -49,8 +98,8 @@ interface SanityImageAssetStub {
|
|
|
49
98
|
vanityName?: string;
|
|
50
99
|
}
|
|
51
100
|
declare function parseSanityImageAssetId(assetId: string): SanityImageAssetStub | undefined;
|
|
52
|
-
declare function
|
|
53
|
-
declare function
|
|
101
|
+
declare function sanityImageAssetId(source: SanityImageSource): string;
|
|
102
|
+
declare function sanityImageAssetStub(source: SanityImageSource): SanityImageAssetStub | undefined;
|
|
54
103
|
|
|
55
104
|
declare const defaultSrcsetWidths: number[];
|
|
56
105
|
interface SanityClientLike {
|
|
@@ -60,4 +109,4 @@ interface SanityClientLike {
|
|
|
60
109
|
declare function sanityImageUrl(client: SanityClientLike, image: SanityImageAssetStub, params?: SanityImageParams): string;
|
|
61
110
|
declare function sanityImageSrcset(client: SanityClientLike, image: SanityImageAssetStub, params?: Omit<SanityImageParams, "width">, widths?: number[]): string;
|
|
62
111
|
|
|
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 };
|
|
112
|
+
export { type AutoMode, type CropMode, type Dpr, type FitMode, type ImageFormat, type Orientation, type SanityClientLike, type SanityImageAssetLike, type SanityImageAssetStub, type SanityImageDimensions, type SanityImageMetadata, type SanityImageObject, type SanityImagePalette, type SanityImagePaletteSwatch, type SanityImageParams, type SanityImageSource, type SanityReference, defaultSrcsetWidths, isSanityImageAssetLike, isSanityImageObject, isSanityReference, parseSanityImageAssetId, sanityImageAssetId, sanityImageAssetStub, sanityImageParamsToSearchParamEntries, sanityImageSrcset, sanityImageUrl };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
// src/asset.ts
|
|
2
|
+
function isSanityImageAssetLike(x) {
|
|
3
|
+
return x != null && typeof x === "object" && "_id" in x && typeof x._id === "string";
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/reference.ts
|
|
7
|
+
function isSanityReference(x) {
|
|
8
|
+
return x != null && typeof x === "object" && "_ref" in x && typeof x._ref === "string";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// src/imageObject.ts
|
|
12
|
+
function isSanityImageObject(x) {
|
|
13
|
+
return x != null && typeof x === "object" && "asset" in x && (isSanityImageAssetLike(x.asset) || isSanityReference(x.asset));
|
|
14
|
+
}
|
|
15
|
+
|
|
1
16
|
// src/params.ts
|
|
2
17
|
function sanityImageParamsToSearchParamEntries({
|
|
3
18
|
auto,
|
|
@@ -68,13 +83,13 @@ function parseSanityImageAssetId(assetId) {
|
|
|
68
83
|
return { id, width: Number(width), height: Number(height), format };
|
|
69
84
|
}
|
|
70
85
|
}
|
|
86
|
+
function sanityImageAssetId(source) {
|
|
87
|
+
return typeof source === "string" ? source : isSanityReference(source) ? source._ref : isSanityImageAssetLike(source) ? source._id : sanityImageAssetId(source.asset);
|
|
88
|
+
}
|
|
71
89
|
function sanityImageAssetStub(source) {
|
|
72
90
|
const id = sanityImageAssetId(source);
|
|
73
91
|
return id ? parseSanityImageAssetId(id) : void 0;
|
|
74
92
|
}
|
|
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
93
|
|
|
79
94
|
// src/url.ts
|
|
80
95
|
var defaultSrcsetWidths = [
|
|
@@ -142,6 +157,9 @@ function sanityImageSrcset(client, image, params, widths = defaultSrcsetWidths)
|
|
|
142
157
|
}
|
|
143
158
|
export {
|
|
144
159
|
defaultSrcsetWidths,
|
|
160
|
+
isSanityImageAssetLike,
|
|
161
|
+
isSanityImageObject,
|
|
162
|
+
isSanityReference,
|
|
145
163
|
parseSanityImageAssetId,
|
|
146
164
|
sanityImageAssetId,
|
|
147
165
|
sanityImageAssetStub,
|
package/package.json
CHANGED
package/src/asset.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { isSanityImageAssetLike, SanityImageAssetLike } from "./asset";
|
|
2
|
+
import { isSanityReference, SanityReference } from "./reference";
|
|
3
|
+
|
|
4
|
+
export interface SanityImageObject {
|
|
5
|
+
asset: SanityImageAssetLike | SanityReference;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function isSanityImageObject(x: any): x is SanityImageObject {
|
|
9
|
+
return (
|
|
10
|
+
x != null &&
|
|
11
|
+
typeof x === "object" &&
|
|
12
|
+
"asset" in x &&
|
|
13
|
+
(isSanityImageAssetLike(x.asset) || isSanityReference(x.asset))
|
|
14
|
+
);
|
|
15
|
+
}
|
package/src/index.ts
CHANGED
package/src/metadata.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface SanityImagePaletteSwatch {
|
|
2
|
+
_type?: "sanity.imagePaletteSwatch" | null;
|
|
3
|
+
background?: string | null;
|
|
4
|
+
foreground?: string | null;
|
|
5
|
+
population?: number | null;
|
|
6
|
+
title?: string | null;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface SanityImagePalette {
|
|
10
|
+
_type?: "sanity.imagePalette" | null;
|
|
11
|
+
darkMuted?: SanityImagePaletteSwatch | null;
|
|
12
|
+
darkVibrant?: SanityImagePaletteSwatch | null;
|
|
13
|
+
dominant?: SanityImagePaletteSwatch | null;
|
|
14
|
+
lightMuted?: SanityImagePaletteSwatch | null;
|
|
15
|
+
lightVibrant?: SanityImagePaletteSwatch | null;
|
|
16
|
+
muted?: SanityImagePaletteSwatch | null;
|
|
17
|
+
vibrant?: SanityImagePaletteSwatch | null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SanityImageDimensions {
|
|
21
|
+
_type?: "sanity.imageDimensions" | null;
|
|
22
|
+
aspectRatio?: number | null;
|
|
23
|
+
width?: number | null;
|
|
24
|
+
height?: number | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SanityImageMetadata {
|
|
28
|
+
_type: "sanity.imageMetadata";
|
|
29
|
+
blurHash?: string | null;
|
|
30
|
+
dimensions?: SanityImageDimensions | null;
|
|
31
|
+
hasAlpha?: boolean | null;
|
|
32
|
+
isOpaque?: boolean | null;
|
|
33
|
+
lqip?: string | null;
|
|
34
|
+
palette?: SanityImagePalette | null;
|
|
35
|
+
}
|
package/src/reference.ts
ADDED
package/src/stub.ts
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
import { isSanityImageAssetLike, SanityImageAssetLike } from "./asset";
|
|
2
|
+
import { SanityImageObject } from "./imageObject";
|
|
3
|
+
import { isSanityReference, SanityReference } from "./reference";
|
|
4
|
+
|
|
5
|
+
export type SanityImageSource =
|
|
6
|
+
| SanityImageObject
|
|
7
|
+
| SanityReference
|
|
8
|
+
| SanityImageAssetLike
|
|
9
|
+
| string;
|
|
10
|
+
|
|
1
11
|
export interface SanityImageAssetStub {
|
|
2
12
|
id: string;
|
|
3
13
|
width: number;
|
|
@@ -16,23 +26,19 @@ export function parseSanityImageAssetId(
|
|
|
16
26
|
}
|
|
17
27
|
}
|
|
18
28
|
|
|
29
|
+
export function sanityImageAssetId(source: SanityImageSource): string {
|
|
30
|
+
return typeof source === "string"
|
|
31
|
+
? source
|
|
32
|
+
: isSanityReference(source)
|
|
33
|
+
? source._ref
|
|
34
|
+
: isSanityImageAssetLike(source)
|
|
35
|
+
? source._id
|
|
36
|
+
: sanityImageAssetId(source.asset);
|
|
37
|
+
}
|
|
38
|
+
|
|
19
39
|
export function sanityImageAssetStub(
|
|
20
|
-
source:
|
|
40
|
+
source: SanityImageSource
|
|
21
41
|
): SanityImageAssetStub | undefined {
|
|
22
42
|
const id = sanityImageAssetId(source);
|
|
23
43
|
return id ? parseSanityImageAssetId(id) : undefined;
|
|
24
44
|
}
|
|
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
|
-
}
|