@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-unit.9 → 0.24.14-alpha.jp-update-cr-links-remove-recursion.6

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.
@@ -80,6 +80,93 @@ class CustomTypesManager extends BaseManager.BaseManager {
80
80
  errors: errors2
81
81
  };
82
82
  }
83
+ updateCRCustomType(args) {
84
+ const { previousPath, newPath } = args;
85
+ const customType = shallowClone(args.customType);
86
+ const previousCustomTypeId = previousPath[0];
87
+ const newCustomTypeId = newPath[0];
88
+ if (!previousCustomTypeId || !newCustomTypeId) {
89
+ throw new Error("Didn't find any customtype id, which should not be possible");
90
+ }
91
+ if (typeof customType === "string") {
92
+ return customType;
93
+ }
94
+ if (customType.fields) {
95
+ const newFields = customType.fields.map((fieldArg) => {
96
+ const field = shallowClone(fieldArg);
97
+ const previousFieldId = previousPath[1];
98
+ const newFieldId = newPath[1];
99
+ if (!previousFieldId || !newFieldId) {
100
+ return field;
101
+ }
102
+ if (typeof field === "string") {
103
+ if (field === previousFieldId && field !== newFieldId) {
104
+ return newFieldId;
105
+ }
106
+ return field;
107
+ }
108
+ if (field.id === previousFieldId && field.id !== newFieldId) {
109
+ field.id = newFieldId;
110
+ }
111
+ return {
112
+ ...field,
113
+ customtypes: field.customtypes.map((customTypeArg) => {
114
+ const customType2 = shallowClone(customTypeArg);
115
+ if (!previousCustomTypeId || !newCustomTypeId) {
116
+ throw new Error("Didn't find any customtype id, which should not be possible");
117
+ }
118
+ if (typeof customType2 === "string") {
119
+ return customType2;
120
+ }
121
+ if (customType2.fields) {
122
+ return {
123
+ ...customType2,
124
+ fields: customType2.fields.map((fieldArg2) => {
125
+ const field2 = shallowClone(fieldArg2);
126
+ if (field2 === previousFieldId && field2 !== newFieldId) {
127
+ return newFieldId;
128
+ }
129
+ return field2;
130
+ })
131
+ };
132
+ }
133
+ return customType2;
134
+ })
135
+ };
136
+ });
137
+ return { ...customType, fields: newFields };
138
+ }
139
+ return customType;
140
+ }
141
+ /**
142
+ * Map over the custom types of a Content Relationship Link and update the API
143
+ * IDs that were changed during the custom type update.
144
+ */
145
+ updateCRCustomTypes(args) {
146
+ const { customTypes, ...updateMeta } = args;
147
+ return customTypes.map((customType) => {
148
+ return this.updateCRCustomType({ customType, ...updateMeta });
149
+ });
150
+ }
151
+ /**
152
+ * Update the Content Relationship API IDs of a single field. The change is
153
+ * determined by the `previousPath` and `newPath` properties.
154
+ */
155
+ updateFieldContentRelationships(args) {
156
+ var _a, _b;
157
+ const { field, ...updateMeta } = args;
158
+ if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
159
+ return field;
160
+ }
161
+ const newCustomTypes = this.updateCRCustomTypes({
162
+ ...updateMeta,
163
+ customTypes: field.config.customtypes
164
+ });
165
+ return {
166
+ ...field,
167
+ config: { ...field.config, customtypes: newCustomTypes }
168
+ };
169
+ }
83
170
  /**
84
171
  * Update the Content Relationship API IDs for all existing custom types and
85
172
  * slices. The change is determined by properties inside the `updateMeta`
@@ -95,34 +182,43 @@ class CustomTypesManager extends BaseManager.BaseManager {
95
182
  newPath = [model.id, ...newPath];
96
183
  const crUpdates = [];
97
184
  const customTypes = await this.readAllCustomTypes();
98
- updateCustomTypeContentRelationships({
99
- models: customTypes.models,
100
- onUpdate: (model2) => {
101
- var _a;
102
- pushIfDefined(crUpdates, (_a = this.sliceMachinePluginRunner) == null ? void 0 : _a.callHook("custom-type:update", {
103
- model: model2
104
- }));
105
- },
106
- previousPath,
107
- newPath
108
- });
185
+ for (const customType of customTypes.models) {
186
+ const updatedCustomTypeModel = customtypes.traverseCustomType({
187
+ customType: customType.model,
188
+ onField: ({ field }) => {
189
+ return this.updateFieldContentRelationships({
190
+ field,
191
+ previousPath,
192
+ newPath
193
+ });
194
+ }
195
+ });
196
+ crUpdates.push(this.sliceMachinePluginRunner.callHook("custom-type:update", {
197
+ model: updatedCustomTypeModel
198
+ }));
199
+ }
109
200
  const { libraries } = await this.slices.readAllSliceLibraries();
110
201
  for (const library of libraries) {
111
202
  const slices = await this.slices.readAllSlicesForLibrary({
112
203
  libraryID: library.libraryID
113
204
  });
114
- updateSharedSliceContentRelationships({
115
- models: slices.models,
116
- onUpdate: (model2) => {
117
- var _a;
118
- pushIfDefined(crUpdates, (_a = this.sliceMachinePluginRunner) == null ? void 0 : _a.callHook("slice:update", {
119
- libraryID: library.libraryID,
120
- model: model2
121
- }));
122
- },
123
- previousPath,
124
- newPath
125
- });
205
+ for (const slice of slices.models) {
206
+ const updatedSliceModel = customtypes.traverseSharedSlice({
207
+ path: ["."],
208
+ slice: slice.model,
209
+ onField: ({ field }) => {
210
+ return this.updateFieldContentRelationships({
211
+ field,
212
+ previousPath,
213
+ newPath
214
+ });
215
+ }
216
+ });
217
+ crUpdates.push(this.sliceMachinePluginRunner.callHook("slice:update", {
218
+ libraryID: library.libraryID,
219
+ model: updatedSliceModel
220
+ }));
221
+ }
126
222
  }
127
223
  const crUpdatesResult = await Promise.all(crUpdates);
128
224
  if (crUpdatesResult.some((result) => result.errors.length > 0)) {
@@ -287,128 +383,11 @@ const InferSliceResponse = index.default.object({
287
383
  }),
288
384
  langSmithUrl: index.default.string().url().optional()
289
385
  });
290
- function updateCRCustomType(args) {
291
- const { previousPath, newPath } = args;
292
- const customType = shallowCloneIfObject(args.customType);
293
- const previousId = previousPath[0];
294
- const newId = newPath[0];
295
- if (!previousId || !newId || typeof customType === "string") {
296
- return customType;
297
- }
298
- if (customType.fields) {
299
- const newFields = customType.fields.map((fieldArg) => {
300
- const field = shallowCloneIfObject(fieldArg);
301
- const previousId2 = previousPath[1];
302
- const newId2 = newPath[1];
303
- if (!previousId2 || !newId2) {
304
- return field;
305
- }
306
- if (typeof field === "string") {
307
- if (field === previousId2 && field !== newId2) {
308
- return newId2;
309
- }
310
- return field;
311
- }
312
- if (field.id === previousId2 && field.id !== newId2) {
313
- field.id = newId2;
314
- }
315
- return {
316
- ...field,
317
- customtypes: field.customtypes.map((customTypeArg) => {
318
- const customType2 = shallowCloneIfObject(customTypeArg);
319
- const previousId3 = previousPath[0];
320
- const newId3 = newPath[0];
321
- if (!previousId3 || !newId3 || typeof customType2 === "string") {
322
- return customType2;
323
- }
324
- if (customType2.fields) {
325
- return {
326
- ...customType2,
327
- fields: customType2.fields.map((fieldArg2) => {
328
- const field2 = shallowCloneIfObject(fieldArg2);
329
- const previousId4 = previousPath[1];
330
- const newId4 = newPath[1];
331
- if (field2 === previousId4 && field2 !== newId4) {
332
- return newId4;
333
- }
334
- return field2;
335
- })
336
- };
337
- }
338
- return customType2;
339
- })
340
- };
341
- });
342
- return { ...customType, fields: newFields };
343
- }
344
- return customType;
345
- }
346
- function updateCRCustomTypes(args) {
347
- const { customTypes, ...updateMeta } = args;
348
- return customTypes.map((customType) => {
349
- return updateCRCustomType({ customType, ...updateMeta });
350
- });
351
- }
352
- function updateFieldContentRelationships(args) {
353
- var _a, _b;
354
- const { field, ...updateMeta } = args;
355
- if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
356
- return field;
357
- }
358
- const newCustomTypes = updateCRCustomTypes({
359
- ...updateMeta,
360
- customTypes: field.config.customtypes
361
- });
362
- return {
363
- ...field,
364
- config: { ...field.config, customtypes: newCustomTypes }
365
- };
366
- }
367
- function updateCustomTypeContentRelationships(args) {
368
- const { models, previousPath, newPath, onUpdate } = args;
369
- for (const customType of models) {
370
- const updatedCustomTypeModel = customtypes.traverseCustomType({
371
- customType: customType.model,
372
- onField: ({ field }) => {
373
- return updateFieldContentRelationships({
374
- field,
375
- previousPath,
376
- newPath
377
- });
378
- }
379
- });
380
- onUpdate(updatedCustomTypeModel);
381
- }
382
- }
383
- function updateSharedSliceContentRelationships(args) {
384
- const { models, previousPath, newPath, onUpdate } = args;
385
- for (const slice of models) {
386
- const updatedSliceModel = customtypes.traverseSharedSlice({
387
- path: ["."],
388
- slice: slice.model,
389
- onField: ({ field }) => {
390
- return updateFieldContentRelationships({
391
- field,
392
- previousPath,
393
- newPath
394
- });
395
- }
396
- });
397
- onUpdate(updatedSliceModel);
398
- }
399
- }
400
- function shallowCloneIfObject(value) {
386
+ function shallowClone(value) {
401
387
  if (typeof value === "object") {
402
388
  return { ...value };
403
389
  }
404
390
  return value;
405
391
  }
406
- function pushIfDefined(array, value) {
407
- if (value) {
408
- array.push(value);
409
- }
410
- }
411
392
  exports.CustomTypesManager = CustomTypesManager;
412
- exports.updateCustomTypeContentRelationships = updateCustomTypeContentRelationships;
413
- exports.updateSharedSliceContentRelationships = updateSharedSliceContentRelationships;
414
393
  //# sourceMappingURL=CustomTypesManager.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"CustomTypesManager.cjs","sources":["../../../../src/managers/customTypes/CustomTypesManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport {\n\tCustomType,\n\tGroup,\n\tNestableWidget,\n\tNestedGroup,\n\tSharedSlice,\n\tUID,\n\ttraverseCustomType,\n\ttraverseSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\nimport {\n\tCallHookReturnType,\n\tCustomTypeCreateHook,\n\tCustomTypeCreateHookData,\n\tCustomTypeReadHookData,\n\tCustomTypeRenameHook,\n\tCustomTypeRenameHookData,\n\tCustomTypeUpdateHook,\n\tCustomTypeUpdateHookData,\n\tHookError,\n} from \"@slicemachine/plugin-kit\";\nimport { z } from \"zod\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport fetch from \"../../lib/fetch\";\n\nimport { OnlyHookErrors } from \"../../types\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\nimport { CustomTypeFormat } from \"./types\";\n\ntype SliceMachineManagerReadCustomTypeLibraryReturnType = {\n\tids: string[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypesManagerReadAllCustomTypesArgs = {\n\tformat: CustomTypeFormat;\n};\n\ntype SliceMachineManagerReadAllCustomTypeReturnType = {\n\tmodels: { model: CustomType }[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerReadCustomTypeReturnType = {\n\tmodel: CustomType | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerPushCustomTypeArgs = {\n\tid: string;\n\tuserAgent?: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType = {\n\t// TODO\n\tmocksConfig?: Record<string, unknown>;\n\terrors: HookError[];\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n\t// TODO\n\tmocksConfig: Record<string, unknown>;\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType = {\n\terrors: HookError[];\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeArgs = {\n\tid: string;\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeReturnType = {\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypeFieldIdChangedMeta = NonNullable<\n\tNonNullable<CustomTypeUpdateHookData[\"updateMeta\"]>[\"fieldIdChanged\"]\n>;\n\ntype CrCustomTypes = readonly CrCustomType[];\ntype CrCustomType =\n\t| string\n\t| { id: string; fields?: readonly CrCustomTypeNestedCr[] };\ntype CrCustomTypeNestedCr =\n\t| string\n\t| { id: string; customtypes: readonly CrCustomTypeFieldLeaf[] };\ntype CrCustomTypeFieldLeaf =\n\t| string\n\t| { id: string; fields?: readonly string[] };\n\nexport class CustomTypesManager extends BaseManager {\n\tasync readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type-library:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tids: t.array(t.string),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tids: data[0]?.ids || [],\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync readAllCustomTypes(\n\t\targs?: CustomTypesManagerReadAllCustomTypesArgs,\n\t): Promise<SliceMachineManagerReadAllCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst res: SliceMachineManagerReadAllCustomTypeReturnType = {\n\t\t\tmodels: [],\n\t\t\terrors: [],\n\t\t};\n\n\t\tconst { ids, errors } = await this.readCustomTypeLibrary();\n\t\tres.errors = [...res.errors, ...errors];\n\n\t\tif (ids) {\n\t\t\tfor (const id of ids) {\n\t\t\t\tconst { model, errors } = await this.readCustomType({ id });\n\t\t\t\tres.errors = [...res.errors, ...errors];\n\n\t\t\t\tif (model && (!args || args.format === model.format)) {\n\t\t\t\t\tres.models.push({ model });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync createCustomType(\n\t\targs: CustomTypeCreateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:create\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync readCustomType(\n\t\targs: CustomTypeReadHookData,\n\t): Promise<SliceMachineManagerReadCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tmodel: CustomType,\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tmodel: data[0]?.model,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs for all existing custom types and\n\t * slices. The change is determined by properties inside the `updateMeta`\n\t * property.\n\t */\n\tprivate async updateContentRelationships(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, updateMeta } = args;\n\n\t\tif (updateMeta?.fieldIdChanged) {\n\t\t\tlet { previousPath, newPath } = updateMeta.fieldIdChanged;\n\n\t\t\tif (previousPath.join(\".\") !== newPath.join(\".\")) {\n\t\t\t\tpreviousPath = [model.id, ...previousPath];\n\t\t\t\tnewPath = [model.id, ...newPath];\n\n\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\tupdateCustomTypeContentRelationships({\n\t\t\t\t\tmodels: customTypes.models,\n\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"custom-type:update\", {\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\n\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t});\n\n\t\t\t\t\tupdateSharedSliceContentRelationships({\n\t\t\t\t\t\tmodels: slices.models,\n\t\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\tnewPath,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn { errors: [] };\n\t}\n\n\tasync updateCustomType(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:update\",\n\t\t\targs,\n\t\t);\n\n\t\tif (args.updateMeta?.fieldIdChanged) {\n\t\t\tawait this.updateContentRelationships(args);\n\t\t}\n\n\t\treturn { errors: hookResult.errors };\n\t}\n\n\tasync renameCustomType(\n\t\targs: CustomTypeRenameHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeRenameHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:rename\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync deleteCustomType(\n\t\targs: CustomTypesMachineManagerDeleteCustomTypeArgs,\n\t): Promise<CustomTypesMachineManagerDeleteCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, errors: readCustomTypeErrors } = await this.readCustomType({\n\t\t\tid: args.id,\n\t\t});\n\n\t\tif (model) {\n\t\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\t\"custom-type:delete\",\n\t\t\t\t{ model },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\terrors: readCustomTypeErrors,\n\t\t\t};\n\t\t}\n\t}\n\n\tasync pushCustomType(\n\t\targs: SliceMachineManagerPushCustomTypeArgs,\n\t): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t// TODO: Handle errors\n\t\tconst { model } = await this.readCustomType({ id: args.id });\n\n\t\tif (model) {\n\t\t\t// TODO: Create a single shared client.\n\t\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\t\trepositoryName,\n\t\t\t\ttoken: authenticationToken,\n\t\t\t\tfetch,\n\t\t\t\tfetchOptions: {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\t// Check if Custom Type already exists on the repository.\n\t\t\t\tawait client.getCustomTypeByID(args.id);\n\n\t\t\t\t// If it exists on the repository, update it.\n\t\t\t\tawait client.updateCustomType(model);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof prismicCustomTypesClient.NotFoundError) {\n\t\t\t\t\t// If it doesn't exist on the repository, insert it.\n\t\t\t\t\tawait client.insertCustomType(model);\n\t\t\t\t} else if (error instanceof prismicCustomTypesClient.ForbiddenError) {\n\t\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\t\t\"You do not have access to push types to this Prismic repository.\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync readCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerReadCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:read\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tassetID: \"mocks.config.json\",\n\t\t\t},\n\t\t);\n\t\tconst data = hookResult.data[0]?.data;\n\n\t\t// TODO: Validate the returned data.\n\n\t\tif (data) {\n\t\t\treturn {\n\t\t\t\tmocksConfig: JSON.parse(data.toString()),\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tmocksConfig: undefined,\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync updateCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerUpdateCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:update\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tasset: {\n\t\t\t\t\tid: \"mocks.config.json\",\n\t\t\t\t\tdata: Buffer.from(JSON.stringify(args.mocksConfig, null, \"\\t\")),\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync fetchRemoteCustomTypes(): Promise<CustomType[]> {\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\trepositoryName,\n\t\t\ttoken: authenticationToken,\n\t\t\tfetch,\n\t\t\tfetchOptions: {\n\t\t\t\theaders: {\n\t\t\t\t\t\"User-Agent\": SLICE_MACHINE_USER_AGENT,\n\t\t\t\t},\n\t\t\t},\n\t\t});\n\n\t\treturn await client.getAllCustomTypes();\n\t}\n\n\tasync inferSlice({\n\t\timageUrl,\n\t}: {\n\t\timageUrl: string;\n\t}): Promise<InferSliceResponse> {\n\t\tconst authToken = await this.user.getAuthenticationToken();\n\t\tconst headers = {\n\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t};\n\n\t\tconst repository = await this.project.getResolvedRepositoryName();\n\t\tconst searchParams = new URLSearchParams({\n\t\t\trepository,\n\t\t});\n\n\t\tconst url = new URL(\"./slices/infer\", API_ENDPOINTS.CustomTypeService);\n\t\turl.search = searchParams.toString();\n\n\t\tconst response = await fetch(url.toString(), {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: headers,\n\t\t\tbody: JSON.stringify({ imageUrl }),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`Failed to infer slice: ${response.statusText}`);\n\t\t}\n\n\t\tconst json = await response.json();\n\n\t\treturn InferSliceResponse.parse(json);\n\t}\n}\n\ntype InferSliceResponse = z.infer<typeof InferSliceResponse>;\n\nconst InferSliceResponse = z.object({\n\tslice: z.custom().transform((value, ctx) => {\n\t\tconst result = SharedSlice.decode(value);\n\t\tif (result._tag === \"Right\") {\n\t\t\treturn result.right;\n\t\t}\n\t\tctx.addIssue({\n\t\t\tcode: z.ZodIssueCode.custom,\n\t\t\tmessage: `Invalid shared slice: ${JSON.stringify(value, null, 2)}`,\n\t\t});\n\n\t\treturn z.NEVER;\n\t}),\n\tlangSmithUrl: z.string().url().optional(),\n});\n\nfunction updateCRCustomType(\n\targs: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,\n): CrCustomType {\n\tconst { previousPath, newPath } = args;\n\n\tconst customType = shallowCloneIfObject(args.customType);\n\n\tconst previousId = previousPath[0];\n\tconst newId = newPath[0];\n\n\tif (!previousId || !newId || typeof customType === \"string\") {\n\t\treturn customType; // we don't support custom type id renaming\n\t}\n\n\tif (customType.fields) {\n\t\tconst newFields = customType.fields.map((fieldArg) => {\n\t\t\tconst field = shallowCloneIfObject(fieldArg);\n\n\t\t\tconst previousId = previousPath[1];\n\t\t\tconst newId = newPath[1];\n\n\t\t\tif (!previousId || !newId) {\n\t\t\t\treturn field;\n\t\t\t}\n\n\t\t\tif (typeof field === \"string\") {\n\t\t\t\tif (field === previousId && field !== newId) {\n\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t// so we update it new one. The field is a string, so return the new\n\t\t\t\t\t// id.\n\t\t\t\t\treturn newId;\n\t\t\t\t}\n\n\t\t\t\treturn field;\n\t\t\t}\n\n\t\t\tif (field.id === previousId && field.id !== newId) {\n\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t// so we update it new one.\n\t\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t\t// something to update further down in customtypes.\n\t\t\t\tfield.id = newId;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...field,\n\t\t\t\tcustomtypes: field.customtypes.map((customTypeArg) => {\n\t\t\t\t\tconst customType = shallowCloneIfObject(customTypeArg);\n\t\t\t\t\tconst previousId = previousPath[0];\n\t\t\t\t\tconst newId = newPath[0];\n\n\t\t\t\t\tif (!previousId || !newId || typeof customType === \"string\") {\n\t\t\t\t\t\treturn customType; // we don't support custom type id renaming\n\t\t\t\t\t}\n\n\t\t\t\t\tif (customType.fields) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...customType,\n\t\t\t\t\t\t\tfields: customType.fields.map((fieldArg) => {\n\t\t\t\t\t\t\t\tconst field = shallowCloneIfObject(fieldArg);\n\t\t\t\t\t\t\t\tconst previousId = previousPath[1];\n\t\t\t\t\t\t\t\tconst newId = newPath[1];\n\n\t\t\t\t\t\t\t\tif (field === previousId && field !== newId) {\n\t\t\t\t\t\t\t\t\t// Matches the previous id, so we update it and return because\n\t\t\t\t\t\t\t\t\t// it's the last level.\n\t\t\t\t\t\t\t\t\treturn newId;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn field;\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\treturn customType;\n\t\t\t\t}),\n\t\t\t};\n\t\t});\n\n\t\treturn { ...customType, fields: newFields };\n\t}\n\n\treturn customType;\n}\n\n/**\n * Map over the custom types of a Content Relationship Link and update the API\n * IDs that were changed during the custom type update.\n */\nfunction updateCRCustomTypes(\n\targs: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,\n): CrCustomTypes {\n\tconst { customTypes, ...updateMeta } = args;\n\n\treturn customTypes.map((customType) => {\n\t\treturn updateCRCustomType({ customType, ...updateMeta });\n\t});\n}\n\n/**\n * Update the Content Relationship API IDs of a single field. The change is\n * determined by the `previousPath` and `newPath` properties.\n */\nfunction updateFieldContentRelationships<\n\tT extends UID | NestableWidget | Group | NestedGroup,\n>(args: { field: T } & CustomTypeFieldIdChangedMeta): T {\n\tconst { field, ...updateMeta } = args;\n\tif (\n\t\tfield.type !== \"Link\" ||\n\t\tfield.config?.select !== \"document\" ||\n\t\t!field.config?.customtypes\n\t) {\n\t\t// not a content relationship field\n\t\treturn field;\n\t}\n\n\tconst newCustomTypes = updateCRCustomTypes({\n\t\t...updateMeta,\n\t\tcustomTypes: field.config.customtypes,\n\t});\n\n\treturn {\n\t\t...field,\n\t\tconfig: { ...field.config, customtypes: newCustomTypes },\n\t};\n}\n\nexport function updateCustomTypeContentRelationships(\n\targs: {\n\t\tmodels: { model: CustomType }[];\n\t\tonUpdate: (model: CustomType) => void;\n\t} & CustomTypeFieldIdChangedMeta,\n): void {\n\tconst { models, previousPath, newPath, onUpdate } = args;\n\n\tfor (const customType of models) {\n\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\tcustomType: customType.model,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({\n\t\t\t\t\tfield,\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\t\t\t},\n\t\t});\n\n\t\tonUpdate(updatedCustomTypeModel);\n\t}\n}\n\nexport function updateSharedSliceContentRelationships(\n\targs: {\n\t\tmodels: { model: SharedSlice }[];\n\t\tonUpdate: (model: SharedSlice) => void;\n\t} & CustomTypeFieldIdChangedMeta,\n): void {\n\tconst { models, previousPath, newPath, onUpdate } = args;\n\n\tfor (const slice of models) {\n\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\tpath: [\".\"],\n\t\t\tslice: slice.model,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({\n\t\t\t\t\tfield,\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\t\t\t},\n\t\t});\n\n\t\tonUpdate(updatedSliceModel);\n\t}\n}\n\nfunction shallowCloneIfObject<T>(value: T): T {\n\tif (typeof value === \"object\") {\n\t\treturn { ...value };\n\t}\n\n\treturn value;\n}\n\nfunction pushIfDefined<T>(array: T[], value: T | undefined) {\n\tif (value) {\n\t\tarray.push(value);\n\t}\n}\n"],"names":["BaseManager","assertPluginsInitialized","errors","decodeHookResult","t","CustomType","model","prismicCustomTypesClient","API_ENDPOINTS","fetch","SLICE_MACHINE_USER_AGENT","UnauthorizedError","z","SharedSlice","previousId","newId","customType","fieldArg","field","traverseCustomType","traverseSharedSlice"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGM,MAAO,2BAA2BA,YAAAA,YAAW;AAAA,EAClD,MAAM,wBAAqB;;AAC1BC,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,aAAa,MAAM,KAAK,yBAAyB,SACtD,4BACA,MAAS;AAEV,UAAM,EAAE,MAAM,QAAAC,QAAA,IAAWC,iBAAAA,iBACxBC,aAAE,KAAK;AAAA,MACN,KAAKA,aAAE,MAAMA,aAAE,MAAM;AAAA,IAAA,CACrB,GACD,UAAU;AAGJ,WAAA;AAAA,MACN,OAAK,UAAK,CAAC,MAAN,mBAAS,QAAO,CAAE;AAAA,MACvB,QAAAF;AAAA,IAAA;AAAA,EAEF;AAAA,EAEA,MAAM,mBACL,MAA+C;AAE/CD,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,MAAsD;AAAA,MAC3D,QAAQ,CAAE;AAAA,MACV,QAAQ,CAAE;AAAA,IAAA;AAGX,UAAM,EAAE,KAAK,QAAAC,QAAA,IAAW,MAAM,KAAK,sBAAqB;AACxD,QAAI,SAAS,CAAC,GAAG,IAAI,QAAQ,GAAGA,OAAM;AAEtC,QAAI,KAAK;AACR,iBAAW,MAAM,KAAK;AACf,cAAA,EAAE,OAAO,QAAAA,aAAW,MAAM,KAAK,eAAe,EAAE,GAAA,CAAI;AAC1D,YAAI,SAAS,CAAC,GAAG,IAAI,QAAQ,GAAGA,QAAM;AAEtC,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AS,cAAiB,OAAY,OAAoB;AACzD,MAAI,OAAO;AACV,UAAM,KAAK,KAAK;AAAA,EACjB;AACD;;;;"}
1
+ {"version":3,"file":"CustomTypesManager.cjs","sources":["../../../../src/managers/customTypes/CustomTypesManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport {\n\tCustomType,\n\tGroup,\n\tNestableWidget,\n\tNestedGroup,\n\tSharedSlice,\n\tUID,\n\ttraverseCustomType,\n\ttraverseSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\nimport {\n\tCallHookReturnType,\n\tCustomTypeCreateHook,\n\tCustomTypeCreateHookData,\n\tCustomTypeReadHookData,\n\tCustomTypeRenameHook,\n\tCustomTypeRenameHookData,\n\tCustomTypeUpdateHook,\n\tCustomTypeUpdateHookData,\n\tHookError,\n} from \"@slicemachine/plugin-kit\";\nimport { z } from \"zod\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport fetch from \"../../lib/fetch\";\n\nimport { OnlyHookErrors } from \"../../types\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\nimport { CustomTypeFormat } from \"./types\";\n\ntype SliceMachineManagerReadCustomTypeLibraryReturnType = {\n\tids: string[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypesManagerReadAllCustomTypesArgs = {\n\tformat: CustomTypeFormat;\n};\n\ntype SliceMachineManagerReadAllCustomTypeReturnType = {\n\tmodels: { model: CustomType }[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerReadCustomTypeReturnType = {\n\tmodel: CustomType | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerPushCustomTypeArgs = {\n\tid: string;\n\tuserAgent?: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType = {\n\t// TODO\n\tmocksConfig?: Record<string, unknown>;\n\terrors: HookError[];\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n\t// TODO\n\tmocksConfig: Record<string, unknown>;\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType = {\n\terrors: HookError[];\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeArgs = {\n\tid: string;\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeReturnType = {\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypeFieldIdChangedMeta = NonNullable<\n\tNonNullable<CustomTypeUpdateHookData[\"updateMeta\"]>[\"fieldIdChanged\"]\n>;\n\ntype CrCustomTypes = readonly CrCustomType[];\ntype CrCustomType =\n\t| string\n\t| { id: string; fields?: readonly CrCustomTypeNestedCr[] };\ntype CrCustomTypeNestedCr =\n\t| string\n\t| { id: string; customtypes: readonly CrCustomTypeFieldLeaf[] };\ntype CrCustomTypeFieldLeaf =\n\t| string\n\t| { id: string; fields?: readonly string[] };\n\nexport class CustomTypesManager extends BaseManager {\n\tasync readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type-library:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tids: t.array(t.string),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tids: data[0]?.ids || [],\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync readAllCustomTypes(\n\t\targs?: CustomTypesManagerReadAllCustomTypesArgs,\n\t): Promise<SliceMachineManagerReadAllCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst res: SliceMachineManagerReadAllCustomTypeReturnType = {\n\t\t\tmodels: [],\n\t\t\terrors: [],\n\t\t};\n\n\t\tconst { ids, errors } = await this.readCustomTypeLibrary();\n\t\tres.errors = [...res.errors, ...errors];\n\n\t\tif (ids) {\n\t\t\tfor (const id of ids) {\n\t\t\t\tconst { model, errors } = await this.readCustomType({ id });\n\t\t\t\tres.errors = [...res.errors, ...errors];\n\n\t\t\t\tif (model && (!args || args.format === model.format)) {\n\t\t\t\t\tres.models.push({ model });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync createCustomType(\n\t\targs: CustomTypeCreateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:create\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync readCustomType(\n\t\targs: CustomTypeReadHookData,\n\t): Promise<SliceMachineManagerReadCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tmodel: CustomType,\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tmodel: data[0]?.model,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tprivate updateCRCustomType(\n\t\targs: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,\n\t): CrCustomType {\n\t\tconst { previousPath, newPath } = args;\n\n\t\tconst customType = shallowClone(args.customType);\n\n\t\tconst previousCustomTypeId = previousPath[0];\n\t\tconst newCustomTypeId = newPath[0];\n\n\t\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\t\tthrow new Error(\n\t\t\t\t\"Didn't find any customtype id, which should not be possible\",\n\t\t\t);\n\t\t}\n\n\t\tif (typeof customType === \"string\") {\n\t\t\treturn customType; // we don't support custom type id renaming\n\t\t}\n\n\t\tif (customType.fields) {\n\t\t\tconst newFields = customType.fields.map((fieldArg) => {\n\t\t\t\tconst field = shallowClone(fieldArg);\n\n\t\t\t\tconst previousFieldId = previousPath[1];\n\t\t\t\tconst newFieldId = newPath[1];\n\n\t\t\t\tif (!previousFieldId || !newFieldId) {\n\t\t\t\t\treturn field;\n\t\t\t\t}\n\n\t\t\t\tif (typeof field === \"string\") {\n\t\t\t\t\tif (field === previousFieldId && field !== newFieldId) {\n\t\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t\t// so we update it new one. The field is a string, so return the new\n\t\t\t\t\t\t// id.\n\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn field;\n\t\t\t\t}\n\n\t\t\t\tif (field.id === previousFieldId && field.id !== newFieldId) {\n\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t// so we update it new one.\n\t\t\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t\t\t// something to update further down in customtypes.\n\t\t\t\t\tfield.id = newFieldId;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\t...field,\n\t\t\t\t\tcustomtypes: field.customtypes.map((customTypeArg) => {\n\t\t\t\t\t\tconst customType = shallowClone(customTypeArg);\n\n\t\t\t\t\t\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\"Didn't find any customtype id, which should not be possible\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (typeof customType === \"string\") {\n\t\t\t\t\t\t\treturn customType; // we don't support custom type id renaming\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (customType.fields) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t...customType,\n\t\t\t\t\t\t\t\tfields: customType.fields.map((fieldArg) => {\n\t\t\t\t\t\t\t\t\tconst field = shallowClone(fieldArg);\n\n\t\t\t\t\t\t\t\t\tif (field === previousFieldId && field !== newFieldId) {\n\t\t\t\t\t\t\t\t\t\t// Matches the previous id, so we update it and return because\n\t\t\t\t\t\t\t\t\t\t// it's the last level.\n\t\t\t\t\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\treturn field;\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn customType;\n\t\t\t\t\t}),\n\t\t\t\t};\n\t\t\t});\n\n\t\t\treturn { ...customType, fields: newFields };\n\t\t}\n\n\t\treturn customType;\n\t}\n\n\t/**\n\t * Map over the custom types of a Content Relationship Link and update the API\n\t * IDs that were changed during the custom type update.\n\t */\n\tprivate updateCRCustomTypes(\n\t\targs: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,\n\t): CrCustomTypes {\n\t\tconst { customTypes, ...updateMeta } = args;\n\n\t\treturn customTypes.map((customType) => {\n\t\t\treturn this.updateCRCustomType({ customType, ...updateMeta });\n\t\t});\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs of a single field. The change is\n\t * determined by the `previousPath` and `newPath` properties.\n\t */\n\tprivate updateFieldContentRelationships<\n\t\tT extends UID | NestableWidget | Group | NestedGroup,\n\t>(args: { field: T } & CustomTypeFieldIdChangedMeta): T {\n\t\tconst { field, ...updateMeta } = args;\n\t\tif (\n\t\t\tfield.type !== \"Link\" ||\n\t\t\tfield.config?.select !== \"document\" ||\n\t\t\t!field.config?.customtypes\n\t\t) {\n\t\t\t// not a content relationship field\n\t\t\treturn field;\n\t\t}\n\n\t\tconst newCustomTypes = this.updateCRCustomTypes({\n\t\t\t...updateMeta,\n\t\t\tcustomTypes: field.config.customtypes,\n\t\t});\n\n\t\treturn {\n\t\t\t...field,\n\t\t\tconfig: { ...field.config, customtypes: newCustomTypes },\n\t\t};\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs for all existing custom types and\n\t * slices. The change is determined by properties inside the `updateMeta`\n\t * property.\n\t */\n\tprivate async updateContentRelationships(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, updateMeta } = args;\n\n\t\tif (updateMeta?.fieldIdChanged) {\n\t\t\tlet { previousPath, newPath } = updateMeta.fieldIdChanged;\n\n\t\t\tif (previousPath.join(\".\") !== newPath.join(\".\")) {\n\t\t\t\tpreviousPath = [model.id, ...previousPath];\n\t\t\t\tnewPath = [model.id, ...newPath];\n\n\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\tfor (const customType of customTypes.models) {\n\t\t\t\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\t\t\t\tcustomType: customType.model,\n\t\t\t\t\t\tonField: ({ field }) => {\n\t\t\t\t\t\t\treturn this.updateFieldContentRelationships({\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\n\t\t\t\t\tcrUpdates.push(\n\t\t\t\t\t\tthis.sliceMachinePluginRunner.callHook(\"custom-type:update\", {\n\t\t\t\t\t\t\tmodel: updatedCustomTypeModel,\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t});\n\n\t\t\t\t\tfor (const slice of slices.models) {\n\t\t\t\t\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\t\t\t\t\tpath: [\".\"],\n\t\t\t\t\t\t\tslice: slice.model,\n\t\t\t\t\t\t\tonField: ({ field }) => {\n\t\t\t\t\t\t\t\treturn this.updateFieldContentRelationships({\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tcrUpdates.push(\n\t\t\t\t\t\t\tthis.sliceMachinePluginRunner.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\tmodel: updatedSliceModel,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn { errors: [] };\n\t}\n\n\tasync updateCustomType(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:update\",\n\t\t\targs,\n\t\t);\n\n\t\tif (args.updateMeta?.fieldIdChanged) {\n\t\t\tawait this.updateContentRelationships(args);\n\t\t}\n\n\t\treturn { errors: hookResult.errors };\n\t}\n\n\tasync renameCustomType(\n\t\targs: CustomTypeRenameHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeRenameHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:rename\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync deleteCustomType(\n\t\targs: CustomTypesMachineManagerDeleteCustomTypeArgs,\n\t): Promise<CustomTypesMachineManagerDeleteCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, errors: readCustomTypeErrors } = await this.readCustomType({\n\t\t\tid: args.id,\n\t\t});\n\n\t\tif (model) {\n\t\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\t\"custom-type:delete\",\n\t\t\t\t{ model },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\terrors: readCustomTypeErrors,\n\t\t\t};\n\t\t}\n\t}\n\n\tasync pushCustomType(\n\t\targs: SliceMachineManagerPushCustomTypeArgs,\n\t): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t// TODO: Handle errors\n\t\tconst { model } = await this.readCustomType({ id: args.id });\n\n\t\tif (model) {\n\t\t\t// TODO: Create a single shared client.\n\t\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\t\trepositoryName,\n\t\t\t\ttoken: authenticationToken,\n\t\t\t\tfetch,\n\t\t\t\tfetchOptions: {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\t// Check if Custom Type already exists on the repository.\n\t\t\t\tawait client.getCustomTypeByID(args.id);\n\n\t\t\t\t// If it exists on the repository, update it.\n\t\t\t\tawait client.updateCustomType(model);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof prismicCustomTypesClient.NotFoundError) {\n\t\t\t\t\t// If it doesn't exist on the repository, insert it.\n\t\t\t\t\tawait client.insertCustomType(model);\n\t\t\t\t} else if (error instanceof prismicCustomTypesClient.ForbiddenError) {\n\t\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\t\t\"You do not have access to push types to this Prismic repository.\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync readCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerReadCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:read\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tassetID: \"mocks.config.json\",\n\t\t\t},\n\t\t);\n\t\tconst data = hookResult.data[0]?.data;\n\n\t\t// TODO: Validate the returned data.\n\n\t\tif (data) {\n\t\t\treturn {\n\t\t\t\tmocksConfig: JSON.parse(data.toString()),\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tmocksConfig: undefined,\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync updateCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerUpdateCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:update\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tasset: {\n\t\t\t\t\tid: \"mocks.config.json\",\n\t\t\t\t\tdata: Buffer.from(JSON.stringify(args.mocksConfig, null, \"\\t\")),\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync fetchRemoteCustomTypes(): Promise<CustomType[]> {\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\trepositoryName,\n\t\t\ttoken: authenticationToken,\n\t\t\tfetch,\n\t\t\tfetchOptions: {\n\t\t\t\theaders: {\n\t\t\t\t\t\"User-Agent\": SLICE_MACHINE_USER_AGENT,\n\t\t\t\t},\n\t\t\t},\n\t\t});\n\n\t\treturn await client.getAllCustomTypes();\n\t}\n\n\tasync inferSlice({\n\t\timageUrl,\n\t}: {\n\t\timageUrl: string;\n\t}): Promise<InferSliceResponse> {\n\t\tconst authToken = await this.user.getAuthenticationToken();\n\t\tconst headers = {\n\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t};\n\n\t\tconst repository = await this.project.getResolvedRepositoryName();\n\t\tconst searchParams = new URLSearchParams({\n\t\t\trepository,\n\t\t});\n\n\t\tconst url = new URL(\"./slices/infer\", API_ENDPOINTS.CustomTypeService);\n\t\turl.search = searchParams.toString();\n\n\t\tconst response = await fetch(url.toString(), {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: headers,\n\t\t\tbody: JSON.stringify({ imageUrl }),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`Failed to infer slice: ${response.statusText}`);\n\t\t}\n\n\t\tconst json = await response.json();\n\n\t\treturn InferSliceResponse.parse(json);\n\t}\n}\n\ntype InferSliceResponse = z.infer<typeof InferSliceResponse>;\n\nconst InferSliceResponse = z.object({\n\tslice: z.custom().transform((value, ctx) => {\n\t\tconst result = SharedSlice.decode(value);\n\t\tif (result._tag === \"Right\") {\n\t\t\treturn result.right;\n\t\t}\n\t\tctx.addIssue({\n\t\t\tcode: z.ZodIssueCode.custom,\n\t\t\tmessage: `Invalid shared slice: ${JSON.stringify(value, null, 2)}`,\n\t\t});\n\n\t\treturn z.NEVER;\n\t}),\n\tlangSmithUrl: z.string().url().optional(),\n});\n\nfunction shallowClone<T>(value: T): T {\n\tif (typeof value === \"object\") {\n\t\treturn { ...value };\n\t}\n\n\treturn value;\n}\n"],"names":["BaseManager","assertPluginsInitialized","errors","decodeHookResult","t","CustomType","customType","fieldArg","field","traverseCustomType","traverseSharedSlice","prismicCustomTypesClient","API_ENDPOINTS","fetch","SLICE_MACHINE_USER_AGENT","UnauthorizedError","z","SharedSlice"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyGM,MAAO,2BAA2BA,YAAAA,YAAW;AAAA,EAClD,MAAM,wBAAqB;;AAC1BC,sDAAyB,KAAK,wBAAwB;AAEtD,UAAM,aAAa,MAAM,KAAK,yBAAyB,SACtD,4BACA,MAAS;AAEV,UAAM,EAAE,MAAM,QAAAC,QAAA,IAAWC,iBAAAA,iBACxBC,aAAE,KAAK;AAAA,MACN,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AyB,KAAK,UAAU,OAAO,MAAM,CAAC;AAAA,IAAA,CAC/D;AAED,WAAOA,MAAAA,QAAE;AAAA,EAAA,CACT;AAAA,EACD,cAAcA,MAAAA,QAAE,OAAS,EAAA,IAAA,EAAM,SAAU;AACzC,CAAA;AAED,SAAS,aAAgB,OAAQ;AAC5B,MAAA,OAAO,UAAU,UAAU;AACvB,WAAA,EAAE,GAAG;EACb;AAEO,SAAA;AACR;;"}
@@ -1,4 +1,4 @@
1
- import { CustomType, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
1
+ import { CustomType } from "@prismicio/types-internal/lib/customtypes";
2
2
  import { CallHookReturnType, CustomTypeCreateHook, CustomTypeCreateHookData, CustomTypeReadHookData, CustomTypeRenameHook, CustomTypeRenameHookData, CustomTypeUpdateHook, CustomTypeUpdateHookData, HookError } from "@slicemachine/plugin-kit";
3
3
  import { z } from "zod";
4
4
  import { DecodeError } from "../../lib/DecodeError";
@@ -46,12 +46,22 @@ type CustomTypesMachineManagerDeleteCustomTypeArgs = {
46
46
  type CustomTypesMachineManagerDeleteCustomTypeReturnType = {
47
47
  errors: (DecodeError | HookError)[];
48
48
  };
49
- type CustomTypeFieldIdChangedMeta = NonNullable<NonNullable<CustomTypeUpdateHookData["updateMeta"]>["fieldIdChanged"]>;
50
49
  export declare class CustomTypesManager extends BaseManager {
51
50
  readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType>;
52
51
  readAllCustomTypes(args?: CustomTypesManagerReadAllCustomTypesArgs): Promise<SliceMachineManagerReadAllCustomTypeReturnType>;
53
52
  createCustomType(args: CustomTypeCreateHookData): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>>;
54
53
  readCustomType(args: CustomTypeReadHookData): Promise<SliceMachineManagerReadCustomTypeReturnType>;
54
+ private updateCRCustomType;
55
+ /**
56
+ * Map over the custom types of a Content Relationship Link and update the API
57
+ * IDs that were changed during the custom type update.
58
+ */
59
+ private updateCRCustomTypes;
60
+ /**
61
+ * Update the Content Relationship API IDs of a single field. The change is
62
+ * determined by the `previousPath` and `newPath` properties.
63
+ */
64
+ private updateFieldContentRelationships;
55
65
  /**
56
66
  * Update the Content Relationship API IDs for all existing custom types and
57
67
  * slices. The change is determined by properties inside the `updateMeta`
@@ -1590,16 +1600,4 @@ declare const InferSliceResponse: z.ZodObject<{
1590
1600
  slice?: unknown;
1591
1601
  langSmithUrl?: string | undefined;
1592
1602
  }>;
1593
- export declare function updateCustomTypeContentRelationships(args: {
1594
- models: {
1595
- model: CustomType;
1596
- }[];
1597
- onUpdate: (model: CustomType) => void;
1598
- } & CustomTypeFieldIdChangedMeta): void;
1599
- export declare function updateSharedSliceContentRelationships(args: {
1600
- models: {
1601
- model: SharedSlice;
1602
- }[];
1603
- onUpdate: (model: SharedSlice) => void;
1604
- } & CustomTypeFieldIdChangedMeta): void;
1605
1603
  export {};
@@ -1,6 +1,6 @@
1
1
  import * as t from "io-ts";
2
2
  import * as prismicCustomTypesClient from "@prismicio/custom-types-client";
3
- import { CustomType, SharedSlice, traverseCustomType, traverseSharedSlice } from "@prismicio/types-internal/lib/customtypes";
3
+ import { CustomType, traverseCustomType, traverseSharedSlice, SharedSlice } from "@prismicio/types-internal/lib/customtypes";
4
4
  import z from './../../_node_modules/zod/lib/index.js';
5
5
  import { assertPluginsInitialized } from "../../lib/assertPluginsInitialized.js";
6
6
  import { decodeHookResult } from "../../lib/decodeHookResult.js";
@@ -60,6 +60,93 @@ class CustomTypesManager extends BaseManager {
60
60
  errors
61
61
  };
62
62
  }
63
+ updateCRCustomType(args) {
64
+ const { previousPath, newPath } = args;
65
+ const customType = shallowClone(args.customType);
66
+ const previousCustomTypeId = previousPath[0];
67
+ const newCustomTypeId = newPath[0];
68
+ if (!previousCustomTypeId || !newCustomTypeId) {
69
+ throw new Error("Didn't find any customtype id, which should not be possible");
70
+ }
71
+ if (typeof customType === "string") {
72
+ return customType;
73
+ }
74
+ if (customType.fields) {
75
+ const newFields = customType.fields.map((fieldArg) => {
76
+ const field = shallowClone(fieldArg);
77
+ const previousFieldId = previousPath[1];
78
+ const newFieldId = newPath[1];
79
+ if (!previousFieldId || !newFieldId) {
80
+ return field;
81
+ }
82
+ if (typeof field === "string") {
83
+ if (field === previousFieldId && field !== newFieldId) {
84
+ return newFieldId;
85
+ }
86
+ return field;
87
+ }
88
+ if (field.id === previousFieldId && field.id !== newFieldId) {
89
+ field.id = newFieldId;
90
+ }
91
+ return {
92
+ ...field,
93
+ customtypes: field.customtypes.map((customTypeArg) => {
94
+ const customType2 = shallowClone(customTypeArg);
95
+ if (!previousCustomTypeId || !newCustomTypeId) {
96
+ throw new Error("Didn't find any customtype id, which should not be possible");
97
+ }
98
+ if (typeof customType2 === "string") {
99
+ return customType2;
100
+ }
101
+ if (customType2.fields) {
102
+ return {
103
+ ...customType2,
104
+ fields: customType2.fields.map((fieldArg2) => {
105
+ const field2 = shallowClone(fieldArg2);
106
+ if (field2 === previousFieldId && field2 !== newFieldId) {
107
+ return newFieldId;
108
+ }
109
+ return field2;
110
+ })
111
+ };
112
+ }
113
+ return customType2;
114
+ })
115
+ };
116
+ });
117
+ return { ...customType, fields: newFields };
118
+ }
119
+ return customType;
120
+ }
121
+ /**
122
+ * Map over the custom types of a Content Relationship Link and update the API
123
+ * IDs that were changed during the custom type update.
124
+ */
125
+ updateCRCustomTypes(args) {
126
+ const { customTypes, ...updateMeta } = args;
127
+ return customTypes.map((customType) => {
128
+ return this.updateCRCustomType({ customType, ...updateMeta });
129
+ });
130
+ }
131
+ /**
132
+ * Update the Content Relationship API IDs of a single field. The change is
133
+ * determined by the `previousPath` and `newPath` properties.
134
+ */
135
+ updateFieldContentRelationships(args) {
136
+ var _a, _b;
137
+ const { field, ...updateMeta } = args;
138
+ if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
139
+ return field;
140
+ }
141
+ const newCustomTypes = this.updateCRCustomTypes({
142
+ ...updateMeta,
143
+ customTypes: field.config.customtypes
144
+ });
145
+ return {
146
+ ...field,
147
+ config: { ...field.config, customtypes: newCustomTypes }
148
+ };
149
+ }
63
150
  /**
64
151
  * Update the Content Relationship API IDs for all existing custom types and
65
152
  * slices. The change is determined by properties inside the `updateMeta`
@@ -75,34 +162,43 @@ class CustomTypesManager extends BaseManager {
75
162
  newPath = [model.id, ...newPath];
76
163
  const crUpdates = [];
77
164
  const customTypes = await this.readAllCustomTypes();
78
- updateCustomTypeContentRelationships({
79
- models: customTypes.models,
80
- onUpdate: (model2) => {
81
- var _a;
82
- pushIfDefined(crUpdates, (_a = this.sliceMachinePluginRunner) == null ? void 0 : _a.callHook("custom-type:update", {
83
- model: model2
84
- }));
85
- },
86
- previousPath,
87
- newPath
88
- });
165
+ for (const customType of customTypes.models) {
166
+ const updatedCustomTypeModel = traverseCustomType({
167
+ customType: customType.model,
168
+ onField: ({ field }) => {
169
+ return this.updateFieldContentRelationships({
170
+ field,
171
+ previousPath,
172
+ newPath
173
+ });
174
+ }
175
+ });
176
+ crUpdates.push(this.sliceMachinePluginRunner.callHook("custom-type:update", {
177
+ model: updatedCustomTypeModel
178
+ }));
179
+ }
89
180
  const { libraries } = await this.slices.readAllSliceLibraries();
90
181
  for (const library of libraries) {
91
182
  const slices = await this.slices.readAllSlicesForLibrary({
92
183
  libraryID: library.libraryID
93
184
  });
94
- updateSharedSliceContentRelationships({
95
- models: slices.models,
96
- onUpdate: (model2) => {
97
- var _a;
98
- pushIfDefined(crUpdates, (_a = this.sliceMachinePluginRunner) == null ? void 0 : _a.callHook("slice:update", {
99
- libraryID: library.libraryID,
100
- model: model2
101
- }));
102
- },
103
- previousPath,
104
- newPath
105
- });
185
+ for (const slice of slices.models) {
186
+ const updatedSliceModel = traverseSharedSlice({
187
+ path: ["."],
188
+ slice: slice.model,
189
+ onField: ({ field }) => {
190
+ return this.updateFieldContentRelationships({
191
+ field,
192
+ previousPath,
193
+ newPath
194
+ });
195
+ }
196
+ });
197
+ crUpdates.push(this.sliceMachinePluginRunner.callHook("slice:update", {
198
+ libraryID: library.libraryID,
199
+ model: updatedSliceModel
200
+ }));
201
+ }
106
202
  }
107
203
  const crUpdatesResult = await Promise.all(crUpdates);
108
204
  if (crUpdatesResult.some((result) => result.errors.length > 0)) {
@@ -267,130 +363,13 @@ const InferSliceResponse = z.object({
267
363
  }),
268
364
  langSmithUrl: z.string().url().optional()
269
365
  });
270
- function updateCRCustomType(args) {
271
- const { previousPath, newPath } = args;
272
- const customType = shallowCloneIfObject(args.customType);
273
- const previousId = previousPath[0];
274
- const newId = newPath[0];
275
- if (!previousId || !newId || typeof customType === "string") {
276
- return customType;
277
- }
278
- if (customType.fields) {
279
- const newFields = customType.fields.map((fieldArg) => {
280
- const field = shallowCloneIfObject(fieldArg);
281
- const previousId2 = previousPath[1];
282
- const newId2 = newPath[1];
283
- if (!previousId2 || !newId2) {
284
- return field;
285
- }
286
- if (typeof field === "string") {
287
- if (field === previousId2 && field !== newId2) {
288
- return newId2;
289
- }
290
- return field;
291
- }
292
- if (field.id === previousId2 && field.id !== newId2) {
293
- field.id = newId2;
294
- }
295
- return {
296
- ...field,
297
- customtypes: field.customtypes.map((customTypeArg) => {
298
- const customType2 = shallowCloneIfObject(customTypeArg);
299
- const previousId3 = previousPath[0];
300
- const newId3 = newPath[0];
301
- if (!previousId3 || !newId3 || typeof customType2 === "string") {
302
- return customType2;
303
- }
304
- if (customType2.fields) {
305
- return {
306
- ...customType2,
307
- fields: customType2.fields.map((fieldArg2) => {
308
- const field2 = shallowCloneIfObject(fieldArg2);
309
- const previousId4 = previousPath[1];
310
- const newId4 = newPath[1];
311
- if (field2 === previousId4 && field2 !== newId4) {
312
- return newId4;
313
- }
314
- return field2;
315
- })
316
- };
317
- }
318
- return customType2;
319
- })
320
- };
321
- });
322
- return { ...customType, fields: newFields };
323
- }
324
- return customType;
325
- }
326
- function updateCRCustomTypes(args) {
327
- const { customTypes, ...updateMeta } = args;
328
- return customTypes.map((customType) => {
329
- return updateCRCustomType({ customType, ...updateMeta });
330
- });
331
- }
332
- function updateFieldContentRelationships(args) {
333
- var _a, _b;
334
- const { field, ...updateMeta } = args;
335
- if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
336
- return field;
337
- }
338
- const newCustomTypes = updateCRCustomTypes({
339
- ...updateMeta,
340
- customTypes: field.config.customtypes
341
- });
342
- return {
343
- ...field,
344
- config: { ...field.config, customtypes: newCustomTypes }
345
- };
346
- }
347
- function updateCustomTypeContentRelationships(args) {
348
- const { models, previousPath, newPath, onUpdate } = args;
349
- for (const customType of models) {
350
- const updatedCustomTypeModel = traverseCustomType({
351
- customType: customType.model,
352
- onField: ({ field }) => {
353
- return updateFieldContentRelationships({
354
- field,
355
- previousPath,
356
- newPath
357
- });
358
- }
359
- });
360
- onUpdate(updatedCustomTypeModel);
361
- }
362
- }
363
- function updateSharedSliceContentRelationships(args) {
364
- const { models, previousPath, newPath, onUpdate } = args;
365
- for (const slice of models) {
366
- const updatedSliceModel = traverseSharedSlice({
367
- path: ["."],
368
- slice: slice.model,
369
- onField: ({ field }) => {
370
- return updateFieldContentRelationships({
371
- field,
372
- previousPath,
373
- newPath
374
- });
375
- }
376
- });
377
- onUpdate(updatedSliceModel);
378
- }
379
- }
380
- function shallowCloneIfObject(value) {
366
+ function shallowClone(value) {
381
367
  if (typeof value === "object") {
382
368
  return { ...value };
383
369
  }
384
370
  return value;
385
371
  }
386
- function pushIfDefined(array, value) {
387
- if (value) {
388
- array.push(value);
389
- }
390
- }
391
372
  export {
392
- CustomTypesManager,
393
- updateCustomTypeContentRelationships,
394
- updateSharedSliceContentRelationships
373
+ CustomTypesManager
395
374
  };
396
375
  //# sourceMappingURL=CustomTypesManager.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CustomTypesManager.js","sources":["../../../../src/managers/customTypes/CustomTypesManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport {\n\tCustomType,\n\tGroup,\n\tNestableWidget,\n\tNestedGroup,\n\tSharedSlice,\n\tUID,\n\ttraverseCustomType,\n\ttraverseSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\nimport {\n\tCallHookReturnType,\n\tCustomTypeCreateHook,\n\tCustomTypeCreateHookData,\n\tCustomTypeReadHookData,\n\tCustomTypeRenameHook,\n\tCustomTypeRenameHookData,\n\tCustomTypeUpdateHook,\n\tCustomTypeUpdateHookData,\n\tHookError,\n} from \"@slicemachine/plugin-kit\";\nimport { z } from \"zod\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport fetch from \"../../lib/fetch\";\n\nimport { OnlyHookErrors } from \"../../types\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\nimport { CustomTypeFormat } from \"./types\";\n\ntype SliceMachineManagerReadCustomTypeLibraryReturnType = {\n\tids: string[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypesManagerReadAllCustomTypesArgs = {\n\tformat: CustomTypeFormat;\n};\n\ntype SliceMachineManagerReadAllCustomTypeReturnType = {\n\tmodels: { model: CustomType }[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerReadCustomTypeReturnType = {\n\tmodel: CustomType | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerPushCustomTypeArgs = {\n\tid: string;\n\tuserAgent?: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType = {\n\t// TODO\n\tmocksConfig?: Record<string, unknown>;\n\terrors: HookError[];\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n\t// TODO\n\tmocksConfig: Record<string, unknown>;\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType = {\n\terrors: HookError[];\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeArgs = {\n\tid: string;\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeReturnType = {\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypeFieldIdChangedMeta = NonNullable<\n\tNonNullable<CustomTypeUpdateHookData[\"updateMeta\"]>[\"fieldIdChanged\"]\n>;\n\ntype CrCustomTypes = readonly CrCustomType[];\ntype CrCustomType =\n\t| string\n\t| { id: string; fields?: readonly CrCustomTypeNestedCr[] };\ntype CrCustomTypeNestedCr =\n\t| string\n\t| { id: string; customtypes: readonly CrCustomTypeFieldLeaf[] };\ntype CrCustomTypeFieldLeaf =\n\t| string\n\t| { id: string; fields?: readonly string[] };\n\nexport class CustomTypesManager extends BaseManager {\n\tasync readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type-library:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tids: t.array(t.string),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tids: data[0]?.ids || [],\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync readAllCustomTypes(\n\t\targs?: CustomTypesManagerReadAllCustomTypesArgs,\n\t): Promise<SliceMachineManagerReadAllCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst res: SliceMachineManagerReadAllCustomTypeReturnType = {\n\t\t\tmodels: [],\n\t\t\terrors: [],\n\t\t};\n\n\t\tconst { ids, errors } = await this.readCustomTypeLibrary();\n\t\tres.errors = [...res.errors, ...errors];\n\n\t\tif (ids) {\n\t\t\tfor (const id of ids) {\n\t\t\t\tconst { model, errors } = await this.readCustomType({ id });\n\t\t\t\tres.errors = [...res.errors, ...errors];\n\n\t\t\t\tif (model && (!args || args.format === model.format)) {\n\t\t\t\t\tres.models.push({ model });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync createCustomType(\n\t\targs: CustomTypeCreateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:create\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync readCustomType(\n\t\targs: CustomTypeReadHookData,\n\t): Promise<SliceMachineManagerReadCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tmodel: CustomType,\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tmodel: data[0]?.model,\n\t\t\terrors,\n\t\t};\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs for all existing custom types and\n\t * slices. The change is determined by properties inside the `updateMeta`\n\t * property.\n\t */\n\tprivate async updateContentRelationships(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, updateMeta } = args;\n\n\t\tif (updateMeta?.fieldIdChanged) {\n\t\t\tlet { previousPath, newPath } = updateMeta.fieldIdChanged;\n\n\t\t\tif (previousPath.join(\".\") !== newPath.join(\".\")) {\n\t\t\t\tpreviousPath = [model.id, ...previousPath];\n\t\t\t\tnewPath = [model.id, ...newPath];\n\n\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\tupdateCustomTypeContentRelationships({\n\t\t\t\t\tmodels: customTypes.models,\n\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"custom-type:update\", {\n\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t},\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\n\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t});\n\n\t\t\t\t\tupdateSharedSliceContentRelationships({\n\t\t\t\t\t\tmodels: slices.models,\n\t\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t},\n\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\tnewPath,\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn { errors: [] };\n\t}\n\n\tasync updateCustomType(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:update\",\n\t\t\targs,\n\t\t);\n\n\t\tif (args.updateMeta?.fieldIdChanged) {\n\t\t\tawait this.updateContentRelationships(args);\n\t\t}\n\n\t\treturn { errors: hookResult.errors };\n\t}\n\n\tasync renameCustomType(\n\t\targs: CustomTypeRenameHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeRenameHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:rename\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync deleteCustomType(\n\t\targs: CustomTypesMachineManagerDeleteCustomTypeArgs,\n\t): Promise<CustomTypesMachineManagerDeleteCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, errors: readCustomTypeErrors } = await this.readCustomType({\n\t\t\tid: args.id,\n\t\t});\n\n\t\tif (model) {\n\t\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\t\"custom-type:delete\",\n\t\t\t\t{ model },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\terrors: readCustomTypeErrors,\n\t\t\t};\n\t\t}\n\t}\n\n\tasync pushCustomType(\n\t\targs: SliceMachineManagerPushCustomTypeArgs,\n\t): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t// TODO: Handle errors\n\t\tconst { model } = await this.readCustomType({ id: args.id });\n\n\t\tif (model) {\n\t\t\t// TODO: Create a single shared client.\n\t\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\t\trepositoryName,\n\t\t\t\ttoken: authenticationToken,\n\t\t\t\tfetch,\n\t\t\t\tfetchOptions: {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\t// Check if Custom Type already exists on the repository.\n\t\t\t\tawait client.getCustomTypeByID(args.id);\n\n\t\t\t\t// If it exists on the repository, update it.\n\t\t\t\tawait client.updateCustomType(model);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof prismicCustomTypesClient.NotFoundError) {\n\t\t\t\t\t// If it doesn't exist on the repository, insert it.\n\t\t\t\t\tawait client.insertCustomType(model);\n\t\t\t\t} else if (error instanceof prismicCustomTypesClient.ForbiddenError) {\n\t\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\t\t\"You do not have access to push types to this Prismic repository.\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync readCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerReadCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:read\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tassetID: \"mocks.config.json\",\n\t\t\t},\n\t\t);\n\t\tconst data = hookResult.data[0]?.data;\n\n\t\t// TODO: Validate the returned data.\n\n\t\tif (data) {\n\t\t\treturn {\n\t\t\t\tmocksConfig: JSON.parse(data.toString()),\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tmocksConfig: undefined,\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync updateCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerUpdateCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:update\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tasset: {\n\t\t\t\t\tid: \"mocks.config.json\",\n\t\t\t\t\tdata: Buffer.from(JSON.stringify(args.mocksConfig, null, \"\\t\")),\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync fetchRemoteCustomTypes(): Promise<CustomType[]> {\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\trepositoryName,\n\t\t\ttoken: authenticationToken,\n\t\t\tfetch,\n\t\t\tfetchOptions: {\n\t\t\t\theaders: {\n\t\t\t\t\t\"User-Agent\": SLICE_MACHINE_USER_AGENT,\n\t\t\t\t},\n\t\t\t},\n\t\t});\n\n\t\treturn await client.getAllCustomTypes();\n\t}\n\n\tasync inferSlice({\n\t\timageUrl,\n\t}: {\n\t\timageUrl: string;\n\t}): Promise<InferSliceResponse> {\n\t\tconst authToken = await this.user.getAuthenticationToken();\n\t\tconst headers = {\n\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t};\n\n\t\tconst repository = await this.project.getResolvedRepositoryName();\n\t\tconst searchParams = new URLSearchParams({\n\t\t\trepository,\n\t\t});\n\n\t\tconst url = new URL(\"./slices/infer\", API_ENDPOINTS.CustomTypeService);\n\t\turl.search = searchParams.toString();\n\n\t\tconst response = await fetch(url.toString(), {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: headers,\n\t\t\tbody: JSON.stringify({ imageUrl }),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`Failed to infer slice: ${response.statusText}`);\n\t\t}\n\n\t\tconst json = await response.json();\n\n\t\treturn InferSliceResponse.parse(json);\n\t}\n}\n\ntype InferSliceResponse = z.infer<typeof InferSliceResponse>;\n\nconst InferSliceResponse = z.object({\n\tslice: z.custom().transform((value, ctx) => {\n\t\tconst result = SharedSlice.decode(value);\n\t\tif (result._tag === \"Right\") {\n\t\t\treturn result.right;\n\t\t}\n\t\tctx.addIssue({\n\t\t\tcode: z.ZodIssueCode.custom,\n\t\t\tmessage: `Invalid shared slice: ${JSON.stringify(value, null, 2)}`,\n\t\t});\n\n\t\treturn z.NEVER;\n\t}),\n\tlangSmithUrl: z.string().url().optional(),\n});\n\nfunction updateCRCustomType(\n\targs: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,\n): CrCustomType {\n\tconst { previousPath, newPath } = args;\n\n\tconst customType = shallowCloneIfObject(args.customType);\n\n\tconst previousId = previousPath[0];\n\tconst newId = newPath[0];\n\n\tif (!previousId || !newId || typeof customType === \"string\") {\n\t\treturn customType; // we don't support custom type id renaming\n\t}\n\n\tif (customType.fields) {\n\t\tconst newFields = customType.fields.map((fieldArg) => {\n\t\t\tconst field = shallowCloneIfObject(fieldArg);\n\n\t\t\tconst previousId = previousPath[1];\n\t\t\tconst newId = newPath[1];\n\n\t\t\tif (!previousId || !newId) {\n\t\t\t\treturn field;\n\t\t\t}\n\n\t\t\tif (typeof field === \"string\") {\n\t\t\t\tif (field === previousId && field !== newId) {\n\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t// so we update it new one. The field is a string, so return the new\n\t\t\t\t\t// id.\n\t\t\t\t\treturn newId;\n\t\t\t\t}\n\n\t\t\t\treturn field;\n\t\t\t}\n\n\t\t\tif (field.id === previousId && field.id !== newId) {\n\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t// so we update it new one.\n\t\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t\t// something to update further down in customtypes.\n\t\t\t\tfield.id = newId;\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\t...field,\n\t\t\t\tcustomtypes: field.customtypes.map((customTypeArg) => {\n\t\t\t\t\tconst customType = shallowCloneIfObject(customTypeArg);\n\t\t\t\t\tconst previousId = previousPath[0];\n\t\t\t\t\tconst newId = newPath[0];\n\n\t\t\t\t\tif (!previousId || !newId || typeof customType === \"string\") {\n\t\t\t\t\t\treturn customType; // we don't support custom type id renaming\n\t\t\t\t\t}\n\n\t\t\t\t\tif (customType.fields) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t...customType,\n\t\t\t\t\t\t\tfields: customType.fields.map((fieldArg) => {\n\t\t\t\t\t\t\t\tconst field = shallowCloneIfObject(fieldArg);\n\t\t\t\t\t\t\t\tconst previousId = previousPath[1];\n\t\t\t\t\t\t\t\tconst newId = newPath[1];\n\n\t\t\t\t\t\t\t\tif (field === previousId && field !== newId) {\n\t\t\t\t\t\t\t\t\t// Matches the previous id, so we update it and return because\n\t\t\t\t\t\t\t\t\t// it's the last level.\n\t\t\t\t\t\t\t\t\treturn newId;\n\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\treturn field;\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\treturn customType;\n\t\t\t\t}),\n\t\t\t};\n\t\t});\n\n\t\treturn { ...customType, fields: newFields };\n\t}\n\n\treturn customType;\n}\n\n/**\n * Map over the custom types of a Content Relationship Link and update the API\n * IDs that were changed during the custom type update.\n */\nfunction updateCRCustomTypes(\n\targs: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,\n): CrCustomTypes {\n\tconst { customTypes, ...updateMeta } = args;\n\n\treturn customTypes.map((customType) => {\n\t\treturn updateCRCustomType({ customType, ...updateMeta });\n\t});\n}\n\n/**\n * Update the Content Relationship API IDs of a single field. The change is\n * determined by the `previousPath` and `newPath` properties.\n */\nfunction updateFieldContentRelationships<\n\tT extends UID | NestableWidget | Group | NestedGroup,\n>(args: { field: T } & CustomTypeFieldIdChangedMeta): T {\n\tconst { field, ...updateMeta } = args;\n\tif (\n\t\tfield.type !== \"Link\" ||\n\t\tfield.config?.select !== \"document\" ||\n\t\t!field.config?.customtypes\n\t) {\n\t\t// not a content relationship field\n\t\treturn field;\n\t}\n\n\tconst newCustomTypes = updateCRCustomTypes({\n\t\t...updateMeta,\n\t\tcustomTypes: field.config.customtypes,\n\t});\n\n\treturn {\n\t\t...field,\n\t\tconfig: { ...field.config, customtypes: newCustomTypes },\n\t};\n}\n\nexport function updateCustomTypeContentRelationships(\n\targs: {\n\t\tmodels: { model: CustomType }[];\n\t\tonUpdate: (model: CustomType) => void;\n\t} & CustomTypeFieldIdChangedMeta,\n): void {\n\tconst { models, previousPath, newPath, onUpdate } = args;\n\n\tfor (const customType of models) {\n\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\tcustomType: customType.model,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({\n\t\t\t\t\tfield,\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\t\t\t},\n\t\t});\n\n\t\tonUpdate(updatedCustomTypeModel);\n\t}\n}\n\nexport function updateSharedSliceContentRelationships(\n\targs: {\n\t\tmodels: { model: SharedSlice }[];\n\t\tonUpdate: (model: SharedSlice) => void;\n\t} & CustomTypeFieldIdChangedMeta,\n): void {\n\tconst { models, previousPath, newPath, onUpdate } = args;\n\n\tfor (const slice of models) {\n\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\tpath: [\".\"],\n\t\t\tslice: slice.model,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({\n\t\t\t\t\tfield,\n\t\t\t\t\tpreviousPath,\n\t\t\t\t\tnewPath,\n\t\t\t\t});\n\t\t\t},\n\t\t});\n\n\t\tonUpdate(updatedSliceModel);\n\t}\n}\n\nfunction shallowCloneIfObject<T>(value: T): T {\n\tif (typeof value === \"object\") {\n\t\treturn { ...value };\n\t}\n\n\treturn value;\n}\n\nfunction pushIfDefined<T>(array: T[], value: T | undefined) {\n\tif (value) {\n\t\tarray.push(value);\n\t}\n}\n"],"names":["errors","model","previousId","newId","customType","fieldArg","field"],"mappings":";;;;;;;;;;;AAyGM,MAAO,2BAA2B,YAAW;AAAA,EAClD,MAAM,wBAAqB;;AAC1B,6BAAyB,KAAK,wBAAwB;AAEtD,UAAM,aAAa,MAAM,KAAK,yBAAyB,SACtD,4BACA,MAAS;AAEV,UAAM,EAAE,MAAM,OAAA,IAAW,iBACxB,EAAE,KAAK;AAAA,MACN,KAAK,EAAE,MAAM,EAAE,MAAM;AAAA,IAAA,CACrB,GACD,UAAU;AAGJ,WAAA;AAAA,MACN,OAAK,UAAK,CAAC,MAAN,mBAAS,QAAO,CAAE;AAAA,MACvB;AAAA,IAAA;AAAA,EA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S,CAAC,EAAE,YAAW;AACtB,eAAO,gCAAgC;AAAA,UACtC;AAAA,UACA;AAAA,UACA;AAAA,QAAA,CACA;AAAA,MACF;AAAA,IAAA,CACA;AAED,aAAS,iBAAiB;AAAA,EAC3B;AACD;AAEA,SAAS,qBAAwB,OAAQ;AACpC,MAAA,OAAO,UAAU,UAAU;AACvB,WAAA,EAAE,GAAG;EACb;AAEO,SAAA;AACR;AAEA,SAAS,cAAiB,OAAY,OAAoB;AACzD,MAAI,OAAO;AACV,UAAM,KAAK,KAAK;AAAA,EACjB;AACD;"}
1
+ {"version":3,"file":"CustomTypesManager.js","sources":["../../../../src/managers/customTypes/CustomTypesManager.ts"],"sourcesContent":["import * as t from \"io-ts\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport {\n\tCustomType,\n\tGroup,\n\tNestableWidget,\n\tNestedGroup,\n\tSharedSlice,\n\tUID,\n\ttraverseCustomType,\n\ttraverseSharedSlice,\n} from \"@prismicio/types-internal/lib/customtypes\";\nimport {\n\tCallHookReturnType,\n\tCustomTypeCreateHook,\n\tCustomTypeCreateHookData,\n\tCustomTypeReadHookData,\n\tCustomTypeRenameHook,\n\tCustomTypeRenameHookData,\n\tCustomTypeUpdateHook,\n\tCustomTypeUpdateHookData,\n\tHookError,\n} from \"@slicemachine/plugin-kit\";\nimport { z } from \"zod\";\n\nimport { DecodeError } from \"../../lib/DecodeError\";\nimport { assertPluginsInitialized } from \"../../lib/assertPluginsInitialized\";\nimport { decodeHookResult } from \"../../lib/decodeHookResult\";\nimport fetch from \"../../lib/fetch\";\n\nimport { OnlyHookErrors } from \"../../types\";\nimport { API_ENDPOINTS } from \"../../constants/API_ENDPOINTS\";\nimport { SLICE_MACHINE_USER_AGENT } from \"../../constants/SLICE_MACHINE_USER_AGENT\";\nimport { UnauthorizedError } from \"../../errors\";\n\nimport { BaseManager } from \"../BaseManager\";\nimport { CustomTypeFormat } from \"./types\";\n\ntype SliceMachineManagerReadCustomTypeLibraryReturnType = {\n\tids: string[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypesManagerReadAllCustomTypesArgs = {\n\tformat: CustomTypeFormat;\n};\n\ntype SliceMachineManagerReadAllCustomTypeReturnType = {\n\tmodels: { model: CustomType }[];\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerReadCustomTypeReturnType = {\n\tmodel: CustomType | undefined;\n\terrors: (DecodeError | HookError)[];\n};\n\ntype SliceMachineManagerPushCustomTypeArgs = {\n\tid: string;\n\tuserAgent?: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n};\n\ntype SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType = {\n\t// TODO\n\tmocksConfig?: Record<string, unknown>;\n\terrors: HookError[];\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgs = {\n\tcustomTypeID: string;\n\t// TODO\n\tmocksConfig: Record<string, unknown>;\n};\n\ntype SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType = {\n\terrors: HookError[];\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeArgs = {\n\tid: string;\n};\n\ntype CustomTypesMachineManagerDeleteCustomTypeReturnType = {\n\terrors: (DecodeError | HookError)[];\n};\n\ntype CustomTypeFieldIdChangedMeta = NonNullable<\n\tNonNullable<CustomTypeUpdateHookData[\"updateMeta\"]>[\"fieldIdChanged\"]\n>;\n\ntype CrCustomTypes = readonly CrCustomType[];\ntype CrCustomType =\n\t| string\n\t| { id: string; fields?: readonly CrCustomTypeNestedCr[] };\ntype CrCustomTypeNestedCr =\n\t| string\n\t| { id: string; customtypes: readonly CrCustomTypeFieldLeaf[] };\ntype CrCustomTypeFieldLeaf =\n\t| string\n\t| { id: string; fields?: readonly string[] };\n\nexport class CustomTypesManager extends BaseManager {\n\tasync readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type-library:read\",\n\t\t\tundefined,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tids: t.array(t.string),\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tids: data[0]?.ids || [],\n\t\t\terrors,\n\t\t};\n\t}\n\n\tasync readAllCustomTypes(\n\t\targs?: CustomTypesManagerReadAllCustomTypesArgs,\n\t): Promise<SliceMachineManagerReadAllCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst res: SliceMachineManagerReadAllCustomTypeReturnType = {\n\t\t\tmodels: [],\n\t\t\terrors: [],\n\t\t};\n\n\t\tconst { ids, errors } = await this.readCustomTypeLibrary();\n\t\tres.errors = [...res.errors, ...errors];\n\n\t\tif (ids) {\n\t\t\tfor (const id of ids) {\n\t\t\t\tconst { model, errors } = await this.readCustomType({ id });\n\t\t\t\tres.errors = [...res.errors, ...errors];\n\n\t\t\t\tif (model && (!args || args.format === model.format)) {\n\t\t\t\t\tres.models.push({ model });\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn res;\n\t}\n\n\tasync createCustomType(\n\t\targs: CustomTypeCreateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:create\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync readCustomType(\n\t\targs: CustomTypeReadHookData,\n\t): Promise<SliceMachineManagerReadCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:read\",\n\t\t\targs,\n\t\t);\n\t\tconst { data, errors } = decodeHookResult(\n\t\t\tt.type({\n\t\t\t\tmodel: CustomType,\n\t\t\t}),\n\t\t\thookResult,\n\t\t);\n\n\t\treturn {\n\t\t\tmodel: data[0]?.model,\n\t\t\terrors,\n\t\t};\n\t}\n\n\tprivate updateCRCustomType(\n\t\targs: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,\n\t): CrCustomType {\n\t\tconst { previousPath, newPath } = args;\n\n\t\tconst customType = shallowClone(args.customType);\n\n\t\tconst previousCustomTypeId = previousPath[0];\n\t\tconst newCustomTypeId = newPath[0];\n\n\t\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\t\tthrow new Error(\n\t\t\t\t\"Didn't find any customtype id, which should not be possible\",\n\t\t\t);\n\t\t}\n\n\t\tif (typeof customType === \"string\") {\n\t\t\treturn customType; // we don't support custom type id renaming\n\t\t}\n\n\t\tif (customType.fields) {\n\t\t\tconst newFields = customType.fields.map((fieldArg) => {\n\t\t\t\tconst field = shallowClone(fieldArg);\n\n\t\t\t\tconst previousFieldId = previousPath[1];\n\t\t\t\tconst newFieldId = newPath[1];\n\n\t\t\t\tif (!previousFieldId || !newFieldId) {\n\t\t\t\t\treturn field;\n\t\t\t\t}\n\n\t\t\t\tif (typeof field === \"string\") {\n\t\t\t\t\tif (field === previousFieldId && field !== newFieldId) {\n\t\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t\t// so we update it new one. The field is a string, so return the new\n\t\t\t\t\t\t// id.\n\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t}\n\n\t\t\t\t\treturn field;\n\t\t\t\t}\n\n\t\t\t\tif (field.id === previousFieldId && field.id !== newFieldId) {\n\t\t\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t\t\t// so we update it new one.\n\t\t\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t\t\t// something to update further down in customtypes.\n\t\t\t\t\tfield.id = newFieldId;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\t...field,\n\t\t\t\t\tcustomtypes: field.customtypes.map((customTypeArg) => {\n\t\t\t\t\t\tconst customType = shallowClone(customTypeArg);\n\n\t\t\t\t\t\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\t\t\t\t\t\tthrow new Error(\n\t\t\t\t\t\t\t\t\"Didn't find any customtype id, which should not be possible\",\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (typeof customType === \"string\") {\n\t\t\t\t\t\t\treturn customType; // we don't support custom type id renaming\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (customType.fields) {\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t...customType,\n\t\t\t\t\t\t\t\tfields: customType.fields.map((fieldArg) => {\n\t\t\t\t\t\t\t\t\tconst field = shallowClone(fieldArg);\n\n\t\t\t\t\t\t\t\t\tif (field === previousFieldId && field !== newFieldId) {\n\t\t\t\t\t\t\t\t\t\t// Matches the previous id, so we update it and return because\n\t\t\t\t\t\t\t\t\t\t// it's the last level.\n\t\t\t\t\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\t\t\treturn field;\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn customType;\n\t\t\t\t\t}),\n\t\t\t\t};\n\t\t\t});\n\n\t\t\treturn { ...customType, fields: newFields };\n\t\t}\n\n\t\treturn customType;\n\t}\n\n\t/**\n\t * Map over the custom types of a Content Relationship Link and update the API\n\t * IDs that were changed during the custom type update.\n\t */\n\tprivate updateCRCustomTypes(\n\t\targs: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,\n\t): CrCustomTypes {\n\t\tconst { customTypes, ...updateMeta } = args;\n\n\t\treturn customTypes.map((customType) => {\n\t\t\treturn this.updateCRCustomType({ customType, ...updateMeta });\n\t\t});\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs of a single field. The change is\n\t * determined by the `previousPath` and `newPath` properties.\n\t */\n\tprivate updateFieldContentRelationships<\n\t\tT extends UID | NestableWidget | Group | NestedGroup,\n\t>(args: { field: T } & CustomTypeFieldIdChangedMeta): T {\n\t\tconst { field, ...updateMeta } = args;\n\t\tif (\n\t\t\tfield.type !== \"Link\" ||\n\t\t\tfield.config?.select !== \"document\" ||\n\t\t\t!field.config?.customtypes\n\t\t) {\n\t\t\t// not a content relationship field\n\t\t\treturn field;\n\t\t}\n\n\t\tconst newCustomTypes = this.updateCRCustomTypes({\n\t\t\t...updateMeta,\n\t\t\tcustomTypes: field.config.customtypes,\n\t\t});\n\n\t\treturn {\n\t\t\t...field,\n\t\t\tconfig: { ...field.config, customtypes: newCustomTypes },\n\t\t};\n\t}\n\n\t/**\n\t * Update the Content Relationship API IDs for all existing custom types and\n\t * slices. The change is determined by properties inside the `updateMeta`\n\t * property.\n\t */\n\tprivate async updateContentRelationships(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, updateMeta } = args;\n\n\t\tif (updateMeta?.fieldIdChanged) {\n\t\t\tlet { previousPath, newPath } = updateMeta.fieldIdChanged;\n\n\t\t\tif (previousPath.join(\".\") !== newPath.join(\".\")) {\n\t\t\t\tpreviousPath = [model.id, ...previousPath];\n\t\t\t\tnewPath = [model.id, ...newPath];\n\n\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\tfor (const customType of customTypes.models) {\n\t\t\t\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\t\t\t\tcustomType: customType.model,\n\t\t\t\t\t\tonField: ({ field }) => {\n\t\t\t\t\t\t\treturn this.updateFieldContentRelationships({\n\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t},\n\t\t\t\t\t});\n\n\t\t\t\t\tcrUpdates.push(\n\t\t\t\t\t\tthis.sliceMachinePluginRunner.callHook(\"custom-type:update\", {\n\t\t\t\t\t\t\tmodel: updatedCustomTypeModel,\n\t\t\t\t\t\t}),\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t});\n\n\t\t\t\t\tfor (const slice of slices.models) {\n\t\t\t\t\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\t\t\t\t\tpath: [\".\"],\n\t\t\t\t\t\t\tslice: slice.model,\n\t\t\t\t\t\t\tonField: ({ field }) => {\n\t\t\t\t\t\t\t\treturn this.updateFieldContentRelationships({\n\t\t\t\t\t\t\t\t\tfield,\n\t\t\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tcrUpdates.push(\n\t\t\t\t\t\t\tthis.sliceMachinePluginRunner.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\tmodel: updatedSliceModel,\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn { errors: [] };\n\t}\n\n\tasync updateCustomType(\n\t\targs: CustomTypeUpdateHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:update\",\n\t\t\targs,\n\t\t);\n\n\t\tif (args.updateMeta?.fieldIdChanged) {\n\t\t\tawait this.updateContentRelationships(args);\n\t\t}\n\n\t\treturn { errors: hookResult.errors };\n\t}\n\n\tasync renameCustomType(\n\t\targs: CustomTypeRenameHookData,\n\t): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeRenameHook>>> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:rename\",\n\t\t\targs,\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync deleteCustomType(\n\t\targs: CustomTypesMachineManagerDeleteCustomTypeArgs,\n\t): Promise<CustomTypesMachineManagerDeleteCustomTypeReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst { model, errors: readCustomTypeErrors } = await this.readCustomType({\n\t\t\tid: args.id,\n\t\t});\n\n\t\tif (model) {\n\t\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\t\"custom-type:delete\",\n\t\t\t\t{ model },\n\t\t\t);\n\n\t\t\treturn {\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\terrors: readCustomTypeErrors,\n\t\t\t};\n\t\t}\n\t}\n\n\tasync pushCustomType(\n\t\targs: SliceMachineManagerPushCustomTypeArgs,\n\t): Promise<void> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\t// TODO: Handle errors\n\t\tconst { model } = await this.readCustomType({ id: args.id });\n\n\t\tif (model) {\n\t\t\t// TODO: Create a single shared client.\n\t\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\t\trepositoryName,\n\t\t\t\ttoken: authenticationToken,\n\t\t\t\tfetch,\n\t\t\t\tfetchOptions: {\n\t\t\t\t\theaders: {\n\t\t\t\t\t\t\"User-Agent\": args.userAgent || SLICE_MACHINE_USER_AGENT,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t});\n\n\t\t\ttry {\n\t\t\t\t// Check if Custom Type already exists on the repository.\n\t\t\t\tawait client.getCustomTypeByID(args.id);\n\n\t\t\t\t// If it exists on the repository, update it.\n\t\t\t\tawait client.updateCustomType(model);\n\t\t\t} catch (error) {\n\t\t\t\tif (error instanceof prismicCustomTypesClient.NotFoundError) {\n\t\t\t\t\t// If it doesn't exist on the repository, insert it.\n\t\t\t\t\tawait client.insertCustomType(model);\n\t\t\t\t} else if (error instanceof prismicCustomTypesClient.ForbiddenError) {\n\t\t\t\t\tthrow new UnauthorizedError(\n\t\t\t\t\t\t\"You do not have access to push types to this Prismic repository.\",\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tcause: error,\n\t\t\t\t\t\t},\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync readCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerReadCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerReadCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:read\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tassetID: \"mocks.config.json\",\n\t\t\t},\n\t\t);\n\t\tconst data = hookResult.data[0]?.data;\n\n\t\t// TODO: Validate the returned data.\n\n\t\tif (data) {\n\t\t\treturn {\n\t\t\t\tmocksConfig: JSON.parse(data.toString()),\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tmocksConfig: undefined,\n\t\t\t\terrors: hookResult.errors,\n\t\t\t};\n\t\t}\n\t}\n\n\t// TODO: Remove\n\tasync updateCustomTypeMocksConfig(\n\t\targs: SliceMachineManagerUpdateCustomTypeMocksConfigArgs,\n\t): Promise<SliceMachineManagerUpdateCustomTypeMocksConfigArgsReturnType> {\n\t\tassertPluginsInitialized(this.sliceMachinePluginRunner);\n\n\t\tconst hookResult = await this.sliceMachinePluginRunner.callHook(\n\t\t\t\"custom-type:asset:update\",\n\t\t\t{\n\t\t\t\tcustomTypeID: args.customTypeID,\n\t\t\t\tasset: {\n\t\t\t\t\tid: \"mocks.config.json\",\n\t\t\t\t\tdata: Buffer.from(JSON.stringify(args.mocksConfig, null, \"\\t\")),\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\n\t}\n\n\tasync fetchRemoteCustomTypes(): Promise<CustomType[]> {\n\t\tconst authenticationToken = await this.user.getAuthenticationToken();\n\t\tconst repositoryName = await this.project.getResolvedRepositoryName();\n\n\t\tconst client = prismicCustomTypesClient.createClient({\n\t\t\tendpoint: API_ENDPOINTS.PrismicModels,\n\t\t\trepositoryName,\n\t\t\ttoken: authenticationToken,\n\t\t\tfetch,\n\t\t\tfetchOptions: {\n\t\t\t\theaders: {\n\t\t\t\t\t\"User-Agent\": SLICE_MACHINE_USER_AGENT,\n\t\t\t\t},\n\t\t\t},\n\t\t});\n\n\t\treturn await client.getAllCustomTypes();\n\t}\n\n\tasync inferSlice({\n\t\timageUrl,\n\t}: {\n\t\timageUrl: string;\n\t}): Promise<InferSliceResponse> {\n\t\tconst authToken = await this.user.getAuthenticationToken();\n\t\tconst headers = {\n\t\t\tAuthorization: `Bearer ${authToken}`,\n\t\t};\n\n\t\tconst repository = await this.project.getResolvedRepositoryName();\n\t\tconst searchParams = new URLSearchParams({\n\t\t\trepository,\n\t\t});\n\n\t\tconst url = new URL(\"./slices/infer\", API_ENDPOINTS.CustomTypeService);\n\t\turl.search = searchParams.toString();\n\n\t\tconst response = await fetch(url.toString(), {\n\t\t\tmethod: \"POST\",\n\t\t\theaders: headers,\n\t\t\tbody: JSON.stringify({ imageUrl }),\n\t\t});\n\n\t\tif (!response.ok) {\n\t\t\tthrow new Error(`Failed to infer slice: ${response.statusText}`);\n\t\t}\n\n\t\tconst json = await response.json();\n\n\t\treturn InferSliceResponse.parse(json);\n\t}\n}\n\ntype InferSliceResponse = z.infer<typeof InferSliceResponse>;\n\nconst InferSliceResponse = z.object({\n\tslice: z.custom().transform((value, ctx) => {\n\t\tconst result = SharedSlice.decode(value);\n\t\tif (result._tag === \"Right\") {\n\t\t\treturn result.right;\n\t\t}\n\t\tctx.addIssue({\n\t\t\tcode: z.ZodIssueCode.custom,\n\t\t\tmessage: `Invalid shared slice: ${JSON.stringify(value, null, 2)}`,\n\t\t});\n\n\t\treturn z.NEVER;\n\t}),\n\tlangSmithUrl: z.string().url().optional(),\n});\n\nfunction shallowClone<T>(value: T): T {\n\tif (typeof value === \"object\") {\n\t\treturn { ...value };\n\t}\n\n\treturn value;\n}\n"],"names":["errors","customType","fieldArg","field"],"mappings":";;;;;;;;;;;AAyGM,MAAO,2BAA2B,YAAW;AAAA,EAClD,MAAM,wBAAqB;;AAC1B,6BAAyB,KAAK,wBAAwB;AAEtD,UAAM,aAAa,MAAM,KAAK,yBAAyB,SACtD,4BACA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c,EAAE,OAAS,EAAA,IAAA,EAAM,SAAU;AACzC,CAAA;AAED,SAAS,aAAgB,OAAQ;AAC5B,MAAA,OAAO,UAAU,UAAU;AACvB,WAAA,EAAE,GAAG;EACb;AAEO,SAAA;AACR;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@slicemachine/manager",
3
- "version": "0.24.14-alpha.jp-update-cr-links-unit.9",
3
+ "version": "0.24.14-alpha.jp-update-cr-links-remove-recursion.6",
4
4
  "description": "Manage all aspects of a Slice Machine project.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -70,7 +70,7 @@
70
70
  "@prismicio/mocks": "2.13.0",
71
71
  "@prismicio/types-internal": "3.10.0-alpha.0",
72
72
  "@segment/analytics-node": "^2.1.2",
73
- "@slicemachine/plugin-kit": "0.4.76-alpha.jp-update-cr-links-unit.9",
73
+ "@slicemachine/plugin-kit": "0.4.76-alpha.jp-update-cr-links-remove-recursion.6",
74
74
  "cookie": "^1.0.1",
75
75
  "cors": "^2.8.5",
76
76
  "execa": "^7.1.1",
@@ -188,6 +188,141 @@ export class CustomTypesManager extends BaseManager {
188
188
  };
189
189
  }
190
190
 
191
+ private updateCRCustomType(
192
+ args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
193
+ ): CrCustomType {
194
+ const { previousPath, newPath } = args;
195
+
196
+ const customType = shallowClone(args.customType);
197
+
198
+ const previousCustomTypeId = previousPath[0];
199
+ const newCustomTypeId = newPath[0];
200
+
201
+ if (!previousCustomTypeId || !newCustomTypeId) {
202
+ throw new Error(
203
+ "Didn't find any customtype id, which should not be possible",
204
+ );
205
+ }
206
+
207
+ if (typeof customType === "string") {
208
+ return customType; // we don't support custom type id renaming
209
+ }
210
+
211
+ if (customType.fields) {
212
+ const newFields = customType.fields.map((fieldArg) => {
213
+ const field = shallowClone(fieldArg);
214
+
215
+ const previousFieldId = previousPath[1];
216
+ const newFieldId = newPath[1];
217
+
218
+ if (!previousFieldId || !newFieldId) {
219
+ return field;
220
+ }
221
+
222
+ if (typeof field === "string") {
223
+ if (field === previousFieldId && field !== newFieldId) {
224
+ // We have reached a field id that matches the id that was renamed,
225
+ // so we update it new one. The field is a string, so return the new
226
+ // id.
227
+ return newFieldId;
228
+ }
229
+
230
+ return field;
231
+ }
232
+
233
+ if (field.id === previousFieldId && field.id !== newFieldId) {
234
+ // We have reached a field id that matches the id that was renamed,
235
+ // so we update it new one.
236
+ // Since field is not a string, we don't exit, as we might have
237
+ // something to update further down in customtypes.
238
+ field.id = newFieldId;
239
+ }
240
+
241
+ return {
242
+ ...field,
243
+ customtypes: field.customtypes.map((customTypeArg) => {
244
+ const customType = shallowClone(customTypeArg);
245
+
246
+ if (!previousCustomTypeId || !newCustomTypeId) {
247
+ throw new Error(
248
+ "Didn't find any customtype id, which should not be possible",
249
+ );
250
+ }
251
+
252
+ if (typeof customType === "string") {
253
+ return customType; // we don't support custom type id renaming
254
+ }
255
+
256
+ if (customType.fields) {
257
+ return {
258
+ ...customType,
259
+ fields: customType.fields.map((fieldArg) => {
260
+ const field = shallowClone(fieldArg);
261
+
262
+ if (field === previousFieldId && field !== newFieldId) {
263
+ // Matches the previous id, so we update it and return because
264
+ // it's the last level.
265
+ return newFieldId;
266
+ }
267
+
268
+ return field;
269
+ }),
270
+ };
271
+ }
272
+
273
+ return customType;
274
+ }),
275
+ };
276
+ });
277
+
278
+ return { ...customType, fields: newFields };
279
+ }
280
+
281
+ return customType;
282
+ }
283
+
284
+ /**
285
+ * Map over the custom types of a Content Relationship Link and update the API
286
+ * IDs that were changed during the custom type update.
287
+ */
288
+ private updateCRCustomTypes(
289
+ args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
290
+ ): CrCustomTypes {
291
+ const { customTypes, ...updateMeta } = args;
292
+
293
+ return customTypes.map((customType) => {
294
+ return this.updateCRCustomType({ customType, ...updateMeta });
295
+ });
296
+ }
297
+
298
+ /**
299
+ * Update the Content Relationship API IDs of a single field. The change is
300
+ * determined by the `previousPath` and `newPath` properties.
301
+ */
302
+ private updateFieldContentRelationships<
303
+ T extends UID | NestableWidget | Group | NestedGroup,
304
+ >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
305
+ const { field, ...updateMeta } = args;
306
+ if (
307
+ field.type !== "Link" ||
308
+ field.config?.select !== "document" ||
309
+ !field.config?.customtypes
310
+ ) {
311
+ // not a content relationship field
312
+ return field;
313
+ }
314
+
315
+ const newCustomTypes = this.updateCRCustomTypes({
316
+ ...updateMeta,
317
+ customTypes: field.config.customtypes,
318
+ });
319
+
320
+ return {
321
+ ...field,
322
+ config: { ...field.config, customtypes: newCustomTypes },
323
+ };
324
+ }
325
+
191
326
  /**
192
327
  * Update the Content Relationship API IDs for all existing custom types and
193
328
  * slices. The change is determined by properties inside the `updateMeta`
@@ -213,19 +348,24 @@ export class CustomTypesManager extends BaseManager {
213
348
  // any custom type and update them to use the new one.
214
349
  const customTypes = await this.readAllCustomTypes();
215
350
 
216
- updateCustomTypeContentRelationships({
217
- models: customTypes.models,
218
- onUpdate: (model) => {
219
- pushIfDefined(
220
- crUpdates,
221
- this.sliceMachinePluginRunner?.callHook("custom-type:update", {
222
- model,
223
- }),
224
- );
225
- },
226
- previousPath,
227
- newPath,
228
- });
351
+ for (const customType of customTypes.models) {
352
+ const updatedCustomTypeModel = traverseCustomType({
353
+ customType: customType.model,
354
+ onField: ({ field }) => {
355
+ return this.updateFieldContentRelationships({
356
+ field,
357
+ previousPath,
358
+ newPath,
359
+ });
360
+ },
361
+ });
362
+
363
+ crUpdates.push(
364
+ this.sliceMachinePluginRunner.callHook("custom-type:update", {
365
+ model: updatedCustomTypeModel,
366
+ }),
367
+ );
368
+ }
229
369
 
230
370
  // Find existing slice with content relationships that link to the renamed
231
371
  // field id in all libraries and update them to use the new one.
@@ -236,20 +376,26 @@ export class CustomTypesManager extends BaseManager {
236
376
  libraryID: library.libraryID,
237
377
  });
238
378
 
239
- updateSharedSliceContentRelationships({
240
- models: slices.models,
241
- onUpdate: (model) => {
242
- pushIfDefined(
243
- crUpdates,
244
- this.sliceMachinePluginRunner?.callHook("slice:update", {
245
- libraryID: library.libraryID,
246
- model,
247
- }),
248
- );
249
- },
250
- previousPath,
251
- newPath,
252
- });
379
+ for (const slice of slices.models) {
380
+ const updatedSliceModel = traverseSharedSlice({
381
+ path: ["."],
382
+ slice: slice.model,
383
+ onField: ({ field }) => {
384
+ return this.updateFieldContentRelationships({
385
+ field,
386
+ previousPath,
387
+ newPath,
388
+ });
389
+ },
390
+ });
391
+
392
+ crUpdates.push(
393
+ this.sliceMachinePluginRunner.callHook("slice:update", {
394
+ libraryID: library.libraryID,
395
+ model: updatedSliceModel,
396
+ }),
397
+ );
398
+ }
253
399
  }
254
400
 
255
401
  // Process all the Content Relationship updates at once.
@@ -495,192 +641,10 @@ const InferSliceResponse = z.object({
495
641
  langSmithUrl: z.string().url().optional(),
496
642
  });
497
643
 
498
- function updateCRCustomType(
499
- args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
500
- ): CrCustomType {
501
- const { previousPath, newPath } = args;
502
-
503
- const customType = shallowCloneIfObject(args.customType);
504
-
505
- const previousId = previousPath[0];
506
- const newId = newPath[0];
507
-
508
- if (!previousId || !newId || typeof customType === "string") {
509
- return customType; // we don't support custom type id renaming
510
- }
511
-
512
- if (customType.fields) {
513
- const newFields = customType.fields.map((fieldArg) => {
514
- const field = shallowCloneIfObject(fieldArg);
515
-
516
- const previousId = previousPath[1];
517
- const newId = newPath[1];
518
-
519
- if (!previousId || !newId) {
520
- return field;
521
- }
522
-
523
- if (typeof field === "string") {
524
- if (field === previousId && field !== newId) {
525
- // We have reached a field id that matches the id that was renamed,
526
- // so we update it new one. The field is a string, so return the new
527
- // id.
528
- return newId;
529
- }
530
-
531
- return field;
532
- }
533
-
534
- if (field.id === previousId && field.id !== newId) {
535
- // We have reached a field id that matches the id that was renamed,
536
- // so we update it new one.
537
- // Since field is not a string, we don't exit, as we might have
538
- // something to update further down in customtypes.
539
- field.id = newId;
540
- }
541
-
542
- return {
543
- ...field,
544
- customtypes: field.customtypes.map((customTypeArg) => {
545
- const customType = shallowCloneIfObject(customTypeArg);
546
- const previousId = previousPath[0];
547
- const newId = newPath[0];
548
-
549
- if (!previousId || !newId || typeof customType === "string") {
550
- return customType; // we don't support custom type id renaming
551
- }
552
-
553
- if (customType.fields) {
554
- return {
555
- ...customType,
556
- fields: customType.fields.map((fieldArg) => {
557
- const field = shallowCloneIfObject(fieldArg);
558
- const previousId = previousPath[1];
559
- const newId = newPath[1];
560
-
561
- if (field === previousId && field !== newId) {
562
- // Matches the previous id, so we update it and return because
563
- // it's the last level.
564
- return newId;
565
- }
566
-
567
- return field;
568
- }),
569
- };
570
- }
571
-
572
- return customType;
573
- }),
574
- };
575
- });
576
-
577
- return { ...customType, fields: newFields };
578
- }
579
-
580
- return customType;
581
- }
582
-
583
- /**
584
- * Map over the custom types of a Content Relationship Link and update the API
585
- * IDs that were changed during the custom type update.
586
- */
587
- function updateCRCustomTypes(
588
- args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
589
- ): CrCustomTypes {
590
- const { customTypes, ...updateMeta } = args;
591
-
592
- return customTypes.map((customType) => {
593
- return updateCRCustomType({ customType, ...updateMeta });
594
- });
595
- }
596
-
597
- /**
598
- * Update the Content Relationship API IDs of a single field. The change is
599
- * determined by the `previousPath` and `newPath` properties.
600
- */
601
- function updateFieldContentRelationships<
602
- T extends UID | NestableWidget | Group | NestedGroup,
603
- >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
604
- const { field, ...updateMeta } = args;
605
- if (
606
- field.type !== "Link" ||
607
- field.config?.select !== "document" ||
608
- !field.config?.customtypes
609
- ) {
610
- // not a content relationship field
611
- return field;
612
- }
613
-
614
- const newCustomTypes = updateCRCustomTypes({
615
- ...updateMeta,
616
- customTypes: field.config.customtypes,
617
- });
618
-
619
- return {
620
- ...field,
621
- config: { ...field.config, customtypes: newCustomTypes },
622
- };
623
- }
624
-
625
- export function updateCustomTypeContentRelationships(
626
- args: {
627
- models: { model: CustomType }[];
628
- onUpdate: (model: CustomType) => void;
629
- } & CustomTypeFieldIdChangedMeta,
630
- ): void {
631
- const { models, previousPath, newPath, onUpdate } = args;
632
-
633
- for (const customType of models) {
634
- const updatedCustomTypeModel = traverseCustomType({
635
- customType: customType.model,
636
- onField: ({ field }) => {
637
- return updateFieldContentRelationships({
638
- field,
639
- previousPath,
640
- newPath,
641
- });
642
- },
643
- });
644
-
645
- onUpdate(updatedCustomTypeModel);
646
- }
647
- }
648
-
649
- export function updateSharedSliceContentRelationships(
650
- args: {
651
- models: { model: SharedSlice }[];
652
- onUpdate: (model: SharedSlice) => void;
653
- } & CustomTypeFieldIdChangedMeta,
654
- ): void {
655
- const { models, previousPath, newPath, onUpdate } = args;
656
-
657
- for (const slice of models) {
658
- const updatedSliceModel = traverseSharedSlice({
659
- path: ["."],
660
- slice: slice.model,
661
- onField: ({ field }) => {
662
- return updateFieldContentRelationships({
663
- field,
664
- previousPath,
665
- newPath,
666
- });
667
- },
668
- });
669
-
670
- onUpdate(updatedSliceModel);
671
- }
672
- }
673
-
674
- function shallowCloneIfObject<T>(value: T): T {
644
+ function shallowClone<T>(value: T): T {
675
645
  if (typeof value === "object") {
676
646
  return { ...value };
677
647
  }
678
648
 
679
649
  return value;
680
650
  }
681
-
682
- function pushIfDefined<T>(array: T[], value: T | undefined) {
683
- if (value) {
684
- array.push(value);
685
- }
686
- }