From a5dcc606651f75207fd169f55a668a60e9f2fe94 Mon Sep 17 00:00:00 2001 From: Richard Sustek Date: Wed, 11 Dec 2024 15:52:28 +0100 Subject: [PATCH] Use discriminated union for ItemProcessingResult --- lib/core/models/core.models.ts | 20 +++++++++--- lib/core/utils/processing-utils.ts | 14 ++++----- lib/export/context/export-context-fetcher.ts | 13 ++++---- lib/export/export-manager.ts | 4 +-- lib/import/context/import-context-fetcher.ts | 13 ++++---- lib/import/import-manager.ts | 32 ++++++++++---------- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/lib/core/models/core.models.ts b/lib/core/models/core.models.ts index 1b7129b..ffc9f59 100644 --- a/lib/core/models/core.models.ts +++ b/lib/core/models/core.models.ts @@ -146,8 +146,18 @@ export interface OriginalManagementError { }; } -export type ItemProcessingResult = { - readonly inputItem: InputItem; - readonly outputItem: OutputItem | undefined; - readonly error?: unknown; -}; +export type ItemProcessingResult = + | { + readonly state: 'valid'; + readonly inputItem: InputItem; + readonly outputItem: OutputItem; + } + | { + readonly state: 'error'; + readonly inputItem: InputItem; + readonly error: unknown; + } + | { + readonly state: '404'; + readonly inputItem: InputItem; + }; diff --git a/lib/core/utils/processing-utils.ts b/lib/core/utils/processing-utils.ts index 17ef3e1..a6fda3a 100644 --- a/lib/core/utils/processing-utils.ts +++ b/lib/core/utils/processing-utils.ts @@ -52,20 +52,20 @@ export async function processItemsAsync(data: { .then>((outputItem) => { if (outputItem === '404') { return { - inputItem: item, - outputItem: undefined, - error: undefined + state: '404', + inputItem: item }; } return { inputItem: item, - outputItem: outputItem + outputItem: outputItem, + state: 'valid' }; }) .catch>((error) => { return { + state: 'error', inputItem: item, - outputItem: undefined, error: error }; }); @@ -82,13 +82,13 @@ export async function processItemsAsync(data: { // Only '' promises at a time const resultItems = await Promise.all(requests); - const failedItemsCount = resultItems.filter((m) => m.error).length; + const failedItemsCount = resultItems.filter((m) => m.state === 'error').length; const failedText = failedItemsCount ? ` Failed (${chalk.red(failedItemsCount)}) items` : ``; logSpinner({ type: 'info', message: `Completed '${chalk.yellow(data.action)}'. Successfully processed (${chalk.green( - resultItems.filter((m) => m.outputItem).length + resultItems.filter((m) => m.state === 'valid').length )}) items.${failedText}` }); diff --git a/lib/export/context/export-context-fetcher.ts b/lib/export/context/export-context-fetcher.ts index c3682a9..f1ccc56 100644 --- a/lib/export/context/export-context-fetcher.ts +++ b/lib/export/context/export-context-fetcher.ts @@ -12,7 +12,6 @@ import { findRequired, FlattenedContentType, is404Error, - isNotUndefined, ItemStateInSourceEnvironmentById, LogSpinnerData, managementClientUtils, @@ -274,8 +273,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getContentItemsByIdsAsync = async (itemIds: ReadonlySet): Promise[]> => { @@ -311,8 +310,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getAssetsByIdsAsync = async (itemIds: ReadonlySet): Promise[]> => { @@ -348,8 +347,8 @@ export async function exportContextFetcherAsync(config: DefaultExportContextConf } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getItemStatesAsync = async (itemIds: ReadonlySet): Promise => { diff --git a/lib/export/export-manager.ts b/lib/export/export-manager.ts index da938e4..9a117c2 100644 --- a/lib/export/export-manager.ts +++ b/lib/export/export-manager.ts @@ -229,8 +229,8 @@ export function exportManager(config: ExportConfig) { } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; return { diff --git a/lib/import/context/import-context-fetcher.ts b/lib/import/context/import-context-fetcher.ts index 209bcd0..dc98c01 100644 --- a/lib/import/context/import-context-fetcher.ts +++ b/lib/import/context/import-context-fetcher.ts @@ -14,7 +14,6 @@ import { WorkflowStep, findRequired, is404Error, - isNotUndefined, managementClientUtils, processItemsAsync, runMapiRequestAsync, @@ -187,8 +186,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) { } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getContentItemsByCodenamesAsync = async ( @@ -226,8 +225,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) { } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getAssetsByCodenamesAsync = async (assetCodenames: ReadonlySet): Promise => { @@ -263,8 +262,8 @@ export async function importContextFetcherAsync(config: ImportContextConfig) { } }) ) - .map((m) => m.outputItem) - .filter(isNotUndefined); + .filter((m) => m.state === 'valid') + .map((m) => m.outputItem); }; const getVariantState = (languageVariant: Readonly): LanguageVariantStateData => { diff --git a/lib/import/import-manager.ts b/lib/import/import-manager.ts index f046197..a449b86 100644 --- a/lib/import/import-manager.ts +++ b/lib/import/import-manager.ts @@ -97,15 +97,15 @@ export function importManager(config: ImportConfig) { const getReportResult = (importResult: ImportResult): ReportResult => { return { errorsCount: - importResult.editedAssets.filter((m) => m.error).length + - importResult.uploadedAssets.filter((m) => m.error).length + - importResult.contentItems.filter((m) => m.error).length + - importResult.languageVariants.filter((m) => m.error).length, + importResult.editedAssets.filter((m) => m.state === 'error').length + + importResult.uploadedAssets.filter((m) => m.state === 'error').length + + importResult.contentItems.filter((m) => m.state === 'error').length + + importResult.languageVariants.filter((m) => m.state === 'error').length, assets: { count: importResult.uploadedAssets.length + importResult.editedAssets.length, successful: [ - ...importResult.uploadedAssets.filter((m) => m.outputItem).map((m) => m.inputItem.codename), - ...importResult.editedAssets.filter((m) => m.outputItem).map((m) => m.inputItem.migrationAsset.codename) + ...importResult.uploadedAssets.filter((m) => m.state === 'valid').map((m) => m.inputItem.codename), + ...importResult.editedAssets.filter((m) => m.state === 'valid').map((m) => m.inputItem.migrationAsset.codename) ].map((m) => { return { codename: m @@ -113,19 +113,19 @@ export function importManager(config: ImportConfig) { }), failed: [ ...importResult.uploadedAssets - .filter((m) => m.error) + .filter((m) => m.state === 'error') .map((m) => { return { codename: m.inputItem.codename, - error: extractErrorData(m.error).message + error: extractErrorData(m.state === 'error').message }; }), ...importResult.editedAssets - .filter((m) => m.error) + .filter((m) => m.state === 'error') .map((m) => { return { codename: m.inputItem.migrationAsset.codename, - error: extractErrorData(m.error).message + error: extractErrorData(m.state === 'error').message }; }) ] @@ -133,26 +133,26 @@ export function importManager(config: ImportConfig) { contentItems: { count: importResult.contentItems.length, successful: importResult.contentItems - .filter((m) => m.outputItem) + .filter((m) => m.state === 'valid') .map((m) => { return { codename: m.inputItem.system.codename }; }), failed: importResult.contentItems - .filter((m) => m.error) + .filter((m) => m.state === 'error') .map((m) => { return { codename: m.inputItem.system.codename, type: m.inputItem.system.type, - error: extractErrorData(m.error).message + error: extractErrorData(m.state === 'error').message }; }) }, languageVariants: { count: importResult.languageVariants.length, successful: importResult.languageVariants - .filter((m) => m.outputItem) + .filter((m) => m.state === 'valid') .map((m) => { return { codename: m.inputItem.system.codename, @@ -161,13 +161,13 @@ export function importManager(config: ImportConfig) { }; }), failed: importResult.languageVariants - .filter((m) => m.error) + .filter((m) => m.state === 'error') .map((m) => { return { codename: m.inputItem.system.codename, language: m.inputItem.system.language, type: m.inputItem.system.type, - error: extractErrorData(m.error).message + error: extractErrorData(m.state === 'error').message }; }) }