/* eslint-disable @typescript-eslint/no-unused-vars */ // eslint-disable-next-line import/no-extraneous-dependencies import 'reflect-metadata'; import type { ZodObject } from 'zod'; import { MethodConfig } from '../../MethodConfig'; import { actionConfigStore } from '../action/store'; import { getArguments } from './getArguments'; import { isZodObject } from '../../utils/isZodObject'; function stringKeyExists(obj: Object): boolean { return Object.keys(obj).findIndex((k: string | number) => typeof k === 'string') !== -1; } function fixParamScheme(config: MethodConfig) { const paramsSchema = config.paramsSchema as ZodObject; if (paramsSchema && isZodObject(paramsSchema) && stringKeyExists(paramsSchema.shape)) { // eslint-disable-next-line no-param-reassign // config.paramsSchema = z.tuple([config.paramsSchema]); // todo нужно выяснить зачем я так сделал... } } export function method(cfg: Partial = {}) { return (target: any, key: string, descriptor: PropertyDescriptor) => { const methodReturnType = Reflect.getMetadata('design:returntype', target, key); if (methodReturnType !== Promise) { throw new Error(`Method ${key} of the ${target.constructor.name} class must return a promise.`); } const config = new MethodConfig(); Object.assign(config, { ...cfg, args: getArguments(target[key]), }); fixParamScheme(config); // // const config = { // ...cfg, // args: getArguments(target[key]), // }; // if (typeof cfg?.params === 'string') { // mm.paramsValidatorFactoryName = cfg.params; // } // addMethodConfig(target, key, cfg); // addMethodManifest(target, key, mm); const ac = actionConfigStore.get(target.constructor); ac.methods = Object.assign(ac.methods, { [key]: config }); }; }