@datocms/cma-client 5.1.15 → 5.1.17
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/generated/resources/Item.js.map +1 -1
- package/dist/cjs/utilities/itemInstancesHrefSchema.js +3 -0
- package/dist/cjs/utilities/itemInstancesHrefSchema.js.map +1 -0
- package/dist/cjs/utilities/normalizedFieldValues.js +39 -39
- package/dist/cjs/utilities/normalizedFieldValues.js.map +1 -1
- package/dist/esm/generated/ApiTypes.d.ts +4 -6
- package/dist/esm/generated/Client.js +1 -1
- package/dist/esm/generated/RawApiTypes.d.ts +4 -6
- package/dist/esm/generated/resources/Item.d.ts +7 -7
- package/dist/esm/generated/resources/Item.js.map +1 -1
- package/dist/esm/utilities/itemDefinition.d.ts +30 -0
- package/dist/esm/utilities/itemInstancesHrefSchema.d.ts +256 -0
- package/dist/esm/utilities/itemInstancesHrefSchema.js +2 -0
- package/dist/esm/utilities/itemInstancesHrefSchema.js.map +1 -0
- 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/types/generated/ApiTypes.d.ts +4 -6
- package/dist/types/generated/RawApiTypes.d.ts +4 -6
- package/dist/types/generated/resources/Item.d.ts +7 -7
- package/dist/types/utilities/itemDefinition.d.ts +30 -0
- package/dist/types/utilities/itemInstancesHrefSchema.d.ts +256 -0
- package/dist/types/utilities/normalizedFieldValues.d.ts +25 -25
- package/package.json +2 -2
- package/src/generated/ApiTypes.ts +7 -5
- package/src/generated/Client.ts +1 -1
- package/src/generated/RawApiTypes.ts +7 -5
- package/src/generated/resources/Item.ts +10 -10
- package/src/utilities/itemDefinition.ts +92 -0
- package/src/utilities/itemInstancesHrefSchema.ts +308 -0
- package/src/utilities/normalizedFieldValues.ts +60 -39
|
@@ -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
|
}
|