@datocms/cma-client 5.2.0-alpha.4 → 5.2.0-alpha.5

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.
Files changed (31) hide show
  1. package/README.md +4 -0
  2. package/dist/cjs/generated/Client.js +1 -1
  3. package/dist/cjs/generated/resources/ItemType.js +12 -12
  4. package/dist/cjs/generated/resources/SearchIndex.js +10 -22
  5. package/dist/cjs/generated/resources/SearchIndex.js.map +1 -1
  6. package/dist/cjs/utilities/schemaRepository.js +131 -0
  7. package/dist/cjs/utilities/schemaRepository.js.map +1 -1
  8. package/dist/esm/generated/ApiTypes.d.ts +161 -158
  9. package/dist/esm/generated/Client.js +1 -1
  10. package/dist/esm/generated/RawApiTypes.d.ts +148 -141
  11. package/dist/esm/generated/resources/ItemType.d.ts +12 -12
  12. package/dist/esm/generated/resources/ItemType.js +12 -12
  13. package/dist/esm/generated/resources/SearchIndex.d.ts +8 -8
  14. package/dist/esm/generated/resources/SearchIndex.js +10 -22
  15. package/dist/esm/generated/resources/SearchIndex.js.map +1 -1
  16. package/dist/esm/utilities/schemaRepository.d.ts +42 -2
  17. package/dist/esm/utilities/schemaRepository.js +132 -1
  18. package/dist/esm/utilities/schemaRepository.js.map +1 -1
  19. package/dist/types/generated/ApiTypes.d.ts +161 -158
  20. package/dist/types/generated/RawApiTypes.d.ts +148 -141
  21. package/dist/types/generated/resources/ItemType.d.ts +12 -12
  22. package/dist/types/generated/resources/SearchIndex.d.ts +8 -8
  23. package/dist/types/utilities/schemaRepository.d.ts +42 -2
  24. package/package.json +3 -3
  25. package/resources.json +10 -12
  26. package/src/generated/ApiTypes.ts +161 -158
  27. package/src/generated/Client.ts +1 -1
  28. package/src/generated/RawApiTypes.ts +148 -141
  29. package/src/generated/resources/ItemType.ts +12 -12
  30. package/src/generated/resources/SearchIndex.ts +10 -22
  31. package/src/utilities/schemaRepository.ts +167 -9
@@ -7,7 +7,7 @@ export default class ItemType extends BaseResource {
7
7
  static readonly TYPE = 'item_type' as const;
8
8
 
9
9
  /**
10
- * Create a new model
10
+ * Create a new model/block model
11
11
  *
12
12
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/create
13
13
  *
@@ -55,7 +55,7 @@ export default class ItemType extends BaseResource {
55
55
  }
56
56
 
57
57
  /**
58
- * Create a new model
58
+ * Create a new model/block model
59
59
  *
60
60
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/create
61
61
  *
@@ -75,7 +75,7 @@ export default class ItemType extends BaseResource {
75
75
  }
76
76
 
77
77
  /**
78
- * Update a model
78
+ * Update a model/block model
79
79
  *
80
80
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/update
81
81
  *
@@ -125,7 +125,7 @@ export default class ItemType extends BaseResource {
125
125
  }
126
126
 
127
127
  /**
128
- * Update a model
128
+ * Update a model/block model
129
129
  *
130
130
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/update
131
131
  *
@@ -144,7 +144,7 @@ export default class ItemType extends BaseResource {
144
144
  }
145
145
 
146
146
  /**
147
- * List all models
147
+ * List all models/block models
148
148
  *
149
149
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/instances
150
150
  *
@@ -160,7 +160,7 @@ export default class ItemType extends BaseResource {
160
160
  }
161
161
 
162
162
  /**
163
- * List all models
163
+ * List all models/block models
164
164
  *
165
165
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/instances
166
166
  *
@@ -175,7 +175,7 @@ export default class ItemType extends BaseResource {
175
175
  }
176
176
 
177
177
  /**
178
- * Retrieve a model
178
+ * Retrieve a model/block model
179
179
  *
180
180
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/self
181
181
  *
@@ -189,7 +189,7 @@ export default class ItemType extends BaseResource {
189
189
  }
190
190
 
191
191
  /**
192
- * Retrieve a model
192
+ * Retrieve a model/block model
193
193
  *
194
194
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/self
195
195
  *
@@ -204,7 +204,7 @@ export default class ItemType extends BaseResource {
204
204
  }
205
205
 
206
206
  /**
207
- * Duplicate model
207
+ * Duplicate model/block model
208
208
  *
209
209
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/duplicate
210
210
  *
@@ -220,7 +220,7 @@ export default class ItemType extends BaseResource {
220
220
  }
221
221
 
222
222
  /**
223
- * Duplicate model
223
+ * Duplicate model/block model
224
224
  *
225
225
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/duplicate
226
226
  *
@@ -237,7 +237,7 @@ export default class ItemType extends BaseResource {
237
237
  }
238
238
 
239
239
  /**
240
- * Delete a model
240
+ * Delete a model/block model
241
241
  *
242
242
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/destroy
243
243
  *
@@ -254,7 +254,7 @@ export default class ItemType extends BaseResource {
254
254
  }
255
255
 
256
256
  /**
257
- * Delete a model
257
+ * Delete a model/block model
258
258
  *
259
259
  * Read more: https://www.datocms.com/docs/content-management-api/resources/item-type/destroy
260
260
  *
@@ -69,7 +69,7 @@ export default class SearchIndex extends BaseResource {
69
69
  }
70
70
 
71
71
  /**
72
- * Create search index
72
+ * Create a search index
73
73
  *
74
74
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/create
75
75
  *
@@ -80,13 +80,7 @@ export default class SearchIndex extends BaseResource {
80
80
  return this.rawCreate(
81
81
  Utils.serializeRequestBody<RawApiTypes.SearchIndexCreateSchema>(body, {
82
82
  type: 'search_index',
83
- attributes: [
84
- 'name',
85
- 'enabled',
86
- 'build_trigger_indexing_enabled',
87
- 'frontend_url',
88
- 'user_agent_suffix',
89
- ],
83
+ attributes: ['name', 'enabled', 'frontend_url', 'user_agent_suffix'],
90
84
  relationships: ['build_triggers'],
91
85
  }),
92
86
  ).then((body) =>
@@ -97,7 +91,7 @@ export default class SearchIndex extends BaseResource {
97
91
  }
98
92
 
99
93
  /**
100
- * Create search index
94
+ * Create a search index
101
95
  *
102
96
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/create
103
97
  *
@@ -115,7 +109,7 @@ export default class SearchIndex extends BaseResource {
115
109
  }
116
110
 
117
111
  /**
118
- * Update search index
112
+ * Update a search index
119
113
  *
120
114
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/update
121
115
  *
@@ -131,13 +125,7 @@ export default class SearchIndex extends BaseResource {
131
125
  Utils.serializeRequestBody<RawApiTypes.SearchIndexUpdateSchema>(body, {
132
126
  id: Utils.toId(searchIndexId),
133
127
  type: 'search_index',
134
- attributes: [
135
- 'name',
136
- 'enabled',
137
- 'build_trigger_indexing_enabled',
138
- 'frontend_url',
139
- 'user_agent_suffix',
140
- ],
128
+ attributes: ['name', 'enabled', 'frontend_url', 'user_agent_suffix'],
141
129
  relationships: ['build_triggers'],
142
130
  }),
143
131
  ).then((body) =>
@@ -148,7 +136,7 @@ export default class SearchIndex extends BaseResource {
148
136
  }
149
137
 
150
138
  /**
151
- * Update search index
139
+ * Update a search index
152
140
  *
153
141
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/update
154
142
  *
@@ -167,7 +155,7 @@ export default class SearchIndex extends BaseResource {
167
155
  }
168
156
 
169
157
  /**
170
- * Trigger a site search indexing
158
+ * Trigger the indexing process
171
159
  *
172
160
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/trigger
173
161
  *
@@ -179,7 +167,7 @@ export default class SearchIndex extends BaseResource {
179
167
  }
180
168
 
181
169
  /**
182
- * Trigger a site search indexing
170
+ * Trigger the indexing process
183
171
  *
184
172
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/trigger
185
173
  *
@@ -194,7 +182,7 @@ export default class SearchIndex extends BaseResource {
194
182
  }
195
183
 
196
184
  /**
197
- * Abort a site search spidering and mark it as failed
185
+ * Abort a the current indexing process and mark it as failed
198
186
  *
199
187
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/abort
200
188
  *
@@ -206,7 +194,7 @@ export default class SearchIndex extends BaseResource {
206
194
  }
207
195
 
208
196
  /**
209
- * Abort a site search spidering and mark it as failed
197
+ * Abort a the current indexing process and mark it as failed
210
198
  *
211
199
  * Read more: https://www.datocms.com/docs/content-management-api/resources/search-index/abort
212
200
  *
@@ -1,7 +1,10 @@
1
1
  import { deserializeResponseBody } from '@datocms/rest-client-utils';
2
2
  import type * as ApiTypes from '../generated/ApiTypes';
3
3
  import type * as RawApiTypes from '../generated/RawApiTypes';
4
- import { blockModelIdsReferencedInField } from './fieldsContainingReferences';
4
+ import {
5
+ blockModelIdsReferencedInField,
6
+ modelIdsReferencedInField,
7
+ } from './fieldsContainingReferences';
5
8
 
6
9
  interface GenericClient {
7
10
  itemTypes: {
@@ -265,9 +268,7 @@ export class SchemaRepository {
265
268
  async getItemTypeFields(
266
269
  itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
267
270
  ): Promise<ApiTypes.Field[]> {
268
- const rawResult = await this.getRawItemTypeFields(
269
- itemType as RawApiTypes.ItemType,
270
- );
271
+ const rawResult = await this.getRawItemTypeFields(itemType);
271
272
  return deserializeResponseBody<ApiTypes.Field[]>({
272
273
  data: rawResult,
273
274
  });
@@ -280,7 +281,7 @@ export class SchemaRepository {
280
281
  * @returns Promise that resolves to an array of fields
281
282
  */
282
283
  async getRawItemTypeFields(
283
- itemType: RawApiTypes.ItemType,
284
+ itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
284
285
  ): Promise<RawApiTypes.Field[]> {
285
286
  // Check if we already have the fields cached
286
287
  const cachedFields = this.fieldsByItemType.get(itemType.id);
@@ -304,9 +305,7 @@ export class SchemaRepository {
304
305
  async getItemTypeFieldsets(
305
306
  itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
306
307
  ): Promise<ApiTypes.Fieldset[]> {
307
- const rawResult = await this.getRawItemTypeFieldsets(
308
- itemType as RawApiTypes.ItemType,
309
- );
308
+ const rawResult = await this.getRawItemTypeFieldsets(itemType);
310
309
  return deserializeResponseBody<ApiTypes.Fieldset[]>({
311
310
  data: rawResult,
312
311
  });
@@ -319,7 +318,7 @@ export class SchemaRepository {
319
318
  * @returns Promise that resolves to an array of fieldsets
320
319
  */
321
320
  async getRawItemTypeFieldsets(
322
- itemType: RawApiTypes.ItemType,
321
+ itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
323
322
  ): Promise<RawApiTypes.Fieldset[]> {
324
323
  // Check if we already have the fieldsets cached
325
324
  const cachedFieldsets = this.fieldsetsByItemType.get(itemType.id);
@@ -589,4 +588,163 @@ export class SchemaRepository {
589
588
  data: rawResult,
590
589
  });
591
590
  }
591
+
592
+ /**
593
+ * Gets all block models that are directly or indirectly nested within the given item types.
594
+ * This method recursively traverses the schema to find all blocks that are nested
595
+ * within the provided item types, either directly through block fields or indirectly through
596
+ * other nested block models.
597
+ *
598
+ * @param itemTypes - Array of item types to find nested blocks for
599
+ * @returns Promise that resolves to array of all block models nested in these item types
600
+ */
601
+ async getRawNestedBlocks(
602
+ itemTypes: Array<ApiTypes.ItemType | RawApiTypes.ItemType>,
603
+ ): Promise<Array<RawApiTypes.ItemType>> {
604
+ await this.prefetchAllModelsAndFields();
605
+
606
+ const allItemTypes = await this.getAllRawItemTypes();
607
+ const visited = new Set<string>();
608
+ const nestedBlocks: Array<RawApiTypes.ItemType> = [];
609
+
610
+ // Helper function to recursively find nested blocks
611
+ const findNestedBlocks = async (
612
+ itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
613
+ alreadyExplored: Set<string> = new Set(),
614
+ ): Promise<void> => {
615
+ if (alreadyExplored.has(itemType.id)) {
616
+ return;
617
+ }
618
+
619
+ alreadyExplored.add(itemType.id);
620
+
621
+ const fields = await this.getRawItemTypeFields(itemType);
622
+
623
+ for (const field of fields) {
624
+ const referencedBlockIds = blockModelIdsReferencedInField(field);
625
+
626
+ for (const blockId of referencedBlockIds) {
627
+ if (!visited.has(blockId)) {
628
+ visited.add(blockId);
629
+ const nestedBlock = allItemTypes.find((it) => it.id === blockId);
630
+ if (nestedBlock) {
631
+ nestedBlocks.push(nestedBlock);
632
+ // Recursively find blocks nested in this block
633
+ await findNestedBlocks(nestedBlock, new Set(alreadyExplored));
634
+ }
635
+ }
636
+ }
637
+ }
638
+ };
639
+
640
+ // Find nested blocks for each provided item type
641
+ for (const itemType of itemTypes) {
642
+ await findNestedBlocks(itemType);
643
+ }
644
+
645
+ return nestedBlocks;
646
+ }
647
+
648
+ /**
649
+ * Gets all block models that are directly or indirectly nested within the given item types.
650
+ * This method recursively traverses the schema to find all blocks that are nested
651
+ * within the provided item types, either directly through block fields or indirectly through
652
+ * other nested block models.
653
+ *
654
+ * @param itemTypes - Array of item types to find nested blocks for
655
+ * @returns Promise that resolves to array of all block models nested in these item types
656
+ */
657
+ async getNestedBlocks(
658
+ itemTypes: Array<ApiTypes.ItemType | RawApiTypes.ItemType>,
659
+ ): Promise<Array<ApiTypes.ItemType>> {
660
+ const rawResult = await this.getRawNestedBlocks(itemTypes);
661
+ return deserializeResponseBody<ApiTypes.ItemType[]>({
662
+ data: rawResult,
663
+ });
664
+ }
665
+
666
+ /**
667
+ * Gets all models that are directly or indirectly nested/referenced within the given item types.
668
+ * This method recursively traverses the schema to find all models that are referenced
669
+ * by the provided item types through link fields, either directly or indirectly through
670
+ * other referenced blocks.
671
+ *
672
+ * @param itemTypes - Array of item types to find nested models for
673
+ * @returns Promise that resolves to array of all models nested in these item types
674
+ */
675
+ async getRawNestedModels(
676
+ itemTypes: Array<ApiTypes.ItemType | RawApiTypes.ItemType>,
677
+ ): Promise<Array<RawApiTypes.ItemType>> {
678
+ await this.prefetchAllModelsAndFields();
679
+
680
+ const allItemTypes = await this.getAllRawItemTypes();
681
+ const visited = new Set<string>();
682
+ const nestedModels: Array<RawApiTypes.ItemType> = [];
683
+
684
+ // Helper function to recursively find nested models
685
+ const findNestedModels = async (
686
+ itemType: ApiTypes.ItemType | RawApiTypes.ItemType,
687
+ alreadyExplored: Set<string> = new Set(),
688
+ ): Promise<void> => {
689
+ if (alreadyExplored.has(itemType.id)) {
690
+ return;
691
+ }
692
+
693
+ alreadyExplored.add(itemType.id);
694
+
695
+ const fields = await this.getRawItemTypeFields(itemType);
696
+
697
+ for (const field of fields) {
698
+ // Find models directly referenced via link fields
699
+ const referencedModelIds = modelIdsReferencedInField(field);
700
+
701
+ for (const modelId of referencedModelIds) {
702
+ if (!visited.has(modelId)) {
703
+ visited.add(modelId);
704
+ const nestedModel = allItemTypes.find((it) => it.id === modelId);
705
+ if (nestedModel) {
706
+ nestedModels.push(nestedModel);
707
+ // Do NOT recurse into models, only into blocks
708
+ }
709
+ }
710
+ }
711
+
712
+ // Find blocks referenced via block fields, then recursively find models in those blocks
713
+ const referencedBlockIds = blockModelIdsReferencedInField(field);
714
+
715
+ for (const blockId of referencedBlockIds) {
716
+ const nestedBlock = allItemTypes.find((it) => it.id === blockId);
717
+ if (nestedBlock) {
718
+ // Recursively find models nested in this block
719
+ await findNestedModels(nestedBlock, new Set(alreadyExplored));
720
+ }
721
+ }
722
+ }
723
+ };
724
+
725
+ // Find nested models for each provided item type
726
+ for (const itemType of itemTypes) {
727
+ await findNestedModels(itemType);
728
+ }
729
+
730
+ return nestedModels;
731
+ }
732
+
733
+ /**
734
+ * Gets all models that are directly or indirectly nested/referenced within the given item types.
735
+ * This method recursively traverses the schema to find all models that are referenced
736
+ * by the provided item types through link fields, either directly or indirectly through
737
+ * other referenced blocks.
738
+ *
739
+ * @param itemTypes - Array of item types to find nested models for
740
+ * @returns Promise that resolves to array of all models nested in these item types
741
+ */
742
+ async getNestedModels(
743
+ itemTypes: Array<ApiTypes.ItemType | RawApiTypes.ItemType>,
744
+ ): Promise<Array<ApiTypes.ItemType>> {
745
+ const rawResult = await this.getRawNestedModels(itemTypes);
746
+ return deserializeResponseBody<ApiTypes.ItemType[]>({
747
+ data: rawResult,
748
+ });
749
+ }
592
750
  }