From 10212e6244a0c2bb88f0287733c605a87f89a3b4 Mon Sep 17 00:00:00 2001 From: manushak Date: Thu, 12 Dec 2024 13:18:47 +0400 Subject: [PATCH] feat(builtins): move common functions into helpers --- src/if-run/builtins/csv-import/index.ts | 113 +++-------------------- src/if-run/builtins/csv-lookup/index.ts | 114 ++---------------------- 2 files changed, 19 insertions(+), 208 deletions(-) diff --git a/src/if-run/builtins/csv-import/index.ts b/src/if-run/builtins/csv-import/index.ts index f459e4ad..7b2aa5e0 100644 --- a/src/if-run/builtins/csv-import/index.ts +++ b/src/if-run/builtins/csv-import/index.ts @@ -1,8 +1,5 @@ /* eslint-disable eqeqeq */ -import {readFile} from 'fs/promises'; -import axios from 'axios'; import {z} from 'zod'; -import {parse} from 'csv-parse/sync'; import {ConfigParams, PluginParams} from '@grnsft/if-core/types'; import {PluginFactory} from '@grnsft/if-core/interfaces'; @@ -10,20 +7,16 @@ import {ERRORS, validate} from '@grnsft/if-core/utils'; import {STRINGS} from '../../config'; -const { - FILE_FETCH_FAILED, - FILE_READ_FAILED, - MISSING_CSV_COLUMN, - MISSING_CONFIG, -} = STRINGS; - -const { - FetchingFileError, - ReadFileError, - MissingCSVColumnError, - ConfigError, - CSVParseError, -} = ERRORS; +import { + fieldAccessor, + nanifyEmptyValues, + parseCSVFile, + retrieveFile, +} from '../util/helpers'; + +const {MISSING_CONFIG} = STRINGS; + +const {ConfigError} = ERRORS; export const CSVImport = PluginFactory({ configValidation: (config: ConfigParams) => { @@ -59,74 +52,6 @@ export const CSVImport = PluginFactory({ }, }); -/** - * Checks if given string is URL. - */ -const isURL = (filepath: string) => { - try { - new URL(filepath); - return true; - } catch (error) { - return false; - } -}; - -/** - * Checks if given `filepath` is url, then tries to fetch it. - * Otherwise tries to read file. - */ -const retrieveFile = async (filepath: string) => { - if (isURL(filepath)) { - const {data} = await axios.get(filepath).catch(error => { - throw new FetchingFileError( - FILE_FETCH_FAILED(filepath, error.response.message) - ); - }); - - return data; - } - - return readFile(filepath).catch(error => { - throw new ReadFileError(FILE_READ_FAILED(filepath, error)); - }); -}; - -/** - * Checks if value is invalid: `undefined`, `null` or an empty string, then sets `nan` instead. - */ -const setNanValue = (value: any) => - value == null || value === '' ? 'nan' : value; - -/** - * Converts empty values to `nan`. - */ -const nanifyEmptyValues = (object: any) => { - if (typeof object === 'object') { - const keys = Object.keys(object); - - keys.forEach(key => { - const value = object[key]; - object[key] = setNanValue(value); - }); - - return object; - } - - return setNanValue(object); -}; - -/** - * If `field` is missing from `object`, then reject with error. - * Otherwise nanify empty values and return data. - */ -const fieldAccessor = (field: string, object: any) => { - if (!(`${field}` in object)) { - throw new MissingCSVColumnError(MISSING_CSV_COLUMN(field)); - } - - return nanifyEmptyValues(object[field]); -}; - /** * 1. If output is anything, then removes query data from csv record to escape duplicates. * 2. Otherwise checks if it's a miltidimensional array, then grabs multiple fields (). @@ -166,21 +91,3 @@ const filterOutput = ( [output]: fieldAccessor(output, dataFromCSV), }; }; - -/** - * Parses CSV file. - */ -const parseCSVFile = (file: string | Buffer) => { - try { - const parsedCSV: any[] = parse(file, { - columns: true, - skip_empty_lines: true, - cast: true, - }); - - return parsedCSV; - } catch (error: any) { - console.error(error); - throw new CSVParseError(error); - } -}; diff --git a/src/if-run/builtins/csv-lookup/index.ts b/src/if-run/builtins/csv-lookup/index.ts index 08f7760c..171a77af 100644 --- a/src/if-run/builtins/csv-lookup/index.ts +++ b/src/if-run/builtins/csv-lookup/index.ts @@ -1,31 +1,21 @@ /* eslint-disable eqeqeq */ -import {readFile} from 'fs/promises'; -import axios from 'axios'; import {z} from 'zod'; -import {parse} from 'csv-parse/sync'; import {ConfigParams, PluginParams} from '@grnsft/if-core/types'; import {PluginFactory} from '@grnsft/if-core/interfaces'; import {ERRORS, validate} from '@grnsft/if-core/utils'; import {STRINGS} from '../../config'; +import { + fieldAccessor, + nanifyEmptyValues, + parseCSVFile, + retrieveFile, +} from '../util/helpers'; -const { - FILE_FETCH_FAILED, - FILE_READ_FAILED, - MISSING_CSV_COLUMN, - MISSING_CONFIG, - NO_QUERY_DATA, -} = STRINGS; - -const { - FetchingFileError, - ReadFileError, - MissingCSVColumnError, - QueryDataNotFoundError, - ConfigError, - CSVParseError, -} = ERRORS; +const {MISSING_CONFIG, NO_QUERY_DATA} = STRINGS; + +const {QueryDataNotFoundError, ConfigError} = ERRORS; export const CSVLookup = PluginFactory({ configValidation: (config: ConfigParams) => { @@ -78,74 +68,6 @@ export const CSVLookup = PluginFactory({ }, }); -/** - * Checks if given string is URL. - */ -const isURL = (filepath: string) => { - try { - new URL(filepath); - return true; - } catch (error) { - return false; - } -}; - -/** - * Checks if given `filepath` is url, then tries to fetch it. - * Otherwise tries to read file. - */ -const retrieveFile = async (filepath: string) => { - if (isURL(filepath)) { - const {data} = await axios.get(filepath).catch(error => { - throw new FetchingFileError( - FILE_FETCH_FAILED(filepath, error.response.message) - ); - }); - - return data; - } - - return readFile(filepath).catch(error => { - throw new ReadFileError(FILE_READ_FAILED(filepath, error)); - }); -}; - -/** - * Checks if value is invalid: `undefined`, `null` or an empty string, then sets `nan` instead. - */ -const setNanValue = (value: any) => - value == null || value === '' ? 'nan' : value; - -/** - * Converts empty values to `nan`. - */ -const nanifyEmptyValues = (object: any) => { - if (typeof object === 'object') { - const keys = Object.keys(object); - - keys.forEach(key => { - const value = object[key]; - object[key] = setNanValue(value); - }); - - return object; - } - - return setNanValue(object); -}; - -/** - * If `field` is missing from `object`, then reject with error. - * Otherwise nanify empty values and return data. - */ -const fieldAccessor = (field: string, object: any) => { - if (!(`${field}` in object)) { - throw new MissingCSVColumnError(MISSING_CSV_COLUMN(field)); - } - - return nanifyEmptyValues(object[field]); -}; - /** * 1. If output is anything, then removes query data from csv record to escape duplicates. * 2. Otherwise checks if it's a miltidimensional array, then grabs multiple fields (). @@ -207,21 +129,3 @@ const withCriteria = (queryData: Record) => (csvRecord: any) => { return ifMatchesCriteria.every(value => value === true); }; - -/** - * Parses CSV file. - */ -const parseCSVFile = (file: string | Buffer) => { - try { - const parsedCSV: any[] = parse(file, { - columns: true, - skip_empty_lines: true, - cast: true, - }); - - return parsedCSV; - } catch (error: any) { - console.error(error); - throw new CSVParseError(error); - } -};