@datocms/cma-client 5.4.5 → 5.4.7
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 +21 -3
- package/dist/cjs/fieldTypes/rich_text.js +2 -2
- package/dist/cjs/fieldTypes/rich_text.js.map +1 -1
- package/dist/cjs/fieldTypes/single_block.js +27 -3
- package/dist/cjs/fieldTypes/single_block.js.map +1 -1
- package/dist/cjs/fieldTypes/structured_text.js +2 -2
- package/dist/cjs/fieldTypes/structured_text.js.map +1 -1
- package/dist/cjs/generated/Client.js +1 -1
- package/dist/cjs/utilities/isBlockOfType.js +5 -33
- package/dist/cjs/utilities/isBlockOfType.js.map +1 -1
- package/dist/esm/fieldTypes/rich_text.js +3 -3
- package/dist/esm/fieldTypes/rich_text.js.map +1 -1
- package/dist/esm/fieldTypes/single_block.d.ts +20 -1
- package/dist/esm/fieldTypes/single_block.js +25 -2
- package/dist/esm/fieldTypes/single_block.js.map +1 -1
- package/dist/esm/fieldTypes/structured_text.js +3 -3
- package/dist/esm/fieldTypes/structured_text.js.map +1 -1
- package/dist/esm/generated/Client.js +1 -1
- package/dist/esm/utilities/isBlockOfType.d.ts +20 -7
- package/dist/esm/utilities/isBlockOfType.js +5 -33
- package/dist/esm/utilities/isBlockOfType.js.map +1 -1
- package/dist/types/fieldTypes/single_block.d.ts +20 -1
- package/dist/types/utilities/isBlockOfType.d.ts +20 -7
- package/package.json +2 -2
- package/src/fieldTypes/rich_text.ts +3 -3
- package/src/fieldTypes/single_block.ts +32 -3
- package/src/fieldTypes/structured_text.ts +3 -3
- package/src/generated/Client.ts +1 -1
- package/src/utilities/isBlockOfType.ts +31 -9
package/README.md
CHANGED
|
@@ -309,10 +309,17 @@ TypeScript doesn't auto-narrow on discriminators buried in nested properties, so
|
|
|
309
309
|
|
|
310
310
|
**TypeScript Signature:**
|
|
311
311
|
```typescript
|
|
312
|
+
// Curried — returns a predicate (use with .filter / .find)
|
|
312
313
|
function isBlockOfType<Id extends string>(
|
|
313
314
|
itemTypeId: Id,
|
|
314
315
|
): <T>(block: T) => block is NarrowBlockByItemType<T, Id>
|
|
315
316
|
|
|
317
|
+
// Direct — checks a single block inline (use inside `if`)
|
|
318
|
+
function isBlockOfType<T, Id extends string>(
|
|
319
|
+
itemTypeId: Id,
|
|
320
|
+
block: T,
|
|
321
|
+
): block is NarrowBlockByItemType<T, Id>
|
|
322
|
+
|
|
316
323
|
type NarrowBlockByItemType<T, Id extends string> = Extract<
|
|
317
324
|
T,
|
|
318
325
|
{ relationships: { item_type: { data: { type: 'item_type'; id: Id } } } }
|
|
@@ -321,8 +328,13 @@ type NarrowBlockByItemType<T, Id extends string> = Extract<
|
|
|
321
328
|
|
|
322
329
|
**Parameters:**
|
|
323
330
|
- `itemTypeId`: The item-type ID literal. For narrowing to work, the argument must be typed as a literal — use `as const` on pre-set ID constants. No `ItemTypeDefinition` type parameter is needed: `Extract` walks the input union using just the ID.
|
|
331
|
+
- `block` (direct form only): The block to check.
|
|
332
|
+
|
|
333
|
+
**Returns:**
|
|
334
|
+
- Curried form: a predicate `(block) => block is D-typed-block`.
|
|
335
|
+
- Direct form: a `boolean` that also acts as a type guard on `block`.
|
|
324
336
|
|
|
325
|
-
|
|
337
|
+
In both cases the guard:
|
|
326
338
|
- Narrows blocks carrying `relationships.item_type.data.id` — that covers `BlockInNestedResponse<D>` and the object variants of `BlockInRequest<D>` (`UpdatedBlockInRequest`, `NewBlockInRequest`).
|
|
327
339
|
- Returns `false` for plain string IDs (unchanged-reference form in request payloads) and for any non-block input.
|
|
328
340
|
|
|
@@ -343,12 +355,18 @@ const images = article.content.filter(
|
|
|
343
355
|
);
|
|
344
356
|
images[0].attributes.upload_id; // ❌ property does not exist on union
|
|
345
357
|
|
|
346
|
-
// After: guard narrows the filter result
|
|
358
|
+
// After (curried): guard narrows the filter result
|
|
347
359
|
const images = article.content.filter(isBlockOfType(IMAGE_BLOCK_ID));
|
|
348
360
|
images[0].attributes.upload_id; // ✅ narrowed
|
|
361
|
+
|
|
362
|
+
// After (direct): inline narrowing on a single block
|
|
363
|
+
const first = article.content[0];
|
|
364
|
+
if (isBlockOfType(IMAGE_BLOCK_ID, first)) {
|
|
365
|
+
first.attributes.upload_id; // ✅ narrowed
|
|
366
|
+
}
|
|
349
367
|
```
|
|
350
368
|
|
|
351
|
-
Use the
|
|
369
|
+
Use the curried form when you need a predicate for `.filter` / `.find`; use the direct form for one-off `if` checks. The `__itemTypeId` discriminator is also available for inline `switch` narrowing on a single value.
|
|
352
370
|
</details>
|
|
353
371
|
|
|
354
372
|
### Recursive Block Operations
|
|
@@ -36,8 +36,8 @@ function isRichTextFieldValueInRequest(value) {
|
|
|
36
36
|
// String ID - referencing existing block
|
|
37
37
|
if ((0, single_block_1.isItemId)(block))
|
|
38
38
|
return true;
|
|
39
|
-
// Object (
|
|
40
|
-
return (0, single_block_1.
|
|
39
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
40
|
+
return (0, single_block_1.isBlockObjectInRequest)(block);
|
|
41
41
|
});
|
|
42
42
|
}
|
|
43
43
|
exports.isRichTextFieldValueInRequest = isRichTextFieldValueInRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rich_text.js","sourceRoot":"","sources":["../../../src/fieldTypes/rich_text.ts"],"names":[],"mappings":";;;AAAA,wCAA4C;AAE5C,8EAG4C;AAE5C,iDAMwB;AA2ExB;;;;GAIG;AAEH;;;GAGG;AACH,SAAgB,oBAAoB,CAClC,KAAc;IAEd,OAAO,CACL,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACpB,KAAK,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,IAAA,cAAS,EAAC,KAAK,CAAC,CAAC,CACtE,CAAC;AACJ,CAAC;AAPD,oDAOC;AAED,SAAgB,6BAA6B,CAC3C,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK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,KAAK,CAAC;IAExC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAC3B,yCAAyC;QACzC,IAAI,IAAA,uBAAQ,EAAC,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QAEjC,6EAA6E;QAC7E,OAAO,IAAA,qCAAsB,EAAC,KAAK,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC;AAdD,sEAcC;AAED,SAAgB,sCAAsC,CAGpD,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAC1D,CAAC;AACJ,CAAC;AATD,wFASC;AAED;;;GAGG;AACH,SAAgB,oCAAoC,CAElD,KAAc;IACd,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAExC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAC3B,uFAAuF;QACvF,OAAO,IAAA,qCAAsB,EAAC,KAAK,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC;AATD,oFASC;AAED,SAAgB,6CAA6C,CAG3D,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,oCAAoC,CAAC,CACjE,CAAC;AACJ,CAAC;AATD,sGASC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isLocalizedSingleBlockFieldValueInNestedResponse = exports.isSingleBlockFieldValueInNestedResponse = exports.isLocalizedSingleBlockFieldValueInRequest = exports.isSingleBlockFieldValueInRequest = exports.isLocalizedSingleBlockFieldValue = exports.isSingleBlockFieldValue = exports.isItemWithOptionalMeta = exports.isItemWithOptionalIdAndMeta = exports.isItemId = void 0;
|
|
3
|
+
exports.isLocalizedSingleBlockFieldValueInNestedResponse = exports.isSingleBlockFieldValueInNestedResponse = exports.isLocalizedSingleBlockFieldValueInRequest = exports.isSingleBlockFieldValueInRequest = exports.isBlockObjectInRequest = exports.isLocalizedSingleBlockFieldValue = exports.isSingleBlockFieldValue = exports.isItemWithOptionalMeta = exports.isItemWithOptionalIdAndMeta = exports.isItemId = void 0;
|
|
4
4
|
const id_1 = require("../utilities/id");
|
|
5
5
|
const normalizedFieldValues_1 = require("../utilities/normalizedFieldValues");
|
|
6
6
|
/**
|
|
@@ -55,6 +55,29 @@ function isLocalizedSingleBlockFieldValue(value) {
|
|
|
55
55
|
Object.values(value).every(isSingleBlockFieldValue));
|
|
56
56
|
}
|
|
57
57
|
exports.isLocalizedSingleBlockFieldValue = isLocalizedSingleBlockFieldValue;
|
|
58
|
+
/**
|
|
59
|
+
* Shape check for a block object on the *request* side. Accepts every object
|
|
60
|
+
* form the CMA allows inside a request payload:
|
|
61
|
+
*
|
|
62
|
+
* 1. New block (no id, full body):
|
|
63
|
+
* { type: 'item', attributes, relationships: { item_type } }
|
|
64
|
+
* 2. Updated block, full body:
|
|
65
|
+
* { type: 'item', id, attributes, relationships: { item_type } }
|
|
66
|
+
* 3. Updated block, id-only (patch some attributes of an existing block):
|
|
67
|
+
* { type: 'item', id, attributes } ← no relationships
|
|
68
|
+
*
|
|
69
|
+
* Case 3 is what `buildBlockRecord` produces when the caller omits
|
|
70
|
+
* `item_type` — the server derives the model from the existing block's id,
|
|
71
|
+
* so re-specifying it in `relationships` is redundant.
|
|
72
|
+
*/
|
|
73
|
+
function isBlockObjectInRequest(block) {
|
|
74
|
+
return (typeof block === 'object' &&
|
|
75
|
+
block !== null &&
|
|
76
|
+
'type' in block &&
|
|
77
|
+
block.type === 'item' &&
|
|
78
|
+
'attributes' in block);
|
|
79
|
+
}
|
|
80
|
+
exports.isBlockObjectInRequest = isBlockObjectInRequest;
|
|
58
81
|
/**
|
|
59
82
|
* Type guard for Single Block field values in API request format.
|
|
60
83
|
* Allows block as string ID, full object with ID, or object without ID.
|
|
@@ -65,8 +88,9 @@ function isSingleBlockFieldValueInRequest(value) {
|
|
|
65
88
|
// String ID - referencing existing block
|
|
66
89
|
if (isItemId(value))
|
|
67
90
|
return true;
|
|
68
|
-
// Object (either with or without ID for updates/creation
|
|
69
|
-
|
|
91
|
+
// Object (either with or without ID for updates/creation, including
|
|
92
|
+
// id-only updates without `relationships`)
|
|
93
|
+
return isBlockObjectInRequest(value);
|
|
70
94
|
}
|
|
71
95
|
exports.isSingleBlockFieldValueInRequest = isSingleBlockFieldValueInRequest;
|
|
72
96
|
function isLocalizedSingleBlockFieldValueInRequest(value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"single_block.js","sourceRoot":"","sources":["../../../src/fieldTypes/single_block.ts"],"names":[],"mappings":";;;AACA,wCAA4C;AAK5C,8EAG4C;AAyH5C;;;;;GAKG;AAEH;;GAEG;AACH,SAAgB,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAFD,4BAEC;AAMD;;GAEG;AACH,SAAgB,2BAA2B,CAEzC,KAAc;IACd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,MAAM;QACrB,YAAY,IAAI,KAAK;QACrB,eAAe,IAAI,KAAK,CACzB,CAAC;AACJ,CAAC;AAXD,kEAWC;AAMD;;GAEG;AACH,SAAgB,sBAAsB,CAEpC,KAAc;IACd,OAAO,CACL,2BAA2B,CAAC,KAAK,CAAC;QAClC,IAAI,IAAI,KAAK;QACb,OAAO,KAAK,CAAC,EAAE,KAAK,QAAQ,CAC7B,CAAC;AACJ,CAAC;AARD,wDAQC;AAED;;;;GAIG;AAEH;;;GAGG;AACH,SAAgB,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AAuF;IACvF,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAC;AACvC,CAAC;AAPD,0FAOC;AAED,SAAgB,gDAAgD,CAG9D,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,uCAAuC,CAAC,CACpE,CAAC;AACJ,CAAC;AATD,4GASC"}
|
|
@@ -56,8 +56,8 @@ function isStructuredTextFieldValueInRequest(value) {
|
|
|
56
56
|
// String ID
|
|
57
57
|
if ((0, single_block_1.isItemId)(item))
|
|
58
58
|
return true;
|
|
59
|
-
// Object (
|
|
60
|
-
return (0, single_block_1.
|
|
59
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
60
|
+
return (0, single_block_1.isBlockObjectInRequest)(item);
|
|
61
61
|
});
|
|
62
62
|
}
|
|
63
63
|
exports.isStructuredTextFieldValueInRequest = isStructuredTextFieldValueInRequest;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured_text.js","sourceRoot":"","sources":["../../../src/fieldTypes/structured_text.ts"],"names":[],"mappings":";;;AAAA,iFASuC;AACvC,wCAA4C;AAE5C,8EAG4C;AAE5C,iDAMwB;AA4HxB;;;GAGG;AACH,SAAS,qBAAqB,CAC5B,IAAgB,EAChB,QAAsD;IAEtD,OAAO,IAAA,yCAAS,EAAC,IAAI,EAAE,CAAC,WAAW,EAAE,EAAE;QACrC,wEAAwE;QACxE,IAAI,IAAA,uCAAO,EAAC,WAAW,CAAC,IAAI,IAAA,6CAAa,EAAC,WAAW,CAAC,EAAE;YACtD,OAAO,QAAQ,CAAC,WAAW,CAAC,CAAC;SAC9B;QACD,qDAAqD;QACrD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,SAAgB,0BAA0B,CACxC,KAAc;IAEd,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CA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,IAAI,CAAC;QAEhC,6EAA6E;QAC7E,OAAO,IAAA,qCAAsB,EAAC,IAAI,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC;AAzBD,kFAyBC;AAED,SAAgB,4CAA4C,CAG1D,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAChE,CAAC;AACJ,CAAC;AATD,oGASC;AAED;;;GAGG;AACH,SAAgB,0CAA0C,CAExD,KAAc;IACd,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAEhC,IAAI,CAAC,IAAA,0CAAU,EAAU,KAAK,CAAC,EAAE;QAC/B,OAAO,KAAK,CAAC;KACd;IAED,gEAAgE;IAChE,OAAO,qBAAqB,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,IAAI,EAAE,EAAE;QACpD,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC;QAEvB,2EAA2E;QAC3E,OAAO,IAAA,qCAAsB,EAAC,IAAI,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;AACL,CAAC;AAhBD,gGAgBC;AAED,SAAgB,mDAAmD,CAGjE,KAAc;IAEd,OAAO,CACL,IAAA,6CAAqB,EAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,0CAA0C,CAAC,CACvE,CAAC;AACJ,CAAC;AATD,kHASC"}
|
|
@@ -90,7 +90,7 @@ class Client {
|
|
|
90
90
|
return this.config.baseUrl || Client.defaultBaseUrl;
|
|
91
91
|
}
|
|
92
92
|
request(options) {
|
|
93
|
-
return (0, rest_client_utils_1.request)(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.4.
|
|
93
|
+
return (0, rest_client_utils_1.request)(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.4.7', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
|
|
94
94
|
? { 'X-Environment': this.config.environment }
|
|
95
95
|
: {})), { 'X-API-Version': '3' }), fetchJobResult: (jobId) => {
|
|
96
96
|
return this.jobResultsFetcher
|
|
@@ -1,39 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.isBlockOfType = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* Call it with the block's `itemTypeId` literal — the ID generic is inferred
|
|
8
|
-
* from the argument, so no explicit type parameter is needed. The returned
|
|
9
|
-
* predicate is generic: given any input type `T`, it narrows to
|
|
10
|
-
* `Extract<T, { relationships: { item_type: { data: { id: Id } } } }>`. It's
|
|
11
|
-
* meant to plug into `Array#filter` / `Array#find` over block-bearing fields
|
|
12
|
-
* in any of these contexts:
|
|
13
|
-
*
|
|
14
|
-
* - `ItemInNestedResponse<D>` (responses with `nested: true`)
|
|
15
|
-
* - `ItemCreateSchema<D>` / `ItemUpdateSchema<D>` (request payloads, object
|
|
16
|
-
* variants; plain string IDs are filtered out — there's no way to narrow
|
|
17
|
-
* them without an external lookup)
|
|
18
|
-
*
|
|
19
|
-
* The default (non-nested) response shape, where block fields are arrays of
|
|
20
|
-
* plain string IDs, is deliberately not supported — the type information is
|
|
21
|
-
* not recoverable from an ID alone.
|
|
22
|
-
*
|
|
23
|
-
* For the literal `Id` to be preserved (and narrowing to work), the argument
|
|
24
|
-
* must be typed as a literal — use `as const` on pre-set ID constants.
|
|
25
|
-
*
|
|
26
|
-
* @example
|
|
27
|
-
* ```ts
|
|
28
|
-
* const SESSION_BLOCK_ID = 'abc123' as const;
|
|
29
|
-
*
|
|
30
|
-
* const record = await client.items.find<Schema.ConferenceDay>(id, { nested: true });
|
|
31
|
-
* const sessions = record.agenda.filter(isBlockOfType(SESSION_BLOCK_ID));
|
|
32
|
-
* sessions[0].attributes.signup_url; // OK — narrowed
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
function isBlockOfType(itemTypeId) {
|
|
36
|
-
return (block) => {
|
|
4
|
+
function isBlockOfType(itemTypeId, ...rest) {
|
|
5
|
+
const check = (block) => {
|
|
37
6
|
if (typeof block !== 'object' || block === null)
|
|
38
7
|
return false;
|
|
39
8
|
const relationships = block.relationships;
|
|
@@ -47,6 +16,9 @@ function isBlockOfType(itemTypeId) {
|
|
|
47
16
|
return false;
|
|
48
17
|
return data.id === itemTypeId;
|
|
49
18
|
};
|
|
19
|
+
if (rest.length > 0)
|
|
20
|
+
return check(rest[0]);
|
|
21
|
+
return (block) => check(block);
|
|
50
22
|
}
|
|
51
23
|
exports.isBlockOfType = isBlockOfType;
|
|
52
24
|
//# sourceMappingURL=isBlockOfType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isBlockOfType.js","sourceRoot":"","sources":["../../../src/utilities/isBlockOfType.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"isBlockOfType.js","sourceRoot":"","sources":["../../../src/utilities/isBlockOfType.ts"],"names":[],"mappings":";;;AAiEA,SAAgB,aAAa,CAC3B,UAAc,EACd,GAAG,IAA2B;IAE9B,MAAM,KAAK,GAAG,CAAC,KAAc,EAAW,EAAE;QACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QAC9D,MAAM,aAAa,GAAI,KAAqC,CAAC,aAAa,CAAC;QAC3E,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,KAAK,IAAI;YAC7D,OAAO,KAAK,CAAC;QACf,MAAM,QAAQ,GAAI,aAAyC,CAAC,SAAS,CAAC;QACtE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QACpE,MAAM,IAAI,GAAI,QAA+B,CAAC,IAAI,CAAC;QACnD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QAC5D,OAAQ,IAAyB,CAAC,EAAE,KAAK,UAAU,CAAC;IACtD,CAAC,CAAC;IACF,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3C,OAAO,CAAI,KAAQ,EAAyC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC;AAjBD,sCAiBC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isValidId } from '../utilities/id';
|
|
2
2
|
import { isLocalizedFieldValue, } from '../utilities/normalizedFieldValues';
|
|
3
|
-
import {
|
|
3
|
+
import { isBlockObjectInRequest, isItemId, isItemWithOptionalMeta, } from './single_block';
|
|
4
4
|
/**
|
|
5
5
|
* =============================================================================
|
|
6
6
|
* TYPE GUARDS - Runtime validation functions
|
|
@@ -31,8 +31,8 @@ export function isRichTextFieldValueInRequest(value) {
|
|
|
31
31
|
// String ID - referencing existing block
|
|
32
32
|
if (isItemId(block))
|
|
33
33
|
return true;
|
|
34
|
-
// Object (
|
|
35
|
-
return
|
|
34
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
35
|
+
return isBlockObjectInRequest(block);
|
|
36
36
|
});
|
|
37
37
|
}
|
|
38
38
|
export function isLocalizedRichTextFieldValueInRequest(value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rich_text.js","sourceRoot":"","sources":["../../../src/fieldTypes/rich_text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EAEL,qBAAqB,GACtB,MAAM,oCAAoC,CAAC;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qB,CAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,6BAA6B,CAAC,CAC1D,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,oCAAoC,CAElD,KAAc;IACd,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAExC,OAAO,KAAK,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAC3B,uFAAuF;QACvF,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,6CAA6C,CAG3D,KAAc;IAEd,OAAO,CACL,qBAAqB,CAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,oCAAoC,CAAC,CACjE,CAAC;AACJ,CAAC"}
|
|
@@ -52,7 +52,7 @@ export type UpdatedBlockInRequest<D extends ItemTypeDefinition = ItemTypeDefinit
|
|
|
52
52
|
__itemTypeId?: D['itemTypeId'];
|
|
53
53
|
type: RawApiTypes.ItemType1;
|
|
54
54
|
id: RawApiTypes.ItemIdentity;
|
|
55
|
-
relationships
|
|
55
|
+
relationships?: RawApiTypes.ItemRelationships<D>;
|
|
56
56
|
meta?: RawApiTypes.ItemMeta;
|
|
57
57
|
attributes: ToItemAttributesInRequest<D>;
|
|
58
58
|
};
|
|
@@ -125,6 +125,25 @@ export declare function isItemWithOptionalMeta<D extends ItemTypeDefinition = It
|
|
|
125
125
|
*/
|
|
126
126
|
export declare function isSingleBlockFieldValue(value: unknown): value is SingleBlockFieldValue;
|
|
127
127
|
export declare function isLocalizedSingleBlockFieldValue(value: unknown): value is LocalizedFieldValue<SingleBlockFieldValue>;
|
|
128
|
+
/**
|
|
129
|
+
* Shape check for a block object on the *request* side. Accepts every object
|
|
130
|
+
* form the CMA allows inside a request payload:
|
|
131
|
+
*
|
|
132
|
+
* 1. New block (no id, full body):
|
|
133
|
+
* { type: 'item', attributes, relationships: { item_type } }
|
|
134
|
+
* 2. Updated block, full body:
|
|
135
|
+
* { type: 'item', id, attributes, relationships: { item_type } }
|
|
136
|
+
* 3. Updated block, id-only (patch some attributes of an existing block):
|
|
137
|
+
* { type: 'item', id, attributes } ← no relationships
|
|
138
|
+
*
|
|
139
|
+
* Case 3 is what `buildBlockRecord` produces when the caller omits
|
|
140
|
+
* `item_type` — the server derives the model from the existing block's id,
|
|
141
|
+
* so re-specifying it in `relationships` is redundant.
|
|
142
|
+
*/
|
|
143
|
+
export declare function isBlockObjectInRequest(block: unknown): block is {
|
|
144
|
+
type: RawApiTypes.ItemType1;
|
|
145
|
+
attributes: Record<string, unknown>;
|
|
146
|
+
};
|
|
128
147
|
/**
|
|
129
148
|
* Type guard for Single Block field values in API request format.
|
|
130
149
|
* Allows block as string ID, full object with ID, or object without ID.
|
|
@@ -47,6 +47,28 @@ export function isLocalizedSingleBlockFieldValue(value) {
|
|
|
47
47
|
return (isLocalizedFieldValue(value) &&
|
|
48
48
|
Object.values(value).every(isSingleBlockFieldValue));
|
|
49
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Shape check for a block object on the *request* side. Accepts every object
|
|
52
|
+
* form the CMA allows inside a request payload:
|
|
53
|
+
*
|
|
54
|
+
* 1. New block (no id, full body):
|
|
55
|
+
* { type: 'item', attributes, relationships: { item_type } }
|
|
56
|
+
* 2. Updated block, full body:
|
|
57
|
+
* { type: 'item', id, attributes, relationships: { item_type } }
|
|
58
|
+
* 3. Updated block, id-only (patch some attributes of an existing block):
|
|
59
|
+
* { type: 'item', id, attributes } ← no relationships
|
|
60
|
+
*
|
|
61
|
+
* Case 3 is what `buildBlockRecord` produces when the caller omits
|
|
62
|
+
* `item_type` — the server derives the model from the existing block's id,
|
|
63
|
+
* so re-specifying it in `relationships` is redundant.
|
|
64
|
+
*/
|
|
65
|
+
export function isBlockObjectInRequest(block) {
|
|
66
|
+
return (typeof block === 'object' &&
|
|
67
|
+
block !== null &&
|
|
68
|
+
'type' in block &&
|
|
69
|
+
block.type === 'item' &&
|
|
70
|
+
'attributes' in block);
|
|
71
|
+
}
|
|
50
72
|
/**
|
|
51
73
|
* Type guard for Single Block field values in API request format.
|
|
52
74
|
* Allows block as string ID, full object with ID, or object without ID.
|
|
@@ -57,8 +79,9 @@ export function isSingleBlockFieldValueInRequest(value) {
|
|
|
57
79
|
// String ID - referencing existing block
|
|
58
80
|
if (isItemId(value))
|
|
59
81
|
return true;
|
|
60
|
-
// Object (either with or without ID for updates/creation
|
|
61
|
-
|
|
82
|
+
// Object (either with or without ID for updates/creation, including
|
|
83
|
+
// id-only updates without `relationships`)
|
|
84
|
+
return isBlockObjectInRequest(value);
|
|
62
85
|
}
|
|
63
86
|
export function isLocalizedSingleBlockFieldValueInRequest(value) {
|
|
64
87
|
return (isLocalizedFieldValue(value) &&
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"single_block.js","sourceRoot":"","sources":["../../../src/fieldTypes/single_block.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAK5C,OAAO,EAEL,qBAAqB,GACtB,MAAM,oCAAoC,CAAC;AAyH5C;;;;;GAKG;AAEH;;GAEG;AACH,MAAM,UAAU,QAAQ,CAAC,KAAc;IACrC,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC;AACnC,CAAC;AAMD;;GAEG;AACH,MAAM,UAAU,2BAA2B,CAEzC,KAAc;IACd,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,MAAM,IAAI,KAAK;QACf,KAAK,CAAC,IAAI,KAAK,MAAM;QACrB,YAAY,IAAI,KAAK;QACrB,eAAe,IAAI,KAAK,CACzB,CAAC;AACJ,CAAC;AAMD;;GAEG;AACH,MAAM,UAAU,sBAAsB,CAEpC,KAAc;IACd,OAAO,CACL,2BAA2B,CAA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AAC;AAED;;;GAGG;AACH,MAAM,UAAU,uCAAuC,CAErD,KAAc;IACd,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,IAAI,CAAC;IAEhC,uFAAuF;IACvF,OAAO,sBAAsB,CAAC,KAAK,CAAC,CAAC;AACvC,CAAC;AAED,MAAM,UAAU,gDAAgD,CAG9D,KAAc;IAEd,OAAO,CACL,qBAAqB,CAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,uCAAuC,CAAC,CACpE,CAAC;AACJ,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { everyNode, isBlock, isDocument, isInlineBlock, } from 'datocms-structured-text-utils';
|
|
2
2
|
import { isValidId } from '../utilities/id';
|
|
3
3
|
import { isLocalizedFieldValue, } from '../utilities/normalizedFieldValues';
|
|
4
|
-
import {
|
|
4
|
+
import { isBlockObjectInRequest, isItemId, isItemWithOptionalMeta, } from './single_block';
|
|
5
5
|
/**
|
|
6
6
|
* Utility function to validate all block/inlineBlock nodes in a structured text document tree.
|
|
7
7
|
* Calls the provided callback for each block/inlineBlock node found and returns true only if all pass.
|
|
@@ -51,8 +51,8 @@ export function isStructuredTextFieldValueInRequest(value) {
|
|
|
51
51
|
// String ID
|
|
52
52
|
if (isItemId(item))
|
|
53
53
|
return true;
|
|
54
|
-
// Object (
|
|
55
|
-
return
|
|
54
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
55
|
+
return isBlockObjectInRequest(item);
|
|
56
56
|
});
|
|
57
57
|
}
|
|
58
58
|
export function isLocalizedStructuredTextFieldValueInRequest(value) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"structured_text.js","sourceRoot":"","sources":["../../../src/fieldTypes/structured_text.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,SAAS,EACT,OAAO,EACP,UAAU,EACV,aAAa,GACd,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,SA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C,CAAC;AACL,CAAC;AAED,MAAM,UAAU,mDAAmD,CAGjE,KAAc;IAEd,OAAO,CACL,qBAAqB,CAAC,KAAK,CAAC;QAC5B,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,0CAA0C,CAAC,CACvE,CAAC;AACJ,CAAC"}
|
|
@@ -64,7 +64,7 @@ export class Client {
|
|
|
64
64
|
return this.config.baseUrl || Client.defaultBaseUrl;
|
|
65
65
|
}
|
|
66
66
|
request(options) {
|
|
67
|
-
return request(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.4.
|
|
67
|
+
return request(Object.assign(Object.assign(Object.assign({}, this.config), options), { logFn: this.config.logFn || console.log, userAgent: '@datocms/cma-client v5.4.7', baseUrl: this.baseUrl, preCallStack: new Error().stack, extraHeaders: Object.assign(Object.assign(Object.assign({}, (this.config.extraHeaders || {})), (this.config.environment
|
|
68
68
|
? { 'X-Environment': this.config.environment }
|
|
69
69
|
: {})), { 'X-API-Version': '3' }), fetchJobResult: (jobId) => {
|
|
70
70
|
return this.jobResultsFetcher
|
|
@@ -19,14 +19,19 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<T, {
|
|
|
19
19
|
};
|
|
20
20
|
}>;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Type guard that narrows a block to a specific model.
|
|
23
23
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
24
|
+
* Two call styles, same narrowing behavior:
|
|
25
|
+
*
|
|
26
|
+
* - Curried: `isBlockOfType(itemTypeId)` returns a predicate, ideal for
|
|
27
|
+
* `Array#filter` / `Array#find`.
|
|
28
|
+
* - Direct: `isBlockOfType(itemTypeId, block)` checks a single value inline,
|
|
29
|
+
* handy inside `if` statements when you already have the block in hand.
|
|
30
|
+
*
|
|
31
|
+
* The ID generic is inferred from the first argument, so no explicit type
|
|
32
|
+
* parameter is needed. Given any input type `T`, the result narrows to
|
|
33
|
+
* `Extract<T, { relationships: { item_type: { data: { id: Id } } } }>`,
|
|
34
|
+
* which covers:
|
|
30
35
|
*
|
|
31
36
|
* - `ItemInNestedResponse<D>` (responses with `nested: true`)
|
|
32
37
|
* - `ItemCreateSchema<D>` / `ItemUpdateSchema<D>` (request payloads, object
|
|
@@ -45,8 +50,16 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<T, {
|
|
|
45
50
|
* const SESSION_BLOCK_ID = 'abc123' as const;
|
|
46
51
|
*
|
|
47
52
|
* const record = await client.items.find<Schema.ConferenceDay>(id, { nested: true });
|
|
53
|
+
*
|
|
54
|
+
* // Curried — predicate for filter/find
|
|
48
55
|
* const sessions = record.agenda.filter(isBlockOfType(SESSION_BLOCK_ID));
|
|
49
56
|
* sessions[0].attributes.signup_url; // OK — narrowed
|
|
57
|
+
*
|
|
58
|
+
* // Direct — inline check on a single block
|
|
59
|
+
* if (isBlockOfType(SESSION_BLOCK_ID, record.agenda[0])) {
|
|
60
|
+
* record.agenda[0].attributes.signup_url; // OK — narrowed
|
|
61
|
+
* }
|
|
50
62
|
* ```
|
|
51
63
|
*/
|
|
52
64
|
export declare function isBlockOfType<Id extends string>(itemTypeId: Id): <T>(block: T) => block is NarrowBlockByItemType<T, Id>;
|
|
65
|
+
export declare function isBlockOfType<T, Id extends string>(itemTypeId: Id, block: T): block is NarrowBlockByItemType<T, Id>;
|
|
@@ -1,36 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* Call it with the block's `itemTypeId` literal — the ID generic is inferred
|
|
5
|
-
* from the argument, so no explicit type parameter is needed. The returned
|
|
6
|
-
* predicate is generic: given any input type `T`, it narrows to
|
|
7
|
-
* `Extract<T, { relationships: { item_type: { data: { id: Id } } } }>`. It's
|
|
8
|
-
* meant to plug into `Array#filter` / `Array#find` over block-bearing fields
|
|
9
|
-
* in any of these contexts:
|
|
10
|
-
*
|
|
11
|
-
* - `ItemInNestedResponse<D>` (responses with `nested: true`)
|
|
12
|
-
* - `ItemCreateSchema<D>` / `ItemUpdateSchema<D>` (request payloads, object
|
|
13
|
-
* variants; plain string IDs are filtered out — there's no way to narrow
|
|
14
|
-
* them without an external lookup)
|
|
15
|
-
*
|
|
16
|
-
* The default (non-nested) response shape, where block fields are arrays of
|
|
17
|
-
* plain string IDs, is deliberately not supported — the type information is
|
|
18
|
-
* not recoverable from an ID alone.
|
|
19
|
-
*
|
|
20
|
-
* For the literal `Id` to be preserved (and narrowing to work), the argument
|
|
21
|
-
* must be typed as a literal — use `as const` on pre-set ID constants.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* const SESSION_BLOCK_ID = 'abc123' as const;
|
|
26
|
-
*
|
|
27
|
-
* const record = await client.items.find<Schema.ConferenceDay>(id, { nested: true });
|
|
28
|
-
* const sessions = record.agenda.filter(isBlockOfType(SESSION_BLOCK_ID));
|
|
29
|
-
* sessions[0].attributes.signup_url; // OK — narrowed
|
|
30
|
-
* ```
|
|
31
|
-
*/
|
|
32
|
-
export function isBlockOfType(itemTypeId) {
|
|
33
|
-
return (block) => {
|
|
1
|
+
export function isBlockOfType(itemTypeId, ...rest) {
|
|
2
|
+
const check = (block) => {
|
|
34
3
|
if (typeof block !== 'object' || block === null)
|
|
35
4
|
return false;
|
|
36
5
|
const relationships = block.relationships;
|
|
@@ -44,5 +13,8 @@ export function isBlockOfType(itemTypeId) {
|
|
|
44
13
|
return false;
|
|
45
14
|
return data.id === itemTypeId;
|
|
46
15
|
};
|
|
16
|
+
if (rest.length > 0)
|
|
17
|
+
return check(rest[0]);
|
|
18
|
+
return (block) => check(block);
|
|
47
19
|
}
|
|
48
20
|
//# sourceMappingURL=isBlockOfType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isBlockOfType.js","sourceRoot":"","sources":["../../../src/utilities/isBlockOfType.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"isBlockOfType.js","sourceRoot":"","sources":["../../../src/utilities/isBlockOfType.ts"],"names":[],"mappings":"AAiEA,MAAM,UAAU,aAAa,CAC3B,UAAc,EACd,GAAG,IAA2B;IAE9B,MAAM,KAAK,GAAG,CAAC,KAAc,EAAW,EAAE;QACxC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QAC9D,MAAM,aAAa,GAAI,KAAqC,CAAC,aAAa,CAAC;QAC3E,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,KAAK,IAAI;YAC7D,OAAO,KAAK,CAAC;QACf,MAAM,QAAQ,GAAI,aAAyC,CAAC,SAAS,CAAC;QACtE,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QACpE,MAAM,IAAI,GAAI,QAA+B,CAAC,IAAI,CAAC;QACnD,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI;YAAE,OAAO,KAAK,CAAC;QAC5D,OAAQ,IAAyB,CAAC,EAAE,KAAK,UAAU,CAAC;IACtD,CAAC,CAAC;IACF,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC3C,OAAO,CAAI,KAAQ,EAAyC,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAC9E,CAAC"}
|
|
@@ -52,7 +52,7 @@ export type UpdatedBlockInRequest<D extends ItemTypeDefinition = ItemTypeDefinit
|
|
|
52
52
|
__itemTypeId?: D['itemTypeId'];
|
|
53
53
|
type: RawApiTypes.ItemType1;
|
|
54
54
|
id: RawApiTypes.ItemIdentity;
|
|
55
|
-
relationships
|
|
55
|
+
relationships?: RawApiTypes.ItemRelationships<D>;
|
|
56
56
|
meta?: RawApiTypes.ItemMeta;
|
|
57
57
|
attributes: ToItemAttributesInRequest<D>;
|
|
58
58
|
};
|
|
@@ -125,6 +125,25 @@ export declare function isItemWithOptionalMeta<D extends ItemTypeDefinition = It
|
|
|
125
125
|
*/
|
|
126
126
|
export declare function isSingleBlockFieldValue(value: unknown): value is SingleBlockFieldValue;
|
|
127
127
|
export declare function isLocalizedSingleBlockFieldValue(value: unknown): value is LocalizedFieldValue<SingleBlockFieldValue>;
|
|
128
|
+
/**
|
|
129
|
+
* Shape check for a block object on the *request* side. Accepts every object
|
|
130
|
+
* form the CMA allows inside a request payload:
|
|
131
|
+
*
|
|
132
|
+
* 1. New block (no id, full body):
|
|
133
|
+
* { type: 'item', attributes, relationships: { item_type } }
|
|
134
|
+
* 2. Updated block, full body:
|
|
135
|
+
* { type: 'item', id, attributes, relationships: { item_type } }
|
|
136
|
+
* 3. Updated block, id-only (patch some attributes of an existing block):
|
|
137
|
+
* { type: 'item', id, attributes } ← no relationships
|
|
138
|
+
*
|
|
139
|
+
* Case 3 is what `buildBlockRecord` produces when the caller omits
|
|
140
|
+
* `item_type` — the server derives the model from the existing block's id,
|
|
141
|
+
* so re-specifying it in `relationships` is redundant.
|
|
142
|
+
*/
|
|
143
|
+
export declare function isBlockObjectInRequest(block: unknown): block is {
|
|
144
|
+
type: RawApiTypes.ItemType1;
|
|
145
|
+
attributes: Record<string, unknown>;
|
|
146
|
+
};
|
|
128
147
|
/**
|
|
129
148
|
* Type guard for Single Block field values in API request format.
|
|
130
149
|
* Allows block as string ID, full object with ID, or object without ID.
|
|
@@ -19,14 +19,19 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<T, {
|
|
|
19
19
|
};
|
|
20
20
|
}>;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* Type guard that narrows a block to a specific model.
|
|
23
23
|
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
24
|
+
* Two call styles, same narrowing behavior:
|
|
25
|
+
*
|
|
26
|
+
* - Curried: `isBlockOfType(itemTypeId)` returns a predicate, ideal for
|
|
27
|
+
* `Array#filter` / `Array#find`.
|
|
28
|
+
* - Direct: `isBlockOfType(itemTypeId, block)` checks a single value inline,
|
|
29
|
+
* handy inside `if` statements when you already have the block in hand.
|
|
30
|
+
*
|
|
31
|
+
* The ID generic is inferred from the first argument, so no explicit type
|
|
32
|
+
* parameter is needed. Given any input type `T`, the result narrows to
|
|
33
|
+
* `Extract<T, { relationships: { item_type: { data: { id: Id } } } }>`,
|
|
34
|
+
* which covers:
|
|
30
35
|
*
|
|
31
36
|
* - `ItemInNestedResponse<D>` (responses with `nested: true`)
|
|
32
37
|
* - `ItemCreateSchema<D>` / `ItemUpdateSchema<D>` (request payloads, object
|
|
@@ -45,8 +50,16 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<T, {
|
|
|
45
50
|
* const SESSION_BLOCK_ID = 'abc123' as const;
|
|
46
51
|
*
|
|
47
52
|
* const record = await client.items.find<Schema.ConferenceDay>(id, { nested: true });
|
|
53
|
+
*
|
|
54
|
+
* // Curried — predicate for filter/find
|
|
48
55
|
* const sessions = record.agenda.filter(isBlockOfType(SESSION_BLOCK_ID));
|
|
49
56
|
* sessions[0].attributes.signup_url; // OK — narrowed
|
|
57
|
+
*
|
|
58
|
+
* // Direct — inline check on a single block
|
|
59
|
+
* if (isBlockOfType(SESSION_BLOCK_ID, record.agenda[0])) {
|
|
60
|
+
* record.agenda[0].attributes.signup_url; // OK — narrowed
|
|
61
|
+
* }
|
|
50
62
|
* ```
|
|
51
63
|
*/
|
|
52
64
|
export declare function isBlockOfType<Id extends string>(itemTypeId: Id): <T>(block: T) => block is NarrowBlockByItemType<T, Id>;
|
|
65
|
+
export declare function isBlockOfType<T, Id extends string>(itemTypeId: Id, block: T): block is NarrowBlockByItemType<T, Id>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datocms/cma-client",
|
|
3
|
-
"version": "5.4.
|
|
3
|
+
"version": "5.4.7",
|
|
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.4.3",
|
|
46
46
|
"@types/uuid": "^9.0.7"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "4a2443c6f351448a165eef4359a3451ffd5b6b08"
|
|
49
49
|
}
|
|
@@ -8,8 +8,8 @@ import type { RichTextEditorConfiguration } from './appearance/rich_text';
|
|
|
8
8
|
import {
|
|
9
9
|
type BlockInNestedResponse,
|
|
10
10
|
type BlockInRequest,
|
|
11
|
+
isBlockObjectInRequest,
|
|
11
12
|
isItemId,
|
|
12
|
-
isItemWithOptionalIdAndMeta,
|
|
13
13
|
isItemWithOptionalMeta,
|
|
14
14
|
} from './single_block';
|
|
15
15
|
import type { RichTextBlocksValidator } from './validators/rich_text_blocks';
|
|
@@ -129,8 +129,8 @@ export function isRichTextFieldValueInRequest<
|
|
|
129
129
|
// String ID - referencing existing block
|
|
130
130
|
if (isItemId(block)) return true;
|
|
131
131
|
|
|
132
|
-
// Object (
|
|
133
|
-
return
|
|
132
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
133
|
+
return isBlockObjectInRequest(block);
|
|
134
134
|
});
|
|
135
135
|
}
|
|
136
136
|
|
|
@@ -68,7 +68,7 @@ export type UpdatedBlockInRequest<
|
|
|
68
68
|
__itemTypeId?: D['itemTypeId'];
|
|
69
69
|
type: RawApiTypes.ItemType1;
|
|
70
70
|
id: RawApiTypes.ItemIdentity;
|
|
71
|
-
relationships
|
|
71
|
+
relationships?: RawApiTypes.ItemRelationships<D>;
|
|
72
72
|
meta?: RawApiTypes.ItemMeta;
|
|
73
73
|
attributes: ToItemAttributesInRequest<D>;
|
|
74
74
|
};
|
|
@@ -204,6 +204,34 @@ export function isLocalizedSingleBlockFieldValue(
|
|
|
204
204
|
);
|
|
205
205
|
}
|
|
206
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Shape check for a block object on the *request* side. Accepts every object
|
|
209
|
+
* form the CMA allows inside a request payload:
|
|
210
|
+
*
|
|
211
|
+
* 1. New block (no id, full body):
|
|
212
|
+
* { type: 'item', attributes, relationships: { item_type } }
|
|
213
|
+
* 2. Updated block, full body:
|
|
214
|
+
* { type: 'item', id, attributes, relationships: { item_type } }
|
|
215
|
+
* 3. Updated block, id-only (patch some attributes of an existing block):
|
|
216
|
+
* { type: 'item', id, attributes } ← no relationships
|
|
217
|
+
*
|
|
218
|
+
* Case 3 is what `buildBlockRecord` produces when the caller omits
|
|
219
|
+
* `item_type` — the server derives the model from the existing block's id,
|
|
220
|
+
* so re-specifying it in `relationships` is redundant.
|
|
221
|
+
*/
|
|
222
|
+
export function isBlockObjectInRequest(block: unknown): block is {
|
|
223
|
+
type: RawApiTypes.ItemType1;
|
|
224
|
+
attributes: Record<string, unknown>;
|
|
225
|
+
} {
|
|
226
|
+
return (
|
|
227
|
+
typeof block === 'object' &&
|
|
228
|
+
block !== null &&
|
|
229
|
+
'type' in block &&
|
|
230
|
+
block.type === 'item' &&
|
|
231
|
+
'attributes' in block
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
|
|
207
235
|
/**
|
|
208
236
|
* Type guard for Single Block field values in API request format.
|
|
209
237
|
* Allows block as string ID, full object with ID, or object without ID.
|
|
@@ -216,8 +244,9 @@ export function isSingleBlockFieldValueInRequest<
|
|
|
216
244
|
// String ID - referencing existing block
|
|
217
245
|
if (isItemId(value)) return true;
|
|
218
246
|
|
|
219
|
-
// Object (either with or without ID for updates/creation
|
|
220
|
-
|
|
247
|
+
// Object (either with or without ID for updates/creation, including
|
|
248
|
+
// id-only updates without `relationships`)
|
|
249
|
+
return isBlockObjectInRequest(value);
|
|
221
250
|
}
|
|
222
251
|
|
|
223
252
|
export function isLocalizedSingleBlockFieldValueInRequest<
|
|
@@ -18,8 +18,8 @@ import type { StructuredTextEditorConfiguration } from './appearance/structured_
|
|
|
18
18
|
import {
|
|
19
19
|
type BlockInNestedResponse,
|
|
20
20
|
type BlockInRequest,
|
|
21
|
+
isBlockObjectInRequest,
|
|
21
22
|
isItemId,
|
|
22
|
-
isItemWithOptionalIdAndMeta,
|
|
23
23
|
isItemWithOptionalMeta,
|
|
24
24
|
} from './single_block';
|
|
25
25
|
import type { RequiredValidator } from './validators';
|
|
@@ -217,8 +217,8 @@ export function isStructuredTextFieldValueInRequest<
|
|
|
217
217
|
// String ID
|
|
218
218
|
if (isItemId(item)) return true;
|
|
219
219
|
|
|
220
|
-
// Object (
|
|
221
|
-
return
|
|
220
|
+
// Object (new, updated, or id-only update — with or without `relationships`)
|
|
221
|
+
return isBlockObjectInRequest(item);
|
|
222
222
|
});
|
|
223
223
|
}
|
|
224
224
|
|
package/src/generated/Client.ts
CHANGED
|
@@ -151,7 +151,7 @@ export class Client {
|
|
|
151
151
|
...this.config,
|
|
152
152
|
...options,
|
|
153
153
|
logFn: this.config.logFn || console.log,
|
|
154
|
-
userAgent: '@datocms/cma-client v5.4.
|
|
154
|
+
userAgent: '@datocms/cma-client v5.4.7',
|
|
155
155
|
baseUrl: this.baseUrl,
|
|
156
156
|
preCallStack: new Error().stack,
|
|
157
157
|
extraHeaders: {
|
|
@@ -14,14 +14,19 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<
|
|
|
14
14
|
>;
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* Type guard that narrows a block to a specific model.
|
|
18
18
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
19
|
+
* Two call styles, same narrowing behavior:
|
|
20
|
+
*
|
|
21
|
+
* - Curried: `isBlockOfType(itemTypeId)` returns a predicate, ideal for
|
|
22
|
+
* `Array#filter` / `Array#find`.
|
|
23
|
+
* - Direct: `isBlockOfType(itemTypeId, block)` checks a single value inline,
|
|
24
|
+
* handy inside `if` statements when you already have the block in hand.
|
|
25
|
+
*
|
|
26
|
+
* The ID generic is inferred from the first argument, so no explicit type
|
|
27
|
+
* parameter is needed. Given any input type `T`, the result narrows to
|
|
28
|
+
* `Extract<T, { relationships: { item_type: { data: { id: Id } } } }>`,
|
|
29
|
+
* which covers:
|
|
25
30
|
*
|
|
26
31
|
* - `ItemInNestedResponse<D>` (responses with `nested: true`)
|
|
27
32
|
* - `ItemCreateSchema<D>` / `ItemUpdateSchema<D>` (request payloads, object
|
|
@@ -40,14 +45,29 @@ export type NarrowBlockByItemType<T, Id extends string> = Extract<
|
|
|
40
45
|
* const SESSION_BLOCK_ID = 'abc123' as const;
|
|
41
46
|
*
|
|
42
47
|
* const record = await client.items.find<Schema.ConferenceDay>(id, { nested: true });
|
|
48
|
+
*
|
|
49
|
+
* // Curried — predicate for filter/find
|
|
43
50
|
* const sessions = record.agenda.filter(isBlockOfType(SESSION_BLOCK_ID));
|
|
44
51
|
* sessions[0].attributes.signup_url; // OK — narrowed
|
|
52
|
+
*
|
|
53
|
+
* // Direct — inline check on a single block
|
|
54
|
+
* if (isBlockOfType(SESSION_BLOCK_ID, record.agenda[0])) {
|
|
55
|
+
* record.agenda[0].attributes.signup_url; // OK — narrowed
|
|
56
|
+
* }
|
|
45
57
|
* ```
|
|
46
58
|
*/
|
|
47
59
|
export function isBlockOfType<Id extends string>(
|
|
48
60
|
itemTypeId: Id,
|
|
49
|
-
): <T>(block: T) => block is NarrowBlockByItemType<T, Id
|
|
50
|
-
|
|
61
|
+
): <T>(block: T) => block is NarrowBlockByItemType<T, Id>;
|
|
62
|
+
export function isBlockOfType<T, Id extends string>(
|
|
63
|
+
itemTypeId: Id,
|
|
64
|
+
block: T,
|
|
65
|
+
): block is NarrowBlockByItemType<T, Id>;
|
|
66
|
+
export function isBlockOfType<Id extends string>(
|
|
67
|
+
itemTypeId: Id,
|
|
68
|
+
...rest: [block: unknown] | []
|
|
69
|
+
): boolean | (<T>(block: T) => block is NarrowBlockByItemType<T, Id>) {
|
|
70
|
+
const check = (block: unknown): boolean => {
|
|
51
71
|
if (typeof block !== 'object' || block === null) return false;
|
|
52
72
|
const relationships = (block as { relationships?: unknown }).relationships;
|
|
53
73
|
if (typeof relationships !== 'object' || relationships === null)
|
|
@@ -58,4 +78,6 @@ export function isBlockOfType<Id extends string>(
|
|
|
58
78
|
if (typeof data !== 'object' || data === null) return false;
|
|
59
79
|
return (data as { id?: unknown }).id === itemTypeId;
|
|
60
80
|
};
|
|
81
|
+
if (rest.length > 0) return check(rest[0]);
|
|
82
|
+
return <T>(block: T): block is NarrowBlockByItemType<T, Id> => check(block);
|
|
61
83
|
}
|