import { Singleton, Container, Scope, Inject } from "typescript-ioc";
import { IService, logger } from "mslib"

/**
  * 统一API上行API装饰器
  * 
  * 自动验签，异常捕获，日志记录，发生异常自动返回错误报文等
  * @param description 服务描述
  * @returns
  */
 export function Service(description?: string): MethodDecorator {
     return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
         let method = descriptor.value;
 
         descriptor.value = async function () {
             let response;
             let _error;
 
             try {
                 logger.info(`TYAPI,input:${JSON.stringify(arguments)}`);
 
                 response = await method.apply(this, arguments);
             } catch (error) {
                 _error = error;
             }
 
             logger[_error ? 'error' : 'info'](`${target.constructor.name},input:${JSON.stringify(arguments)},response:${JSON.stringify(response)}`, _error);
 
             return response;
         }
     }
 }