@nonphoto/sanity-image 3.0.0 → 3.2.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,3 +1,18 @@
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
+
1
16
  type ImageFormat = "jpg" | "pjpg" | "png" | "webp";
2
17
  type FitMode = "clip" | "crop" | "fill" | "fillmax" | "max" | "scale" | "min";
3
18
  type CropMode = "top" | "bottom" | "left" | "right" | "center" | "focalpoint" | "entropy";
@@ -41,6 +56,7 @@ type SanityImageParams = {
41
56
  };
42
57
  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
58
 
59
+ type SanityImageSource = SanityImageObject | SanityReference | SanityImageAssetLike | string;
44
60
  interface SanityImageAssetStub {
45
61
  id: string;
46
62
  width: number;
@@ -49,8 +65,8 @@ interface SanityImageAssetStub {
49
65
  vanityName?: string;
50
66
  }
51
67
  declare function parseSanityImageAssetId(assetId: string): SanityImageAssetStub | undefined;
52
- declare function sanityImageAssetStub(source: unknown): SanityImageAssetStub | undefined;
53
- declare function sanityImageAssetId(source: unknown): string | undefined;
68
+ declare function sanityImageAssetId(source: SanityImageSource): string;
69
+ declare function sanityImageAssetStub(source: SanityImageSource): SanityImageAssetStub | undefined;
54
70
 
55
71
  declare const defaultSrcsetWidths: number[];
56
72
  interface SanityClientLike {
@@ -60,4 +76,4 @@ interface SanityClientLike {
60
76
  declare function sanityImageUrl(client: SanityClientLike, image: SanityImageAssetStub, params?: SanityImageParams): string;
61
77
  declare function sanityImageSrcset(client: SanityClientLike, image: SanityImageAssetStub, params?: Omit<SanityImageParams, "width">, widths?: number[]): string;
62
78
 
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 };
79
+ export { type AutoMode, type CropMode, type Dpr, type FitMode, type ImageFormat, type Orientation, type SanityClientLike, type SanityImageAssetLike, type SanityImageAssetStub, type SanityImageObject, 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nonphoto/sanity-image",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "author": "Jonas Luebbers <jonas@jonasluebbers.com> (https://www.jonasluebbers.com)",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/asset.ts ADDED
@@ -0,0 +1,12 @@
1
+ export interface SanityImageAssetLike {
2
+ _id: string;
3
+ }
4
+
5
+ export function isSanityImageAssetLike(x: any): x is SanityImageAssetLike {
6
+ return (
7
+ x != null &&
8
+ typeof x === "object" &&
9
+ "_id" in x &&
10
+ typeof x._id === "string"
11
+ );
12
+ }
@@ -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
@@ -1,3 +1,6 @@
1
+ export * from "./asset";
2
+ export * from "./imageObject";
1
3
  export * from "./params";
4
+ export * from "./reference";
2
5
  export * from "./stub";
3
6
  export * from "./url";
@@ -0,0 +1,12 @@
1
+ export interface SanityReference {
2
+ _ref: string;
3
+ }
4
+
5
+ export function isSanityReference(x: any): x is SanityReference {
6
+ return (
7
+ x != null &&
8
+ typeof x === "object" &&
9
+ "_ref" in x &&
10
+ typeof x._ref === "string"
11
+ );
12
+ }
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: unknown
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
- }