@starlightcms/js-sdk 4.0.0-beta.0 → 4.0.0-beta.2

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.
@@ -70,4 +70,54 @@ export type TextField = string | undefined;
70
70
  * @group Data Fields
71
71
  */
72
72
  export type VisualField = VisualData | undefined;
73
+ /**
74
+ * Represents an Integer Field returned by the API.
75
+ *
76
+ * Field types are used to type Entry and Singleton objects when requesting
77
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
78
+ * for more info.
79
+ *
80
+ * @group Data Fields
81
+ */
82
+ export type IntegerField = number | undefined;
83
+ /**
84
+ * Represents a Float Field returned by the API.
85
+ *
86
+ * Field types are used to type Entry and Singleton objects when requesting
87
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
88
+ * for more info.
89
+ *
90
+ * @group Data Fields
91
+ */
92
+ export type FloatField = number | undefined;
93
+ /**
94
+ * Represents a Decimal Field returned by the API.
95
+ *
96
+ * Field types are used to type Entry and Singleton objects when requesting
97
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
98
+ * for more info.
99
+ *
100
+ * @group Data Fields
101
+ */
102
+ export type DecimalField = number | undefined;
103
+ export type SelectFieldOptions = {
104
+ /**
105
+ * Indicates if the Select Field accepts multiple selected choices.
106
+ *
107
+ * @default false
108
+ */
109
+ multiple?: boolean;
110
+ };
111
+ /**
112
+ * Represents a Select Field returned by the API.
113
+ *
114
+ * Field types are used to type Entry and Singleton objects when requesting
115
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
116
+ * for more info.
117
+ *
118
+ * @group Data Fields
119
+ */
120
+ export type SelectField<Options extends SelectFieldOptions = {
121
+ multiple: false;
122
+ }> = (Options['multiple'] extends false ? string : string | string[]) | undefined;
73
123
  //# 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,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
+ {"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;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAA;AAE7C;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,SAAS,CAAA;AAE3C;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAA;AAE7C,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CACrB,OAAO,SAAS,kBAAkB,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAA;CAAE,IACtD,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,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 | 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"]}
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\n/**\n * Represents an Integer 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 IntegerField = number | undefined\n\n/**\n * Represents a Float 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 FloatField = number | undefined\n\n/**\n * Represents a Decimal 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 DecimalField = number | undefined\n\nexport type SelectFieldOptions = {\n /**\n * Indicates if the Select Field accepts multiple selected choices.\n *\n * @default false\n */\n multiple?: boolean\n}\n\n/**\n * Represents a Select 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 SelectField<\n Options extends SelectFieldOptions = { multiple: false },\n> = (Options['multiple'] extends false ? string : string | string[]) | undefined\n"]}
@@ -7,7 +7,9 @@
7
7
  *
8
8
  * @group Data Groups
9
9
  */
10
- export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
10
+ export type Group<Fields extends Record<string, unknown>> = {
11
+ [Property in keyof Fields]: Fields[Property];
12
+ } | undefined;
11
13
  /**
12
14
  * Represents a Repeater Group returned by the API.
13
15
  *
@@ -22,5 +24,7 @@ export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
22
24
  *
23
25
  * @group Data Groups
24
26
  */
25
- export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null | undefined;
27
+ export type RepeaterGroup<Fields extends Record<string, unknown>> = Array<{
28
+ [Property in keyof Fields]: Fields[Property];
29
+ }> | null | undefined;
26
30
  //# 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,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
+ {"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,IACpD;KAAG,QAAQ,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC;CAAE,GAChD,SAAS,CAAA;AAEb;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,KAAK,CAAC;KAAG,QAAQ,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC;CAAE,CAAC,GACvD,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 | 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"]}
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>> =\n | { [Property in keyof Fields]: Fields[Property] }\n | 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 | Array<{ [Property in keyof Fields]: Fields[Property] }>\n | null\n | undefined\n"]}
@@ -1,12 +1,83 @@
1
1
  import { RepeaterGroup } from './groups';
2
+ import { Get, Paths } from 'type-fest';
3
+ /**
4
+ * Returns the type of the group or field in the given structure at the
5
+ * specified path. Use dot notation `.` to access items inside groups
6
+ * and bracket notation `[]` to access items inside repeaters.
7
+ *
8
+ * Syntax example:
9
+ * - foo
10
+ * - foo.bar
11
+ * - foo.bar[0].baz
12
+ * - foo.bar[0].baz.quo
13
+ *
14
+ * This utility is useful to get the type of a deeply nested group or field
15
+ * inside a Model, Singleton or Form structure.
16
+ *
17
+ * @example Retrieving the type of a deeply nested group or field.
18
+ * ```ts
19
+ * import { Group, StringField, GetFromStructure } from '@starlightcms/js-sdk'
20
+ *
21
+ * type ComplexModel = {
22
+ * info: Group<{
23
+ * title: StringField
24
+ * meta: Group<{
25
+ * keywords: StringField
26
+ * tags: RepeaterGroup<{
27
+ * name: StringField
28
+ * }>
29
+ * }>
30
+ * }>
31
+ * }
32
+ *
33
+ * type MetaGroupType = GetFromStructure<ComplexModel, 'info.meta'>
34
+ * // Resulting type -> Group<{ keywords: StringField, tags: RepeaterGroup<{ name: StringField }> }>
35
+ *
36
+ * type KeywordsFieldType = GetFromStructure<ComplexModel, 'info.meta.keywords'>
37
+ * // Resulting type -> StringField
38
+ *
39
+ * type TagNameFieldType = GetFromStructure<ComplexModel, 'info.meta.tags[0].name'>
40
+ * // Resulting type -> StringField
41
+ * ```
42
+ *
43
+ * @group Utility Types
44
+ */
45
+ export type GetFromStructure<Structure, Path extends Paths<Structure, {
46
+ bracketNotation: true;
47
+ maxRecursionDepth: 4;
48
+ }>> = Get<Structure, Path>;
2
49
  /**
3
50
  * Returns the item type of the given Repeater Group.
4
51
  *
5
- * For instance, given a `RepeaterGroup<Foo>`, this utility would return
6
- * the `Foo` type. This is useful to "pick" the type of the content held
7
- * by items of a Repeater Group.
52
+ * This utility is useful to get the type of the content
53
+ * held in items of a specific Repeater Group.
54
+ *
55
+ * @example Retrieving the type of a repeater item.
56
+ * ```ts
57
+ * import {
58
+ * RepeaterGroup,
59
+ * MediaField,
60
+ * StringField,
61
+ * GetRepeaterItem,
62
+ * GetFromStructure
63
+ * } from '@starlightcms/js-sdk'
64
+ *
65
+ * type SlideshowModel = {
66
+ * slides: RepeaterGroup<{
67
+ * image: MediaField
68
+ * description: StringField
69
+ * }>
70
+ * }
71
+ *
72
+ * type Slide = GetRepeaterItem<SlideshowModel['slides']>
73
+ * // Resulting type -> { image: MediaField, description: StringField }
74
+ *
75
+ * // Also works with the GetFromStructure utility
76
+ * type Slide = GetRepeaterItem<GetFromStructure<SlideshowModel, 'slides'>>
77
+ * // Resulting type -> { image: MediaField, description: StringField }
78
+ * ```
8
79
  *
9
- * @group Utilities
80
+ * @group Utility Types
10
81
  */
11
- export type RepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
82
+ export type GetRepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
12
83
  //# sourceMappingURL=utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,CACtB,QAAQ,SAAS,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IACrD,QAAQ,SAAS,SAAS,CAAC,MAAM,QAAQ,CAAC,EAAE,GAAG,QAAQ,GAAG,KAAK,CAAA"}
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,WAAW,CAAA;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,MAAM,gBAAgB,CAC1B,SAAS,EACT,IAAI,SAAS,KAAK,CAChB,SAAS,EACT;IAAE,eAAe,EAAE,IAAI,CAAC;IAAC,iBAAiB,EAAE,CAAC,CAAA;CAAE,CAChD,IACC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,eAAe,CACzB,QAAQ,SAAS,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IACrD,QAAQ,SAAS,SAAS,CAAC,MAAM,QAAQ,CAAC,EAAE,GAAG,QAAQ,GAAG,KAAK,CAAA"}
@@ -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'\nimport { Get, Paths } from 'type-fest'\n\n/**\n * Returns the type of the group or field in the given structure at the\n * specified path. Use dot notation `.` to access items inside groups\n * and bracket notation `[]` to access items inside repeaters.\n *\n * Syntax example:\n * - foo\n * - foo.bar\n * - foo.bar[0].baz\n * - foo.bar[0].baz.quo\n *\n * This utility is useful to get the type of a deeply nested group or field\n * inside a Model, Singleton or Form structure.\n *\n * @example Retrieving the type of a deeply nested group or field.\n * ```ts\n * import { Group, StringField, GetFromStructure } from '@starlightcms/js-sdk'\n *\n * type ComplexModel = {\n * info: Group<{\n * title: StringField\n * meta: Group<{\n * keywords: StringField\n * tags: RepeaterGroup<{\n * name: StringField\n * }>\n * }>\n * }>\n * }\n *\n * type MetaGroupType = GetFromStructure<ComplexModel, 'info.meta'>\n * // Resulting type -> Group<{ keywords: StringField, tags: RepeaterGroup<{ name: StringField }> }>\n *\n * type KeywordsFieldType = GetFromStructure<ComplexModel, 'info.meta.keywords'>\n * // Resulting type -> StringField\n *\n * type TagNameFieldType = GetFromStructure<ComplexModel, 'info.meta.tags[0].name'>\n * // Resulting type -> StringField\n * ```\n *\n * @group Utility Types\n */\nexport type GetFromStructure<\n Structure,\n Path extends Paths<\n Structure,\n { bracketNotation: true; maxRecursionDepth: 4 }\n >,\n> = Get<Structure, Path>\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * This utility is useful to get the type of the content\n * held in items of a specific Repeater Group.\n *\n * @example Retrieving the type of a repeater item.\n * ```ts\n * import {\n * RepeaterGroup,\n * MediaField,\n * StringField,\n * GetRepeaterItem,\n * GetFromStructure\n * } from '@starlightcms/js-sdk'\n *\n * type SlideshowModel = {\n * slides: RepeaterGroup<{\n * image: MediaField\n * description: StringField\n * }>\n * }\n *\n * type Slide = GetRepeaterItem<SlideshowModel['slides']>\n * // Resulting type -> { image: MediaField, description: StringField }\n *\n * // Also works with the GetFromStructure utility\n * type Slide = GetRepeaterItem<GetFromStructure<SlideshowModel, 'slides'>>\n * // Resulting type -> { image: MediaField, description: StringField }\n * ```\n *\n * @group Utility Types\n */\nexport type GetRepeaterItem<\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"]}
@@ -70,4 +70,54 @@ export type TextField = string | undefined;
70
70
  * @group Data Fields
71
71
  */
72
72
  export type VisualField = VisualData | undefined;
73
+ /**
74
+ * Represents an Integer Field returned by the API.
75
+ *
76
+ * Field types are used to type Entry and Singleton objects when requesting
77
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
78
+ * for more info.
79
+ *
80
+ * @group Data Fields
81
+ */
82
+ export type IntegerField = number | undefined;
83
+ /**
84
+ * Represents a Float Field returned by the API.
85
+ *
86
+ * Field types are used to type Entry and Singleton objects when requesting
87
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
88
+ * for more info.
89
+ *
90
+ * @group Data Fields
91
+ */
92
+ export type FloatField = number | undefined;
93
+ /**
94
+ * Represents a Decimal Field returned by the API.
95
+ *
96
+ * Field types are used to type Entry and Singleton objects when requesting
97
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
98
+ * for more info.
99
+ *
100
+ * @group Data Fields
101
+ */
102
+ export type DecimalField = number | undefined;
103
+ export type SelectFieldOptions = {
104
+ /**
105
+ * Indicates if the Select Field accepts multiple selected choices.
106
+ *
107
+ * @default false
108
+ */
109
+ multiple?: boolean;
110
+ };
111
+ /**
112
+ * Represents a Select Field returned by the API.
113
+ *
114
+ * Field types are used to type Entry and Singleton objects when requesting
115
+ * them using some SDK methods. See {@apilink DefaultModelDefinition}
116
+ * for more info.
117
+ *
118
+ * @group Data Fields
119
+ */
120
+ export type SelectField<Options extends SelectFieldOptions = {
121
+ multiple: false;
122
+ }> = (Options['multiple'] extends false ? string : string | string[]) | undefined;
73
123
  //# 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,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
+ {"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;AAEhD;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAA;AAE7C;;;;;;;;GAQG;AACH,MAAM,MAAM,UAAU,GAAG,MAAM,GAAG,SAAS,CAAA;AAE3C;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,GAAG,MAAM,GAAG,SAAS,CAAA;AAE7C,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;;;;;;GAQG;AACH,MAAM,MAAM,WAAW,CACrB,OAAO,SAAS,kBAAkB,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAA;CAAE,IACtD,CAAC,OAAO,CAAC,UAAU,CAAC,SAAS,KAAK,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,EAAE,CAAC,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 | 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"]}
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\n/**\n * Represents an Integer 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 IntegerField = number | undefined\n\n/**\n * Represents a Float 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 FloatField = number | undefined\n\n/**\n * Represents a Decimal 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 DecimalField = number | undefined\n\nexport type SelectFieldOptions = {\n /**\n * Indicates if the Select Field accepts multiple selected choices.\n *\n * @default false\n */\n multiple?: boolean\n}\n\n/**\n * Represents a Select 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 SelectField<\n Options extends SelectFieldOptions = { multiple: false },\n> = (Options['multiple'] extends false ? string : string | string[]) | undefined\n"]}
@@ -7,7 +7,9 @@
7
7
  *
8
8
  * @group Data Groups
9
9
  */
10
- export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
10
+ export type Group<Fields extends Record<string, unknown>> = {
11
+ [Property in keyof Fields]: Fields[Property];
12
+ } | undefined;
11
13
  /**
12
14
  * Represents a Repeater Group returned by the API.
13
15
  *
@@ -22,5 +24,7 @@ export type Group<Fields extends Record<string, unknown>> = Fields | undefined;
22
24
  *
23
25
  * @group Data Groups
24
26
  */
25
- export type RepeaterGroup<Fields extends Record<string, unknown>> = Fields[] | null | undefined;
27
+ export type RepeaterGroup<Fields extends Record<string, unknown>> = Array<{
28
+ [Property in keyof Fields]: Fields[Property];
29
+ }> | null | undefined;
26
30
  //# 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,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
+ {"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,IACpD;KAAG,QAAQ,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC;CAAE,GAChD,SAAS,CAAA;AAEb;;;;;;;;;;;;;GAaG;AACH,MAAM,MAAM,aAAa,CAAC,MAAM,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAC5D,KAAK,CAAC;KAAG,QAAQ,IAAI,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC;CAAE,CAAC,GACvD,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 | 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"]}
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>> =\n | { [Property in keyof Fields]: Fields[Property] }\n | 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 | Array<{ [Property in keyof Fields]: Fields[Property] }>\n | null\n | undefined\n"]}
@@ -1,12 +1,83 @@
1
1
  import { RepeaterGroup } from './groups';
2
+ import { Get, Paths } from 'type-fest';
3
+ /**
4
+ * Returns the type of the group or field in the given structure at the
5
+ * specified path. Use dot notation `.` to access items inside groups
6
+ * and bracket notation `[]` to access items inside repeaters.
7
+ *
8
+ * Syntax example:
9
+ * - foo
10
+ * - foo.bar
11
+ * - foo.bar[0].baz
12
+ * - foo.bar[0].baz.quo
13
+ *
14
+ * This utility is useful to get the type of a deeply nested group or field
15
+ * inside a Model, Singleton or Form structure.
16
+ *
17
+ * @example Retrieving the type of a deeply nested group or field.
18
+ * ```ts
19
+ * import { Group, StringField, GetFromStructure } from '@starlightcms/js-sdk'
20
+ *
21
+ * type ComplexModel = {
22
+ * info: Group<{
23
+ * title: StringField
24
+ * meta: Group<{
25
+ * keywords: StringField
26
+ * tags: RepeaterGroup<{
27
+ * name: StringField
28
+ * }>
29
+ * }>
30
+ * }>
31
+ * }
32
+ *
33
+ * type MetaGroupType = GetFromStructure<ComplexModel, 'info.meta'>
34
+ * // Resulting type -> Group<{ keywords: StringField, tags: RepeaterGroup<{ name: StringField }> }>
35
+ *
36
+ * type KeywordsFieldType = GetFromStructure<ComplexModel, 'info.meta.keywords'>
37
+ * // Resulting type -> StringField
38
+ *
39
+ * type TagNameFieldType = GetFromStructure<ComplexModel, 'info.meta.tags[0].name'>
40
+ * // Resulting type -> StringField
41
+ * ```
42
+ *
43
+ * @group Utility Types
44
+ */
45
+ export type GetFromStructure<Structure, Path extends Paths<Structure, {
46
+ bracketNotation: true;
47
+ maxRecursionDepth: 4;
48
+ }>> = Get<Structure, Path>;
2
49
  /**
3
50
  * Returns the item type of the given Repeater Group.
4
51
  *
5
- * For instance, given a `RepeaterGroup<Foo>`, this utility would return
6
- * the `Foo` type. This is useful to "pick" the type of the content held
7
- * by items of a Repeater Group.
52
+ * This utility is useful to get the type of the content
53
+ * held in items of a specific Repeater Group.
54
+ *
55
+ * @example Retrieving the type of a repeater item.
56
+ * ```ts
57
+ * import {
58
+ * RepeaterGroup,
59
+ * MediaField,
60
+ * StringField,
61
+ * GetRepeaterItem,
62
+ * GetFromStructure
63
+ * } from '@starlightcms/js-sdk'
64
+ *
65
+ * type SlideshowModel = {
66
+ * slides: RepeaterGroup<{
67
+ * image: MediaField
68
+ * description: StringField
69
+ * }>
70
+ * }
71
+ *
72
+ * type Slide = GetRepeaterItem<SlideshowModel['slides']>
73
+ * // Resulting type -> { image: MediaField, description: StringField }
74
+ *
75
+ * // Also works with the GetFromStructure utility
76
+ * type Slide = GetRepeaterItem<GetFromStructure<SlideshowModel, 'slides'>>
77
+ * // Resulting type -> { image: MediaField, description: StringField }
78
+ * ```
8
79
  *
9
- * @group Utilities
80
+ * @group Utility Types
10
81
  */
11
- export type RepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
82
+ export type GetRepeaterItem<Repeater extends RepeaterGroup<Record<string, unknown>>> = Repeater extends readonly (infer ItemType)[] ? ItemType : never;
12
83
  //# sourceMappingURL=utilities.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAExC;;;;;;;;GAQG;AACH,MAAM,MAAM,YAAY,CACtB,QAAQ,SAAS,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IACrD,QAAQ,SAAS,SAAS,CAAC,MAAM,QAAQ,CAAC,EAAE,GAAG,QAAQ,GAAG,KAAK,CAAA"}
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../../../src/types/utilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AACxC,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,WAAW,CAAA;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,MAAM,gBAAgB,CAC1B,SAAS,EACT,IAAI,SAAS,KAAK,CAChB,SAAS,EACT;IAAE,eAAe,EAAE,IAAI,CAAC;IAAC,iBAAiB,EAAE,CAAC,CAAA;CAAE,CAChD,IACC,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAA;AAExB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,MAAM,eAAe,CACzB,QAAQ,SAAS,aAAa,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,IACrD,QAAQ,SAAS,SAAS,CAAC,MAAM,QAAQ,CAAC,EAAE,GAAG,QAAQ,GAAG,KAAK,CAAA"}
@@ -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'\nimport { Get, Paths } from 'type-fest'\n\n/**\n * Returns the type of the group or field in the given structure at the\n * specified path. Use dot notation `.` to access items inside groups\n * and bracket notation `[]` to access items inside repeaters.\n *\n * Syntax example:\n * - foo\n * - foo.bar\n * - foo.bar[0].baz\n * - foo.bar[0].baz.quo\n *\n * This utility is useful to get the type of a deeply nested group or field\n * inside a Model, Singleton or Form structure.\n *\n * @example Retrieving the type of a deeply nested group or field.\n * ```ts\n * import { Group, StringField, GetFromStructure } from '@starlightcms/js-sdk'\n *\n * type ComplexModel = {\n * info: Group<{\n * title: StringField\n * meta: Group<{\n * keywords: StringField\n * tags: RepeaterGroup<{\n * name: StringField\n * }>\n * }>\n * }>\n * }\n *\n * type MetaGroupType = GetFromStructure<ComplexModel, 'info.meta'>\n * // Resulting type -> Group<{ keywords: StringField, tags: RepeaterGroup<{ name: StringField }> }>\n *\n * type KeywordsFieldType = GetFromStructure<ComplexModel, 'info.meta.keywords'>\n * // Resulting type -> StringField\n *\n * type TagNameFieldType = GetFromStructure<ComplexModel, 'info.meta.tags[0].name'>\n * // Resulting type -> StringField\n * ```\n *\n * @group Utility Types\n */\nexport type GetFromStructure<\n Structure,\n Path extends Paths<\n Structure,\n { bracketNotation: true; maxRecursionDepth: 4 }\n >,\n> = Get<Structure, Path>\n\n/**\n * Returns the item type of the given Repeater Group.\n *\n * This utility is useful to get the type of the content\n * held in items of a specific Repeater Group.\n *\n * @example Retrieving the type of a repeater item.\n * ```ts\n * import {\n * RepeaterGroup,\n * MediaField,\n * StringField,\n * GetRepeaterItem,\n * GetFromStructure\n * } from '@starlightcms/js-sdk'\n *\n * type SlideshowModel = {\n * slides: RepeaterGroup<{\n * image: MediaField\n * description: StringField\n * }>\n * }\n *\n * type Slide = GetRepeaterItem<SlideshowModel['slides']>\n * // Resulting type -> { image: MediaField, description: StringField }\n *\n * // Also works with the GetFromStructure utility\n * type Slide = GetRepeaterItem<GetFromStructure<SlideshowModel, 'slides'>>\n * // Resulting type -> { image: MediaField, description: StringField }\n * ```\n *\n * @group Utility Types\n */\nexport type GetRepeaterItem<\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": "4.0.0-beta.0",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "The Starlight SDK for JavaScript",
5
5
  "main": "dist/cjs/index.js",
6
6
  "exports": {
@@ -41,6 +41,9 @@
41
41
  "url": "https://github.com/starlightcms/js-sdk/issues"
42
42
  },
43
43
  "homepage": "https://github.com/starlightcms/js-sdk#readme",
44
+ "dependencies": {
45
+ "type-fest": "5.5.0"
46
+ },
44
47
  "devDependencies": {
45
48
  "@eslint/eslintrc": "^3.3.3",
46
49
  "@eslint/js": "^9.39.2",