@kontent-ai/migration-toolkit 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +3 -7
  2. package/dist/es2022/core/index.d.ts +1 -0
  3. package/dist/es2022/core/index.js +1 -0
  4. package/dist/es2022/core/index.js.map +1 -1
  5. package/dist/es2022/core/models/migration.models.d.ts +35 -133
  6. package/dist/es2022/core/models/migration.schema.d.ts +1300 -0
  7. package/dist/es2022/core/models/migration.schema.js +114 -0
  8. package/dist/es2022/core/models/migration.schema.js.map +1 -0
  9. package/dist/es2022/core/utils/confirm.utils.js +3 -3
  10. package/dist/es2022/core/utils/confirm.utils.js.map +1 -1
  11. package/dist/es2022/core/utils/error.utils.js +8 -2
  12. package/dist/es2022/core/utils/error.utils.js.map +1 -1
  13. package/dist/es2022/core/utils/global.utils.d.ts +4 -1
  14. package/dist/es2022/core/utils/global.utils.js +1 -3
  15. package/dist/es2022/core/utils/global.utils.js.map +1 -1
  16. package/dist/es2022/export/context/export-context-fetcher.js +10 -6
  17. package/dist/es2022/export/context/export-context-fetcher.js.map +1 -1
  18. package/dist/es2022/export/export-manager.js +3 -3
  19. package/dist/es2022/export/export-manager.js.map +1 -1
  20. package/dist/es2022/import/import-manager.js.map +1 -1
  21. package/dist/es2022/metadata.js +2 -2
  22. package/dist/es2022/node/cli/actions/export-action.js +2 -2
  23. package/dist/es2022/node/cli/actions/export-action.js.map +1 -1
  24. package/dist/es2022/node/cli/actions/import-action.js +2 -2
  25. package/dist/es2022/node/cli/actions/import-action.js.map +1 -1
  26. package/dist/es2022/node/cli/app.js +3 -0
  27. package/dist/es2022/node/cli/app.js.map +1 -1
  28. package/dist/es2022/node/cli/args/args-setter.js +3 -0
  29. package/dist/es2022/node/cli/args/args-setter.js.map +1 -1
  30. package/dist/es2022/node/cli/cli.models.d.ts +1 -0
  31. package/dist/es2022/toolkit/file.js +3 -3
  32. package/dist/es2022/toolkit/file.js.map +1 -1
  33. package/dist/es2022/translation/transforms/export-transforms.js.map +1 -1
  34. package/dist/es2022/zip/zip-transformer.js +9 -6
  35. package/dist/es2022/zip/zip-transformer.js.map +1 -1
  36. package/dist/es2022/zip/zip.models.d.ts +1 -1
  37. package/lib/core/index.ts +1 -0
  38. package/lib/core/models/migration.models.ts +56 -159
  39. package/lib/core/models/migration.schema.ts +180 -0
  40. package/lib/core/utils/confirm.utils.ts +4 -4
  41. package/lib/core/utils/error.utils.ts +9 -6
  42. package/lib/core/utils/global.utils.ts +3 -3
  43. package/lib/export/context/export-context-fetcher.ts +9 -6
  44. package/lib/export/export-manager.ts +16 -20
  45. package/lib/import/import-manager.ts +2 -6
  46. package/lib/metadata.ts +2 -2
  47. package/lib/node/cli/actions/export-action.ts +2 -2
  48. package/lib/node/cli/actions/import-action.ts +2 -2
  49. package/lib/node/cli/app.ts +4 -0
  50. package/lib/node/cli/args/args-setter.ts +3 -0
  51. package/lib/node/cli/cli.models.ts +1 -0
  52. package/lib/toolkit/file.ts +3 -9
  53. package/lib/translation/transforms/export-transforms.ts +1 -4
  54. package/lib/zip/zip-transformer.ts +25 -11
  55. package/lib/zip/zip.models.ts +1 -1
  56. package/package.json +10 -10
@@ -1,5 +1,5 @@
1
- import { getDefaultLogger, mapAsync } from '../core/index.js';
2
1
  import chalk from 'chalk';
2
+ import { MigrationItemsSchema, ZipMigrationAssetsSchema, getDefaultLogger, mapAsync } from '../core/index.js';
3
3
  export function zipTransformer(zip, logger) {
4
4
  const loggerToUse = logger ?? getDefaultLogger();
5
5
  const filename = 'items.json';
@@ -37,15 +37,18 @@ export function zipTransformer(zip, logger) {
37
37
  zip.addFile(assetsFilename, JSON.stringify(assetRecords));
38
38
  };
39
39
  const parseItems = async () => {
40
- const fileContent = await zip.getFileContentAsync(filename);
41
- return fileContent ? JSON.parse(fileContent) : [];
40
+ const content = await zip.getFileContentAsync(filename);
41
+ if (!content) {
42
+ return [];
43
+ }
44
+ return MigrationItemsSchema.parse(JSON.parse(content));
42
45
  };
43
46
  const parseAssets = async () => {
44
- const text = await zip.getFileContentAsync(assetsFilename);
45
- if (!text) {
47
+ const content = await zip.getFileContentAsync(assetsFilename);
48
+ if (!content) {
46
49
  return [];
47
50
  }
48
- const assetRecords = JSON.parse(text);
51
+ const assetRecords = ZipMigrationAssetsSchema.parse(JSON.parse(content));
49
52
  return await mapAsync(assetRecords, async (assetRecord) => {
50
53
  const binaryFile = await zip.getBinaryDataAsync(`${assetRecord._zipFilename}`);
51
54
  if (!binaryFile) {
@@ -1 +1 @@
1
- {"version":3,"file":"zip-transformer.js","sourceRoot":"","sources":["../../../lib/zip/zip-transformer.ts"],"names":[],"mappings":"AACA,OAAO,EAAwD,gBAAgB,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AACpH,OAAO,KAAK,MAAM,OAAO,CAAC;AAM1B,MAAM,UAAU,cAAc,CAAC,GAAgB,EAAE,MAAe;IAC5D,MAAM,WAAW,GAAW,MAAM,IAAI,gBAAgB,EAAE,CAAC;IACzD,MAAM,QAAQ,GAAW,YAAY,CAAC;IACtC,MAAM,cAAc,GAAW,aAAa,CAAC;IAC7C,MAAM,sBAAsB,GAAW,aAAa,CAAC;IAErD,MAAM,oBAAoB,GAAG,CAAC,KAAqB,EAA+C,EAAE;QAChG,MAAM,qBAAqB,GAAW,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;QACjE,OAAO;YACH,aAAa,EAAE,qBAAqB;YACpC,QAAQ,EAAE,GAAG,sBAAsB,IAAI,qBAAqB,IAAI,KAAK,CAAC,QAAQ,IAAI,KAAK,CAAC,QAAQ,EAAE;SACrG,CAAC;IACN,CAAC,CAAC;IAEF,MAAM,cAAc,GAAG,CAAC,KAA+B,EAAQ,EAAE;QAC7D,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,K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,MAAM,KAAK,GAAG,MAAM,UAAU,EAAE,CAAC;YACjC,MAAM,MAAM,GAAG,MAAM,WAAW,EAAE,CAAC;YAEnC,MAAM,aAAa,GAAkB;gBACjC,MAAM,EAAE,MAAM;gBACd,KAAK,EAAE,KAAK;aACf,CAAC;YACF,OAAO,aAAa,CAAC;QACzB,CAAC;KACJ,CAAC;AACN,CAAC"}
@@ -2,7 +2,7 @@ import { Logger } from '../core/index.js';
2
2
  export type FileBinaryData = Buffer | Blob;
3
3
  export type ZipCompressionLevel = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
4
4
  export type ZipPackager = {
5
- addFile(filePath: string, data: string | Blob | Buffer): void;
5
+ addFile(filePath: string, data: string | FileBinaryData): void;
6
6
  addFolder(name: string): ZipPackager;
7
7
  generateZipAsync(config: {
8
8
  logger?: Logger;
package/lib/core/index.ts CHANGED
@@ -12,6 +12,7 @@ export * from './utils/management-client-utils.js';
12
12
  export * from './models/log.models.js';
13
13
  export * from './models/core.models.js';
14
14
  export * from './models/migration.models.js';
15
+ export * from './models/migration.schema.js';
15
16
 
16
17
  export * from './logs/loggers.js';
17
18
  export * from './helpers/workflow-helper.js';
@@ -1,165 +1,62 @@
1
- export type MigrationElementType =
2
- | 'text'
3
- | 'rich_text'
4
- | 'number'
5
- | 'multiple_choice'
6
- | 'date_time'
7
- | 'asset'
8
- | 'modular_content'
9
- | 'taxonomy'
10
- | 'url_slug'
11
- | 'custom'
12
- | 'subpages';
13
-
14
- export type MigrationUrlSlugMode = 'autogenerated' | 'custom';
15
-
16
- export interface MigrationRichTextElementValue {
17
- value: string;
18
- components: readonly MigrationComponent[];
19
- }
20
-
21
- export interface MigrationUrlSlugElementValue {
22
- value: string | undefined;
23
- mode: MigrationUrlSlugMode;
24
- }
25
-
26
- export interface MigrationComponent {
27
- system: {
28
- id: string;
29
- type: MigrationReference;
30
- };
31
- elements: MigrationElements;
32
- }
33
-
34
- export type MigrationElementValue =
35
- | string
36
- | undefined
37
- | MigrationReference[]
38
- | number
39
- | MigrationRichTextElementValue
40
- | MigrationUrlSlugElementValue;
41
-
42
- export interface MigrationElement<
43
- TElementType extends MigrationElementType = MigrationElementType,
44
- TValue extends MigrationElementValue = MigrationElementValue
45
- > {
46
- /**
47
- * Value of the element
48
- */
49
- readonly value: TValue;
50
-
51
- /**
52
- * Type of the element
53
- */
54
- readonly type: TElementType;
55
- }
1
+ import { z } from 'zod';
2
+ import {
3
+ MigrationComponentSchema,
4
+ MigrationElementTypeSchema,
5
+ MigrationElementValueSchema,
6
+ MigrationReferenceSchema,
7
+ MigrationUrlSlugElementValueSchema,
8
+ MigrationUrlSlugModeSchema,
9
+ MigrationRichTextElementValueSchema,
10
+ MigrationElementSchema,
11
+ MigrationElementsSchema,
12
+ MigrationAssetDescriptionSchema,
13
+ MigrationDataSchema,
14
+ MigrationAssetSchema,
15
+ BaseMigrationItemSchema,
16
+ BaseMigrationItemVersionSchema
17
+ } from './migration.schema.js';
56
18
 
57
19
  export namespace MigrationElementModels {
58
- export interface TextElement extends MigrationElement<'text', string | undefined> {}
59
- export interface NumberElement extends MigrationElement<'number', number | undefined> {}
60
- export interface RichTextElement extends MigrationElement<'rich_text', MigrationRichTextElementValue | undefined> {}
61
- export interface MultipleChoiceElement
62
- extends MigrationElement<'multiple_choice', MigrationReference[] | undefined> {}
63
- export interface DateTimeElement extends MigrationElement<'date_time', string | undefined> {}
64
- export interface AssetElement extends MigrationElement<'asset', MigrationReference[] | undefined> {}
65
- export interface LinkedItemsElement extends MigrationElement<'modular_content', MigrationReference[] | undefined> {}
66
- export interface TaxonomyElement extends MigrationElement<'taxonomy', MigrationReference[] | undefined> {}
67
- export interface UrlSlugElement extends MigrationElement<'url_slug', MigrationUrlSlugElementValue | undefined> {}
68
- export interface CustomElement extends MigrationElement<'custom', string | undefined> {}
69
- export interface SubpagesElement extends MigrationElement<'subpages', MigrationReference[] | undefined> {}
70
- }
71
-
72
- export interface MigrationElements {
73
- [elementCodename: string]: MigrationElement;
74
- }
20
+ type MigrationElementDef<
21
+ TElementType extends MigrationElementType = MigrationElementType,
22
+ TValue extends MigrationElementValue = MigrationElementValue
23
+ > = {
24
+ readonly value: TValue;
25
+ readonly type: TElementType;
26
+ };
75
27
 
76
- export interface MigrationItemVersion<TElements extends MigrationElements = MigrationElements> {
28
+ export type TextElement = MigrationElementDef<'text', string | undefined>;
29
+ export type NumberElement = MigrationElementDef<'number', number | undefined>;
30
+ export type RichTextElement = MigrationElementDef<'rich_text', MigrationRichTextElementValue | undefined>;
31
+ export type MultipleChoiceElement = MigrationElementDef<'multiple_choice', MigrationReference[] | undefined>;
32
+ export type DateTimeElement = MigrationElementDef<'date_time', string | undefined>;
33
+ export type AssetElement = MigrationElementDef<'asset', MigrationReference[] | undefined>;
34
+ export type LinkedItemsElement = MigrationElementDef<'modular_content', MigrationReference[] | undefined>;
35
+ export type TaxonomyElement = MigrationElementDef<'taxonomy', MigrationReference[] | undefined>;
36
+ export type UrlSlugElement = MigrationElementDef<'url_slug', MigrationUrlSlugElementValue | undefined>;
37
+ export type CustomElement = MigrationElementDef<'custom', string | undefined>;
38
+ export type SubpagesElement = MigrationElementDef<'subpages', MigrationReference[] | undefined>;
39
+ }
40
+
41
+ export type MigrationReference = z.infer<typeof MigrationReferenceSchema>;
42
+ export type MigrationUrlSlugMode = z.infer<typeof MigrationUrlSlugModeSchema>;
43
+ export type MigrationElementType = z.infer<typeof MigrationElementTypeSchema>;
44
+ export type MigrationUrlSlugElementValue = z.infer<typeof MigrationUrlSlugElementValueSchema>;
45
+ export type MigrationRichTextElementValue = z.infer<typeof MigrationRichTextElementValueSchema>;
46
+ export type MigrationComponent = z.infer<typeof MigrationComponentSchema>;
47
+ export type MigrationElementValue = z.infer<typeof MigrationElementValueSchema>;
48
+ export type MigrationElement = z.infer<typeof MigrationElementSchema>;
49
+ export type MigrationElements = z.infer<typeof MigrationElementsSchema>;
50
+ export type MigrationAssetDescription = z.infer<typeof MigrationAssetDescriptionSchema>;
51
+ export type MigrationAsset = z.infer<typeof MigrationAssetSchema>;
52
+ export type MigrationData = z.infer<typeof MigrationDataSchema>;
53
+
54
+ export type MigrationItemVersion<TElements extends MigrationElements = MigrationElements> = z.infer<
55
+ typeof BaseMigrationItemVersionSchema
56
+ > & {
77
57
  readonly elements: TElements;
78
- readonly workflow_step: MigrationReference;
79
- }
80
-
81
- export interface MigrationItem<TElements extends MigrationElements = MigrationElements> {
82
- readonly system: {
83
- /**
84
- * Codename of the content item
85
- */
86
- readonly codename: string;
87
- /**
88
- * Name of the content item
89
- */
90
- readonly name: string;
91
- /**
92
- * Language of the language variant
93
- */
94
- readonly language: MigrationReference;
95
- /**
96
- * Content type of the item
97
- */
98
- readonly type: MigrationReference;
99
- /**
100
- * Collection of the item
101
- */
102
- readonly collection: MigrationReference;
58
+ };
103
59
 
104
- /**
105
- * Workflow of the item
106
- */
107
- readonly workflow: MigrationReference;
108
- };
60
+ export type MigrationItem<TElements extends MigrationElements = MigrationElements> = z.infer<typeof BaseMigrationItemSchema> & {
109
61
  readonly versions: MigrationItemVersion<TElements>[];
110
- }
111
-
112
- export interface MigrationReference {
113
- /**
114
- * Codename of the referenced object
115
- */
116
- readonly codename: string;
117
- }
118
-
119
- export interface MigrationAssetDescription {
120
- readonly language: MigrationReference;
121
- readonly description?: string;
122
- }
123
-
124
- export interface MigrationAsset {
125
- /**
126
- * Codename of the asset
127
- */
128
- readonly codename: string;
129
- /**
130
- * Binary data of the asset
131
- */
132
- readonly binaryData: Buffer | Blob;
133
- /**
134
- * Filename of the asset, will be used as a filename in Kontent.ai after importing the asset
135
- */
136
- readonly filename: string;
137
- /**
138
- * Title of the asset
139
- */
140
- readonly title: string;
141
-
142
- /**
143
- * Optional
144
- * Collection of the asset
145
- */
146
- readonly collection?: MigrationReference;
147
-
148
- /**
149
- * Optional.
150
- * Descriptions of the assets
151
- */
152
- readonly descriptions?: readonly MigrationAssetDescription[];
153
- }
154
-
155
- export interface MigrationData {
156
- /**
157
- * Array of migration items to process (export / import)
158
- */
159
- readonly items: readonly MigrationItem[];
160
-
161
- /**
162
- * Array of migration assets to process
163
- */
164
- readonly assets: readonly MigrationAsset[];
165
- }
62
+ };
@@ -0,0 +1,180 @@
1
+ import { z } from 'zod';
2
+
3
+ interface Elements {
4
+ readonly [key: string]: Element;
5
+ }
6
+
7
+ type UrlSlugMode = 'autogenerated' | 'custom';
8
+
9
+ type UrlSlugElementValue = {
10
+ readonly value?: string;
11
+ readonly mode: UrlSlugMode;
12
+ };
13
+
14
+ type Reference = { readonly codename: string };
15
+
16
+ type Component = {
17
+ readonly system: {
18
+ readonly id: string;
19
+ readonly type: Reference;
20
+ };
21
+ readonly elements: Elements;
22
+ };
23
+
24
+ type RichTextElementValue = {
25
+ readonly value: string;
26
+ readonly components: Readonly<Component[]>;
27
+ };
28
+
29
+ type ElementValue = string | undefined | number | Reference[] | RichTextElementValue | UrlSlugElementValue;
30
+
31
+ type Element = {
32
+ readonly type: ElementType;
33
+ readonly value?: ElementValue;
34
+ };
35
+
36
+ type ElementType =
37
+ | 'text'
38
+ | 'rich_text'
39
+ | 'number'
40
+ | 'multiple_choice'
41
+ | 'date_time'
42
+ | 'asset'
43
+ | 'modular_content'
44
+ | 'taxonomy'
45
+ | 'url_slug'
46
+ | 'custom'
47
+ | 'subpages';
48
+
49
+ export const MigrationUrlSlugModeSchema = z.enum(['autogenerated', 'custom']).readonly();
50
+ export const MigrationElementTypeSchema = z
51
+ .enum([
52
+ 'text',
53
+ 'rich_text',
54
+ 'number',
55
+ 'multiple_choice',
56
+ 'date_time',
57
+ 'asset',
58
+ 'modular_content',
59
+ 'taxonomy',
60
+ 'url_slug',
61
+ 'custom',
62
+ 'subpages'
63
+ ])
64
+ .readonly();
65
+
66
+ export const MigrationReferenceSchema = z
67
+ .strictObject({
68
+ codename: z.string().readonly()
69
+ })
70
+ .readonly();
71
+
72
+ /**
73
+ * ZodType is needed to be specified here due to the use of 'lazy' & circular dependency between types
74
+ * Otherwise TS has no way of statically inferring the type
75
+ */
76
+ export const MigrationElementsSchema: z.ZodReadonly<z.ZodType<Elements>> = z
77
+ .record(
78
+ z.string(),
79
+ z.lazy(() => MigrationElementSchema)
80
+ )
81
+ .readonly();
82
+
83
+ export const MigrationComponentSchema = z
84
+ .strictObject({
85
+ system: z.strictObject({
86
+ id: z.string(),
87
+ type: MigrationReferenceSchema
88
+ }),
89
+ elements: MigrationElementsSchema
90
+ })
91
+ .readonly();
92
+
93
+ export const MigrationUrlSlugElementValueSchema = z
94
+ .strictObject({
95
+ value: z.optional(z.string()),
96
+ mode: MigrationUrlSlugModeSchema
97
+ })
98
+ .readonly();
99
+
100
+ export const MigrationRichTextElementValueSchema = z
101
+ .strictObject({
102
+ value: z.string(),
103
+ components: z.array(MigrationComponentSchema).readonly()
104
+ })
105
+ .readonly();
106
+
107
+ export const MigrationElementValueSchema = z.union([
108
+ z.string(),
109
+ z.undefined(),
110
+ z.number(),
111
+ z.array(MigrationReferenceSchema),
112
+ MigrationRichTextElementValueSchema,
113
+ MigrationUrlSlugElementValueSchema
114
+ ]);
115
+
116
+ export const MigrationElementSchema = z
117
+ .strictObject({
118
+ type: MigrationElementTypeSchema,
119
+ value: MigrationElementValueSchema
120
+ })
121
+ .readonly();
122
+
123
+ export const BaseMigrationItemVersionSchema = z.strictObject({
124
+ workflow_step: MigrationReferenceSchema
125
+ });
126
+
127
+ export const MigrationItemVersionSchema = BaseMigrationItemVersionSchema.extend({
128
+ elements: MigrationElementsSchema
129
+ }).readonly();
130
+
131
+ export const BaseMigrationItemSchema = z.strictObject({
132
+ system: z
133
+ .strictObject({
134
+ codename: z.string(),
135
+ name: z.string(),
136
+ language: MigrationReferenceSchema,
137
+ type: MigrationReferenceSchema,
138
+ collection: MigrationReferenceSchema,
139
+ workflow: MigrationReferenceSchema
140
+ })
141
+ .readonly()
142
+ });
143
+
144
+ export const MigrationItemSchema = BaseMigrationItemSchema.extend({
145
+ versions: z.array(MigrationItemVersionSchema)
146
+ }).readonly();
147
+
148
+ export const MigrationAssetDescriptionSchema = z
149
+ .strictObject({
150
+ language: MigrationReferenceSchema,
151
+ description: z.optional(z.string())
152
+ })
153
+ .readonly();
154
+
155
+ const BaseMigrationAssetSchema = z.strictObject({
156
+ codename: z.string(),
157
+ filename: z.string(),
158
+ title: z.string(),
159
+ collection: z.optional(MigrationReferenceSchema),
160
+ descriptions: z.optional(z.array(MigrationAssetDescriptionSchema)).readonly()
161
+ });
162
+
163
+ export const MigrationAssetSchema = BaseMigrationAssetSchema.extend({
164
+ binaryData: z.union([z.instanceof(Buffer), z.instanceof(Blob)])
165
+ }).readonly();
166
+
167
+ export const ZipMigrationAssetSchema = BaseMigrationAssetSchema.extend({
168
+ _zipFilename: z.string()
169
+ }).readonly();
170
+
171
+ export const MigrationAssetsSchema = z.array(MigrationAssetSchema).readonly();
172
+ export const ZipMigrationAssetsSchema = z.array(ZipMigrationAssetSchema).readonly();
173
+ export const MigrationItemsSchema = z.array(MigrationItemSchema).readonly();
174
+
175
+ export const MigrationDataSchema = z
176
+ .strictObject({
177
+ items: MigrationItemsSchema,
178
+ assets: MigrationAssetsSchema
179
+ })
180
+ .readonly();
@@ -23,7 +23,7 @@ export async function confirmExportAsync(data: {
23
23
  data.dataToExport.itemsCount
24
24
  )}' content ${getItemsPluralText(data.dataToExport.itemsCount)} from ${chalk.yellow(
25
25
  environment.name
26
- )} (${chalk.yellow(environment.environment)}?`;
26
+ )} (${chalk.magenta(environment.environment)})?`;
27
27
 
28
28
  await confirmAsync({
29
29
  force: data.force,
@@ -65,9 +65,9 @@ export async function confirmMigrateAsync(data: {
65
65
 
66
66
  const text: string = `Are you sure to migrate '${chalk.cyan(data.dataToMigrate.itemsCount)}' ${getItemsPluralText(
67
67
  data.dataToMigrate.itemsCount
68
- )} from ${chalk.yellow(sourceEnvironment.name)} (${chalk.yellow(
68
+ )} from ${chalk.yellow(sourceEnvironment.name)} (${chalk.magenta(
69
69
  sourceEnvironment.environment
70
- )}) to environment ${chalk.yellow(targetEnvironment.name)} (${chalk.yellow(targetEnvironment.environment)}) ?`;
70
+ )}) to environment ${chalk.yellow(targetEnvironment.name)} (${chalk.magenta(targetEnvironment.environment)}) ?`;
71
71
 
72
72
  await confirmAsync({
73
73
  force: data.force,
@@ -91,7 +91,7 @@ export async function confirmImportAsync(data: {
91
91
  data.logger
92
92
  ).getEnvironmentAsync();
93
93
 
94
- const text: string = `Are you sure to import data into ${chalk.yellow(environment.name)} (${chalk.yellow(
94
+ const text: string = `Are you sure to import data into ${chalk.yellow(environment.name)} (${chalk.magenta(
95
95
  environment.environment
96
96
  )})?`;
97
97
 
@@ -1,6 +1,7 @@
1
1
  import { SharedModels } from '@kontent-ai/management-sdk';
2
2
  import chalk from 'chalk';
3
3
  import { ErrorData, OriginalManagementError } from '../models/core.models.js';
4
+ import { ZodError } from 'zod';
4
5
 
5
6
  export function extractErrorData(error: unknown): ErrorData {
6
7
  let isUnknownError: boolean = true;
@@ -10,15 +11,17 @@ export function extractErrorData(error: unknown): ErrorData {
10
11
 
11
12
  if (error instanceof SharedModels.ContentManagementBaseKontentError) {
12
13
  isUnknownError = false;
13
- message = `${error.message}`;
14
-
15
- const originalError: OriginalManagementError | undefined = error.originalError as
16
- | OriginalManagementError
17
- | undefined;
14
+ const originalError = error.originalError as OriginalManagementError | undefined;
18
15
 
19
16
  requestUrl = originalError?.response?.config?.url;
20
17
  requestData = originalError?.response?.config?.data;
21
- message += error.validationErrors.map((m) => m.message).join(', ');
18
+
19
+ message = `${error.message}: ${error.validationErrors.map((m) => m.message).join(', ')}`;
20
+ } else if (error instanceof ZodError) {
21
+ isUnknownError = false;
22
+ message = `Found '${chalk.red(error.issues.length)}' parsing errors: \n${error.issues.reduce<string>((current, issue, index) => {
23
+ return (current += `\n${index + 1}. ${chalk.red(issue.message)} (${chalk.yellow('path')}: ${issue.path.join(',')})`);
24
+ }, '')}`;
22
25
  } else if (error instanceof Error) {
23
26
  message = error.message;
24
27
  }
@@ -3,6 +3,8 @@ import { ITrackingEventData, getTrackingService } from '@kontent-ai-consulting/t
3
3
  import { isBrowser, isNode, isWebWorker } from 'browser-or-node';
4
4
  import { EnvContext } from '../models/core.models.js';
5
5
 
6
+ export type Writeable<T> = { -readonly [P in keyof T]: T[P] };
7
+
6
8
  export const isNotUndefined = <T>(item: T | undefined): item is T => item !== undefined;
7
9
 
8
10
  export function formatBytes(bytes: number): string {
@@ -28,9 +30,7 @@ export function getCurrentEnvironment(): EnvContext {
28
30
  throw Error(`Invalid current environment. This library can be used in node.js or in browsers.`);
29
31
  }
30
32
 
31
- export function getDefaultZipFilename(): string {
32
- return `data.zip`;
33
- }
33
+ export const defaultZipFilename: string = 'data.zip';
34
34
 
35
35
  export async function executeWithTrackingAsync<TResult>(data: {
36
36
  func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
@@ -62,10 +62,11 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf
62
62
  return await runMapiRequestAsync({
63
63
  logger: config.logger,
64
64
  logSpinner: logSpinner,
65
- func: async () =>
66
- (
65
+ func: async () => {
66
+ return (
67
67
  await config.managementClient.viewContentItem().byItemCodename(sourceItem.itemCodename).toPromise()
68
- ).data,
68
+ ).data;
69
+ },
69
70
  action: 'view',
70
71
  type: 'contentItem',
71
72
  itemName: `codename -> ${sourceItem.itemCodename}`
@@ -79,14 +80,16 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf
79
80
  return await runMapiRequestAsync({
80
81
  logger: config.logger,
81
82
  logSpinner: logSpinner,
82
- func: async () =>
83
- (
83
+ func: async () => {
84
+ return (
84
85
  await config.managementClient
85
86
  .viewLanguageVariant()
86
87
  .byItemCodename(sourceItem.itemCodename)
87
88
  .byLanguageCodename(sourceItem.languageCodename)
88
89
  .toPromise()
89
- ).data,
90
+ ).data;
91
+ },
92
+
90
93
  action: 'view',
91
94
  type: 'languageVariant',
92
95
  itemName: `codename -> ${sourceItem.itemCodename} -> latest (${sourceItem.languageCodename})`