@starlightcms/js-sdk 4.0.0-beta.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.
- package/dist/cjs/types/utilities.d.ts +1 -1
- package/dist/cjs/types/utilities.js.map +1 -1
- package/dist/cjs/utils/image.d.ts +70 -2
- package/dist/cjs/utils/image.d.ts.map +1 -1
- package/dist/cjs/utils/image.js +88 -7
- package/dist/cjs/utils/image.js.map +1 -1
- package/dist/esm/types/utilities.d.ts +1 -1
- package/dist/esm/types/utilities.js.map +1 -1
- package/dist/esm/utils/image.d.ts +70 -2
- package/dist/esm/utils/image.d.ts.map +1 -1
- package/dist/esm/utils/image.js +86 -5
- package/dist/esm/utils/image.js.map +1 -1
- package/package.json +1 -1
|
@@ -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
|
|
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
|
|
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
|
-
|
|
3
|
-
|
|
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;
|
|
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"}
|
package/dist/cjs/utils/image.js
CHANGED
|
@@ -1,20 +1,101 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
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
|
-
|
|
42
|
+
if (!media)
|
|
43
|
+
return undefined;
|
|
10
44
|
if (variation) {
|
|
11
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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":";;;
|
|
1
|
+
{"version":3,"file":"image.js","sourceRoot":"","sources":["../../../src/utils/image.ts"],"names":[],"mappings":";;;AAwGA,wCAKC;AA3GD;;;;;GAKG;AACH,MAAM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, 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"]}
|
|
@@ -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
|
|
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
|
|
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
|
-
|
|
3
|
-
|
|
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;
|
|
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"}
|
package/dist/esm/utils/image.js
CHANGED
|
@@ -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
|
-
|
|
38
|
+
if (!media)
|
|
39
|
+
return undefined;
|
|
7
40
|
if (variation) {
|
|
8
|
-
|
|
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
|
-
|
|
13
|
-
|
|
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,Y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AAC,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"]}
|