From 8412b19a289ebf612954a914704956981b5d1a90 Mon Sep 17 00:00:00 2001 From: Egor Didenko Date: Wed, 29 May 2024 17:55:46 -0400 Subject: [PATCH] feat(validators): improved code style and types --- blocks/Config/normalizeConfigValue.js | 6 +++--- types/exported.d.ts | 14 +++++++------- utils/validators/file/validateUploadError.js | 20 +++++++++++--------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/blocks/Config/normalizeConfigValue.js b/blocks/Config/normalizeConfigValue.js index 38d9782c3..727bb75c3 100644 --- a/blocks/Config/normalizeConfigValue.js +++ b/blocks/Config/normalizeConfigValue.js @@ -75,7 +75,7 @@ const asFunction = (value) => { }; /** - * @template {Array}T + * @template {Function[]} T * @param {unknown} value * @returns {T} */ @@ -153,8 +153,8 @@ const mapping = { secureDeliveryProxyUrlResolver: /** @type {typeof asFunction} */ (asFunction), iconHrefResolver: /** @type {typeof asFunction} */ (asFunction), - fileValidators: /** @type {typeof asValidators} */ (asValidators), - collectionValidators: /** @type {typeof asValidators} */ (asValidators), + fileValidators: /** @type {typeof asValidators} */ (asValidators), + collectionValidators: /** @type {typeof asValidators} */ (asValidators), }; /** diff --git a/types/exported.d.ts b/types/exported.d.ts index 6c535deeb..9ebacb294 100644 --- a/types/exported.d.ts +++ b/types/exported.d.ts @@ -1,6 +1,6 @@ -import { LocaleDefinition } from '../abstract/localeRegistry'; -import { complexConfigKeys } from '../blocks/Config/Config'; -import { FuncFileValidator, FuncCollectionValidator } from '../abstract/ValidationManager'; +import type { LocaleDefinition } from '../abstract/localeRegistry'; +import type { complexConfigKeys } from '../blocks/Config/Config'; +import type { FuncFileValidator, FuncCollectionValidator } from '../abstract/ValidationManager'; export type UploadError = import('@uploadcare/upload-client').UploadError; export type UploadcareFile = import('@uploadcare/upload-client').UploadcareFile; @@ -16,8 +16,8 @@ export type SecureDeliveryProxyUrlResolver = ( export type SecureUploadsSignatureAndExpire = { secureSignature: string; secureExpire: string }; export type SecureUploadsSignatureResolver = () => Promise; export type IconHrefResolver = (iconName: string) => string; -export type FileValidator = FuncFileValidator | any; -export type CollectionValidator = FuncCollectionValidator | any; +export type FileValidator = FuncFileValidator[]; +export type CollectionValidator = FuncCollectionValidator[]; export type ConfigType = { pubkey: string; @@ -72,8 +72,8 @@ export type ConfigType = { secureDeliveryProxyUrlResolver: SecureDeliveryProxyUrlResolver | null; iconHrefResolver: IconHrefResolver | null; - fileValidators: FileValidator[] | null; - collectionValidators: CollectionValidator[] | null; + fileValidators: FileValidator | null; + collectionValidators: CollectionValidator | null; }; export type ConfigComplexType = Pick; export type ConfigPlainType = Omit; diff --git a/utils/validators/file/validateUploadError.js b/utils/validators/file/validateUploadError.js index c46afba53..4c7512c34 100644 --- a/utils/validators/file/validateUploadError.js +++ b/utils/validators/file/validateUploadError.js @@ -17,20 +17,22 @@ export const validateUploadError = (outputEntry, internalEntry, block) => { entry: outputEntry, error: cause, }); - } else if (cause instanceof NetworkError) { + } + + if (cause instanceof NetworkError) { return buildOutputFileError({ type: 'NETWORK_ERROR', message: cause.message, entry: outputEntry, error: cause, }); - } else { - const error = cause instanceof Error ? cause : new Error('Unknown error', { cause }); - return buildOutputFileError({ - type: 'UNKNOWN_ERROR', - message: error.message, - entry: outputEntry, - error, - }); } + + const error = cause instanceof Error ? cause : new Error('Unknown error', { cause }); + return buildOutputFileError({ + type: 'UNKNOWN_ERROR', + message: error.message, + entry: outputEntry, + error, + }); };