@datocms/cma-client 5.1.14 → 5.1.16
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/README.md +31 -28
- package/dist/cjs/generated/Client.js +1 -1
- package/dist/cjs/utilities/normalizedFieldValues.js +39 -39
- package/dist/cjs/utilities/normalizedFieldValues.js.map +1 -1
- package/dist/cjs/utilities/schemaRepository.js +33 -25
- package/dist/cjs/utilities/schemaRepository.js.map +1 -1
- package/dist/esm/generated/Client.js +1 -1
- package/dist/esm/utilities/normalizedFieldValues.d.ts +25 -25
- package/dist/esm/utilities/normalizedFieldValues.js +39 -39
- package/dist/esm/utilities/normalizedFieldValues.js.map +1 -1
- package/dist/esm/utilities/schemaRepository.d.ts +1 -0
- package/dist/esm/utilities/schemaRepository.js +33 -25
- package/dist/esm/utilities/schemaRepository.js.map +1 -1
- package/dist/types/utilities/normalizedFieldValues.d.ts +25 -25
- package/dist/types/utilities/schemaRepository.d.ts +1 -0
- package/package.json +2 -2
- package/src/generated/Client.ts +1 -1
- package/src/utilities/normalizedFieldValues.ts +60 -39
- package/src/utilities/schemaRepository.ts +41 -30
|
@@ -14,8 +14,8 @@ export type LocalizedFieldValue<T = unknown, L extends string = string> = Partia
|
|
|
14
14
|
* This function handles both full Schema field objects and simplified Schema field objects
|
|
15
15
|
* by checking the appropriate property based on the object structure.
|
|
16
16
|
*
|
|
17
|
-
* @param field - The DatoCMS field definition (either full or simple schema)
|
|
18
17
|
* @returns true if the field is localized, false otherwise
|
|
18
|
+
* @param field - The DatoCMS field definition (either full or simple schema)
|
|
19
19
|
*/
|
|
20
20
|
export declare function isLocalized(field: RawApiTypes.Field | ApiTypes.Field): boolean;
|
|
21
21
|
export declare function isLocalizedFieldValue<T = unknown, L extends string = string>(value: unknown): value is LocalizedFieldValue<T, L>;
|
|
@@ -37,11 +37,11 @@ export type NormalizedFieldValueEntry<T = unknown, L extends string = string> =
|
|
|
37
37
|
* This function normalizes the handling of field values by converting them into a consistent
|
|
38
38
|
* array format, regardless of whether the field is localized or not.
|
|
39
39
|
*
|
|
40
|
-
* @param field - The DatoCMS field definition that determines localization behavior
|
|
41
40
|
* @param value - The field value to convert (either a localized object or direct value)
|
|
41
|
+
* @param field - The DatoCMS field definition that determines localization behavior
|
|
42
42
|
* @returns Array of entries where each entry contains a locale (string for localized, undefined for non-localized) and the corresponding value
|
|
43
43
|
*/
|
|
44
|
-
export declare function toNormalizedFieldValueEntries<T = unknown, L extends string = string>(
|
|
44
|
+
export declare function toNormalizedFieldValueEntries<T = unknown, L extends string = string>(value: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field): NormalizedFieldValueEntry<T, L>[];
|
|
45
45
|
/**
|
|
46
46
|
* Converts an array of possibly localized entries back into the appropriate field value format.
|
|
47
47
|
*
|
|
@@ -49,118 +49,118 @@ export declare function toNormalizedFieldValueEntries<T = unknown, L extends str
|
|
|
49
49
|
* array of entries and converts them back to either a localized object or a direct value,
|
|
50
50
|
* depending on the field's localization setting.
|
|
51
51
|
*
|
|
52
|
-
* @param field - The DatoCMS field definition that determines the output format
|
|
53
52
|
* @param entries - Array of entries to convert back to field value format
|
|
53
|
+
* @param field - The DatoCMS field definition that determines the output format
|
|
54
54
|
* @returns Either a localized object (for localized fields) or the direct value (for non-localized fields)
|
|
55
55
|
*/
|
|
56
|
-
export declare function fromNormalizedFieldValueEntries<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field
|
|
56
|
+
export declare function fromNormalizedFieldValueEntries<T = unknown, L extends string = string>(entries: NormalizedFieldValueEntry<T, L>[], field: RawApiTypes.Field | ApiTypes.Field): T | LocalizedFieldValue<T, L>;
|
|
57
57
|
/**
|
|
58
58
|
* Maps field values using a provided mapping function.
|
|
59
59
|
* For localized fields, applies the mapping function to each locale value.
|
|
60
60
|
* For non-localized fields, applies the mapping function directly to the value.
|
|
61
61
|
*
|
|
62
62
|
* @template T - The type that the mapping function returns
|
|
63
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
63
64
|
* @param field - The DatoCMS field definition
|
|
64
|
-
* @param value - The field value (either localized object or direct value)
|
|
65
65
|
* @param mapFn - The function to apply to each locale value or the direct value
|
|
66
66
|
* @returns The mapped value with the same structure as the input
|
|
67
67
|
*/
|
|
68
|
-
export declare function mapNormalizedFieldValues<TInput = unknown, TOutput = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
68
|
+
export declare function mapNormalizedFieldValues<TInput = unknown, TOutput = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: TInput | LocalizedFieldValue<TInput, L>, field: RawApiTypes.Field | ApiTypes.Field, mapFn: (locale: L | undefined, localeValue: TInput) => TOutput): TOutput | LocalizedFieldValue<TOutput, L>;
|
|
69
69
|
/**
|
|
70
70
|
* Maps field values using a provided mapping function (async version).
|
|
71
71
|
* For localized fields, applies the mapping function to each locale value.
|
|
72
72
|
* For non-localized fields, applies the mapping function directly to the value.
|
|
73
73
|
*
|
|
74
74
|
* @template T - The type that the mapping function returns
|
|
75
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
75
76
|
* @param field - The DatoCMS field definition
|
|
76
|
-
* @param value - The field value (either localized object or direct value)
|
|
77
77
|
* @param mapFn - The function to apply to each locale value or the direct value
|
|
78
78
|
* @returns The mapped value with the same structure as the input
|
|
79
79
|
*/
|
|
80
|
-
export declare function mapNormalizedFieldValuesAsync<TInput = unknown, TOutput = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
80
|
+
export declare function mapNormalizedFieldValuesAsync<TInput = unknown, TOutput = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: TInput | LocalizedFieldValue<TInput, L>, field: RawApiTypes.Field | ApiTypes.Field, mapFn: (locale: L | undefined, localeValue: TInput) => Promise<TOutput>): Promise<TOutput | LocalizedFieldValue<TOutput, L>>;
|
|
81
81
|
/**
|
|
82
82
|
* Filters field values using a provided filter function.
|
|
83
83
|
* For localized fields, filters each locale value.
|
|
84
84
|
* For non-localized fields, returns the value if the filter passes, otherwise undefined.
|
|
85
85
|
*
|
|
86
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
86
87
|
* @param field - The DatoCMS field definition
|
|
87
|
-
* @param value - The field value (either localized object or direct value)
|
|
88
88
|
* @param filterFn - The function to test each locale value or the direct value
|
|
89
89
|
* @returns The filtered value with the same structure as the input
|
|
90
90
|
*/
|
|
91
|
-
export declare function filterNormalizedFieldValues<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
91
|
+
export declare function filterNormalizedFieldValues<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, filterFn: (locale: L | undefined, localeValue: T) => boolean): T | LocalizedFieldValue<T, L> | undefined;
|
|
92
92
|
/**
|
|
93
93
|
* Filters field values using a provided filter function (async version).
|
|
94
94
|
* For localized fields, filters each locale value.
|
|
95
95
|
* For non-localized fields, returns the value if the filter passes, otherwise undefined.
|
|
96
96
|
*
|
|
97
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
97
98
|
* @param field - The DatoCMS field definition
|
|
98
|
-
* @param value - The field value (either localized object or direct value)
|
|
99
99
|
* @param filterFn - The function to test each locale value or the direct value
|
|
100
100
|
* @returns The filtered value with the same structure as the input
|
|
101
101
|
*/
|
|
102
|
-
export declare function filterNormalizedFieldValuesAsync<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
102
|
+
export declare function filterNormalizedFieldValuesAsync<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, filterFn: (locale: L | undefined, localeValue: T) => Promise<boolean>): Promise<T | LocalizedFieldValue<T, L> | undefined>;
|
|
103
103
|
/**
|
|
104
104
|
* Tests whether at least one field value passes the test implemented by the provided function.
|
|
105
105
|
* For localized fields, tests each locale value.
|
|
106
106
|
* For non-localized fields, tests the direct value.
|
|
107
107
|
*
|
|
108
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
108
109
|
* @param field - The DatoCMS field definition
|
|
109
|
-
* @param value - The field value (either localized object or direct value)
|
|
110
110
|
* @param testFn - The function to test each locale value or the direct value
|
|
111
111
|
* @returns true if at least one value passes the test, false otherwise
|
|
112
112
|
*/
|
|
113
|
-
export declare function someNormalizedFieldValues<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
113
|
+
export declare function someNormalizedFieldValues<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, testFn: (locale: L | undefined, localeValue: T) => boolean): boolean;
|
|
114
114
|
/**
|
|
115
115
|
* Tests whether at least one field value passes the test implemented by the provided function (async version).
|
|
116
116
|
* For localized fields, tests each locale value.
|
|
117
117
|
* For non-localized fields, tests the direct value.
|
|
118
118
|
*
|
|
119
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
119
120
|
* @param field - The DatoCMS field definition
|
|
120
|
-
* @param value - The field value (either localized object or direct value)
|
|
121
121
|
* @param testFn - The function to test each locale value or the direct value
|
|
122
122
|
* @returns true if at least one value passes the test, false otherwise
|
|
123
123
|
*/
|
|
124
|
-
export declare function someNormalizedFieldValuesAsync<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
124
|
+
export declare function someNormalizedFieldValuesAsync<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, testFn: (locale: L | undefined, localeValue: T) => Promise<boolean>): Promise<boolean>;
|
|
125
125
|
/**
|
|
126
126
|
* Tests whether all field values pass the test implemented by the provided function.
|
|
127
127
|
* For localized fields, tests each locale value.
|
|
128
128
|
* For non-localized fields, tests the direct value.
|
|
129
129
|
*
|
|
130
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
130
131
|
* @param field - The DatoCMS field definition
|
|
131
|
-
* @param value - The field value (either localized object or direct value)
|
|
132
132
|
* @param testFn - The function to test each locale value or the direct value
|
|
133
133
|
* @returns true if all values pass the test, false otherwise
|
|
134
134
|
*/
|
|
135
|
-
export declare function everyNormalizedFieldValue<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
135
|
+
export declare function everyNormalizedFieldValue<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, testFn: (locale: L | undefined, localeValue: T) => boolean): boolean;
|
|
136
136
|
/**
|
|
137
137
|
* Tests whether all field values pass the test implemented by the provided function (async version).
|
|
138
138
|
* For localized fields, tests each locale value.
|
|
139
139
|
* For non-localized fields, tests the direct value.
|
|
140
140
|
*
|
|
141
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
141
142
|
* @param field - The DatoCMS field definition
|
|
142
|
-
* @param value - The field value (either localized object or direct value)
|
|
143
143
|
* @param testFn - The function to test each locale value or the direct value
|
|
144
144
|
* @returns true if all values pass the test, false otherwise
|
|
145
145
|
*/
|
|
146
|
-
export declare function everyNormalizedFieldValueAsync<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
146
|
+
export declare function everyNormalizedFieldValueAsync<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, testFn: (locale: L | undefined, localeValue: T) => Promise<boolean>): Promise<boolean>;
|
|
147
147
|
/**
|
|
148
148
|
* Visits each field value with the provided function.
|
|
149
149
|
* For localized fields, visits each locale value.
|
|
150
150
|
* For non-localized fields, visits the direct value.
|
|
151
151
|
*
|
|
152
|
-
* @param field - The DatoCMS field definition
|
|
153
152
|
* @param value - The field value (either localized object or direct value)
|
|
153
|
+
* @param field - The DatoCMS field definition
|
|
154
154
|
* @param visitFn - The function to call for each locale value or the direct value
|
|
155
155
|
*/
|
|
156
|
-
export declare function visitNormalizedFieldValues<T = unknown, L extends string = string>(
|
|
156
|
+
export declare function visitNormalizedFieldValues<T = unknown, L extends string = string>(value: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, visitFn: (locale: L | undefined, localeValue: T) => void): void;
|
|
157
157
|
/**
|
|
158
158
|
* Visits each field value with the provided function (async version).
|
|
159
159
|
* For localized fields, visits each locale value.
|
|
160
160
|
* For non-localized fields, visits the direct value.
|
|
161
161
|
*
|
|
162
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
162
163
|
* @param field - The DatoCMS field definition
|
|
163
|
-
* @param value - The field value (either localized object or direct value)
|
|
164
164
|
* @param visitFn - The function to call for each locale value or the direct value
|
|
165
165
|
*/
|
|
166
|
-
export declare function visitNormalizedFieldValuesAsync<T = unknown, L extends string = string>(field: RawApiTypes.Field | ApiTypes.Field,
|
|
166
|
+
export declare function visitNormalizedFieldValuesAsync<T = unknown, L extends string = string>(localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>, field: RawApiTypes.Field | ApiTypes.Field, visitFn: (locale: L | undefined, localeValue: T) => Promise<void>): Promise<void>;
|
|
@@ -111,6 +111,7 @@ export declare class SchemaRepository {
|
|
|
111
111
|
private pluginsPromise;
|
|
112
112
|
private pluginsById;
|
|
113
113
|
private pluginsByPackageName;
|
|
114
|
+
private prefetchPromise;
|
|
114
115
|
/**
|
|
115
116
|
* Creates a new SchemaRepository instance.
|
|
116
117
|
* @param client - The DatoCMS client instance
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datocms/cma-client",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.16",
|
|
4
4
|
"description": "JS client for DatoCMS REST Content Management API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datocms",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"@datocms/dashboard-client": "^5.1.13",
|
|
46
46
|
"@types/uuid": "^9.0.7"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "f11b6b358c281fb2efff2e337be07f6c4ba30e77"
|
|
49
49
|
}
|
package/src/generated/Client.ts
CHANGED
|
@@ -147,7 +147,7 @@ export class Client {
|
|
|
147
147
|
...this.config,
|
|
148
148
|
...options,
|
|
149
149
|
logFn: this.config.logFn || console.log,
|
|
150
|
-
userAgent: '@datocms/cma-client v5.1.
|
|
150
|
+
userAgent: '@datocms/cma-client v5.1.16',
|
|
151
151
|
baseUrl: this.baseUrl,
|
|
152
152
|
preCallStack: new Error().stack,
|
|
153
153
|
extraHeaders: {
|
|
@@ -38,8 +38,8 @@ export type LocalizedFieldValue<
|
|
|
38
38
|
* This function handles both full Schema field objects and simplified Schema field objects
|
|
39
39
|
* by checking the appropriate property based on the object structure.
|
|
40
40
|
*
|
|
41
|
-
* @param field - The DatoCMS field definition (either full or simple schema)
|
|
42
41
|
* @returns true if the field is localized, false otherwise
|
|
42
|
+
* @param field - The DatoCMS field definition (either full or simple schema)
|
|
43
43
|
*/
|
|
44
44
|
export function isLocalized(
|
|
45
45
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
@@ -88,16 +88,16 @@ export type NormalizedFieldValueEntry<
|
|
|
88
88
|
* This function normalizes the handling of field values by converting them into a consistent
|
|
89
89
|
* array format, regardless of whether the field is localized or not.
|
|
90
90
|
*
|
|
91
|
-
* @param field - The DatoCMS field definition that determines localization behavior
|
|
92
91
|
* @param value - The field value to convert (either a localized object or direct value)
|
|
92
|
+
* @param field - The DatoCMS field definition that determines localization behavior
|
|
93
93
|
* @returns Array of entries where each entry contains a locale (string for localized, undefined for non-localized) and the corresponding value
|
|
94
94
|
*/
|
|
95
95
|
export function toNormalizedFieldValueEntries<
|
|
96
96
|
T = unknown,
|
|
97
97
|
L extends string = string,
|
|
98
98
|
>(
|
|
99
|
-
field: RawApiTypes.Field | ApiTypes.Field,
|
|
100
99
|
value: T | LocalizedFieldValue<T, L>,
|
|
100
|
+
field: RawApiTypes.Field | ApiTypes.Field,
|
|
101
101
|
): NormalizedFieldValueEntry<T, L>[] {
|
|
102
102
|
if (isLocalized(field)) {
|
|
103
103
|
const localizedValue = value as LocalizedFieldValue<T, L>;
|
|
@@ -118,16 +118,16 @@ export function toNormalizedFieldValueEntries<
|
|
|
118
118
|
* array of entries and converts them back to either a localized object or a direct value,
|
|
119
119
|
* depending on the field's localization setting.
|
|
120
120
|
*
|
|
121
|
-
* @param field - The DatoCMS field definition that determines the output format
|
|
122
121
|
* @param entries - Array of entries to convert back to field value format
|
|
122
|
+
* @param field - The DatoCMS field definition that determines the output format
|
|
123
123
|
* @returns Either a localized object (for localized fields) or the direct value (for non-localized fields)
|
|
124
124
|
*/
|
|
125
125
|
export function fromNormalizedFieldValueEntries<
|
|
126
126
|
T = unknown,
|
|
127
127
|
L extends string = string,
|
|
128
128
|
>(
|
|
129
|
-
field: RawApiTypes.Field | ApiTypes.Field,
|
|
130
129
|
entries: NormalizedFieldValueEntry<T, L>[],
|
|
130
|
+
field: RawApiTypes.Field | ApiTypes.Field,
|
|
131
131
|
): T | LocalizedFieldValue<T, L> {
|
|
132
132
|
if (isLocalized(field)) {
|
|
133
133
|
return Object.fromEntries(
|
|
@@ -148,8 +148,8 @@ export function fromNormalizedFieldValueEntries<
|
|
|
148
148
|
* For non-localized fields, applies the mapping function directly to the value.
|
|
149
149
|
*
|
|
150
150
|
* @template T - The type that the mapping function returns
|
|
151
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
151
152
|
* @param field - The DatoCMS field definition
|
|
152
|
-
* @param value - The field value (either localized object or direct value)
|
|
153
153
|
* @param mapFn - The function to apply to each locale value or the direct value
|
|
154
154
|
* @returns The mapped value with the same structure as the input
|
|
155
155
|
*/
|
|
@@ -158,16 +158,19 @@ export function mapNormalizedFieldValues<
|
|
|
158
158
|
TOutput = unknown,
|
|
159
159
|
L extends string = string,
|
|
160
160
|
>(
|
|
161
|
+
localizedOrNonLocalizedFieldValue: TInput | LocalizedFieldValue<TInput, L>,
|
|
161
162
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
162
|
-
value: TInput | LocalizedFieldValue<TInput, L>,
|
|
163
163
|
mapFn: (locale: L | undefined, localeValue: TInput) => TOutput,
|
|
164
164
|
): TOutput | LocalizedFieldValue<TOutput, L> {
|
|
165
|
-
const entries = toNormalizedFieldValueEntries<TInput, L>(
|
|
165
|
+
const entries = toNormalizedFieldValueEntries<TInput, L>(
|
|
166
|
+
localizedOrNonLocalizedFieldValue,
|
|
167
|
+
field,
|
|
168
|
+
);
|
|
166
169
|
const mappedEntries = entries.map(({ locale, value }) => ({
|
|
167
170
|
locale,
|
|
168
171
|
value: mapFn(locale, value),
|
|
169
172
|
}));
|
|
170
|
-
return fromNormalizedFieldValueEntries<TOutput, L>(
|
|
173
|
+
return fromNormalizedFieldValueEntries<TOutput, L>(mappedEntries, field);
|
|
171
174
|
}
|
|
172
175
|
|
|
173
176
|
/**
|
|
@@ -176,8 +179,8 @@ export function mapNormalizedFieldValues<
|
|
|
176
179
|
* For non-localized fields, applies the mapping function directly to the value.
|
|
177
180
|
*
|
|
178
181
|
* @template T - The type that the mapping function returns
|
|
182
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
179
183
|
* @param field - The DatoCMS field definition
|
|
180
|
-
* @param value - The field value (either localized object or direct value)
|
|
181
184
|
* @param mapFn - The function to apply to each locale value or the direct value
|
|
182
185
|
* @returns The mapped value with the same structure as the input
|
|
183
186
|
*/
|
|
@@ -186,18 +189,21 @@ export async function mapNormalizedFieldValuesAsync<
|
|
|
186
189
|
TOutput = unknown,
|
|
187
190
|
L extends string = string,
|
|
188
191
|
>(
|
|
192
|
+
localizedOrNonLocalizedFieldValue: TInput | LocalizedFieldValue<TInput, L>,
|
|
189
193
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
190
|
-
value: TInput | LocalizedFieldValue<TInput, L>,
|
|
191
194
|
mapFn: (locale: L | undefined, localeValue: TInput) => Promise<TOutput>,
|
|
192
195
|
): Promise<TOutput | LocalizedFieldValue<TOutput, L>> {
|
|
193
|
-
const entries = toNormalizedFieldValueEntries<TInput, L>(
|
|
196
|
+
const entries = toNormalizedFieldValueEntries<TInput, L>(
|
|
197
|
+
localizedOrNonLocalizedFieldValue,
|
|
198
|
+
field,
|
|
199
|
+
);
|
|
194
200
|
const mappedEntries = await Promise.all(
|
|
195
201
|
entries.map(async ({ locale, value }) => ({
|
|
196
202
|
locale,
|
|
197
203
|
value: await mapFn(locale, value),
|
|
198
204
|
})),
|
|
199
205
|
);
|
|
200
|
-
return fromNormalizedFieldValueEntries<TOutput, L>(
|
|
206
|
+
return fromNormalizedFieldValueEntries<TOutput, L>(mappedEntries, field);
|
|
201
207
|
}
|
|
202
208
|
|
|
203
209
|
/**
|
|
@@ -205,8 +211,8 @@ export async function mapNormalizedFieldValuesAsync<
|
|
|
205
211
|
* For localized fields, filters each locale value.
|
|
206
212
|
* For non-localized fields, returns the value if the filter passes, otherwise undefined.
|
|
207
213
|
*
|
|
214
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
208
215
|
* @param field - The DatoCMS field definition
|
|
209
|
-
* @param value - The field value (either localized object or direct value)
|
|
210
216
|
* @param filterFn - The function to test each locale value or the direct value
|
|
211
217
|
* @returns The filtered value with the same structure as the input
|
|
212
218
|
*/
|
|
@@ -214,17 +220,20 @@ export function filterNormalizedFieldValues<
|
|
|
214
220
|
T = unknown,
|
|
215
221
|
L extends string = string,
|
|
216
222
|
>(
|
|
223
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
217
224
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
218
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
219
225
|
filterFn: (locale: L | undefined, localeValue: T) => boolean,
|
|
220
226
|
): T | LocalizedFieldValue<T, L> | undefined {
|
|
221
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
227
|
+
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
228
|
+
localizedOrNonLocalizedFieldValue,
|
|
229
|
+
field,
|
|
230
|
+
);
|
|
222
231
|
const filteredEntries = entries.filter((entry) =>
|
|
223
232
|
filterFn(entry.locale, entry.value),
|
|
224
233
|
);
|
|
225
234
|
|
|
226
235
|
if (isLocalized(field)) {
|
|
227
|
-
return fromNormalizedFieldValueEntries<T, L>(
|
|
236
|
+
return fromNormalizedFieldValueEntries<T, L>(filteredEntries, field);
|
|
228
237
|
}
|
|
229
238
|
|
|
230
239
|
return filteredEntries.length > 0 ? filteredEntries[0]?.value : undefined;
|
|
@@ -235,8 +244,8 @@ export function filterNormalizedFieldValues<
|
|
|
235
244
|
* For localized fields, filters each locale value.
|
|
236
245
|
* For non-localized fields, returns the value if the filter passes, otherwise undefined.
|
|
237
246
|
*
|
|
247
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
238
248
|
* @param field - The DatoCMS field definition
|
|
239
|
-
* @param value - The field value (either localized object or direct value)
|
|
240
249
|
* @param filterFn - The function to test each locale value or the direct value
|
|
241
250
|
* @returns The filtered value with the same structure as the input
|
|
242
251
|
*/
|
|
@@ -244,11 +253,14 @@ export async function filterNormalizedFieldValuesAsync<
|
|
|
244
253
|
T = unknown,
|
|
245
254
|
L extends string = string,
|
|
246
255
|
>(
|
|
256
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
247
257
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
248
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
249
258
|
filterFn: (locale: L | undefined, localeValue: T) => Promise<boolean>,
|
|
250
259
|
): Promise<T | LocalizedFieldValue<T, L> | undefined> {
|
|
251
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
260
|
+
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
261
|
+
localizedOrNonLocalizedFieldValue,
|
|
262
|
+
field,
|
|
263
|
+
);
|
|
252
264
|
const results = await Promise.all(
|
|
253
265
|
entries.map(async ({ locale, value }) => ({
|
|
254
266
|
locale,
|
|
@@ -262,7 +274,7 @@ export async function filterNormalizedFieldValuesAsync<
|
|
|
262
274
|
.map(({ locale, value }) => ({ locale, value }));
|
|
263
275
|
|
|
264
276
|
if (isLocalized(field)) {
|
|
265
|
-
return fromNormalizedFieldValueEntries<T, L>(
|
|
277
|
+
return fromNormalizedFieldValueEntries<T, L>(filteredEntries, field);
|
|
266
278
|
}
|
|
267
279
|
|
|
268
280
|
return filteredEntries.length > 0 ? filteredEntries[0]?.value : undefined;
|
|
@@ -273,8 +285,8 @@ export async function filterNormalizedFieldValuesAsync<
|
|
|
273
285
|
* For localized fields, tests each locale value.
|
|
274
286
|
* For non-localized fields, tests the direct value.
|
|
275
287
|
*
|
|
288
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
276
289
|
* @param field - The DatoCMS field definition
|
|
277
|
-
* @param value - The field value (either localized object or direct value)
|
|
278
290
|
* @param testFn - The function to test each locale value or the direct value
|
|
279
291
|
* @returns true if at least one value passes the test, false otherwise
|
|
280
292
|
*/
|
|
@@ -282,11 +294,14 @@ export function someNormalizedFieldValues<
|
|
|
282
294
|
T = unknown,
|
|
283
295
|
L extends string = string,
|
|
284
296
|
>(
|
|
297
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
285
298
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
286
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
287
299
|
testFn: (locale: L | undefined, localeValue: T) => boolean,
|
|
288
300
|
): boolean {
|
|
289
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
301
|
+
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
302
|
+
localizedOrNonLocalizedFieldValue,
|
|
303
|
+
field,
|
|
304
|
+
);
|
|
290
305
|
return entries.some(({ locale, value }) => testFn(locale, value));
|
|
291
306
|
}
|
|
292
307
|
|
|
@@ -295,8 +310,8 @@ export function someNormalizedFieldValues<
|
|
|
295
310
|
* For localized fields, tests each locale value.
|
|
296
311
|
* For non-localized fields, tests the direct value.
|
|
297
312
|
*
|
|
313
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
298
314
|
* @param field - The DatoCMS field definition
|
|
299
|
-
* @param value - The field value (either localized object or direct value)
|
|
300
315
|
* @param testFn - The function to test each locale value or the direct value
|
|
301
316
|
* @returns true if at least one value passes the test, false otherwise
|
|
302
317
|
*/
|
|
@@ -304,11 +319,14 @@ export async function someNormalizedFieldValuesAsync<
|
|
|
304
319
|
T = unknown,
|
|
305
320
|
L extends string = string,
|
|
306
321
|
>(
|
|
322
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
307
323
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
308
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
309
324
|
testFn: (locale: L | undefined, localeValue: T) => Promise<boolean>,
|
|
310
325
|
): Promise<boolean> {
|
|
311
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
326
|
+
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
327
|
+
localizedOrNonLocalizedFieldValue,
|
|
328
|
+
field,
|
|
329
|
+
);
|
|
312
330
|
const results = await Promise.all(
|
|
313
331
|
entries.map(({ locale, value }) => testFn(locale, value)),
|
|
314
332
|
);
|
|
@@ -320,8 +338,8 @@ export async function someNormalizedFieldValuesAsync<
|
|
|
320
338
|
* For localized fields, tests each locale value.
|
|
321
339
|
* For non-localized fields, tests the direct value.
|
|
322
340
|
*
|
|
341
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
323
342
|
* @param field - The DatoCMS field definition
|
|
324
|
-
* @param value - The field value (either localized object or direct value)
|
|
325
343
|
* @param testFn - The function to test each locale value or the direct value
|
|
326
344
|
* @returns true if all values pass the test, false otherwise
|
|
327
345
|
*/
|
|
@@ -329,13 +347,13 @@ export function everyNormalizedFieldValue<
|
|
|
329
347
|
T = unknown,
|
|
330
348
|
L extends string = string,
|
|
331
349
|
>(
|
|
350
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
332
351
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
333
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
334
352
|
testFn: (locale: L | undefined, localeValue: T) => boolean,
|
|
335
353
|
): boolean {
|
|
336
354
|
return !someNormalizedFieldValues(
|
|
355
|
+
localizedOrNonLocalizedFieldValue,
|
|
337
356
|
field,
|
|
338
|
-
value,
|
|
339
357
|
(locale, localeValue) => !testFn(locale, localeValue),
|
|
340
358
|
);
|
|
341
359
|
}
|
|
@@ -345,8 +363,8 @@ export function everyNormalizedFieldValue<
|
|
|
345
363
|
* For localized fields, tests each locale value.
|
|
346
364
|
* For non-localized fields, tests the direct value.
|
|
347
365
|
*
|
|
366
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
348
367
|
* @param field - The DatoCMS field definition
|
|
349
|
-
* @param value - The field value (either localized object or direct value)
|
|
350
368
|
* @param testFn - The function to test each locale value or the direct value
|
|
351
369
|
* @returns true if all values pass the test, false otherwise
|
|
352
370
|
*/
|
|
@@ -354,13 +372,13 @@ export async function everyNormalizedFieldValueAsync<
|
|
|
354
372
|
T = unknown,
|
|
355
373
|
L extends string = string,
|
|
356
374
|
>(
|
|
375
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
357
376
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
358
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
359
377
|
testFn: (locale: L | undefined, localeValue: T) => Promise<boolean>,
|
|
360
378
|
): Promise<boolean> {
|
|
361
379
|
return !(await someNormalizedFieldValuesAsync(
|
|
380
|
+
localizedOrNonLocalizedFieldValue,
|
|
362
381
|
field,
|
|
363
|
-
value,
|
|
364
382
|
async (locale, localeValue) => !(await testFn(locale, localeValue)),
|
|
365
383
|
));
|
|
366
384
|
}
|
|
@@ -370,19 +388,19 @@ export async function everyNormalizedFieldValueAsync<
|
|
|
370
388
|
* For localized fields, visits each locale value.
|
|
371
389
|
* For non-localized fields, visits the direct value.
|
|
372
390
|
*
|
|
373
|
-
* @param field - The DatoCMS field definition
|
|
374
391
|
* @param value - The field value (either localized object or direct value)
|
|
392
|
+
* @param field - The DatoCMS field definition
|
|
375
393
|
* @param visitFn - The function to call for each locale value or the direct value
|
|
376
394
|
*/
|
|
377
395
|
export function visitNormalizedFieldValues<
|
|
378
396
|
T = unknown,
|
|
379
397
|
L extends string = string,
|
|
380
398
|
>(
|
|
381
|
-
field: RawApiTypes.Field | ApiTypes.Field,
|
|
382
399
|
value: T | LocalizedFieldValue<T, L>,
|
|
400
|
+
field: RawApiTypes.Field | ApiTypes.Field,
|
|
383
401
|
visitFn: (locale: L | undefined, localeValue: T) => void,
|
|
384
402
|
): void {
|
|
385
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
403
|
+
const entries = toNormalizedFieldValueEntries<T, L>(value, field);
|
|
386
404
|
for (const { locale, value } of entries) {
|
|
387
405
|
visitFn(locale, value);
|
|
388
406
|
}
|
|
@@ -393,18 +411,21 @@ export function visitNormalizedFieldValues<
|
|
|
393
411
|
* For localized fields, visits each locale value.
|
|
394
412
|
* For non-localized fields, visits the direct value.
|
|
395
413
|
*
|
|
414
|
+
* @param localizedOrNonLocalizedFieldValue - The field value (either localized object or direct value)
|
|
396
415
|
* @param field - The DatoCMS field definition
|
|
397
|
-
* @param value - The field value (either localized object or direct value)
|
|
398
416
|
* @param visitFn - The function to call for each locale value or the direct value
|
|
399
417
|
*/
|
|
400
418
|
export async function visitNormalizedFieldValuesAsync<
|
|
401
419
|
T = unknown,
|
|
402
420
|
L extends string = string,
|
|
403
421
|
>(
|
|
422
|
+
localizedOrNonLocalizedFieldValue: T | LocalizedFieldValue<T, L>,
|
|
404
423
|
field: RawApiTypes.Field | ApiTypes.Field,
|
|
405
|
-
value: T | LocalizedFieldValue<T, L>,
|
|
406
424
|
visitFn: (locale: L | undefined, localeValue: T) => Promise<void>,
|
|
407
425
|
): Promise<void> {
|
|
408
|
-
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
426
|
+
const entries = toNormalizedFieldValueEntries<T, L>(
|
|
427
|
+
localizedOrNonLocalizedFieldValue,
|
|
428
|
+
field,
|
|
429
|
+
);
|
|
409
430
|
await Promise.all(entries.map(({ locale, value }) => visitFn(locale, value)));
|
|
410
431
|
}
|
|
@@ -103,6 +103,7 @@ export class SchemaRepository {
|
|
|
103
103
|
private pluginsPromise: Promise<RawApiTypes.Plugin[]> | null = null;
|
|
104
104
|
private pluginsById: Map<string, RawApiTypes.Plugin> = new Map();
|
|
105
105
|
private pluginsByPackageName: Map<string, RawApiTypes.Plugin> = new Map();
|
|
106
|
+
private prefetchPromise: Promise<void> | null = null;
|
|
106
107
|
|
|
107
108
|
/**
|
|
108
109
|
* Creates a new SchemaRepository instance.
|
|
@@ -457,42 +458,52 @@ export class SchemaRepository {
|
|
|
457
458
|
* @returns Promise that resolves when all data has been fetched and cached
|
|
458
459
|
*/
|
|
459
460
|
async prefetchAllModelsAndFields(): Promise<void> {
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
});
|
|
463
|
-
|
|
464
|
-
if (!included) {
|
|
465
|
-
throw new Error('This should not happen');
|
|
461
|
+
if (this.prefetchPromise) {
|
|
462
|
+
return this.prefetchPromise;
|
|
466
463
|
}
|
|
467
464
|
|
|
468
|
-
const
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
(item): item is RawApiTypes.Field => item.type === 'field',
|
|
473
|
-
);
|
|
465
|
+
const prefetch = async () => {
|
|
466
|
+
const { included } = await this.client.site.rawFind({
|
|
467
|
+
include: 'item_types,item_types.fields',
|
|
468
|
+
});
|
|
474
469
|
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
this.itemTypesByApiKey.set(itemType.attributes.api_key, itemType);
|
|
479
|
-
this.itemTypesById.set(itemType.id, itemType);
|
|
480
|
-
}
|
|
470
|
+
if (!included) {
|
|
471
|
+
throw new Error('This should not happen');
|
|
472
|
+
}
|
|
481
473
|
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
474
|
+
const allItemTypes = included.filter(
|
|
475
|
+
(item): item is RawApiTypes.ItemType => item.type === 'item_type',
|
|
476
|
+
);
|
|
477
|
+
const allFields = included.filter(
|
|
478
|
+
(item): item is RawApiTypes.Field => item.type === 'field',
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
// Populate item types caches
|
|
482
|
+
this.itemTypesPromise = Promise.resolve(allItemTypes);
|
|
483
|
+
for (const itemType of allItemTypes) {
|
|
484
|
+
this.itemTypesByApiKey.set(itemType.attributes.api_key, itemType);
|
|
485
|
+
this.itemTypesById.set(itemType.id, itemType);
|
|
488
486
|
}
|
|
489
|
-
fieldsByItemTypeId.get(itemTypeId)!.push(field);
|
|
490
|
-
}
|
|
491
487
|
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
488
|
+
// Group fields by item type and populate fields cache
|
|
489
|
+
const fieldsByItemTypeId = new Map<string, RawApiTypes.Field[]>();
|
|
490
|
+
for (const field of allFields) {
|
|
491
|
+
const itemTypeId = field.relationships.item_type.data.id;
|
|
492
|
+
if (!fieldsByItemTypeId.has(itemTypeId)) {
|
|
493
|
+
fieldsByItemTypeId.set(itemTypeId, []);
|
|
494
|
+
}
|
|
495
|
+
fieldsByItemTypeId.get(itemTypeId)!.push(field);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// Populate the fields cache
|
|
499
|
+
for (const [itemTypeId, fields] of fieldsByItemTypeId) {
|
|
500
|
+
this.fieldsByItemType.set(itemTypeId, fields);
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
this.prefetchPromise = prefetch();
|
|
505
|
+
|
|
506
|
+
return this.prefetchPromise;
|
|
496
507
|
}
|
|
497
508
|
|
|
498
509
|
/**
|