@starlightcms/js-sdk 3.2.0 → 4.0.0-beta.1

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.
@@ -9,7 +9,7 @@ import { VisualData } from './visual';
9
9
  *
10
10
  * @group Data Fields
11
11
  */
12
- export type BooleanField = boolean;
12
+ export type BooleanField = boolean | undefined;
13
13
  /**
14
14
  * Represents an HTML Field returned by the API.
15
15
  *
@@ -19,7 +19,7 @@ export type BooleanField = boolean;
19
19
  *
20
20
  * @group Data Fields
21
21
  */
22
- export type HtmlField = string;
22
+ export type HtmlField = string | undefined;
23
23
  /**
24
24
  * Represents a Media Field returned by the API.
25
25
  *
@@ -29,7 +29,7 @@ export type HtmlField = string;
29
29
  *
30
30
  * @group Data Fields
31
31
  */
32
- export type MediaField = MediaObject;
32
+ export type MediaField = MediaObject | undefined;
33
33
  /**
34
34
  * Represents a Relation Field returned by the API.
35
35
  *
@@ -39,7 +39,7 @@ export type MediaField = MediaObject;
39
39
  *
40
40
  * @group Data Fields
41
41
  */
42
- export type RelationField<T> = Relation<T>;
42
+ export type RelationField<T> = Relation<T> | undefined;
43
43
  /**
44
44
  * Represents a String Field returned by the API.
45
45
  *
@@ -49,7 +49,7 @@ export type RelationField<T> = Relation<T>;
49
49
  *
50
50
  * @group Data Fields
51
51
  */
52
- export type StringField = string;
52
+ export type StringField = string | undefined;
53
53
  /**
54
54
  * Represents a Text Field returned by the API.
55
55
  *
@@ -59,7 +59,7 @@ export type StringField = string;
59
59
  *
60
60
  * @group Data Fields
61
61
  */
62
- export type TextField = string;
62
+ export type TextField = string | undefined;
63
63
  /**
64
64
  * Represents a Visual Field returned by the API.
65
65
  *
@@ -69,5 +69,5 @@ export type TextField = string;
69
69
  *
70
70
  * @group Data Fields
71
71
  */
72
- export type VisualField = VisualData;
72
+ export type VisualField = VisualData | undefined;
73
73
  //# sourceMappingURL=fields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,CAAA;AAElC;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,CAAA;AAEpC;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAA;AAEhC;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,CAAA"}
1
+ {"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,SAAS,CAAA;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,GAAG,SAAS,CAAA;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,CAAA;AAEtD;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,SAAS,CAAA;AAE5C;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,SAAS,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"fields.js","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"","sourcesContent":["import { MediaObject, Relation } from './entities'\nimport { VisualData } from './visual'\n\n/**\n * Represents a Boolean Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type BooleanField = boolean\n\n/**\n * Represents an HTML Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type HtmlField = string\n\n/**\n * Represents a Media Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type MediaField = MediaObject\n\n/**\n * Represents a Relation Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type RelationField<T> = Relation<T>\n\n/**\n * Represents a String Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type StringField = string\n\n/**\n * Represents a Text Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type TextField = string\n\n/**\n * Represents a Visual Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type VisualField = VisualData\n"]}
1
+ {"version":3,"file":"fields.js","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"","sourcesContent":["import { MediaObject, Relation } from './entities'\nimport { VisualData } from './visual'\n\n/**\n * Represents a Boolean Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type BooleanField = boolean | undefined\n\n/**\n * Represents an HTML Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type HtmlField = string | undefined\n\n/**\n * Represents a Media Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type MediaField = MediaObject | undefined\n\n/**\n * Represents a Relation Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type RelationField<T> = Relation<T> | undefined\n\n/**\n * Represents a String Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type StringField = string | undefined\n\n/**\n * Represents a Text Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type TextField = string | undefined\n\n/**\n * Represents a Visual Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type VisualField = VisualData | undefined\n"]}
@@ -7,17 +7,7 @@
7
7
  *
8
8
  * @group Data Groups
9
9
  */
10
- export type Group<Fields extends Record<string, unknown>> = Fields;
11
- /**
12
- * Represents a Legacy Group returned by the API.
13
- *
14
- * Group types are used to type Entry and Singleton objects when requesting
15
- * them using some SDK methods. See {@apilink DefaultModelDefinition}
16
- * for more info.
17
- *
18
- * @group Data Groups
19
- */
20
- export type LegacyGroup<Fields extends Record<string, unknown>> = Fields;
10
+ export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
21
11
  /**
22
12
  * Represents a Repeater Group returned by the API.
23
13
  *
@@ -32,5 +22,5 @@ export type LegacyGroup<Fields extends Record<string, unknown>> = Fields;
32
22
  *
33
23
  * @group Data Groups
34
24
  */
35
- export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null;
25
+ export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null | undefined;
36
26
  //# sourceMappingURL=groups.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"groups.d.ts","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,CAAA;AAElE;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,CAAA;AAExE;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,MAAM,EAAE,GACR,IAAI,CAAA"}
1
+ {"version":3,"file":"groups.d.ts","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,GAAG,SAAS,CAAA;AAE9E;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,MAAM,EAAE,GACR,IAAI,GACJ,SAAS,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"groups.js","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents a Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type Group<Fields extends Record<string, unknown>> = Fields\n\n/**\n * Represents a Legacy Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type LegacyGroup<Fields extends Record<string, unknown>> = Fields\n\n/**\n * Represents a Repeater Group returned by the API.\n *\n * Note that the API return empty repeaters as `null`.\n *\n * To retrieve the type of a `RepeaterGroup` item, use the\n * {@apilink RepeaterItem} utility type.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type RepeaterGroup<Fields extends Record<string, unknown>> =\n | Fields[]\n | null\n"]}
1
+ {"version":3,"file":"groups.js","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents a Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type Group<Fields extends Record<string, unknown>> = Fields | undefined\n\n/**\n * Represents a Repeater Group returned by the API.\n *\n * Note that the API return empty repeaters as `null`.\n *\n * To retrieve the type of a `RepeaterGroup` item, use the\n * {@apilink RepeaterItem} utility type.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type RepeaterGroup<Fields extends Record<string, unknown>> =\n | Fields[]\n | null\n | undefined\n"]}
@@ -6,7 +6,7 @@ import { RepeaterGroup } from './groups';
6
6
  * the `Foo` type. This is useful to "pick" the type of the content held
7
7
  * by items of a Repeater Group.
8
8
  *
9
- * @group Utilities
9
+ * @group Utility Types
10
10
  */
11
11
  export type RepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
12
12
  //# sourceMappingURL=utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"","sourcesContent":["import { RepeaterGroup } from './groups'\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * For instance, given a `RepeaterGroup<Foo>`, this utility would return\n * the `Foo` type. This is useful to \"pick\" the type of the content held\n * by items of a Repeater Group.\n *\n * @group Utilities\n */\nexport type RepeaterItem<\n Repeater extends RepeaterGroup<Record<string, unknown>>,\n> = Repeater extends readonly (infer ItemType)[] ? ItemType : never\n"]}
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"","sourcesContent":["import { RepeaterGroup } from './groups'\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * For instance, given a `RepeaterGroup<Foo>`, this utility would return\n * the `Foo` type. This is useful to \"pick\" the type of the content held\n * by items of a Repeater Group.\n *\n * @group Utility Types\n */\nexport type RepeaterItem<\n Repeater extends RepeaterGroup<Record<string, unknown>>,\n> = Repeater extends readonly (infer ItemType)[] ? ItemType : never\n"]}
@@ -1,4 +1,72 @@
1
1
  import { MediaFile, MediaObject } from '../types';
2
- export declare const getMediaFile: (media: MediaObject, variation?: string) => MediaFile;
3
- export declare const getMediaSource: (media: MediaObject, variation?: string) => string;
2
+ /**
3
+ * Returns the given variation of the provided MediaObject.
4
+ *
5
+ * @param media The MediaObject to analyze. If undefined, the function will also
6
+ * return undefined.
7
+ * @param variation A string or array of strings with the variation name that
8
+ * should be returned. If an array is given, the first variation found will
9
+ * be returned.
10
+ *
11
+ * @returns The MediaFile of the first given variation found. If the provided
12
+ * variations weren't found, or if no variation parameter was provided, returns
13
+ * the optimized variation file (if it exists) or the original file. Returns
14
+ * undefined if no media object is provided.
15
+ *
16
+ * @example Getting the optimized or original file of the given MediaObject.
17
+ * ```ts
18
+ * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'
19
+ *
20
+ * const response = await Starlight.posts.entries.get('foo')
21
+ *
22
+ * // `info.featured_image` is the path of an arbitrary Media content field.
23
+ * const file = getMediaFile(response.data.data.info.featured_image)
24
+ * ```
25
+ *
26
+ * @category Media Utilities
27
+ */
28
+ export declare const getMediaFile: (media?: MediaObject, variation?: string | string[]) => MediaFile | undefined;
29
+ /**
30
+ * Returns the source URL of the optimized file (if it exists) or the original
31
+ * file of the given MediaObject. If the variation parameter is provided,
32
+ * returns the source URL of that variation instead, if it exists, but falls
33
+ * back to the optimized or original files if it doesn't.
34
+ *
35
+ * @param media The MediaObject to analyze. If undefined, the function will also
36
+ * return undefined.
37
+ * @param variation A string or array of strings with the variation name that
38
+ * should be returned. If an array is given, the first variation found will
39
+ * be returned.
40
+ *
41
+ * @returns The source URL of the first given variation found. If the provided
42
+ * variations weren't found, or if no variation parameter was provided, returns
43
+ * the optimized variation source URL (if it exists) or the original source URL.
44
+ * Returns undefined if no media object is provided.
45
+ *
46
+ * @example Getting the optimized or original source URL of the given MediaObject.
47
+ * ```ts
48
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
49
+ *
50
+ * const response = await Starlight.posts.entries.get('foo')
51
+ *
52
+ * // `content.background` is the path of an arbitrary Media content field.
53
+ * const imageUrl = getMediaSource(response.data.data.content.background)
54
+ * ```
55
+ *
56
+ * @example Getting the source URL of a specific variation of the given MediaObject.
57
+ * ```ts
58
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
59
+ *
60
+ * const response = await Starlight.posts.entries.get('foo')
61
+ *
62
+ * // `content.background` is the path of an arbitrary Media content field.
63
+ * const imageUrl = getMediaSource(
64
+ * response.data.data.content.background,
65
+ * ['large', 'medium']
66
+ * )
67
+ * ```
68
+ *
69
+ * @category Media Utilities
70
+ */
71
+ export declare function getMediaSource(media?: MediaObject, variation?: string | string[]): string | undefined;
4
72
  //# sourceMappingURL=image.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AASjD,eAAO,MAAM,YAAY,GACvB,OAAO,WAAW,EAClB,YAAY,MAAM,KACjB,SASF,CAAA;AAED,eAAO,MAAM,cAAc,GACzB,OAAO,WAAW,EAClB,YAAY,MAAM,KACjB,MAEF,CAAA"}
1
+ {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAejD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,YAAY,GACvB,QAAQ,WAAW,EACnB,YAAY,MAAM,GAAG,MAAM,EAAE,KAC5B,SAAS,GAAG,SAgBd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,cAAc,CAC5B,KAAK,CAAC,EAAE,WAAW,EACnB,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,sBAG9B"}
@@ -1,20 +1,101 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMediaSource = exports.getMediaFile = void 0;
3
+ exports.getMediaFile = void 0;
4
+ exports.getMediaSource = getMediaSource;
5
+ /**
6
+ * Returns the optimized variation file of the given MediaObject
7
+ * or the original file if the optimized variation doesn't exist.
8
+ *
9
+ * @param media The MediaObject to analyze.
10
+ */
4
11
  const getOptimizedOrOriginal = (media) => {
5
12
  var _a;
6
13
  return ((_a = media.files.find((file) => file.variation === 'optimized')) !== null && _a !== void 0 ? _a : media.files.find((file) => file.variation === 'original'));
7
14
  };
15
+ /**
16
+ * Returns the given variation of the provided MediaObject.
17
+ *
18
+ * @param media The MediaObject to analyze. If undefined, the function will also
19
+ * return undefined.
20
+ * @param variation A string or array of strings with the variation name that
21
+ * should be returned. If an array is given, the first variation found will
22
+ * be returned.
23
+ *
24
+ * @returns The MediaFile of the first given variation found. If the provided
25
+ * variations weren't found, or if no variation parameter was provided, returns
26
+ * the optimized variation file (if it exists) or the original file. Returns
27
+ * undefined if no media object is provided.
28
+ *
29
+ * @example Getting the optimized or original file of the given MediaObject.
30
+ * ```ts
31
+ * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'
32
+ *
33
+ * const response = await Starlight.posts.entries.get('foo')
34
+ *
35
+ * // `info.featured_image` is the path of an arbitrary Media content field.
36
+ * const file = getMediaFile(response.data.data.info.featured_image)
37
+ * ```
38
+ *
39
+ * @category Media Utilities
40
+ */
8
41
  const getMediaFile = (media, variation) => {
9
- var _a;
42
+ if (!media)
43
+ return undefined;
10
44
  if (variation) {
11
- return ((_a = media.files.find((file) => file.variation === variation)) !== null && _a !== void 0 ? _a : getOptimizedOrOriginal(media));
45
+ const variationArray = Array.isArray(variation) ? variation : [variation];
46
+ for (const variationName of variationArray) {
47
+ const file = media.files.find((file) => file.variation === variationName);
48
+ if (file) {
49
+ return file;
50
+ }
51
+ }
12
52
  }
13
53
  return getOptimizedOrOriginal(media);
14
54
  };
15
55
  exports.getMediaFile = getMediaFile;
16
- const getMediaSource = (media, variation) => {
17
- return (0, exports.getMediaFile)(media, variation).path;
18
- };
19
- exports.getMediaSource = getMediaSource;
56
+ /**
57
+ * Returns the source URL of the optimized file (if it exists) or the original
58
+ * file of the given MediaObject. If the variation parameter is provided,
59
+ * returns the source URL of that variation instead, if it exists, but falls
60
+ * back to the optimized or original files if it doesn't.
61
+ *
62
+ * @param media The MediaObject to analyze. If undefined, the function will also
63
+ * return undefined.
64
+ * @param variation A string or array of strings with the variation name that
65
+ * should be returned. If an array is given, the first variation found will
66
+ * be returned.
67
+ *
68
+ * @returns The source URL of the first given variation found. If the provided
69
+ * variations weren't found, or if no variation parameter was provided, returns
70
+ * the optimized variation source URL (if it exists) or the original source URL.
71
+ * Returns undefined if no media object is provided.
72
+ *
73
+ * @example Getting the optimized or original source URL of the given MediaObject.
74
+ * ```ts
75
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
76
+ *
77
+ * const response = await Starlight.posts.entries.get('foo')
78
+ *
79
+ * // `content.background` is the path of an arbitrary Media content field.
80
+ * const imageUrl = getMediaSource(response.data.data.content.background)
81
+ * ```
82
+ *
83
+ * @example Getting the source URL of a specific variation of the given MediaObject.
84
+ * ```ts
85
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
86
+ *
87
+ * const response = await Starlight.posts.entries.get('foo')
88
+ *
89
+ * // `content.background` is the path of an arbitrary Media content field.
90
+ * const imageUrl = getMediaSource(
91
+ * response.data.data.content.background,
92
+ * ['large', 'medium']
93
+ * )
94
+ * ```
95
+ *
96
+ * @category Media Utilities
97
+ */
98
+ function getMediaSource(media, variation) {
99
+ return media ? (0, exports.getMediaFile)(media, variation).path : undefined;
100
+ }
20
101
  //# sourceMappingURL=image.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":";;;AAEA,MAAM,sBAAsB,GAAG,CAAC,KAAkB,EAAE,EAAE;;IACpD,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAAC,mCACzD,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,UAAU,CAAe,CACzE,CAAA;AACH,CAAC,CAAA;AAEM,MAAM,YAAY,GAAG,CAC1B,KAAkB,EAClB,SAAkB,EACP,EAAE;;IACb,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,SAAS,CAAC,mCACxD,sBAAsB,CAAC,KAAK,CAAC,CAC9B,CAAA;IACH,CAAC;IAED,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAA;AACtC,CAAC,CAAA;AAZY,QAAA,YAAY,gBAYxB;AAEM,MAAM,cAAc,GAAG,CAC5B,KAAkB,EAClB,SAAkB,EACV,EAAE;IACV,OAAO,IAAA,oBAAY,EAAC,KAAK,EAAE,SAAS,CAAC,CAAC,IAAI,CAAA;AAC5C,CAAC,CAAA;AALY,QAAA,cAAc,kBAK1B","sourcesContent":["import { MediaFile, MediaObject } from '../types'\n\nconst getOptimizedOrOriginal = (media: MediaObject) => {\n return (\n media.files.find((file) => file.variation === 'optimized') ??\n (media.files.find((file) => file.variation === 'original') as MediaFile)\n )\n}\n\nexport const getMediaFile = (\n media: MediaObject,\n variation?: string\n): MediaFile => {\n if (variation) {\n return (\n media.files.find((file) => file.variation === variation) ??\n getOptimizedOrOriginal(media)\n )\n }\n\n return getOptimizedOrOriginal(media)\n}\n\nexport const getMediaSource = (\n media: MediaObject,\n variation?: string\n): string => {\n return getMediaFile(media, variation).path\n}\n"]}
1
+ {"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":";;;AAwGA,wCAKC;AA3GD;;;;;GAKG;AACH,MAAM,sBAAsB,GAAG,CAAC,KAAkB,EAAE,EAAE;;IACpD,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAAC,mCACzD,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,UAAU,CAAe,CACzE,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACI,MAAM,YAAY,GAAG,CAC1B,KAAmB,EACnB,SAA6B,EACN,EAAE;IACzB,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAA;IAE5B,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;QAEzE,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;YAC3C,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,aAAa,CAAC,CAAA;YAEzE,IAAI,IAAI,EAAE,CAAC;gBACT,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAA;AACtC,CAAC,CAAA;AAnBY,QAAA,YAAY,gBAmBxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,SAAgB,cAAc,CAC5B,KAAmB,EACnB,SAA6B;IAE7B,OAAO,KAAK,CAAC,CAAC,CAAC,IAAA,oBAAY,EAAC,KAAK,EAAE,SAAS,CAAE,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;AACjE,CAAC","sourcesContent":["import { MediaFile, MediaObject } from '../types'\n\n/**\n * Returns the optimized variation file of the given MediaObject\n * or the original file if the optimized variation doesn't exist.\n *\n * @param media The MediaObject to analyze.\n */\nconst getOptimizedOrOriginal = (media: MediaObject) => {\n return (\n media.files.find((file) => file.variation === 'optimized') ??\n (media.files.find((file) => file.variation === 'original') as MediaFile)\n )\n}\n\n/**\n * Returns the given variation of the provided MediaObject.\n *\n * @param media The MediaObject to analyze. If undefined, the function will also\n * return undefined.\n * @param variation A string or array of strings with the variation name that\n * should be returned. If an array is given, the first variation found will\n * be returned.\n *\n * @returns The MediaFile of the first given variation found. If the provided\n * variations weren't found, or if no variation parameter was provided, returns\n * the optimized variation file (if it exists) or the original file. Returns\n * undefined if no media object is provided.\n *\n * @example Getting the optimized or original file of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `info.featured_image` is the path of an arbitrary Media content field.\n * const file = getMediaFile(response.data.data.info.featured_image)\n * ```\n *\n * @category Media Utilities\n */\nexport const getMediaFile = (\n media?: MediaObject,\n variation?: string | string[],\n): MediaFile | undefined => {\n if (!media) return undefined\n\n if (variation) {\n const variationArray = Array.isArray(variation) ? variation : [variation]\n\n for (const variationName of variationArray) {\n const file = media.files.find((file) => file.variation === variationName)\n\n if (file) {\n return file\n }\n }\n }\n\n return getOptimizedOrOriginal(media)\n}\n\n/**\n * Returns the source URL of the optimized file (if it exists) or the original\n * file of the given MediaObject. If the variation parameter is provided,\n * returns the source URL of that variation instead, if it exists, but falls\n * back to the optimized or original files if it doesn't.\n *\n * @param media The MediaObject to analyze. If undefined, the function will also\n * return undefined.\n * @param variation A string or array of strings with the variation name that\n * should be returned. If an array is given, the first variation found will\n * be returned.\n *\n * @returns The source URL of the first given variation found. If the provided\n * variations weren't found, or if no variation parameter was provided, returns\n * the optimized variation source URL (if it exists) or the original source URL.\n * Returns undefined if no media object is provided.\n *\n * @example Getting the optimized or original source URL of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `content.background` is the path of an arbitrary Media content field.\n * const imageUrl = getMediaSource(response.data.data.content.background)\n * ```\n *\n * @example Getting the source URL of a specific variation of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `content.background` is the path of an arbitrary Media content field.\n * const imageUrl = getMediaSource(\n * response.data.data.content.background,\n * ['large', 'medium']\n * )\n * ```\n *\n * @category Media Utilities\n */\nexport function getMediaSource(\n media?: MediaObject,\n variation?: string | string[],\n) {\n return media ? getMediaFile(media, variation)!.path : undefined\n}\n"]}
@@ -9,7 +9,7 @@ import { VisualData } from './visual';
9
9
  *
10
10
  * @group Data Fields
11
11
  */
12
- export type BooleanField = boolean;
12
+ export type BooleanField = boolean | undefined;
13
13
  /**
14
14
  * Represents an HTML Field returned by the API.
15
15
  *
@@ -19,7 +19,7 @@ export type BooleanField = boolean;
19
19
  *
20
20
  * @group Data Fields
21
21
  */
22
- export type HtmlField = string;
22
+ export type HtmlField = string | undefined;
23
23
  /**
24
24
  * Represents a Media Field returned by the API.
25
25
  *
@@ -29,7 +29,7 @@ export type HtmlField = string;
29
29
  *
30
30
  * @group Data Fields
31
31
  */
32
- export type MediaField = MediaObject;
32
+ export type MediaField = MediaObject | undefined;
33
33
  /**
34
34
  * Represents a Relation Field returned by the API.
35
35
  *
@@ -39,7 +39,7 @@ export type MediaField = MediaObject;
39
39
  *
40
40
  * @group Data Fields
41
41
  */
42
- export type RelationField<T> = Relation<T>;
42
+ export type RelationField<T> = Relation<T> | undefined;
43
43
  /**
44
44
  * Represents a String Field returned by the API.
45
45
  *
@@ -49,7 +49,7 @@ export type RelationField<T> = Relation<T>;
49
49
  *
50
50
  * @group Data Fields
51
51
  */
52
- export type StringField = string;
52
+ export type StringField = string | undefined;
53
53
  /**
54
54
  * Represents a Text Field returned by the API.
55
55
  *
@@ -59,7 +59,7 @@ export type StringField = string;
59
59
  *
60
60
  * @group Data Fields
61
61
  */
62
- export type TextField = string;
62
+ export type TextField = string | undefined;
63
63
  /**
64
64
  * Represents a Visual Field returned by the API.
65
65
  *
@@ -69,5 +69,5 @@ export type TextField = string;
69
69
  *
70
70
  * @group Data Fields
71
71
  */
72
- export type VisualField = VisualData;
72
+ export type VisualField = VisualData | undefined;
73
73
  //# sourceMappingURL=fields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,CAAA;AAElC;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,CAAA;AAEpC;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,CAAA;AAEhC;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,CAAA;AAE9B;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,CAAA"}
1
+ {"version":3,"file":"fields.d.ts","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAClD,OAAO,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAErC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,OAAO,GAAG,SAAS,CAAA;AAE9C;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,WAAW,GAAG,SAAS,CAAA;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,CAAC,CAAC,IAAI,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,CAAA;AAEtD;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,SAAS,CAAA;AAE5C;;;;;;;;GAQG;AACH,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,SAAS,CAAA;AAE1C;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,GAAG,UAAU,GAAG,SAAS,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"fields.js","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"","sourcesContent":["import { MediaObject, Relation } from './entities'\nimport { VisualData } from './visual'\n\n/**\n * Represents a Boolean Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type BooleanField = boolean\n\n/**\n * Represents an HTML Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type HtmlField = string\n\n/**\n * Represents a Media Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type MediaField = MediaObject\n\n/**\n * Represents a Relation Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type RelationField<T> = Relation<T>\n\n/**\n * Represents a String Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type StringField = string\n\n/**\n * Represents a Text Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type TextField = string\n\n/**\n * Represents a Visual Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type VisualField = VisualData\n"]}
1
+ {"version":3,"file":"fields.js","sourceRoot":"","sources":["../../../src/types/fields.ts"],"names":[],"mappings":"","sourcesContent":["import { MediaObject, Relation } from './entities'\nimport { VisualData } from './visual'\n\n/**\n * Represents a Boolean Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type BooleanField = boolean | undefined\n\n/**\n * Represents an HTML Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type HtmlField = string | undefined\n\n/**\n * Represents a Media Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type MediaField = MediaObject | undefined\n\n/**\n * Represents a Relation Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type RelationField<T> = Relation<T> | undefined\n\n/**\n * Represents a String Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type StringField = string | undefined\n\n/**\n * Represents a Text Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type TextField = string | undefined\n\n/**\n * Represents a Visual Field returned by the API.\n *\n * Field types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Fields\n */\nexport type VisualField = VisualData | undefined\n"]}
@@ -7,17 +7,7 @@
7
7
  *
8
8
  * @group Data Groups
9
9
  */
10
- export type Group<Fields extends Record<string, unknown>> = Fields;
11
- /**
12
- * Represents a Legacy Group returned by the API.
13
- *
14
- * Group types are used to type Entry and Singleton objects when requesting
15
- * them using some SDK methods. See {@apilink DefaultModelDefinition}
16
- * for more info.
17
- *
18
- * @group Data Groups
19
- */
20
- export type LegacyGroup<Fields extends Record<string, unknown>> = Fields;
10
+ export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
21
11
  /**
22
12
  * Represents a Repeater Group returned by the API.
23
13
  *
@@ -32,5 +22,5 @@ export type LegacyGroup<Fields extends Record<string, unknown>> = Fields;
32
22
  *
33
23
  * @group Data Groups
34
24
  */
35
- export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null;
25
+ export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null | undefined;
36
26
  //# sourceMappingURL=groups.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"groups.d.ts","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,CAAA;AAElE;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,CAAA;AAExE;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,MAAM,EAAE,GACR,IAAI,CAAA"}
1
+ {"version":3,"file":"groups.d.ts","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,MAAM,MAAM,KAAK,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,GAAG,SAAS,CAAA;AAE9E;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,MAAM,EAAE,GACR,IAAI,GACJ,SAAS,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"groups.js","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents a Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type Group<Fields extends Record<string, unknown>> = Fields\n\n/**\n * Represents a Legacy Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type LegacyGroup<Fields extends Record<string, unknown>> = Fields\n\n/**\n * Represents a Repeater Group returned by the API.\n *\n * Note that the API return empty repeaters as `null`.\n *\n * To retrieve the type of a `RepeaterGroup` item, use the\n * {@apilink RepeaterItem} utility type.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type RepeaterGroup<Fields extends Record<string, unknown>> =\n | Fields[]\n | null\n"]}
1
+ {"version":3,"file":"groups.js","sourceRoot":"","sources":["../../../src/types/groups.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Represents a Group returned by the API.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type Group<Fields extends Record<string, unknown>> = Fields | undefined\n\n/**\n * Represents a Repeater Group returned by the API.\n *\n * Note that the API return empty repeaters as `null`.\n *\n * To retrieve the type of a `RepeaterGroup` item, use the\n * {@apilink RepeaterItem} utility type.\n *\n * Group types are used to type Entry and Singleton objects when requesting\n * them using some SDK methods. See {@apilink DefaultModelDefinition}\n * for more info.\n *\n * @group Data Groups\n */\nexport type RepeaterGroup<Fields extends Record<string, unknown>> =\n | Fields[]\n | null\n | undefined\n"]}
@@ -6,7 +6,7 @@ import { RepeaterGroup } from './groups';
6
6
  * the `Foo` type. This is useful to "pick" the type of the content held
7
7
  * by items of a Repeater Group.
8
8
  *
9
- * @group Utilities
9
+ * @group Utility Types
10
10
  */
11
11
  export type RepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
12
12
  //# sourceMappingURL=utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"","sourcesContent":["import { RepeaterGroup } from './groups'\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * For instance, given a `RepeaterGroup<Foo>`, this utility would return\n * the `Foo` type. This is useful to \"pick\" the type of the content held\n * by items of a Repeater Group.\n *\n * @group Utilities\n */\nexport type RepeaterItem<\n Repeater extends RepeaterGroup<Record<string, unknown>>,\n> = Repeater extends readonly (infer ItemType)[] ? ItemType : never\n"]}
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"","sourcesContent":["import { RepeaterGroup } from './groups'\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * For instance, given a `RepeaterGroup<Foo>`, this utility would return\n * the `Foo` type. This is useful to \"pick\" the type of the content held\n * by items of a Repeater Group.\n *\n * @group Utility Types\n */\nexport type RepeaterItem<\n Repeater extends RepeaterGroup<Record<string, unknown>>,\n> = Repeater extends readonly (infer ItemType)[] ? ItemType : never\n"]}
@@ -1,4 +1,72 @@
1
1
  import { MediaFile, MediaObject } from '../types';
2
- export declare const getMediaFile: (media: MediaObject, variation?: string) => MediaFile;
3
- export declare const getMediaSource: (media: MediaObject, variation?: string) => string;
2
+ /**
3
+ * Returns the given variation of the provided MediaObject.
4
+ *
5
+ * @param media The MediaObject to analyze. If undefined, the function will also
6
+ * return undefined.
7
+ * @param variation A string or array of strings with the variation name that
8
+ * should be returned. If an array is given, the first variation found will
9
+ * be returned.
10
+ *
11
+ * @returns The MediaFile of the first given variation found. If the provided
12
+ * variations weren't found, or if no variation parameter was provided, returns
13
+ * the optimized variation file (if it exists) or the original file. Returns
14
+ * undefined if no media object is provided.
15
+ *
16
+ * @example Getting the optimized or original file of the given MediaObject.
17
+ * ```ts
18
+ * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'
19
+ *
20
+ * const response = await Starlight.posts.entries.get('foo')
21
+ *
22
+ * // `info.featured_image` is the path of an arbitrary Media content field.
23
+ * const file = getMediaFile(response.data.data.info.featured_image)
24
+ * ```
25
+ *
26
+ * @category Media Utilities
27
+ */
28
+ export declare const getMediaFile: (media?: MediaObject, variation?: string | string[]) => MediaFile | undefined;
29
+ /**
30
+ * Returns the source URL of the optimized file (if it exists) or the original
31
+ * file of the given MediaObject. If the variation parameter is provided,
32
+ * returns the source URL of that variation instead, if it exists, but falls
33
+ * back to the optimized or original files if it doesn't.
34
+ *
35
+ * @param media The MediaObject to analyze. If undefined, the function will also
36
+ * return undefined.
37
+ * @param variation A string or array of strings with the variation name that
38
+ * should be returned. If an array is given, the first variation found will
39
+ * be returned.
40
+ *
41
+ * @returns The source URL of the first given variation found. If the provided
42
+ * variations weren't found, or if no variation parameter was provided, returns
43
+ * the optimized variation source URL (if it exists) or the original source URL.
44
+ * Returns undefined if no media object is provided.
45
+ *
46
+ * @example Getting the optimized or original source URL of the given MediaObject.
47
+ * ```ts
48
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
49
+ *
50
+ * const response = await Starlight.posts.entries.get('foo')
51
+ *
52
+ * // `content.background` is the path of an arbitrary Media content field.
53
+ * const imageUrl = getMediaSource(response.data.data.content.background)
54
+ * ```
55
+ *
56
+ * @example Getting the source URL of a specific variation of the given MediaObject.
57
+ * ```ts
58
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
59
+ *
60
+ * const response = await Starlight.posts.entries.get('foo')
61
+ *
62
+ * // `content.background` is the path of an arbitrary Media content field.
63
+ * const imageUrl = getMediaSource(
64
+ * response.data.data.content.background,
65
+ * ['large', 'medium']
66
+ * )
67
+ * ```
68
+ *
69
+ * @category Media Utilities
70
+ */
71
+ export declare function getMediaSource(media?: MediaObject, variation?: string | string[]): string | undefined;
4
72
  //# sourceMappingURL=image.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AASjD,eAAO,MAAM,YAAY,GACvB,OAAO,WAAW,EAClB,YAAY,MAAM,KACjB,SASF,CAAA;AAED,eAAO,MAAM,cAAc,GACzB,OAAO,WAAW,EAClB,YAAY,MAAM,KACjB,MAEF,CAAA"}
1
+ {"version":3,"file":"image.d.ts","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAejD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,eAAO,MAAM,YAAY,GACvB,QAAQ,WAAW,EACnB,YAAY,MAAM,GAAG,MAAM,EAAE,KAC5B,SAAS,GAAG,SAgBd,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,cAAc,CAC5B,KAAK,CAAC,EAAE,WAAW,EACnB,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,sBAG9B"}
@@ -1,15 +1,96 @@
1
+ /**
2
+ * Returns the optimized variation file of the given MediaObject
3
+ * or the original file if the optimized variation doesn't exist.
4
+ *
5
+ * @param media The MediaObject to analyze.
6
+ */
1
7
  const getOptimizedOrOriginal = (media) => {
2
8
  var _a;
3
9
  return ((_a = media.files.find((file) => file.variation === 'optimized')) !== null && _a !== void 0 ? _a : media.files.find((file) => file.variation === 'original'));
4
10
  };
11
+ /**
12
+ * Returns the given variation of the provided MediaObject.
13
+ *
14
+ * @param media The MediaObject to analyze. If undefined, the function will also
15
+ * return undefined.
16
+ * @param variation A string or array of strings with the variation name that
17
+ * should be returned. If an array is given, the first variation found will
18
+ * be returned.
19
+ *
20
+ * @returns The MediaFile of the first given variation found. If the provided
21
+ * variations weren't found, or if no variation parameter was provided, returns
22
+ * the optimized variation file (if it exists) or the original file. Returns
23
+ * undefined if no media object is provided.
24
+ *
25
+ * @example Getting the optimized or original file of the given MediaObject.
26
+ * ```ts
27
+ * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'
28
+ *
29
+ * const response = await Starlight.posts.entries.get('foo')
30
+ *
31
+ * // `info.featured_image` is the path of an arbitrary Media content field.
32
+ * const file = getMediaFile(response.data.data.info.featured_image)
33
+ * ```
34
+ *
35
+ * @category Media Utilities
36
+ */
5
37
  export const getMediaFile = (media, variation) => {
6
- var _a;
38
+ if (!media)
39
+ return undefined;
7
40
  if (variation) {
8
- return ((_a = media.files.find((file) => file.variation === variation)) !== null && _a !== void 0 ? _a : getOptimizedOrOriginal(media));
41
+ const variationArray = Array.isArray(variation) ? variation : [variation];
42
+ for (const variationName of variationArray) {
43
+ const file = media.files.find((file) => file.variation === variationName);
44
+ if (file) {
45
+ return file;
46
+ }
47
+ }
9
48
  }
10
49
  return getOptimizedOrOriginal(media);
11
50
  };
12
- export const getMediaSource = (media, variation) => {
13
- return getMediaFile(media, variation).path;
14
- };
51
+ /**
52
+ * Returns the source URL of the optimized file (if it exists) or the original
53
+ * file of the given MediaObject. If the variation parameter is provided,
54
+ * returns the source URL of that variation instead, if it exists, but falls
55
+ * back to the optimized or original files if it doesn't.
56
+ *
57
+ * @param media The MediaObject to analyze. If undefined, the function will also
58
+ * return undefined.
59
+ * @param variation A string or array of strings with the variation name that
60
+ * should be returned. If an array is given, the first variation found will
61
+ * be returned.
62
+ *
63
+ * @returns The source URL of the first given variation found. If the provided
64
+ * variations weren't found, or if no variation parameter was provided, returns
65
+ * the optimized variation source URL (if it exists) or the original source URL.
66
+ * Returns undefined if no media object is provided.
67
+ *
68
+ * @example Getting the optimized or original source URL of the given MediaObject.
69
+ * ```ts
70
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
71
+ *
72
+ * const response = await Starlight.posts.entries.get('foo')
73
+ *
74
+ * // `content.background` is the path of an arbitrary Media content field.
75
+ * const imageUrl = getMediaSource(response.data.data.content.background)
76
+ * ```
77
+ *
78
+ * @example Getting the source URL of a specific variation of the given MediaObject.
79
+ * ```ts
80
+ * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'
81
+ *
82
+ * const response = await Starlight.posts.entries.get('foo')
83
+ *
84
+ * // `content.background` is the path of an arbitrary Media content field.
85
+ * const imageUrl = getMediaSource(
86
+ * response.data.data.content.background,
87
+ * ['large', 'medium']
88
+ * )
89
+ * ```
90
+ *
91
+ * @category Media Utilities
92
+ */
93
+ export function getMediaSource(media, variation) {
94
+ return media ? getMediaFile(media, variation).path : undefined;
95
+ }
15
96
  //# sourceMappingURL=image.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAEA,MAAM,sBAAsB,GAAG,CAAC,KAAkB,EAAE,EAAE;;IACpD,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAAC,mCACzD,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,UAAU,CAAe,CACzE,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAkB,EAClB,SAAkB,EACP,EAAE;;IACb,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,SAAS,CAAC,mCACxD,sBAAsB,CAAC,KAAK,CAAC,CAC9B,CAAA;IACH,CAAC;IAED,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAA;AACtC,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,cAAc,GAAG,CAC5B,KAAkB,EAClB,SAAkB,EACV,EAAE;IACV,OAAO,YAAY,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,IAAI,CAAA;AAC5C,CAAC,CAAA","sourcesContent":["import { MediaFile, MediaObject } from '../types'\n\nconst getOptimizedOrOriginal = (media: MediaObject) => {\n return (\n media.files.find((file) => file.variation === 'optimized') ??\n (media.files.find((file) => file.variation === 'original') as MediaFile)\n )\n}\n\nexport const getMediaFile = (\n media: MediaObject,\n variation?: string\n): MediaFile => {\n if (variation) {\n return (\n media.files.find((file) => file.variation === variation) ??\n getOptimizedOrOriginal(media)\n )\n }\n\n return getOptimizedOrOriginal(media)\n}\n\nexport const getMediaSource = (\n media: MediaObject,\n variation?: string\n): string => {\n return getMediaFile(media, variation).path\n}\n"]}
1
+ {"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,sBAAsB,GAAG,CAAC,KAAkB,EAAE,EAAE;;IACpD,OAAO,CACL,MAAA,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,WAAW,CAAC,mCACzD,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,UAAU,CAAe,CACzE,CAAA;AACH,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAC1B,KAAmB,EACnB,SAA6B,EACN,EAAE;IACzB,IAAI,CAAC,KAAK;QAAE,OAAO,SAAS,CAAA;IAE5B,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAA;QAEzE,KAAK,MAAM,aAAa,IAAI,cAAc,EAAE,CAAC;YAC3C,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,KAAK,aAAa,CAAC,CAAA;YAEzE,IAAI,IAAI,EAAE,CAAC;gBACT,OAAO,IAAI,CAAA;YACb,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAA;AACtC,CAAC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAmB,EACnB,SAA6B;IAE7B,OAAO,KAAK,CAAC,CAAC,CAAC,YAAY,CAAC,KAAK,EAAE,SAAS,CAAE,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAA;AACjE,CAAC","sourcesContent":["import { MediaFile, MediaObject } from '../types'\n\n/**\n * Returns the optimized variation file of the given MediaObject\n * or the original file if the optimized variation doesn't exist.\n *\n * @param media The MediaObject to analyze.\n */\nconst getOptimizedOrOriginal = (media: MediaObject) => {\n return (\n media.files.find((file) => file.variation === 'optimized') ??\n (media.files.find((file) => file.variation === 'original') as MediaFile)\n )\n}\n\n/**\n * Returns the given variation of the provided MediaObject.\n *\n * @param media The MediaObject to analyze. If undefined, the function will also\n * return undefined.\n * @param variation A string or array of strings with the variation name that\n * should be returned. If an array is given, the first variation found will\n * be returned.\n *\n * @returns The MediaFile of the first given variation found. If the provided\n * variations weren't found, or if no variation parameter was provided, returns\n * the optimized variation file (if it exists) or the original file. Returns\n * undefined if no media object is provided.\n *\n * @example Getting the optimized or original file of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaFile } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `info.featured_image` is the path of an arbitrary Media content field.\n * const file = getMediaFile(response.data.data.info.featured_image)\n * ```\n *\n * @category Media Utilities\n */\nexport const getMediaFile = (\n media?: MediaObject,\n variation?: string | string[],\n): MediaFile | undefined => {\n if (!media) return undefined\n\n if (variation) {\n const variationArray = Array.isArray(variation) ? variation : [variation]\n\n for (const variationName of variationArray) {\n const file = media.files.find((file) => file.variation === variationName)\n\n if (file) {\n return file\n }\n }\n }\n\n return getOptimizedOrOriginal(media)\n}\n\n/**\n * Returns the source URL of the optimized file (if it exists) or the original\n * file of the given MediaObject. If the variation parameter is provided,\n * returns the source URL of that variation instead, if it exists, but falls\n * back to the optimized or original files if it doesn't.\n *\n * @param media The MediaObject to analyze. If undefined, the function will also\n * return undefined.\n * @param variation A string or array of strings with the variation name that\n * should be returned. If an array is given, the first variation found will\n * be returned.\n *\n * @returns The source URL of the first given variation found. If the provided\n * variations weren't found, or if no variation parameter was provided, returns\n * the optimized variation source URL (if it exists) or the original source URL.\n * Returns undefined if no media object is provided.\n *\n * @example Getting the optimized or original source URL of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `content.background` is the path of an arbitrary Media content field.\n * const imageUrl = getMediaSource(response.data.data.content.background)\n * ```\n *\n * @example Getting the source URL of a specific variation of the given MediaObject.\n * ```ts\n * import Starlight, { getMediaSource } from '@starlightcms/js-sdk'\n *\n * const response = await Starlight.posts.entries.get('foo')\n *\n * // `content.background` is the path of an arbitrary Media content field.\n * const imageUrl = getMediaSource(\n * response.data.data.content.background,\n * ['large', 'medium']\n * )\n * ```\n *\n * @category Media Utilities\n */\nexport function getMediaSource(\n media?: MediaObject,\n variation?: string | string[],\n) {\n return media ? getMediaFile(media, variation)!.path : undefined\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@starlightcms/js-sdk",
3
- "version": "3.2.0",
3
+ "version": "4.0.0-beta.1",
4
4
  "description": "The Starlight SDK for JavaScript",
5
5
  "main": "dist/cjs/index.js",
6
6
  "exports": {