@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-delete-field-data-flow.11 → 0.24.14-alpha.xru-slice-create-placeholder-for-mcp-server.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/managers/customTypes/CustomTypesManager.cjs +3 -168
- package/dist/managers/customTypes/CustomTypesManager.cjs.map +1 -1
- package/dist/managers/customTypes/CustomTypesManager.d.ts +9 -119
- package/dist/managers/customTypes/CustomTypesManager.js +5 -170
- package/dist/managers/customTypes/CustomTypesManager.js.map +1 -1
- package/package.json +4 -4
- package/src/managers/customTypes/CustomTypesManager.ts +3 -292
@@ -80,68 +80,12 @@ class CustomTypesManager extends BaseManager.BaseManager {
|
|
80
80
|
errors: errors2
|
81
81
|
};
|
82
82
|
}
|
83
|
-
/**
|
84
|
-
* Update the Content Relationship API IDs for all existing custom types and
|
85
|
-
* slices. The change is determined by properties inside the `updated`
|
86
|
-
* property.
|
87
|
-
*/
|
88
|
-
async updateContentRelationships(args) {
|
89
|
-
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
90
|
-
const { model, updates } = args;
|
91
|
-
if (updates) {
|
92
|
-
for (const [previousPathStr, newPathStr] of Object.entries(updates)) {
|
93
|
-
if (previousPathStr !== newPathStr) {
|
94
|
-
const previousPath = [model.id, ...previousPathStr.split(".")];
|
95
|
-
const newPath = [model.id, ...newPathStr.split(".")];
|
96
|
-
const crUpdates = [];
|
97
|
-
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
|
-
});
|
109
|
-
const { libraries } = await this.slices.readAllSliceLibraries();
|
110
|
-
for (const library of libraries) {
|
111
|
-
const slices = await this.slices.readAllSlicesForLibrary({
|
112
|
-
libraryID: library.libraryID
|
113
|
-
});
|
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
|
-
});
|
126
|
-
}
|
127
|
-
const crUpdatesResult = await Promise.all(crUpdates);
|
128
|
-
if (crUpdatesResult.some((result) => result.errors.length > 0)) {
|
129
|
-
return {
|
130
|
-
errors: crUpdatesResult.flatMap((result) => result.errors)
|
131
|
-
};
|
132
|
-
}
|
133
|
-
}
|
134
|
-
}
|
135
|
-
}
|
136
|
-
return { errors: [] };
|
137
|
-
}
|
138
83
|
async updateCustomType(args) {
|
139
84
|
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
140
85
|
const hookResult = await this.sliceMachinePluginRunner.callHook("custom-type:update", args);
|
141
|
-
|
142
|
-
|
143
|
-
}
|
144
|
-
return { errors: hookResult.errors };
|
86
|
+
return {
|
87
|
+
errors: hookResult.errors
|
88
|
+
};
|
145
89
|
}
|
146
90
|
async renameCustomType(args) {
|
147
91
|
assertPluginsInitialized.assertPluginsInitialized(this.sliceMachinePluginRunner);
|
@@ -287,114 +231,5 @@ const InferSliceResponse = index.default.object({
|
|
287
231
|
}),
|
288
232
|
langSmithUrl: index.default.string().url().optional()
|
289
233
|
});
|
290
|
-
function updateCRCustomType(args) {
|
291
|
-
const [previousCustomTypeId, previousFieldId] = args.previousPath;
|
292
|
-
const [newCustomTypeId, newFieldId] = args.newPath;
|
293
|
-
if (!previousCustomTypeId || !newCustomTypeId) {
|
294
|
-
throw new Error("Could not find a customtype id in previousPath and/or newPath, which should not be possible.");
|
295
|
-
}
|
296
|
-
if (!previousFieldId || !newFieldId) {
|
297
|
-
throw new Error("Could not find a field id in previousPath and/or newPath, which should not be possible.");
|
298
|
-
}
|
299
|
-
const customType = shallowCloneIfObject(args.customType);
|
300
|
-
if (typeof customType === "string" || !customType.fields) {
|
301
|
-
return customType;
|
302
|
-
}
|
303
|
-
const matchedCustomTypeId = customType.id === previousCustomTypeId;
|
304
|
-
const newFields = customType.fields.map((fieldArg) => {
|
305
|
-
const customTypeField = shallowCloneIfObject(fieldArg);
|
306
|
-
if (typeof customTypeField === "string") {
|
307
|
-
if (matchedCustomTypeId && customTypeField === previousFieldId && customTypeField !== newFieldId) {
|
308
|
-
return newFieldId;
|
309
|
-
}
|
310
|
-
return customTypeField;
|
311
|
-
}
|
312
|
-
if (matchedCustomTypeId && customTypeField.id === previousFieldId && customTypeField.id !== newFieldId) {
|
313
|
-
customTypeField.id = newFieldId;
|
314
|
-
}
|
315
|
-
return {
|
316
|
-
...customTypeField,
|
317
|
-
customtypes: customTypeField.customtypes.map((customTypeArg) => {
|
318
|
-
const nestedCustomType = shallowCloneIfObject(customTypeArg);
|
319
|
-
if (typeof nestedCustomType === "string" || !nestedCustomType.fields || // Since we are on the last level, if we don't start matching right
|
320
|
-
// at the custom type id, we can return exit early because it's not
|
321
|
-
// a match.
|
322
|
-
nestedCustomType.id !== previousCustomTypeId) {
|
323
|
-
return nestedCustomType;
|
324
|
-
}
|
325
|
-
return {
|
326
|
-
...nestedCustomType,
|
327
|
-
fields: nestedCustomType.fields.map((fieldArg2) => {
|
328
|
-
const nestedCustomTypeField = shallowCloneIfObject(fieldArg2);
|
329
|
-
if (nestedCustomTypeField === previousFieldId && nestedCustomTypeField !== newFieldId) {
|
330
|
-
return newFieldId;
|
331
|
-
}
|
332
|
-
return nestedCustomTypeField;
|
333
|
-
})
|
334
|
-
};
|
335
|
-
})
|
336
|
-
};
|
337
|
-
});
|
338
|
-
return { ...customType, fields: newFields };
|
339
|
-
}
|
340
|
-
function updateFieldContentRelationships(args) {
|
341
|
-
var _a, _b;
|
342
|
-
const { field, ...updatedPaths } = args;
|
343
|
-
if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
|
344
|
-
return field;
|
345
|
-
}
|
346
|
-
const newCustomTypes = field.config.customtypes.map((customType) => {
|
347
|
-
return updateCRCustomType({ customType, ...updatedPaths });
|
348
|
-
});
|
349
|
-
return {
|
350
|
-
...field,
|
351
|
-
config: { ...field.config, customtypes: newCustomTypes }
|
352
|
-
};
|
353
|
-
}
|
354
|
-
function updateCustomTypeContentRelationships(args) {
|
355
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
356
|
-
for (const { model: customType } of models) {
|
357
|
-
const updatedCustomTypeModel = customtypes.traverseCustomType({
|
358
|
-
customType,
|
359
|
-
onField: ({ field }) => {
|
360
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
361
|
-
}
|
362
|
-
});
|
363
|
-
if (!isEqualModel(customType, updatedCustomTypeModel)) {
|
364
|
-
onUpdate(updatedCustomTypeModel);
|
365
|
-
}
|
366
|
-
}
|
367
|
-
}
|
368
|
-
function updateSharedSliceContentRelationships(args) {
|
369
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
370
|
-
for (const { model: slice } of models) {
|
371
|
-
const updatedSliceModel = customtypes.traverseSharedSlice({
|
372
|
-
path: ["."],
|
373
|
-
slice,
|
374
|
-
onField: ({ field }) => {
|
375
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
376
|
-
}
|
377
|
-
});
|
378
|
-
if (!isEqualModel(slice, updatedSliceModel)) {
|
379
|
-
onUpdate(updatedSliceModel);
|
380
|
-
}
|
381
|
-
}
|
382
|
-
}
|
383
|
-
function isEqualModel(modelA, modelB) {
|
384
|
-
return JSON.stringify(modelA) === JSON.stringify(modelB);
|
385
|
-
}
|
386
|
-
function shallowCloneIfObject(value) {
|
387
|
-
if (typeof value === "object") {
|
388
|
-
return { ...value };
|
389
|
-
}
|
390
|
-
return value;
|
391
|
-
}
|
392
|
-
function pushIfDefined(array, value) {
|
393
|
-
if (value) {
|
394
|
-
array.push(value);
|
395
|
-
}
|
396
|
-
}
|
397
234
|
exports.CustomTypesManager = CustomTypesManager;
|
398
|
-
exports.updateCustomTypeContentRelationships = updateCustomTypeContentRelationships;
|
399
|
-
exports.updateSharedSliceContentRelationships = updateSharedSliceContentRelationships;
|
400
235
|
//# 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 CustomTypeFieldUpdatedPaths = {\n\tpreviousPath: string[];\n\tnewPath: string[];\n};\n\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 `updated`\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, updates } = args;\n\n\t\tif (updates) {\n\t\t\tfor (const [previousPathStr, newPathStr] of Object.entries(updates)) {\n\t\t\t\tif (previousPathStr !== newPathStr) {\n\t\t\t\t\tconst previousPath = [model.id, ...previousPathStr.split(\".\")];\n\t\t\t\t\tconst newPath = [model.id, ...newPathStr.split(\".\")];\n\t\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\t\tupdateCustomTypeContentRelationships({\n\t\t\t\t\t\tmodels: customTypes.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(\"custom-type:update\", {\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\n\t\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tupdateSharedSliceContentRelationships({\n\t\t\t\t\t\t\tmodels: slices.models,\n\t\t\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t\t};\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.updates) {\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 } & CustomTypeFieldUpdatedPaths,\n): CrCustomType {\n\tconst [previousCustomTypeId, previousFieldId] = args.previousPath;\n\tconst [newCustomTypeId, newFieldId] = args.newPath;\n\n\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\tthrow new Error(\n\t\t\t\"Could not find a customtype id in previousPath and/or newPath, which should not be possible.\",\n\t\t);\n\t}\n\n\tif (!previousFieldId || !newFieldId) {\n\t\tthrow new Error(\n\t\t\t\"Could not find a field id in previousPath and/or newPath, which should not be possible.\",\n\t\t);\n\t}\n\n\tconst customType = shallowCloneIfObject(args.customType);\n\n\tif (typeof customType === \"string\" || !customType.fields) {\n\t\treturn customType;\n\t}\n\n\tconst matchedCustomTypeId = customType.id === previousCustomTypeId;\n\n\tconst newFields = customType.fields.map((fieldArg) => {\n\t\tconst customTypeField = shallowCloneIfObject(fieldArg);\n\n\t\tif (typeof customTypeField === \"string\") {\n\t\t\tif (\n\t\t\t\tmatchedCustomTypeId &&\n\t\t\t\tcustomTypeField === previousFieldId &&\n\t\t\t\tcustomTypeField !== newFieldId\n\t\t\t) {\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. The field is a string, so return the new\n\t\t\t\t// id.\n\t\t\t\treturn newFieldId;\n\t\t\t}\n\n\t\t\treturn customTypeField;\n\t\t}\n\n\t\tif (\n\t\t\tmatchedCustomTypeId &&\n\t\t\tcustomTypeField.id === previousFieldId &&\n\t\t\tcustomTypeField.id !== newFieldId\n\t\t) {\n\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t// so we update it new one.\n\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t// something to update further down in customtypes.\n\t\t\tcustomTypeField.id = newFieldId;\n\t\t}\n\n\t\treturn {\n\t\t\t...customTypeField,\n\t\t\tcustomtypes: customTypeField.customtypes.map((customTypeArg) => {\n\t\t\t\tconst nestedCustomType = shallowCloneIfObject(customTypeArg);\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof nestedCustomType === \"string\" ||\n\t\t\t\t\t!nestedCustomType.fields ||\n\t\t\t\t\t// Since we are on the last level, if we don't start matching right\n\t\t\t\t\t// at the custom type id, we can return exit early because it's not\n\t\t\t\t\t// a match.\n\t\t\t\t\tnestedCustomType.id !== previousCustomTypeId\n\t\t\t\t) {\n\t\t\t\t\treturn nestedCustomType;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\t...nestedCustomType,\n\t\t\t\t\tfields: nestedCustomType.fields.map((fieldArg) => {\n\t\t\t\t\t\tconst nestedCustomTypeField = shallowCloneIfObject(fieldArg);\n\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tnestedCustomTypeField === previousFieldId &&\n\t\t\t\t\t\t\tnestedCustomTypeField !== newFieldId\n\t\t\t\t\t\t) {\n\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// it's the last level.\n\t\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn nestedCustomTypeField;\n\t\t\t\t\t}),\n\t\t\t\t};\n\t\t\t}),\n\t\t};\n\t});\n\n\treturn { ...customType, fields: newFields };\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 } & CustomTypeFieldUpdatedPaths): T {\n\tconst { field, ...updatedPaths } = 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 = field.config.customtypes.map((customType) => {\n\t\treturn updateCRCustomType({ customType, ...updatedPaths });\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} & CustomTypeFieldUpdatedPaths,\n): void {\n\tconst { models, onUpdate, ...updatedPaths } = args;\n\n\tfor (const { model: customType } of models) {\n\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\tcustomType,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({ ...updatedPaths, field });\n\t\t\t},\n\t\t});\n\n\t\tif (!isEqualModel(customType, updatedCustomTypeModel)) {\n\t\t\tonUpdate(updatedCustomTypeModel);\n\t\t}\n\t}\n}\n\nexport function updateSharedSliceContentRelationships(\n\targs: {\n\t\tmodels: { model: SharedSlice }[];\n\t\tonUpdate: (model: SharedSlice) => void;\n\t} & CustomTypeFieldUpdatedPaths,\n): void {\n\tconst { models, onUpdate, ...updatedPaths } = args;\n\n\tfor (const { model: slice } of models) {\n\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\tpath: [\".\"],\n\t\t\tslice,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({ ...updatedPaths, field });\n\t\t\t},\n\t\t});\n\n\t\tif (!isEqualModel(slice, updatedSliceModel)) {\n\t\t\tonUpdate(updatedSliceModel);\n\t\t}\n\t}\n}\n\nfunction isEqualModel<T extends CustomType | SharedSlice>(\n\tmodelA: T,\n\tmodelB: T,\n): boolean {\n\treturn JSON.stringify(modelA) === JSON.stringify(modelB);\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","fieldArg","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,GA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B;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\tSharedSlice,\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\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\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\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\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"],"names":["BaseManager","assertPluginsInitialized","errors","decodeHookResult","t","CustomType","prismicCustomTypesClient","API_ENDPOINTS","fetch","SLICE_MACHINE_USER_AGENT","UnauthorizedError","z","SharedSlice"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFM,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,Q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iB;AACjE,QAAA,SAAS,aAAa;AAE1B,UAAM,WAAW,MAAMC,MAAAA,QAAM,IAAI,YAAY;AAAA,MAC5C,QAAQ;AAAA,MACR;AAAA,MACA,MAAM,KAAK,UAAU,EAAE,UAAU;AAAA,IAAA,CACjC;AAEG,QAAA,CAAC,SAAS,IAAI;AACjB,YAAM,IAAI,MAAM,0BAA0B,SAAS,YAAY;AAAA,IAChE;AAEM,UAAA,OAAO,MAAM,SAAS;AAErB,WAAA,mBAAmB,MAAM,IAAI;AAAA,EACrC;AACA;AAID,MAAM,qBAAqBG,cAAE,OAAO;AAAA,EACnC,OAAOA,MAAE,QAAA,OAAA,EAAS,UAAU,CAAC,OAAO,QAAO;AACpC,UAAA,SAASC,YAAAA,YAAY,OAAO,KAAK;AACnC,QAAA,OAAO,SAAS,SAAS;AAC5B,aAAO,OAAO;AAAA,IACf;AACA,QAAI,SAAS;AAAA,MACZ,MAAMD,MAAAA,QAAE,aAAa;AAAA,MACrB,SAAS,yBAAyB,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;;"}
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { CustomType
|
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,21 +46,11 @@ type CustomTypesMachineManagerDeleteCustomTypeArgs = {
|
|
46
46
|
type CustomTypesMachineManagerDeleteCustomTypeReturnType = {
|
47
47
|
errors: (DecodeError | HookError)[];
|
48
48
|
};
|
49
|
-
type CustomTypeFieldUpdatedPaths = {
|
50
|
-
previousPath: string[];
|
51
|
-
newPath: string[];
|
52
|
-
};
|
53
49
|
export declare class CustomTypesManager extends BaseManager {
|
54
50
|
readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType>;
|
55
51
|
readAllCustomTypes(args?: CustomTypesManagerReadAllCustomTypesArgs): Promise<SliceMachineManagerReadAllCustomTypeReturnType>;
|
56
52
|
createCustomType(args: CustomTypeCreateHookData): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeCreateHook>>>;
|
57
53
|
readCustomType(args: CustomTypeReadHookData): Promise<SliceMachineManagerReadCustomTypeReturnType>;
|
58
|
-
/**
|
59
|
-
* Update the Content Relationship API IDs for all existing custom types and
|
60
|
-
* slices. The change is determined by properties inside the `updated`
|
61
|
-
* property.
|
62
|
-
*/
|
63
|
-
private updateContentRelationships;
|
64
54
|
updateCustomType(args: CustomTypeUpdateHookData): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>>;
|
65
55
|
renameCustomType(args: CustomTypeRenameHookData): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeRenameHook>>>;
|
66
56
|
deleteCustomType(args: CustomTypesMachineManagerDeleteCustomTypeArgs): Promise<CustomTypesMachineManagerDeleteCustomTypeReturnType>;
|
@@ -218,18 +208,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
218
208
|
useAsTitle?: boolean | undefined;
|
219
209
|
placeholder?: string | undefined;
|
220
210
|
select?: "media" | "document" | "web" | null | undefined;
|
221
|
-
customtypes?: readonly
|
222
|
-
id: string;
|
223
|
-
} & {
|
224
|
-
fields?: readonly (string | {
|
225
|
-
id: string;
|
226
|
-
customtypes: readonly (string | ({
|
227
|
-
id: string;
|
228
|
-
} & {
|
229
|
-
fields?: readonly string[] | undefined;
|
230
|
-
}))[];
|
231
|
-
})[] | undefined;
|
232
|
-
}))[] | undefined;
|
211
|
+
customtypes?: readonly string[] | undefined;
|
233
212
|
masks?: readonly string[] | undefined;
|
234
213
|
tags?: readonly string[] | undefined;
|
235
214
|
allowTargetBlank?: boolean | undefined;
|
@@ -404,18 +383,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
404
383
|
useAsTitle?: boolean | undefined;
|
405
384
|
placeholder?: string | undefined;
|
406
385
|
select?: "media" | "document" | "web" | null | undefined;
|
407
|
-
customtypes?: readonly
|
408
|
-
id: string;
|
409
|
-
} & {
|
410
|
-
fields?: readonly (string | {
|
411
|
-
id: string;
|
412
|
-
customtypes: readonly (string | ({
|
413
|
-
id: string;
|
414
|
-
} & {
|
415
|
-
fields?: readonly string[] | undefined;
|
416
|
-
}))[];
|
417
|
-
})[] | undefined;
|
418
|
-
}))[] | undefined;
|
386
|
+
customtypes?: readonly string[] | undefined;
|
419
387
|
masks?: readonly string[] | undefined;
|
420
388
|
tags?: readonly string[] | undefined;
|
421
389
|
allowTargetBlank?: boolean | undefined;
|
@@ -590,18 +558,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
590
558
|
useAsTitle?: boolean | undefined;
|
591
559
|
placeholder?: string | undefined;
|
592
560
|
select?: "media" | "document" | "web" | null | undefined;
|
593
|
-
customtypes?: readonly
|
594
|
-
id: string;
|
595
|
-
} & {
|
596
|
-
fields?: readonly (string | {
|
597
|
-
id: string;
|
598
|
-
customtypes: readonly (string | ({
|
599
|
-
id: string;
|
600
|
-
} & {
|
601
|
-
fields?: readonly string[] | undefined;
|
602
|
-
}))[];
|
603
|
-
})[] | undefined;
|
604
|
-
}))[] | undefined;
|
561
|
+
customtypes?: readonly string[] | undefined;
|
605
562
|
masks?: readonly string[] | undefined;
|
606
563
|
tags?: readonly string[] | undefined;
|
607
564
|
allowTargetBlank?: boolean | undefined;
|
@@ -775,18 +732,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
775
732
|
useAsTitle?: boolean | undefined;
|
776
733
|
placeholder?: string | undefined;
|
777
734
|
select?: "media" | "document" | "web" | null | undefined;
|
778
|
-
customtypes?: readonly
|
779
|
-
id: string;
|
780
|
-
} & {
|
781
|
-
fields?: readonly (string | {
|
782
|
-
id: string;
|
783
|
-
customtypes: readonly (string | ({
|
784
|
-
id: string;
|
785
|
-
} & {
|
786
|
-
fields?: readonly string[] | undefined;
|
787
|
-
}))[];
|
788
|
-
})[] | undefined;
|
789
|
-
}))[] | undefined;
|
735
|
+
customtypes?: readonly string[] | undefined;
|
790
736
|
masks?: readonly string[] | undefined;
|
791
737
|
tags?: readonly string[] | undefined;
|
792
738
|
allowTargetBlank?: boolean | undefined;
|
@@ -976,18 +922,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
976
922
|
useAsTitle?: boolean | undefined;
|
977
923
|
placeholder?: string | undefined;
|
978
924
|
select?: "media" | "document" | "web" | null | undefined;
|
979
|
-
customtypes?: readonly
|
980
|
-
id: string;
|
981
|
-
} & {
|
982
|
-
fields?: readonly (string | {
|
983
|
-
id: string;
|
984
|
-
customtypes: readonly (string | ({
|
985
|
-
id: string;
|
986
|
-
} & {
|
987
|
-
fields?: readonly string[] | undefined;
|
988
|
-
}))[];
|
989
|
-
})[] | undefined;
|
990
|
-
}))[] | undefined;
|
925
|
+
customtypes?: readonly string[] | undefined;
|
991
926
|
masks?: readonly string[] | undefined;
|
992
927
|
tags?: readonly string[] | undefined;
|
993
928
|
allowTargetBlank?: boolean | undefined;
|
@@ -1162,18 +1097,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
1162
1097
|
useAsTitle?: boolean | undefined;
|
1163
1098
|
placeholder?: string | undefined;
|
1164
1099
|
select?: "media" | "document" | "web" | null | undefined;
|
1165
|
-
customtypes?: readonly
|
1166
|
-
id: string;
|
1167
|
-
} & {
|
1168
|
-
fields?: readonly (string | {
|
1169
|
-
id: string;
|
1170
|
-
customtypes: readonly (string | ({
|
1171
|
-
id: string;
|
1172
|
-
} & {
|
1173
|
-
fields?: readonly string[] | undefined;
|
1174
|
-
}))[];
|
1175
|
-
})[] | undefined;
|
1176
|
-
}))[] | undefined;
|
1100
|
+
customtypes?: readonly string[] | undefined;
|
1177
1101
|
masks?: readonly string[] | undefined;
|
1178
1102
|
tags?: readonly string[] | undefined;
|
1179
1103
|
allowTargetBlank?: boolean | undefined;
|
@@ -1348,18 +1272,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
1348
1272
|
useAsTitle?: boolean | undefined;
|
1349
1273
|
placeholder?: string | undefined;
|
1350
1274
|
select?: "media" | "document" | "web" | null | undefined;
|
1351
|
-
customtypes?: readonly
|
1352
|
-
id: string;
|
1353
|
-
} & {
|
1354
|
-
fields?: readonly (string | {
|
1355
|
-
id: string;
|
1356
|
-
customtypes: readonly (string | ({
|
1357
|
-
id: string;
|
1358
|
-
} & {
|
1359
|
-
fields?: readonly string[] | undefined;
|
1360
|
-
}))[];
|
1361
|
-
})[] | undefined;
|
1362
|
-
}))[] | undefined;
|
1275
|
+
customtypes?: readonly string[] | undefined;
|
1363
1276
|
masks?: readonly string[] | undefined;
|
1364
1277
|
tags?: readonly string[] | undefined;
|
1365
1278
|
allowTargetBlank?: boolean | undefined;
|
@@ -1533,18 +1446,7 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
1533
1446
|
useAsTitle?: boolean | undefined;
|
1534
1447
|
placeholder?: string | undefined;
|
1535
1448
|
select?: "media" | "document" | "web" | null | undefined;
|
1536
|
-
customtypes?: readonly
|
1537
|
-
id: string;
|
1538
|
-
} & {
|
1539
|
-
fields?: readonly (string | {
|
1540
|
-
id: string;
|
1541
|
-
customtypes: readonly (string | ({
|
1542
|
-
id: string;
|
1543
|
-
} & {
|
1544
|
-
fields?: readonly string[] | undefined;
|
1545
|
-
}))[];
|
1546
|
-
})[] | undefined;
|
1547
|
-
}))[] | undefined;
|
1449
|
+
customtypes?: readonly string[] | undefined;
|
1548
1450
|
masks?: readonly string[] | undefined;
|
1549
1451
|
tags?: readonly string[] | undefined;
|
1550
1452
|
allowTargetBlank?: boolean | undefined;
|
@@ -1593,16 +1495,4 @@ declare const InferSliceResponse: z.ZodObject<{
|
|
1593
1495
|
slice?: unknown;
|
1594
1496
|
langSmithUrl?: string | undefined;
|
1595
1497
|
}>;
|
1596
|
-
export declare function updateCustomTypeContentRelationships(args: {
|
1597
|
-
models: {
|
1598
|
-
model: CustomType;
|
1599
|
-
}[];
|
1600
|
-
onUpdate: (model: CustomType) => void;
|
1601
|
-
} & CustomTypeFieldUpdatedPaths): void;
|
1602
|
-
export declare function updateSharedSliceContentRelationships(args: {
|
1603
|
-
models: {
|
1604
|
-
model: SharedSlice;
|
1605
|
-
}[];
|
1606
|
-
onUpdate: (model: SharedSlice) => void;
|
1607
|
-
} & CustomTypeFieldUpdatedPaths): void;
|
1608
1498
|
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
|
3
|
+
import { CustomType, 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,68 +60,12 @@ class CustomTypesManager extends BaseManager {
|
|
60
60
|
errors
|
61
61
|
};
|
62
62
|
}
|
63
|
-
/**
|
64
|
-
* Update the Content Relationship API IDs for all existing custom types and
|
65
|
-
* slices. The change is determined by properties inside the `updated`
|
66
|
-
* property.
|
67
|
-
*/
|
68
|
-
async updateContentRelationships(args) {
|
69
|
-
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
70
|
-
const { model, updates } = args;
|
71
|
-
if (updates) {
|
72
|
-
for (const [previousPathStr, newPathStr] of Object.entries(updates)) {
|
73
|
-
if (previousPathStr !== newPathStr) {
|
74
|
-
const previousPath = [model.id, ...previousPathStr.split(".")];
|
75
|
-
const newPath = [model.id, ...newPathStr.split(".")];
|
76
|
-
const crUpdates = [];
|
77
|
-
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
|
-
});
|
89
|
-
const { libraries } = await this.slices.readAllSliceLibraries();
|
90
|
-
for (const library of libraries) {
|
91
|
-
const slices = await this.slices.readAllSlicesForLibrary({
|
92
|
-
libraryID: library.libraryID
|
93
|
-
});
|
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
|
-
});
|
106
|
-
}
|
107
|
-
const crUpdatesResult = await Promise.all(crUpdates);
|
108
|
-
if (crUpdatesResult.some((result) => result.errors.length > 0)) {
|
109
|
-
return {
|
110
|
-
errors: crUpdatesResult.flatMap((result) => result.errors)
|
111
|
-
};
|
112
|
-
}
|
113
|
-
}
|
114
|
-
}
|
115
|
-
}
|
116
|
-
return { errors: [] };
|
117
|
-
}
|
118
63
|
async updateCustomType(args) {
|
119
64
|
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
120
65
|
const hookResult = await this.sliceMachinePluginRunner.callHook("custom-type:update", args);
|
121
|
-
|
122
|
-
|
123
|
-
}
|
124
|
-
return { errors: hookResult.errors };
|
66
|
+
return {
|
67
|
+
errors: hookResult.errors
|
68
|
+
};
|
125
69
|
}
|
126
70
|
async renameCustomType(args) {
|
127
71
|
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
@@ -267,116 +211,7 @@ const InferSliceResponse = z.object({
|
|
267
211
|
}),
|
268
212
|
langSmithUrl: z.string().url().optional()
|
269
213
|
});
|
270
|
-
function updateCRCustomType(args) {
|
271
|
-
const [previousCustomTypeId, previousFieldId] = args.previousPath;
|
272
|
-
const [newCustomTypeId, newFieldId] = args.newPath;
|
273
|
-
if (!previousCustomTypeId || !newCustomTypeId) {
|
274
|
-
throw new Error("Could not find a customtype id in previousPath and/or newPath, which should not be possible.");
|
275
|
-
}
|
276
|
-
if (!previousFieldId || !newFieldId) {
|
277
|
-
throw new Error("Could not find a field id in previousPath and/or newPath, which should not be possible.");
|
278
|
-
}
|
279
|
-
const customType = shallowCloneIfObject(args.customType);
|
280
|
-
if (typeof customType === "string" || !customType.fields) {
|
281
|
-
return customType;
|
282
|
-
}
|
283
|
-
const matchedCustomTypeId = customType.id === previousCustomTypeId;
|
284
|
-
const newFields = customType.fields.map((fieldArg) => {
|
285
|
-
const customTypeField = shallowCloneIfObject(fieldArg);
|
286
|
-
if (typeof customTypeField === "string") {
|
287
|
-
if (matchedCustomTypeId && customTypeField === previousFieldId && customTypeField !== newFieldId) {
|
288
|
-
return newFieldId;
|
289
|
-
}
|
290
|
-
return customTypeField;
|
291
|
-
}
|
292
|
-
if (matchedCustomTypeId && customTypeField.id === previousFieldId && customTypeField.id !== newFieldId) {
|
293
|
-
customTypeField.id = newFieldId;
|
294
|
-
}
|
295
|
-
return {
|
296
|
-
...customTypeField,
|
297
|
-
customtypes: customTypeField.customtypes.map((customTypeArg) => {
|
298
|
-
const nestedCustomType = shallowCloneIfObject(customTypeArg);
|
299
|
-
if (typeof nestedCustomType === "string" || !nestedCustomType.fields || // Since we are on the last level, if we don't start matching right
|
300
|
-
// at the custom type id, we can return exit early because it's not
|
301
|
-
// a match.
|
302
|
-
nestedCustomType.id !== previousCustomTypeId) {
|
303
|
-
return nestedCustomType;
|
304
|
-
}
|
305
|
-
return {
|
306
|
-
...nestedCustomType,
|
307
|
-
fields: nestedCustomType.fields.map((fieldArg2) => {
|
308
|
-
const nestedCustomTypeField = shallowCloneIfObject(fieldArg2);
|
309
|
-
if (nestedCustomTypeField === previousFieldId && nestedCustomTypeField !== newFieldId) {
|
310
|
-
return newFieldId;
|
311
|
-
}
|
312
|
-
return nestedCustomTypeField;
|
313
|
-
})
|
314
|
-
};
|
315
|
-
})
|
316
|
-
};
|
317
|
-
});
|
318
|
-
return { ...customType, fields: newFields };
|
319
|
-
}
|
320
|
-
function updateFieldContentRelationships(args) {
|
321
|
-
var _a, _b;
|
322
|
-
const { field, ...updatedPaths } = args;
|
323
|
-
if (field.type !== "Link" || ((_a = field.config) == null ? void 0 : _a.select) !== "document" || !((_b = field.config) == null ? void 0 : _b.customtypes)) {
|
324
|
-
return field;
|
325
|
-
}
|
326
|
-
const newCustomTypes = field.config.customtypes.map((customType) => {
|
327
|
-
return updateCRCustomType({ customType, ...updatedPaths });
|
328
|
-
});
|
329
|
-
return {
|
330
|
-
...field,
|
331
|
-
config: { ...field.config, customtypes: newCustomTypes }
|
332
|
-
};
|
333
|
-
}
|
334
|
-
function updateCustomTypeContentRelationships(args) {
|
335
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
336
|
-
for (const { model: customType } of models) {
|
337
|
-
const updatedCustomTypeModel = traverseCustomType({
|
338
|
-
customType,
|
339
|
-
onField: ({ field }) => {
|
340
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
341
|
-
}
|
342
|
-
});
|
343
|
-
if (!isEqualModel(customType, updatedCustomTypeModel)) {
|
344
|
-
onUpdate(updatedCustomTypeModel);
|
345
|
-
}
|
346
|
-
}
|
347
|
-
}
|
348
|
-
function updateSharedSliceContentRelationships(args) {
|
349
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
350
|
-
for (const { model: slice } of models) {
|
351
|
-
const updatedSliceModel = traverseSharedSlice({
|
352
|
-
path: ["."],
|
353
|
-
slice,
|
354
|
-
onField: ({ field }) => {
|
355
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
356
|
-
}
|
357
|
-
});
|
358
|
-
if (!isEqualModel(slice, updatedSliceModel)) {
|
359
|
-
onUpdate(updatedSliceModel);
|
360
|
-
}
|
361
|
-
}
|
362
|
-
}
|
363
|
-
function isEqualModel(modelA, modelB) {
|
364
|
-
return JSON.stringify(modelA) === JSON.stringify(modelB);
|
365
|
-
}
|
366
|
-
function shallowCloneIfObject(value) {
|
367
|
-
if (typeof value === "object") {
|
368
|
-
return { ...value };
|
369
|
-
}
|
370
|
-
return value;
|
371
|
-
}
|
372
|
-
function pushIfDefined(array, value) {
|
373
|
-
if (value) {
|
374
|
-
array.push(value);
|
375
|
-
}
|
376
|
-
}
|
377
214
|
export {
|
378
|
-
CustomTypesManager
|
379
|
-
updateCustomTypeContentRelationships,
|
380
|
-
updateSharedSliceContentRelationships
|
215
|
+
CustomTypesManager
|
381
216
|
};
|
382
217
|
//# 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 CustomTypeFieldUpdatedPaths = {\n\tpreviousPath: string[];\n\tnewPath: string[];\n};\n\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 `updated`\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, updates } = args;\n\n\t\tif (updates) {\n\t\t\tfor (const [previousPathStr, newPathStr] of Object.entries(updates)) {\n\t\t\t\tif (previousPathStr !== newPathStr) {\n\t\t\t\t\tconst previousPath = [model.id, ...previousPathStr.split(\".\")];\n\t\t\t\t\tconst newPath = [model.id, ...newPathStr.split(\".\")];\n\t\t\t\t\tconst crUpdates: Promise<{ errors: HookError[] }>[] = [];\n\n\t\t\t\t\t// Find existing content relationships that link to the renamed field id in\n\t\t\t\t\t// any custom type and update them to use the new one.\n\t\t\t\t\tconst customTypes = await this.readAllCustomTypes();\n\n\t\t\t\t\tupdateCustomTypeContentRelationships({\n\t\t\t\t\t\tmodels: customTypes.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(\"custom-type:update\", {\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\n\t\t\t\t\t// Find existing slice with content relationships that link to the renamed\n\t\t\t\t\t// field id in all libraries and update them to use the new one.\n\t\t\t\t\tconst { libraries } = await this.slices.readAllSliceLibraries();\n\n\t\t\t\t\tfor (const library of libraries) {\n\t\t\t\t\t\tconst slices = await this.slices.readAllSlicesForLibrary({\n\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t});\n\n\t\t\t\t\t\tupdateSharedSliceContentRelationships({\n\t\t\t\t\t\t\tmodels: slices.models,\n\t\t\t\t\t\t\tonUpdate: (model) => {\n\t\t\t\t\t\t\t\tpushIfDefined(\n\t\t\t\t\t\t\t\t\tcrUpdates,\n\t\t\t\t\t\t\t\t\tthis.sliceMachinePluginRunner?.callHook(\"slice:update\", {\n\t\t\t\t\t\t\t\t\t\tlibraryID: library.libraryID,\n\t\t\t\t\t\t\t\t\t\tmodel,\n\t\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\tpreviousPath,\n\t\t\t\t\t\t\tnewPath,\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process all the Content Relationship updates at once.\n\t\t\t\t\tconst crUpdatesResult = await Promise.all(crUpdates);\n\n\t\t\t\t\tif (crUpdatesResult.some((result) => result.errors.length > 0)) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\terrors: crUpdatesResult.flatMap((result) => result.errors),\n\t\t\t\t\t\t};\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.updates) {\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 } & CustomTypeFieldUpdatedPaths,\n): CrCustomType {\n\tconst [previousCustomTypeId, previousFieldId] = args.previousPath;\n\tconst [newCustomTypeId, newFieldId] = args.newPath;\n\n\tif (!previousCustomTypeId || !newCustomTypeId) {\n\t\tthrow new Error(\n\t\t\t\"Could not find a customtype id in previousPath and/or newPath, which should not be possible.\",\n\t\t);\n\t}\n\n\tif (!previousFieldId || !newFieldId) {\n\t\tthrow new Error(\n\t\t\t\"Could not find a field id in previousPath and/or newPath, which should not be possible.\",\n\t\t);\n\t}\n\n\tconst customType = shallowCloneIfObject(args.customType);\n\n\tif (typeof customType === \"string\" || !customType.fields) {\n\t\treturn customType;\n\t}\n\n\tconst matchedCustomTypeId = customType.id === previousCustomTypeId;\n\n\tconst newFields = customType.fields.map((fieldArg) => {\n\t\tconst customTypeField = shallowCloneIfObject(fieldArg);\n\n\t\tif (typeof customTypeField === \"string\") {\n\t\t\tif (\n\t\t\t\tmatchedCustomTypeId &&\n\t\t\t\tcustomTypeField === previousFieldId &&\n\t\t\t\tcustomTypeField !== newFieldId\n\t\t\t) {\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. The field is a string, so return the new\n\t\t\t\t// id.\n\t\t\t\treturn newFieldId;\n\t\t\t}\n\n\t\t\treturn customTypeField;\n\t\t}\n\n\t\tif (\n\t\t\tmatchedCustomTypeId &&\n\t\t\tcustomTypeField.id === previousFieldId &&\n\t\t\tcustomTypeField.id !== newFieldId\n\t\t) {\n\t\t\t// We have reached a field id that matches the id that was renamed,\n\t\t\t// so we update it new one.\n\t\t\t// Since field is not a string, we don't exit, as we might have\n\t\t\t// something to update further down in customtypes.\n\t\t\tcustomTypeField.id = newFieldId;\n\t\t}\n\n\t\treturn {\n\t\t\t...customTypeField,\n\t\t\tcustomtypes: customTypeField.customtypes.map((customTypeArg) => {\n\t\t\t\tconst nestedCustomType = shallowCloneIfObject(customTypeArg);\n\n\t\t\t\tif (\n\t\t\t\t\ttypeof nestedCustomType === \"string\" ||\n\t\t\t\t\t!nestedCustomType.fields ||\n\t\t\t\t\t// Since we are on the last level, if we don't start matching right\n\t\t\t\t\t// at the custom type id, we can return exit early because it's not\n\t\t\t\t\t// a match.\n\t\t\t\t\tnestedCustomType.id !== previousCustomTypeId\n\t\t\t\t) {\n\t\t\t\t\treturn nestedCustomType;\n\t\t\t\t}\n\n\t\t\t\treturn {\n\t\t\t\t\t...nestedCustomType,\n\t\t\t\t\tfields: nestedCustomType.fields.map((fieldArg) => {\n\t\t\t\t\t\tconst nestedCustomTypeField = shallowCloneIfObject(fieldArg);\n\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\tnestedCustomTypeField === previousFieldId &&\n\t\t\t\t\t\t\tnestedCustomTypeField !== newFieldId\n\t\t\t\t\t\t) {\n\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// it's the last level.\n\t\t\t\t\t\t\treturn newFieldId;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn nestedCustomTypeField;\n\t\t\t\t\t}),\n\t\t\t\t};\n\t\t\t}),\n\t\t};\n\t});\n\n\treturn { ...customType, fields: newFields };\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 } & CustomTypeFieldUpdatedPaths): T {\n\tconst { field, ...updatedPaths } = 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 = field.config.customtypes.map((customType) => {\n\t\treturn updateCRCustomType({ customType, ...updatedPaths });\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} & CustomTypeFieldUpdatedPaths,\n): void {\n\tconst { models, onUpdate, ...updatedPaths } = args;\n\n\tfor (const { model: customType } of models) {\n\t\tconst updatedCustomTypeModel = traverseCustomType({\n\t\t\tcustomType,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({ ...updatedPaths, field });\n\t\t\t},\n\t\t});\n\n\t\tif (!isEqualModel(customType, updatedCustomTypeModel)) {\n\t\t\tonUpdate(updatedCustomTypeModel);\n\t\t}\n\t}\n}\n\nexport function updateSharedSliceContentRelationships(\n\targs: {\n\t\tmodels: { model: SharedSlice }[];\n\t\tonUpdate: (model: SharedSlice) => void;\n\t} & CustomTypeFieldUpdatedPaths,\n): void {\n\tconst { models, onUpdate, ...updatedPaths } = args;\n\n\tfor (const { model: slice } of models) {\n\t\tconst updatedSliceModel = traverseSharedSlice({\n\t\t\tpath: [\".\"],\n\t\t\tslice,\n\t\t\tonField: ({ field }) => {\n\t\t\t\treturn updateFieldContentRelationships({ ...updatedPaths, field });\n\t\t\t},\n\t\t});\n\n\t\tif (!isEqualModel(slice, updatedSliceModel)) {\n\t\t\tonUpdate(updatedSliceModel);\n\t\t}\n\t}\n}\n\nfunction isEqualModel<T extends CustomType | SharedSlice>(\n\tmodelA: T,\n\tmodelB: T,\n): boolean {\n\treturn JSON.stringify(modelA) === JSON.stringify(modelB);\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","fieldArg"],"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,EAEF;AAAA,EAEA,MAAM,mBACL,MAA+C;AAE/C,6BAAyB,KAAK,wBAAwB;AAEtD,UAAM,MAAsD;AAAA,MAC3D,QAAQ,CAAE;AAAA,MACV,QAAQ,CAAE;AAAA,IAAA;AAGX,UAAM,EAAE,KAAK,OAAA,IAAW,MAAM,KAAK,sBAAqB;AACxD,QAAI,SAAS,CAAC,GAAG,IAAI,QAAQ,GAAG,MAAM;AAEtC,QAAI,KAAK;AACR,iBAAW,MAAM,KAAK;AACf,cAAA,EAAE,OAAO,QA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],"sourcesContent":["import * as t from \"io-ts\";\nimport * as prismicCustomTypesClient from \"@prismicio/custom-types-client\";\nimport {\n\tCustomType,\n\tSharedSlice,\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\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\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\treturn {\n\t\t\terrors: hookResult.errors,\n\t\t};\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: ${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AAS,YAAY,OAAO,KAAK;AACnC,QAAA,OAAO,SAAS,SAAS;AAC5B,aAAO,OAAO;AAAA,IACf;AACA,QAAI,SAAS;AAAA,MACZ,MAAM,EAAE,aAAa;AAAA,MACrB,SAAS,yBAAyB,KAAK,UAAU,OAAO,MAAM,CAAC;AAAA,IAAA,CAC/D;AAED,WAAO,EAAE;AAAA,EAAA,CACT;AAAA,EACD,cAAc,EAAE,OAAS,EAAA,IAAA,EAAM,SAAU;AACzC,CAAA;"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@slicemachine/manager",
|
3
|
-
"version": "0.24.14-alpha.
|
3
|
+
"version": "0.24.14-alpha.xru-slice-create-placeholder-for-mcp-server.2",
|
4
4
|
"description": "Manage all aspects of a Slice Machine project.",
|
5
5
|
"repository": {
|
6
6
|
"type": "git",
|
@@ -67,10 +67,10 @@
|
|
67
67
|
"@antfu/ni": "^0.20.0",
|
68
68
|
"@prismicio/client": "7.17.0",
|
69
69
|
"@prismicio/custom-types-client": "2.1.0",
|
70
|
-
"@prismicio/mocks": "2.
|
71
|
-
"@prismicio/types-internal": "3.
|
70
|
+
"@prismicio/mocks": "2.12.0",
|
71
|
+
"@prismicio/types-internal": "3.8.0",
|
72
72
|
"@segment/analytics-node": "^2.1.2",
|
73
|
-
"@slicemachine/plugin-kit": "0.4.76-alpha.
|
73
|
+
"@slicemachine/plugin-kit": "0.4.76-alpha.xru-slice-create-placeholder-for-mcp-server.2",
|
74
74
|
"cookie": "^1.0.1",
|
75
75
|
"cors": "^2.8.5",
|
76
76
|
"execa": "^7.1.1",
|
@@ -2,13 +2,7 @@ import * as t from "io-ts";
|
|
2
2
|
import * as prismicCustomTypesClient from "@prismicio/custom-types-client";
|
3
3
|
import {
|
4
4
|
CustomType,
|
5
|
-
Group,
|
6
|
-
NestableWidget,
|
7
|
-
NestedGroup,
|
8
5
|
SharedSlice,
|
9
|
-
UID,
|
10
|
-
traverseCustomType,
|
11
|
-
traverseSharedSlice,
|
12
6
|
} from "@prismicio/types-internal/lib/customtypes";
|
13
7
|
import {
|
14
8
|
CallHookReturnType,
|
@@ -88,21 +82,6 @@ type CustomTypesMachineManagerDeleteCustomTypeReturnType = {
|
|
88
82
|
errors: (DecodeError | HookError)[];
|
89
83
|
};
|
90
84
|
|
91
|
-
type CustomTypeFieldUpdatedPaths = {
|
92
|
-
previousPath: string[];
|
93
|
-
newPath: string[];
|
94
|
-
};
|
95
|
-
|
96
|
-
type CrCustomType =
|
97
|
-
| string
|
98
|
-
| { id: string; fields?: readonly CrCustomTypeNestedCr[] };
|
99
|
-
type CrCustomTypeNestedCr =
|
100
|
-
| string
|
101
|
-
| { id: string; customtypes: readonly CrCustomTypeFieldLeaf[] };
|
102
|
-
type CrCustomTypeFieldLeaf =
|
103
|
-
| string
|
104
|
-
| { id: string; fields?: readonly string[] };
|
105
|
-
|
106
85
|
export class CustomTypesManager extends BaseManager {
|
107
86
|
async readCustomTypeLibrary(): Promise<SliceMachineManagerReadCustomTypeLibraryReturnType> {
|
108
87
|
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
@@ -188,83 +167,6 @@ export class CustomTypesManager extends BaseManager {
|
|
188
167
|
};
|
189
168
|
}
|
190
169
|
|
191
|
-
/**
|
192
|
-
* Update the Content Relationship API IDs for all existing custom types and
|
193
|
-
* slices. The change is determined by properties inside the `updated`
|
194
|
-
* property.
|
195
|
-
*/
|
196
|
-
private async updateContentRelationships(
|
197
|
-
args: CustomTypeUpdateHookData,
|
198
|
-
): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {
|
199
|
-
assertPluginsInitialized(this.sliceMachinePluginRunner);
|
200
|
-
|
201
|
-
const { model, updates } = args;
|
202
|
-
|
203
|
-
if (updates) {
|
204
|
-
for (const [previousPathStr, newPathStr] of Object.entries(updates)) {
|
205
|
-
if (previousPathStr !== newPathStr) {
|
206
|
-
const previousPath = [model.id, ...previousPathStr.split(".")];
|
207
|
-
const newPath = [model.id, ...newPathStr.split(".")];
|
208
|
-
const crUpdates: Promise<{ errors: HookError[] }>[] = [];
|
209
|
-
|
210
|
-
// Find existing content relationships that link to the renamed field id in
|
211
|
-
// any custom type and update them to use the new one.
|
212
|
-
const customTypes = await this.readAllCustomTypes();
|
213
|
-
|
214
|
-
updateCustomTypeContentRelationships({
|
215
|
-
models: customTypes.models,
|
216
|
-
onUpdate: (model) => {
|
217
|
-
pushIfDefined(
|
218
|
-
crUpdates,
|
219
|
-
this.sliceMachinePluginRunner?.callHook("custom-type:update", {
|
220
|
-
model,
|
221
|
-
}),
|
222
|
-
);
|
223
|
-
},
|
224
|
-
previousPath,
|
225
|
-
newPath,
|
226
|
-
});
|
227
|
-
|
228
|
-
// Find existing slice with content relationships that link to the renamed
|
229
|
-
// field id in all libraries and update them to use the new one.
|
230
|
-
const { libraries } = await this.slices.readAllSliceLibraries();
|
231
|
-
|
232
|
-
for (const library of libraries) {
|
233
|
-
const slices = await this.slices.readAllSlicesForLibrary({
|
234
|
-
libraryID: library.libraryID,
|
235
|
-
});
|
236
|
-
|
237
|
-
updateSharedSliceContentRelationships({
|
238
|
-
models: slices.models,
|
239
|
-
onUpdate: (model) => {
|
240
|
-
pushIfDefined(
|
241
|
-
crUpdates,
|
242
|
-
this.sliceMachinePluginRunner?.callHook("slice:update", {
|
243
|
-
libraryID: library.libraryID,
|
244
|
-
model,
|
245
|
-
}),
|
246
|
-
);
|
247
|
-
},
|
248
|
-
previousPath,
|
249
|
-
newPath,
|
250
|
-
});
|
251
|
-
}
|
252
|
-
|
253
|
-
// Process all the Content Relationship updates at once.
|
254
|
-
const crUpdatesResult = await Promise.all(crUpdates);
|
255
|
-
|
256
|
-
if (crUpdatesResult.some((result) => result.errors.length > 0)) {
|
257
|
-
return {
|
258
|
-
errors: crUpdatesResult.flatMap((result) => result.errors),
|
259
|
-
};
|
260
|
-
}
|
261
|
-
}
|
262
|
-
}
|
263
|
-
}
|
264
|
-
|
265
|
-
return { errors: [] };
|
266
|
-
}
|
267
|
-
|
268
170
|
async updateCustomType(
|
269
171
|
args: CustomTypeUpdateHookData,
|
270
172
|
): Promise<OnlyHookErrors<CallHookReturnType<CustomTypeUpdateHook>>> {
|
@@ -275,11 +177,9 @@ export class CustomTypesManager extends BaseManager {
|
|
275
177
|
args,
|
276
178
|
);
|
277
179
|
|
278
|
-
|
279
|
-
|
280
|
-
}
|
281
|
-
|
282
|
-
return { errors: hookResult.errors };
|
180
|
+
return {
|
181
|
+
errors: hookResult.errors,
|
182
|
+
};
|
283
183
|
}
|
284
184
|
|
285
185
|
async renameCustomType(
|
@@ -493,192 +393,3 @@ const InferSliceResponse = z.object({
|
|
493
393
|
}),
|
494
394
|
langSmithUrl: z.string().url().optional(),
|
495
395
|
});
|
496
|
-
|
497
|
-
function updateCRCustomType(
|
498
|
-
args: { customType: CrCustomType } & CustomTypeFieldUpdatedPaths,
|
499
|
-
): CrCustomType {
|
500
|
-
const [previousCustomTypeId, previousFieldId] = args.previousPath;
|
501
|
-
const [newCustomTypeId, newFieldId] = args.newPath;
|
502
|
-
|
503
|
-
if (!previousCustomTypeId || !newCustomTypeId) {
|
504
|
-
throw new Error(
|
505
|
-
"Could not find a customtype id in previousPath and/or newPath, which should not be possible.",
|
506
|
-
);
|
507
|
-
}
|
508
|
-
|
509
|
-
if (!previousFieldId || !newFieldId) {
|
510
|
-
throw new Error(
|
511
|
-
"Could not find a field id in previousPath and/or newPath, which should not be possible.",
|
512
|
-
);
|
513
|
-
}
|
514
|
-
|
515
|
-
const customType = shallowCloneIfObject(args.customType);
|
516
|
-
|
517
|
-
if (typeof customType === "string" || !customType.fields) {
|
518
|
-
return customType;
|
519
|
-
}
|
520
|
-
|
521
|
-
const matchedCustomTypeId = customType.id === previousCustomTypeId;
|
522
|
-
|
523
|
-
const newFields = customType.fields.map((fieldArg) => {
|
524
|
-
const customTypeField = shallowCloneIfObject(fieldArg);
|
525
|
-
|
526
|
-
if (typeof customTypeField === "string") {
|
527
|
-
if (
|
528
|
-
matchedCustomTypeId &&
|
529
|
-
customTypeField === previousFieldId &&
|
530
|
-
customTypeField !== newFieldId
|
531
|
-
) {
|
532
|
-
// We have reached a field id that matches the id that was renamed,
|
533
|
-
// so we update it new one. The field is a string, so return the new
|
534
|
-
// id.
|
535
|
-
return newFieldId;
|
536
|
-
}
|
537
|
-
|
538
|
-
return customTypeField;
|
539
|
-
}
|
540
|
-
|
541
|
-
if (
|
542
|
-
matchedCustomTypeId &&
|
543
|
-
customTypeField.id === previousFieldId &&
|
544
|
-
customTypeField.id !== newFieldId
|
545
|
-
) {
|
546
|
-
// We have reached a field id that matches the id that was renamed,
|
547
|
-
// so we update it new one.
|
548
|
-
// Since field is not a string, we don't exit, as we might have
|
549
|
-
// something to update further down in customtypes.
|
550
|
-
customTypeField.id = newFieldId;
|
551
|
-
}
|
552
|
-
|
553
|
-
return {
|
554
|
-
...customTypeField,
|
555
|
-
customtypes: customTypeField.customtypes.map((customTypeArg) => {
|
556
|
-
const nestedCustomType = shallowCloneIfObject(customTypeArg);
|
557
|
-
|
558
|
-
if (
|
559
|
-
typeof nestedCustomType === "string" ||
|
560
|
-
!nestedCustomType.fields ||
|
561
|
-
// Since we are on the last level, if we don't start matching right
|
562
|
-
// at the custom type id, we can return exit early because it's not
|
563
|
-
// a match.
|
564
|
-
nestedCustomType.id !== previousCustomTypeId
|
565
|
-
) {
|
566
|
-
return nestedCustomType;
|
567
|
-
}
|
568
|
-
|
569
|
-
return {
|
570
|
-
...nestedCustomType,
|
571
|
-
fields: nestedCustomType.fields.map((fieldArg) => {
|
572
|
-
const nestedCustomTypeField = shallowCloneIfObject(fieldArg);
|
573
|
-
|
574
|
-
if (
|
575
|
-
nestedCustomTypeField === previousFieldId &&
|
576
|
-
nestedCustomTypeField !== newFieldId
|
577
|
-
) {
|
578
|
-
// Matches the previous id, so we update it and return because
|
579
|
-
// it's the last level.
|
580
|
-
return newFieldId;
|
581
|
-
}
|
582
|
-
|
583
|
-
return nestedCustomTypeField;
|
584
|
-
}),
|
585
|
-
};
|
586
|
-
}),
|
587
|
-
};
|
588
|
-
});
|
589
|
-
|
590
|
-
return { ...customType, fields: newFields };
|
591
|
-
}
|
592
|
-
|
593
|
-
/**
|
594
|
-
* Update the Content Relationship API IDs of a single field. The change is
|
595
|
-
* determined by the `previousPath` and `newPath` properties.
|
596
|
-
*/
|
597
|
-
function updateFieldContentRelationships<
|
598
|
-
T extends UID | NestableWidget | Group | NestedGroup,
|
599
|
-
>(args: { field: T } & CustomTypeFieldUpdatedPaths): T {
|
600
|
-
const { field, ...updatedPaths } = args;
|
601
|
-
if (
|
602
|
-
field.type !== "Link" ||
|
603
|
-
field.config?.select !== "document" ||
|
604
|
-
!field.config?.customtypes
|
605
|
-
) {
|
606
|
-
// not a content relationship field
|
607
|
-
return field;
|
608
|
-
}
|
609
|
-
|
610
|
-
const newCustomTypes = field.config.customtypes.map((customType) => {
|
611
|
-
return updateCRCustomType({ customType, ...updatedPaths });
|
612
|
-
});
|
613
|
-
|
614
|
-
return {
|
615
|
-
...field,
|
616
|
-
config: { ...field.config, customtypes: newCustomTypes },
|
617
|
-
};
|
618
|
-
}
|
619
|
-
|
620
|
-
export function updateCustomTypeContentRelationships(
|
621
|
-
args: {
|
622
|
-
models: { model: CustomType }[];
|
623
|
-
onUpdate: (model: CustomType) => void;
|
624
|
-
} & CustomTypeFieldUpdatedPaths,
|
625
|
-
): void {
|
626
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
627
|
-
|
628
|
-
for (const { model: customType } of models) {
|
629
|
-
const updatedCustomTypeModel = traverseCustomType({
|
630
|
-
customType,
|
631
|
-
onField: ({ field }) => {
|
632
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
633
|
-
},
|
634
|
-
});
|
635
|
-
|
636
|
-
if (!isEqualModel(customType, updatedCustomTypeModel)) {
|
637
|
-
onUpdate(updatedCustomTypeModel);
|
638
|
-
}
|
639
|
-
}
|
640
|
-
}
|
641
|
-
|
642
|
-
export function updateSharedSliceContentRelationships(
|
643
|
-
args: {
|
644
|
-
models: { model: SharedSlice }[];
|
645
|
-
onUpdate: (model: SharedSlice) => void;
|
646
|
-
} & CustomTypeFieldUpdatedPaths,
|
647
|
-
): void {
|
648
|
-
const { models, onUpdate, ...updatedPaths } = args;
|
649
|
-
|
650
|
-
for (const { model: slice } of models) {
|
651
|
-
const updatedSliceModel = traverseSharedSlice({
|
652
|
-
path: ["."],
|
653
|
-
slice,
|
654
|
-
onField: ({ field }) => {
|
655
|
-
return updateFieldContentRelationships({ ...updatedPaths, field });
|
656
|
-
},
|
657
|
-
});
|
658
|
-
|
659
|
-
if (!isEqualModel(slice, updatedSliceModel)) {
|
660
|
-
onUpdate(updatedSliceModel);
|
661
|
-
}
|
662
|
-
}
|
663
|
-
}
|
664
|
-
|
665
|
-
function isEqualModel<T extends CustomType | SharedSlice>(
|
666
|
-
modelA: T,
|
667
|
-
modelB: T,
|
668
|
-
): boolean {
|
669
|
-
return JSON.stringify(modelA) === JSON.stringify(modelB);
|
670
|
-
}
|
671
|
-
|
672
|
-
function shallowCloneIfObject<T>(value: T): T {
|
673
|
-
if (typeof value === "object") {
|
674
|
-
return { ...value };
|
675
|
-
}
|
676
|
-
|
677
|
-
return value;
|
678
|
-
}
|
679
|
-
|
680
|
-
function pushIfDefined<T>(array: T[], value: T | undefined) {
|
681
|
-
if (value) {
|
682
|
-
array.push(value);
|
683
|
-
}
|
684
|
-
}
|