diff --git a/src/utils/parseArguments.ts b/src/utils/parseArguments.ts index ce07476..f12787d 100644 --- a/src/utils/parseArguments.ts +++ b/src/utils/parseArguments.ts @@ -1,12 +1,11 @@ -import type { CFXCallback, CFXParameters } from '../types'; +import type { CFXParameters } from '../types'; import { convertNamedPlaceholders } from '../config'; export const parseArguments = ( invokingResource: string, query: string, - parameters?: CFXParameters, - cb?: CFXCallback -): [string, CFXParameters, CFXCallback | undefined] => { + parameters?: CFXParameters +): [string, CFXParameters] => { if (convertNamedPlaceholders && parameters && typeof parameters === 'object' && !Array.isArray(parameters)) if (query.includes(':') || query.includes('@')) { const placeholders = convertNamedPlaceholders(query, parameters); @@ -14,12 +13,7 @@ export const parseArguments = ( parameters = placeholders[1]; } - if (cb && typeof cb !== 'function') cb = undefined; - - if (parameters && typeof parameters === 'function') { - cb = parameters; - parameters = []; - } else if (parameters === null || parameters === undefined) parameters = []; + if (!parameters || typeof parameters === 'function') parameters = []; if (parameters && !Array.isArray(parameters)) { let arr: unknown[] = []; @@ -31,7 +25,7 @@ export const parseArguments = ( if (queryParams !== null) { if (parameters.length === 0) { for (let i = 0; i < queryParams.length; i++) parameters[i] = null; - return [query, parameters, cb]; + return [query, parameters]; } const diff = queryParams.length - parameters.length; @@ -47,5 +41,5 @@ export const parseArguments = ( } } - return [query, parameters, cb]; + return [query, parameters]; }; diff --git a/src/utils/parseExecute.ts b/src/utils/parseExecute.ts index 38b961b..de03fbd 100644 --- a/src/utils/parseExecute.ts +++ b/src/utils/parseExecute.ts @@ -14,6 +14,10 @@ export const executeType = (query: string) => { }; export const parseExecute = (placeholders: number, parameters: CFXParameters) => { + const parametersType = typeof parameters; + + if (!parameters || parametersType !== 'object') return []; + if (!Array.isArray(parameters)) { if (typeof parameters === 'object') { const arr: unknown[] = []; diff --git a/src/utils/parseTransaction.ts b/src/utils/parseTransaction.ts index 59396db..03bd614 100644 --- a/src/utils/parseTransaction.ts +++ b/src/utils/parseTransaction.ts @@ -1,22 +1,13 @@ -import { CFXCallback, CFXParameters, TransactionQuery } from '../types'; +import { CFXParameters, TransactionQuery } from '../types'; import { parseArguments } from './parseArguments'; const isTransactionQuery = (query: TransactionQuery | string): query is TransactionQuery => (query as TransactionQuery).query !== undefined; -export const parseTransaction = ( - invokingResource: string, - queries: TransactionQuery, - parameters: CFXParameters, - cb?: CFXCallback -) => { +export const parseTransaction = (invokingResource: string, queries: TransactionQuery, parameters: CFXParameters) => { if (!Array.isArray(queries)) throw new Error(`Transaction queries must be array, received '${typeof queries}'.`); - if (cb && typeof cb !== 'function') cb = undefined; - - if (parameters && typeof parameters === 'function') cb = parameters; - - if (parameters === null || parameters === undefined || typeof parameters === 'function') parameters = []; + if (!parameters || typeof parameters === 'function') parameters = []; if (Array.isArray(queries[0])) { const transactions = queries.map((query) => { @@ -27,7 +18,7 @@ export const parseTransaction = ( return { query: parsedQuery, params: parsedParameters }; }); - return { transactions, cb }; + return transactions; } const transactions = queries.map((query) => { @@ -44,5 +35,5 @@ export const parseTransaction = ( return { query: parsedQuery, params: parsedParameters }; }); - return { transactions, cb }; + return transactions; }; diff --git a/src/utils/setCallback.ts b/src/utils/setCallback.ts new file mode 100644 index 0000000..398d2c7 --- /dev/null +++ b/src/utils/setCallback.ts @@ -0,0 +1,6 @@ +import type { CFXCallback, CFXParameters } from '../types'; + +export const setCallback = (parameters?: CFXParameters | CFXCallback, cb?: CFXCallback) => { + if (cb && typeof cb === 'function') return cb; + if (parameters && typeof parameters === 'function') return parameters; +};