@kontent-ai/migration-toolkit 2.9.1 → 3.0.0-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 (235) hide show
  1. package/README.md +19 -17
  2. package/dist/core/config.js +3 -3
  3. package/dist/core/config.js.map +1 -1
  4. package/dist/core/helpers/workflow-helper.d.ts +1 -1
  5. package/dist/core/helpers/workflow-helper.js +28 -26
  6. package/dist/core/helpers/workflow-helper.js.map +1 -1
  7. package/dist/core/logs/loggers.d.ts +1 -1
  8. package/dist/core/logs/loggers.js +15 -15
  9. package/dist/core/logs/loggers.js.map +1 -1
  10. package/dist/core/models/core.models.d.ts +27 -22
  11. package/dist/core/models/error.models.d.ts +14 -0
  12. package/dist/core/models/error.models.js +25 -0
  13. package/dist/core/models/error.models.js.map +1 -0
  14. package/dist/core/models/log.models.d.ts +2 -2
  15. package/dist/core/models/migration.models.d.ts +14 -14
  16. package/dist/core/models/migration.schema.d.ts +9 -9
  17. package/dist/core/models/migration.schema.js +28 -28
  18. package/dist/core/models/migration.schema.js.map +1 -1
  19. package/dist/core/utils/array.utils.d.ts +1 -1
  20. package/dist/core/utils/array.utils.js +4 -3
  21. package/dist/core/utils/array.utils.js.map +1 -1
  22. package/dist/core/utils/binary-data.utils.d.ts +1 -1
  23. package/dist/core/utils/binary-data.utils.js +6 -6
  24. package/dist/core/utils/binary-data.utils.js.map +1 -1
  25. package/dist/core/utils/confirm.utils.d.ts +1 -1
  26. package/dist/core/utils/confirm.utils.js +21 -20
  27. package/dist/core/utils/confirm.utils.js.map +1 -1
  28. package/dist/core/utils/error.utils.d.ts +1 -1
  29. package/dist/core/utils/error.utils.js +16 -14
  30. package/dist/core/utils/error.utils.js.map +1 -1
  31. package/dist/core/utils/external-id.utils.js +1 -1
  32. package/dist/core/utils/external-id.utils.js.map +1 -1
  33. package/dist/core/utils/global.utils.d.ts +3 -3
  34. package/dist/core/utils/global.utils.js +20 -18
  35. package/dist/core/utils/global.utils.js.map +1 -1
  36. package/dist/core/utils/http.utils.d.ts +2 -2
  37. package/dist/core/utils/http.utils.js +4 -4
  38. package/dist/core/utils/http.utils.js.map +1 -1
  39. package/dist/core/utils/management-client-utils.d.ts +4 -4
  40. package/dist/core/utils/management-client-utils.js +34 -34
  41. package/dist/core/utils/management-client-utils.js.map +1 -1
  42. package/dist/core/utils/processing-utils.d.ts +4 -4
  43. package/dist/core/utils/processing-utils.js +13 -13
  44. package/dist/core/utils/processing-utils.js.map +1 -1
  45. package/dist/core/utils/run.utils.d.ts +2 -2
  46. package/dist/core/utils/run.utils.js +7 -7
  47. package/dist/core/utils/run.utils.js.map +1 -1
  48. package/dist/export/context/export-context-fetcher.d.ts +1 -1
  49. package/dist/export/context/export-context-fetcher.js +92 -62
  50. package/dist/export/context/export-context-fetcher.js.map +1 -1
  51. package/dist/export/export-manager.d.ts +2 -2
  52. package/dist/export/export-manager.js +46 -37
  53. package/dist/export/export-manager.js.map +1 -1
  54. package/dist/export/export.models.d.ts +21 -2
  55. package/dist/export/utils/export.utils.d.ts +1 -1
  56. package/dist/export/utils/export.utils.js +3 -2
  57. package/dist/export/utils/export.utils.js.map +1 -1
  58. package/dist/file/file-manager.d.ts +2 -2
  59. package/dist/file/file-manager.js +7 -7
  60. package/dist/file/file-manager.js.map +1 -1
  61. package/dist/import/comparers/asset-comparer.d.ts +2 -2
  62. package/dist/import/comparers/asset-comparer.js +12 -11
  63. package/dist/import/comparers/asset-comparer.js.map +1 -1
  64. package/dist/import/context/import-context-fetcher.d.ts +1 -1
  65. package/dist/import/context/import-context-fetcher.js +77 -73
  66. package/dist/import/context/import-context-fetcher.js.map +1 -1
  67. package/dist/import/import-manager.d.ts +1 -1
  68. package/dist/import/import-manager.js +59 -56
  69. package/dist/import/import-manager.js.map +1 -1
  70. package/dist/import/import.models.d.ts +6 -2
  71. package/dist/import/importers/assets-importer.d.ts +3 -3
  72. package/dist/import/importers/assets-importer.js +46 -43
  73. package/dist/import/importers/assets-importer.js.map +1 -1
  74. package/dist/import/importers/content-items-importer.d.ts +3 -3
  75. package/dist/import/importers/content-items-importer.js +28 -25
  76. package/dist/import/importers/content-items-importer.js.map +1 -1
  77. package/dist/import/importers/language-variant-importer.d.ts +3 -3
  78. package/dist/import/importers/language-variant-importer.js +45 -45
  79. package/dist/import/importers/language-variant-importer.js.map +1 -1
  80. package/dist/import/importers/workflow-importer.d.ts +3 -2
  81. package/dist/import/importers/workflow-importer.js +58 -54
  82. package/dist/import/importers/workflow-importer.js.map +1 -1
  83. package/dist/import/utils/import.utils.d.ts +1 -1
  84. package/dist/import/utils/import.utils.js +3 -2
  85. package/dist/import/utils/import.utils.js.map +1 -1
  86. package/dist/index.d.ts +11 -7
  87. package/dist/index.js +10 -8
  88. package/dist/index.js.map +1 -1
  89. package/dist/metadata.js +3 -3
  90. package/dist/metadata.js.map +1 -1
  91. package/dist/node/cli/actions/export-action.d.ts +1 -1
  92. package/dist/node/cli/actions/export-action.js +28 -23
  93. package/dist/node/cli/actions/export-action.js.map +1 -1
  94. package/dist/node/cli/actions/import-action.d.ts +1 -1
  95. package/dist/node/cli/actions/import-action.js +15 -12
  96. package/dist/node/cli/actions/import-action.js.map +1 -1
  97. package/dist/node/cli/actions/migrate-action.d.ts +1 -1
  98. package/dist/node/cli/actions/migrate-action.js +25 -22
  99. package/dist/node/cli/actions/migrate-action.js.map +1 -1
  100. package/dist/node/cli/app.js +13 -12
  101. package/dist/node/cli/app.js.map +1 -1
  102. package/dist/node/cli/args/args-fetcher.d.ts +1 -1
  103. package/dist/node/cli/args/args-fetcher.js +14 -13
  104. package/dist/node/cli/args/args-fetcher.js.map +1 -1
  105. package/dist/node/cli/args/args-setter.d.ts +1 -1
  106. package/dist/node/cli/args/args-setter.js +8 -6
  107. package/dist/node/cli/args/args-setter.js.map +1 -1
  108. package/dist/node/cli/cli.models.d.ts +2 -2
  109. package/dist/node/cli/commands.js +68 -56
  110. package/dist/node/cli/commands.js.map +1 -1
  111. package/dist/toolkit/elements-builder.d.ts +1 -1
  112. package/dist/toolkit/elements-builder.js +12 -12
  113. package/dist/toolkit/elements-builder.js.map +1 -1
  114. package/dist/toolkit/export.d.ts +2 -2
  115. package/dist/toolkit/export.js +8 -8
  116. package/dist/toolkit/export.js.map +1 -1
  117. package/dist/toolkit/file.d.ts +2 -1
  118. package/dist/toolkit/file.js +18 -16
  119. package/dist/toolkit/file.js.map +1 -1
  120. package/dist/toolkit/import.d.ts +1 -1
  121. package/dist/toolkit/import.js +12 -12
  122. package/dist/toolkit/import.js.map +1 -1
  123. package/dist/toolkit/migrate.d.ts +13 -4
  124. package/dist/toolkit/migrate.js +15 -13
  125. package/dist/toolkit/migrate.js.map +1 -1
  126. package/dist/translation/extraction/items-extraction.processor.d.ts +5 -4
  127. package/dist/translation/extraction/items-extraction.processor.js +73 -35
  128. package/dist/translation/extraction/items-extraction.processor.js.map +1 -1
  129. package/dist/translation/helpers/export-placeholder.utils.d.ts +11 -0
  130. package/dist/translation/helpers/export-placeholder.utils.js +11 -0
  131. package/dist/translation/helpers/export-placeholder.utils.js.map +1 -0
  132. package/dist/translation/helpers/rich-text.processor.d.ts +6 -6
  133. package/dist/translation/helpers/rich-text.processor.js +38 -38
  134. package/dist/translation/helpers/rich-text.processor.js.map +1 -1
  135. package/dist/translation/transforms/export-transforms.d.ts +2 -2
  136. package/dist/translation/transforms/export-transforms.js +93 -67
  137. package/dist/translation/transforms/export-transforms.js.map +1 -1
  138. package/dist/translation/transforms/import-transforms.d.ts +2 -2
  139. package/dist/translation/transforms/import-transforms.js +38 -40
  140. package/dist/translation/transforms/import-transforms.js.map +1 -1
  141. package/dist/zip/zip-manager.d.ts +4 -3
  142. package/dist/zip/zip-manager.js +9 -9
  143. package/dist/zip/zip-manager.js.map +1 -1
  144. package/dist/zip/zip-packager.d.ts +2 -2
  145. package/dist/zip/zip-packager.js +19 -17
  146. package/dist/zip/zip-packager.js.map +1 -1
  147. package/dist/zip/zip-transformer.d.ts +3 -2
  148. package/dist/zip/zip-transformer.js +16 -15
  149. package/dist/zip/zip-transformer.js.map +1 -1
  150. package/dist/zip/zip.models.d.ts +2 -2
  151. package/lib/core/config.ts +6 -6
  152. package/lib/core/helpers/workflow-helper.ts +190 -185
  153. package/lib/core/logs/loggers.ts +52 -52
  154. package/lib/core/models/core.models.ts +128 -119
  155. package/lib/core/models/error.models.ts +41 -0
  156. package/lib/core/models/log.models.ts +20 -20
  157. package/lib/core/models/migration.models.ts +54 -54
  158. package/lib/core/models/migration.schema.ts +108 -108
  159. package/lib/core/utils/array.utils.ts +43 -42
  160. package/lib/core/utils/binary-data.utils.ts +16 -16
  161. package/lib/core/utils/confirm.utils.ts +106 -105
  162. package/lib/core/utils/error.utils.ts +69 -72
  163. package/lib/core/utils/external-id.utils.ts +4 -4
  164. package/lib/core/utils/global.utils.ts +81 -75
  165. package/lib/core/utils/http.utils.ts +23 -23
  166. package/lib/core/utils/management-client-utils.ts +186 -186
  167. package/lib/core/utils/processing-utils.ts +82 -82
  168. package/lib/core/utils/run.utils.ts +50 -50
  169. package/lib/export/context/export-context-fetcher.ts +472 -447
  170. package/lib/export/export-manager.ts +233 -232
  171. package/lib/export/export.models.ts +71 -56
  172. package/lib/export/utils/export.utils.ts +9 -7
  173. package/lib/file/file-manager.ts +28 -28
  174. package/lib/import/comparers/asset-comparer.ts +107 -106
  175. package/lib/import/context/import-context-fetcher.ts +462 -462
  176. package/lib/import/import-manager.ts +243 -240
  177. package/lib/import/import.models.ts +69 -67
  178. package/lib/import/importers/assets-importer.ts +240 -236
  179. package/lib/import/importers/content-items-importer.ts +126 -122
  180. package/lib/import/importers/language-variant-importer.ts +324 -328
  181. package/lib/import/importers/workflow-importer.ts +372 -367
  182. package/lib/import/utils/import.utils.ts +9 -7
  183. package/lib/index.ts +12 -8
  184. package/lib/metadata.ts +3 -4
  185. package/lib/node/cli/actions/export-action.ts +43 -38
  186. package/lib/node/cli/actions/import-action.ts +32 -29
  187. package/lib/node/cli/actions/migrate-action.ts +49 -46
  188. package/lib/node/cli/app.ts +21 -21
  189. package/lib/node/cli/args/args-fetcher.ts +55 -54
  190. package/lib/node/cli/args/args-setter.ts +34 -32
  191. package/lib/node/cli/cli.models.ts +17 -17
  192. package/lib/node/cli/commands.ts +165 -153
  193. package/lib/toolkit/elements-builder.ts +69 -69
  194. package/lib/toolkit/export.ts +21 -21
  195. package/lib/toolkit/file.ts +59 -53
  196. package/lib/toolkit/import.ts +32 -32
  197. package/lib/toolkit/migrate.ts +60 -49
  198. package/lib/translation/extraction/items-extraction.processor.ts +203 -173
  199. package/lib/translation/helpers/export-placeholder.utils.ts +10 -0
  200. package/lib/translation/helpers/rich-text.processor.ts +311 -311
  201. package/lib/translation/transforms/export-transforms.ts +313 -279
  202. package/lib/translation/transforms/import-transforms.ts +188 -191
  203. package/lib/zip/zip-manager.ts +28 -27
  204. package/lib/zip/zip-packager.ts +75 -73
  205. package/lib/zip/zip-transformer.ts +112 -107
  206. package/lib/zip/zip.models.ts +7 -7
  207. package/package.json +91 -84
  208. package/dist/core/index.d.ts +0 -16
  209. package/dist/core/index.js +0 -17
  210. package/dist/core/index.js.map +0 -1
  211. package/dist/export/index.d.ts +0 -2
  212. package/dist/export/index.js +0 -3
  213. package/dist/export/index.js.map +0 -1
  214. package/dist/file/index.d.ts +0 -1
  215. package/dist/file/index.js +0 -2
  216. package/dist/file/index.js.map +0 -1
  217. package/dist/import/index.d.ts +0 -2
  218. package/dist/import/index.js +0 -3
  219. package/dist/import/index.js.map +0 -1
  220. package/dist/toolkit/index.d.ts +0 -5
  221. package/dist/toolkit/index.js +0 -6
  222. package/dist/toolkit/index.js.map +0 -1
  223. package/dist/translation/index.d.ts +0 -4
  224. package/dist/translation/index.js +0 -5
  225. package/dist/translation/index.js.map +0 -1
  226. package/dist/zip/index.d.ts +0 -4
  227. package/dist/zip/index.js +0 -5
  228. package/dist/zip/index.js.map +0 -1
  229. package/lib/core/index.ts +0 -18
  230. package/lib/export/index.ts +0 -2
  231. package/lib/file/index.ts +0 -1
  232. package/lib/import/index.ts +0 -2
  233. package/lib/toolkit/index.ts +0 -5
  234. package/lib/translation/index.ts +0 -4
  235. package/lib/zip/index.ts +0 -4
@@ -1,88 +1,85 @@
1
- import { SharedModels } from '@kontent-ai/management-sdk';
2
- import chalk from 'chalk';
3
- import { match } from 'ts-pattern';
4
- import { ZodError } from 'zod';
5
- import type { ErrorData, OriginalManagementError } from '../models/core.models.js';
1
+ import { SharedModels } from "@kontent-ai/management-sdk";
2
+ import chalk from "chalk";
3
+ import { match } from "ts-pattern";
4
+ import { ZodError } from "zod";
5
+ import type { ErrorData, OriginalManagementError } from "../models/core.models.js";
6
6
 
7
7
  export function extractErrorData(error: unknown): ErrorData {
8
- return match(error)
9
- .returnType<ErrorData>()
10
- .when(
11
- (error) => error instanceof SharedModels.ContentManagementBaseKontentError,
12
- (error) => {
13
- const originalError = error.originalError as OriginalManagementError | undefined;
8
+ return match(error)
9
+ .returnType<ErrorData>()
10
+ .when(
11
+ (error) => error instanceof SharedModels.ContentManagementBaseKontentError,
12
+ (error) => {
13
+ const originalError = error.originalError as OriginalManagementError | undefined;
14
14
 
15
- return {
16
- isUnknownError: false,
17
- error: error,
18
- message: `${error.message} ${error.validationErrors.map((m) => m.message).join(', ')}`,
19
- requestUrl: originalError?.response?.config?.url,
20
- requestData: originalError?.response?.config?.data
21
- };
22
- }
23
- )
24
- .when(
25
- (error) => error instanceof ZodError,
26
- (error) => {
27
- return {
28
- isUnknownError: false,
29
- error: error,
30
- message: `Found '${chalk.red(error.issues.length)}' parsing errors: \n${error.issues.reduce<string>(
31
- (current, issue, index) => {
32
- return (current += `\n${index + 1}. ${chalk.red(issue.message)} (${chalk.yellow('path')}: ${issue.path.join(
33
- ','
34
- )})`);
35
- },
36
- ''
37
- )}`
38
- };
39
- }
40
- )
41
- .when(
42
- (error) => error instanceof Error,
43
- (error) => {
44
- return {
45
- isUnknownError: false,
46
- error: error,
47
- message: error.message
48
- };
49
- }
50
- )
51
- .otherwise((error) => {
52
- return {
53
- isUnknownError: true,
54
- error: error,
55
- message: `Unknown error`
56
- };
57
- });
15
+ return {
16
+ isUnknownError: false,
17
+ error: error,
18
+ message: `${error.message} ${error.validationErrors.map((m) => m.message).join(", ")}`,
19
+ requestUrl: originalError?.response?.config?.url,
20
+ requestData: originalError?.response?.config?.data,
21
+ };
22
+ },
23
+ )
24
+ .when(
25
+ (error) => error instanceof ZodError,
26
+ (error) => {
27
+ const parsingErrorsMsg = error.issues.reduce<string>((current, issue, index) => {
28
+ const message = `\n${index + 1}. ${chalk.red(issue.message)} (${chalk.yellow("path")}: ${issue.path.join(",")})`;
29
+ return `${current}${message}`;
30
+ }, "");
31
+ return {
32
+ isUnknownError: false,
33
+ error: error,
34
+ message: `Found '${chalk.red(error.issues.length)}' parsing errors: \n${parsingErrorsMsg}`,
35
+ };
36
+ },
37
+ )
38
+ .when(
39
+ (error) => error instanceof Error,
40
+ (error) => {
41
+ return {
42
+ isUnknownError: false,
43
+ error: error,
44
+ message: error.message,
45
+ };
46
+ },
47
+ )
48
+ .otherwise((error) => {
49
+ return {
50
+ isUnknownError: true,
51
+ error: error,
52
+ message: `Unknown error`,
53
+ };
54
+ });
58
55
  }
59
56
 
60
57
  export function is404Error(error: unknown): boolean {
61
- if (error instanceof SharedModels.ContentManagementBaseKontentError) {
62
- const originalError = error.originalError as OriginalManagementError | undefined;
58
+ if (error instanceof SharedModels.ContentManagementBaseKontentError) {
59
+ const originalError = error.originalError as OriginalManagementError | undefined;
63
60
 
64
- if (originalError?.response?.status === 404) {
65
- return true;
66
- }
67
- }
61
+ if (originalError?.response?.status === 404) {
62
+ return true;
63
+ }
64
+ }
68
65
 
69
- return false;
66
+ return false;
70
67
  }
71
68
 
72
69
  export function handleError(error: unknown): void {
73
- const errorData = extractErrorData(error);
70
+ const errorData = extractErrorData(error);
74
71
 
75
- if (errorData.isUnknownError) {
76
- console.error(error);
77
- }
72
+ if (errorData.isUnknownError) {
73
+ console.error(error);
74
+ }
78
75
 
79
- if (errorData.requestData) {
80
- console.log(`${chalk.red('Request data')}: ${errorData.requestData}`);
81
- }
76
+ if (errorData.requestData) {
77
+ console.log(`${chalk.red("Request data")}: ${errorData.requestData}`);
78
+ }
82
79
 
83
- if (errorData.requestUrl) {
84
- console.log(`${chalk.red('Request url')}: ${errorData.requestUrl}`);
85
- }
80
+ if (errorData.requestUrl) {
81
+ console.log(`${chalk.red("Request url")}: ${errorData.requestUrl}`);
82
+ }
86
83
 
87
- console.error(`${chalk.red('Error:')} ${errorData.message}`);
84
+ console.error(`${chalk.red("Error:")} ${errorData.message}`);
88
85
  }
@@ -1,9 +1,9 @@
1
1
  export interface ExternalIdGenerator {
2
- readonly assetExternalId: (codename: string) => string;
3
- readonly contentItemExternalId: (codename: string) => string;
2
+ readonly assetExternalId: (codename: string) => string;
3
+ readonly contentItemExternalId: (codename: string) => string;
4
4
  }
5
5
 
6
6
  export const defaultExternalIdGenerator: ExternalIdGenerator = {
7
- assetExternalId: (codename) => `asset_${codename}`,
8
- contentItemExternalId: (codename) => `item_${codename}`
7
+ assetExternalId: (codename) => `asset_${codename}`,
8
+ contentItemExternalId: (codename) => `item_${codename}`,
9
9
  };
@@ -1,98 +1,104 @@
1
- import type { ITrackingEventData } from '@kontent-ai-consulting/tools-analytics';
2
- import { getTrackingService } from '@kontent-ai-consulting/tools-analytics';
3
- import { isBrowser, isNode, isWebWorker } from 'browser-or-node';
4
- import { format } from 'bytes';
5
- import { getDefaultLogger } from '../logs/loggers.js';
6
- import type { EnvContext } from '../models/core.models.js';
7
- import type { Logger } from '../models/log.models.js';
8
- import { extractErrorData } from './error.utils.js';
1
+ import type { ITrackingEventData } from "@kontent-ai-consulting/tools-analytics";
2
+ import { getTrackingService } from "@kontent-ai-consulting/tools-analytics";
3
+ import { isBrowser, isNode, isWebWorker } from "browser-or-node";
4
+ import { format } from "bytes";
5
+ import { getDefaultLogger } from "../logs/loggers.js";
6
+ import type { EnvContext } from "../models/core.models.js";
7
+ import { MigrationToolkitError } from "../models/error.models.js";
8
+ import type { Logger } from "../models/log.models.js";
9
+ import { extractErrorData } from "./error.utils.js";
9
10
 
10
11
  export type Writeable<T> = { -readonly [P in keyof T]: T[P] };
11
12
 
12
13
  export const isNotUndefined = <T>(item: T | undefined): item is T => item !== undefined;
13
14
 
14
- export const isString = (value: unknown): value is string => typeof value === 'string';
15
+ export const isString = (value: unknown): value is string => typeof value === "string";
15
16
  export const isArray = <T>(value: unknown): value is Array<T> => Array.isArray(value);
16
17
 
17
18
  export function formatBytes(bytes: number): string {
18
- return format(bytes) ?? 'n/a';
19
+ return format(bytes) ?? "n/a";
19
20
  }
20
21
 
21
22
  export function exitProgram(data: { readonly message: string }): never {
22
- throw Error(data.message);
23
+ throw new MigrationToolkitError("exitProgram", data.message);
23
24
  }
24
25
 
25
26
  export function getCurrentEnvironment(): EnvContext {
26
- if (isNode) {
27
- return 'node';
28
- }
29
- if (isBrowser || isWebWorker) {
30
- return 'browser';
31
- }
32
-
33
- throw Error(`Invalid current environment. This library can be used in node.js or in browsers.`);
27
+ if (isNode) {
28
+ return "node";
29
+ }
30
+ if (isBrowser || isWebWorker) {
31
+ return "browser";
32
+ }
33
+
34
+ throw new MigrationToolkitError(
35
+ "invalidEnvironment",
36
+ `Invalid current environment. This library can be used in node.js or in browsers.`,
37
+ );
34
38
  }
35
39
 
36
- export const defaultZipFilename: string = 'data.zip';
40
+ export const defaultZipFilename: string = "data.zip";
37
41
 
38
42
  export async function executeWithTrackingAsync<TResult>(data: {
39
- readonly func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
40
- readonly event: Readonly<ITrackingEventData>;
41
- readonly logger?: Logger;
43
+ readonly func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
44
+ readonly event: Readonly<ITrackingEventData>;
45
+ readonly logger?: Logger;
42
46
  }): Promise<TResult extends void ? void : Readonly<TResult>> {
43
- const trackingService = getTrackingService();
44
- const logger = data.logger ?? getDefaultLogger();
45
-
46
- const event = await runTrackingFuncWithErrorHadlingAsync({
47
- func: async () => {
48
- return await trackingService.trackEventAsync(data.event);
49
- },
50
- logger: logger
51
- });
52
-
53
- try {
54
- const result = await data.func();
55
-
56
- if (event) {
57
- await runTrackingFuncWithErrorHadlingAsync({
58
- func: async () => {
59
- await trackingService.setEventResultAsync({
60
- eventId: event.eventId,
61
- result: 'success'
62
- });
63
- },
64
- logger: logger
65
- });
66
- }
67
-
68
- return result;
69
- } catch (error) {
70
- if (event) {
71
- await runTrackingFuncWithErrorHadlingAsync({
72
- func: async () => {
73
- await trackingService.setEventResultAsync({
74
- eventId: event.eventId,
75
- result: 'fail'
76
- });
77
- },
78
- logger: logger
79
- });
80
- }
81
-
82
- throw error;
83
- }
47
+ const trackingService = getTrackingService();
48
+ const logger = data.logger ?? getDefaultLogger();
49
+
50
+ const event = await runTrackingFuncWithErrorHadlingAsync({
51
+ func: async () => {
52
+ return await trackingService.trackEventAsync(data.event);
53
+ },
54
+ logger: logger,
55
+ });
56
+
57
+ try {
58
+ const result = await data.func();
59
+
60
+ if (event) {
61
+ await runTrackingFuncWithErrorHadlingAsync({
62
+ func: async () => {
63
+ await trackingService.setEventResultAsync({
64
+ eventId: event.eventId,
65
+ result: "success",
66
+ });
67
+ },
68
+ logger: logger,
69
+ });
70
+ }
71
+
72
+ return result;
73
+ } catch (error) {
74
+ if (event) {
75
+ await runTrackingFuncWithErrorHadlingAsync({
76
+ func: async () => {
77
+ await trackingService.setEventResultAsync({
78
+ eventId: event.eventId,
79
+ result: "fail",
80
+ });
81
+ },
82
+ logger: logger,
83
+ });
84
+ }
85
+
86
+ throw error;
87
+ }
84
88
  }
85
89
 
86
90
  async function runTrackingFuncWithErrorHadlingAsync<T>(data: {
87
- readonly func: () => Promise<T>;
88
- readonly logger: Logger;
89
- }): Promise<T | void> {
90
- try {
91
- return await data.func();
92
- } catch (trackingError) {
93
- data.logger.log({
94
- message: `Failed to track event. ${extractErrorData(trackingError).message}`,
95
- type: 'warning'
96
- });
97
- }
91
+ readonly func: () => Promise<T>;
92
+ readonly logger: Logger;
93
+ }): Promise<T | undefined> {
94
+ try {
95
+ return await data.func();
96
+ } catch (trackingError) {
97
+ data.logger.log({
98
+ message: `Failed to track event. ${extractErrorData(trackingError).message}`,
99
+ type: "warning",
100
+ });
101
+
102
+ return undefined;
103
+ }
98
104
  }
@@ -1,32 +1,32 @@
1
- import type { IRetryStrategyOptions } from '@kontent-ai/core-sdk';
2
- import { HttpService } from '@kontent-ai/core-sdk';
3
- import { match } from 'ts-pattern';
4
- import type { OriginalManagementError } from '../models/core.models.js';
1
+ import type { IRetryStrategyOptions } from "@kontent-ai/core-sdk";
2
+ import { HttpService } from "@kontent-ai/core-sdk";
3
+ import { match } from "ts-pattern";
4
+ import type { OriginalManagementError } from "../models/core.models.js";
5
5
 
6
6
  const rateExceededErrorCode: number = 10000;
7
7
 
8
8
  export const defaultHttpService: Readonly<HttpService> = new HttpService({
9
- logErrorsToConsole: false
9
+ logErrorsToConsole: false,
10
10
  });
11
11
 
12
12
  export const defaultRetryStrategy: Readonly<IRetryStrategyOptions> = {
13
- addJitter: true,
14
- canRetryError: (err) => {
15
- const originalError = err as OriginalManagementError | undefined;
16
- const errorCode: number = originalError?.response?.data?.error_code ?? -1;
13
+ addJitter: true,
14
+ canRetryError: (err) => {
15
+ const originalError = err as OriginalManagementError | undefined;
16
+ const errorCode: number = originalError?.response?.data?.error_code ?? -1;
17
17
 
18
- return (
19
- match(errorCode)
20
- // retry rate exceeded error
21
- .with(rateExceededErrorCode, () => true)
22
- // if error code is set, do not retry the request
23
- .when(
24
- (errorCode) => errorCode >= 0,
25
- () => false
26
- )
27
- .otherwise(() => true)
28
- );
29
- },
30
- maxAttempts: 3,
31
- deltaBackoffMs: 1000
18
+ return (
19
+ match(errorCode)
20
+ // retry rate exceeded error
21
+ .with(rateExceededErrorCode, () => true)
22
+ // if error code is set, do not retry the request
23
+ .when(
24
+ (errorCode) => errorCode >= 0,
25
+ () => false,
26
+ )
27
+ .otherwise(() => true)
28
+ );
29
+ },
30
+ maxAttempts: 3,
31
+ deltaBackoffMs: 1000,
32
32
  };