mirror of
https://github.com/zoriya/flood.git
synced 2025-12-05 23:06:20 +00:00
54 lines
1.4 KiB
TypeScript
54 lines
1.4 KiB
TypeScript
export interface MethodCallConfig {
|
|
readonly methodCall: string;
|
|
readonly transformValue: (value: unknown) => string | boolean | number | string[] | Record<string, unknown>;
|
|
}
|
|
|
|
export type MethodCallConfigs = Readonly<{
|
|
[propLabel: string]: MethodCallConfig;
|
|
}>;
|
|
|
|
export type MultiMethodCalls = Array<{
|
|
methodName: string;
|
|
params: Array<string | Buffer>;
|
|
}>;
|
|
|
|
export const stringTransformer = (value: unknown): string => {
|
|
return value as string;
|
|
};
|
|
|
|
export const stringArrayTransformer = (value: unknown): string[] => {
|
|
return value as string[];
|
|
};
|
|
|
|
export const booleanTransformer = (value: unknown): boolean => {
|
|
return value === '1';
|
|
};
|
|
|
|
export const numberTransformer = (value: unknown): number => {
|
|
return Number(value);
|
|
};
|
|
|
|
export const getMethodCalls = (configs: MethodCallConfigs) => {
|
|
return Object.values(configs).map((config) => config.methodCall);
|
|
};
|
|
|
|
export const processMethodCallResponse = async <T extends MethodCallConfigs, P extends keyof T>(
|
|
response: Array<Parameters<T[P]['transformValue']>[0]>,
|
|
configs: T,
|
|
): Promise<
|
|
{
|
|
[propLabel in P]: ReturnType<T[propLabel]['transformValue']>;
|
|
}
|
|
> => {
|
|
return Object.assign(
|
|
{},
|
|
...(await Promise.all(
|
|
Object.keys(configs).map(async (propLabel, index) => {
|
|
return {
|
|
[propLabel]: configs[propLabel].transformValue(response[index]),
|
|
};
|
|
}),
|
|
)),
|
|
);
|
|
};
|