Options
All
  • Public
  • Public/Protected
  • All
Menu

Namespace AnnotationTools

一些用于开发自定义装饰器的工具函数。

Index

Functions

add_handler

create_decorator

  • create_decorator<T>(processor: (constructor: any, meta: any, options?: T) => void): (options?: T) => (constructor: any) => void
  • 用于创建自定义装饰器。

    import { AnnotationTools, Meta, ToraRouter } from 'tora'
    
    // 这里实现一个向 ToraRouter 添加一个单结果查询的 API 的装饰器。
    export const GetOneApi = AnnotationTools.create_decorator(
        (target: any, meta?: ApiMetaOptions<any>, options?: {
            id_field?: string // 指定需要作为 ID 进行查询的字段。
            projection?: string[] // 指定结果集需要的字段。
        }) => {
    
            // 这里查询需要注入对应的数据库查询服务。
            // 需要注意的是为了进行编译时期检查,这里会检查是否通过装饰器 `Meta` 传入了对应的查询服务。
            if (!meta?.dao) {
                throw new Error(`[dao] not found in meta of ${target}`)
            }
    
            const desc: HandlerDescriptor = {
                // 设置请求方法及请求路径
                method_and_path: { 'POST-get-one': ['POST', 'get-one'] },
                auth: true, // 需要进行认证
                wrap_result: true, // 需要对结果进行默认包裹
                pos: '', // 代码中的位置,调试时可以帮助定位问题
                param_types: [meta.dao, ApiParams], // 注入的实例列表
                property_key: 'get_one',
                handler: async function (dao: BaseMongo<any>, params: ApiParams<{
                    id: string | number
                }>) {
                    // 检查要求 id 字段必须存在
                    const id = params.ensure('id')
    
                    // 索引字段,优先使用直接传入的 key,其次使用 `Meta` 设置的 key,最后使用 `_id`
                    const id_field = options?.id_field ?? meta?.id_field ?? '_id'
                    const queryOptions: DaoQueryOptions<any> = {}
    
                    // 设置查询选项
                    if (options?.projection) {
                        queryOptions.projection = options.projection
                    }
    
                    // 进行查询并返回结果
                    return await dao.getOne({ [id_field]: id }, queryOptions)
                }
            }
    
            // 添加监听函数
            AnnotationTools.add_handler(target.prototype, desc)
        })
    
    /**
     * 像下面这样添加 `GetOneApi` 装饰器,可以向 TestRouter 添加一个 API。
     * 由于 `GetOneApi` 检查了 `Meta` 传入的参数,需要保证 `Meta` 先执行。
     * 也就是说 `Meta` 要在 `GetOneApi` 的下面。
     */
    @GetOneApi()
    @Meta({ id_field: '_id', dao: MongoQueryService })
    @ToraRouter('/')
    class TestRouter {
    
        constructor() {
        }
    }
    

    Type parameters

    • T

    Parameters

    • processor: (constructor: any, meta: any, options?: T) => void

      自定义装饰器的处理函数。

        • (constructor: any, meta: any, options?: T): void
        • Parameters

          • constructor: any
          • meta: any
          • Optional options: T

          Returns void

    Returns (options?: T) => (constructor: any) => void

    decorator 新的装饰器。

      • (options?: T): (constructor: any) => void
      • Parameters

        • Optional options: T

        Returns (constructor: any) => void

          • (constructor: any): void
          • Parameters

            • constructor: any

            Returns void

define_custom_data

  • define_custom_data<T>(proto: any, index: string, data: T): void
  • 添加自定义数据。
    自定义数据是一个挂在目标 Class 原型上的一个对象,可以通过 index 获取对应内容。

    Type parameters

    • T = any

    Parameters

    • proto: any

      Tora 组件类原型

    • index: string

      内容索引

    • data: T

      需要设置的内容

    Returns void

get_custom_data

  • get_custom_data<T>(proto: any, index: string): T | undefined
  • 查询自定义数据。
    自定义数据是一个挂在目标 Class 原型上的一个对象,可以通过 index 获取对应内容。

    Type parameters

    • T

    Parameters

    • proto: any

      Tora 组件类原型

    • index: string

      内容索引

    Returns T | undefined

    data 查询结果

get_param_types

  • get_param_types(proto: any, property_key: string): any[]
  • 获取成员函数参数的类型列表。

    import { ToraRouter } from 'tora'
    
    export function SomeCustomDecorator() {
        return function (target: any, key: string, desc: PropertyDescriptor) {
            const param_types = AnnotationTools.get_param_types(target, key)
            console.log(param_types)
            // do something useful。
        }
    }
    
    @ToraRouter('/')
    class TestRouter {
    
        constructor() {
        }
    
        /**
         * 这里使用上面定义的装饰器,拿到的 param_types 如下:
         * param_types => [
         *     [class SessionContext],
         *     [class ApiParams extends Judgement]
         * ]
         */
        @SomeCustomDeractor()
        async test(
            cs: SessionContext,
            params: ApiParams<{
                a: string
                b: number
            }>
        ) {
            return 'OK'
        }
    }
    

    Parameters

    • proto: any

      Tora 组件类的原型。

    • property_key: string

      成员函数名。

    Returns any[]

    type_list 类型列表。

Generated using TypeDoc