{"version":3,"file":"index.mjs","names":[],"sources":["../src/parameter/constants.ts","../src/utils/object.ts","../src/utils/handler.ts","../src/utils/meta.ts","../src/parameter/module.ts","../src/method/arguments.ts","../src/method/module.ts","../src/mount.ts","../src/module.ts","../src/class/module.ts","../src/mixed/module.ts","../src/index.ts"],"sourcesContent":["export enum ParameterType {\n    CONTEXT = 'context',\n    RESPONSE = 'response',\n    REQUEST = 'request',\n    NEXT = 'next',\n    PARAM = 'param',\n    HEADER = 'header',\n\n    COOKIE = 'cookie',\n    QUERY = 'query',\n    BODY = 'body',\n}\n","export function isObject(item: unknown) : item is Record<string, any> {\n    return (\n        !!item &&\n        typeof item === 'object' &&\n        !Array.isArray(item)\n    );\n}\n","import type { CoreHandlerOptions } from 'routup';\nimport { defineCoreHandler } from 'routup';\nimport {\n    isObject,\n} from './object';\nimport type { ClassType, HandlerInterface } from '../type';\n\nexport function isHandlerClassInstance(input: unknown) : input is HandlerInterface {\n    return isObject(input) &&\n        typeof (input as any).run === 'function';\n}\n\nexport function createHandlerForClassType(\n    item: ClassType,\n    options: Partial<CoreHandlerOptions>,\n) {\n    return defineCoreHandler({\n        ...options,\n        fn: (event) => {\n            const middle = new (item as ClassType)();\n\n            if (isHandlerClassInstance(middle)) {\n                return middle.run(event);\n            }\n\n            /* istanbul ignore next */\n            return middle(event);\n        },\n    });\n}\n","import type { DecoratorMeta } from '../type';\n\nconst symbol = Symbol.for('@routup/decorators:DecoratorMeta');\n\nexport function useDecoratorMeta(target: Record<string, any>) : DecoratorMeta {\n    if (symbol in target) {\n        return (target as any)[symbol];\n    }\n\n    (target as any)[symbol] = {\n        url: '',\n        middlewares: [],\n        methods: {},\n        parameters: {},\n    } as DecoratorMeta;\n\n    return (target as any)[symbol];\n}\n","import { useDecoratorMeta } from '../utils';\nimport { ParameterType } from './constants';\n\nexport function createParameterDecorator(\n    type: `${ParameterType}`,\n    property?: string,\n) :ParameterDecorator {\n    return (\n        target: any,\n        propertyKey: string | symbol | undefined,\n        parameterIndex: number,\n    ) => {\n        /* istanbul ignore next */\n        if (typeof propertyKey !== 'string') {\n            return;\n        }\n\n        const meta = useDecoratorMeta(target);\n        if (typeof meta.parameters[propertyKey] === 'undefined') {\n            meta.parameters[propertyKey] = [];\n        }\n\n        meta.parameters[propertyKey].push({\n            index: parameterIndex,\n            property,\n            type,\n        });\n    };\n}\n\nexport function DContext() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.CONTEXT);\n}\n\nexport function DRequest() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.REQUEST);\n}\n\nexport function DResponse() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.RESPONSE);\n}\n\nexport function DNext() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.NEXT);\n}\n\n// useRequestParams\nexport function DPaths() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.PARAM);\n}\n\nexport function DPath(property: string) : ParameterDecorator {\n    return createParameterDecorator(ParameterType.PARAM, property);\n}\n\nexport function DHeaders() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.HEADER);\n}\n\nexport function DHeader(property: string) : ParameterDecorator {\n    return createParameterDecorator(ParameterType.HEADER, property);\n}\n\nexport function DBody(property?: string) : ParameterDecorator {\n    return createParameterDecorator(ParameterType.BODY, property);\n}\n\nexport function DQuery(property?: string) : ParameterDecorator {\n    return createParameterDecorator(ParameterType.QUERY, property);\n}\n\nexport function DCookies() : ParameterDecorator {\n    return createParameterDecorator(ParameterType.COOKIE);\n}\n\nexport function DCookie(property: string) : ParameterDecorator {\n    return createParameterDecorator(ParameterType.COOKIE, property);\n}\n","import { readRequestBody } from '@routup/body';\nimport { useRequestCookie, useRequestCookies } from '@routup/cookie';\nimport { useRequestQuery } from '@routup/query';\nimport { ParameterType } from '../parameter';\nimport type { DecoratorParameterOptions } from '../parameter';\nimport type { HandlerContext } from '../type';\n\nexport async function buildDecoratorMethodArguments(\n    context: HandlerContext,\n    parameters: DecoratorParameterOptions[],\n): Promise<any[]> {\n    /* istanbul ignore next */\n    if (\n        !Array.isArray(parameters) ||\n        parameters.length === 0\n    ) {\n        return [context.event];\n    }\n\n    const items: unknown[] = [];\n\n    for (const parameter of parameters) {\n        if (parameter.type === ParameterType.CONTEXT) {\n            items[parameter.index] = context.event;\n            continue;\n        }\n\n        if (parameter.type === ParameterType.REQUEST) {\n            items[parameter.index] = context.event.request;\n            continue;\n        }\n\n        if (parameter.type === ParameterType.RESPONSE) {\n            items[parameter.index] = context.event.response;\n            continue;\n        }\n\n        if (parameter.type === ParameterType.NEXT) {\n            items[parameter.index] = context.event.next;\n            continue;\n        }\n\n        if (parameter.type === ParameterType.PARAM) {\n            if (parameter.property) {\n                items[parameter.index] = context.event.params[parameter.property];\n            } else {\n                items[parameter.index] = context.event.params;\n            }\n            continue;\n        }\n\n        if (parameter.type === ParameterType.HEADER) {\n            if (parameter.property) {\n                items[parameter.index] = context.event.headers.get(parameter.property.toLowerCase());\n            } else {\n                items[parameter.index] = context.event.headers;\n            }\n            continue;\n        }\n\n        if (parameter.type === ParameterType.BODY) {\n            items[parameter.index] = parameter.property ?\n                await readRequestBody(context.event, parameter.property) :\n                await readRequestBody(context.event);\n            continue;\n        }\n\n        if (parameter.type === ParameterType.QUERY) {\n            items[parameter.index] = parameter.property ?\n                useRequestQuery(context.event, parameter.property) :\n                useRequestQuery(context.event);\n            continue;\n        }\n\n        if (parameter.type === ParameterType.COOKIE) {\n            items[parameter.index] = parameter.property ?\n                useRequestCookie(context.event, parameter.property) :\n                useRequestCookies(context.event);\n            continue;\n        }\n\n        throw new SyntaxError(`Parameter ${parameter.type} could not be extracted.`);\n    }\n\n    return items;\n}\n","import type { ClassType } from '../type';\nimport { useDecoratorMeta } from '../utils';\n\nexport function createMethodDecorator(\n    method: string,\n    url: string,\n    handlers?: ClassType[],\n) : MethodDecorator {\n    return (\n        target: any,\n        propertyKey: string | symbol,\n        descriptor: TypedPropertyDescriptor<any>,\n    ) : void => {\n        /* istanbul ignore next */\n        if (typeof propertyKey !== 'string') {\n            return;\n        }\n\n        const meta = useDecoratorMeta(target);\n\n        meta.methods[propertyKey] = {\n            method,\n            url,\n            middlewares: handlers || [],\n        };\n    };\n}\n\nexport function DDelete(url: string, handlers?: ClassType[]) : MethodDecorator {\n    return createMethodDecorator('delete', url, handlers);\n}\n\nexport function DGet(url: string, handlers?: ClassType[]) : MethodDecorator {\n    return createMethodDecorator('get', url, handlers);\n}\n\nexport function DPost(url: string, handlers?: ClassType[]) : MethodDecorator {\n    return createMethodDecorator('post', url, handlers);\n}\n\nexport function DPut(url: string, handlers?: ClassType[]) : MethodDecorator {\n    return createMethodDecorator('put', url, handlers);\n}\n\nexport function DPatch(url: string, handlers?: ClassType[]) : MethodDecorator {\n    return createMethodDecorator('patch', url, handlers);\n}\n","import type { IApp, MethodName } from 'routup';\nimport { App, defineCoreHandler } from 'routup';\nimport { buildDecoratorMethodArguments } from './method';\nimport type { ClassType, DecoratorMeta } from './type';\nimport { createHandlerForClassType, isObject, useDecoratorMeta } from './utils';\n\nfunction buildControllerApp(\n    controller: Record<string, any>,\n    meta: DecoratorMeta,\n): App {\n    const childApp = new App();\n\n    for (let i = 0; i < meta.middlewares.length; i++) {\n        const handler = createHandlerForClassType(meta.middlewares[i]!, {});\n\n        childApp.use(handler);\n    }\n\n    const propertyKeys = Object.keys(meta.methods);\n    for (const propertyKey of propertyKeys) {\n        const method = meta.methods[propertyKey];\n        if (!method) {\n            continue;\n        }\n\n        if (method.middlewares) {\n            for (let i = 0; i < method.middlewares.length; i++) {\n                childApp.use(createHandlerForClassType(\n                    method.middlewares[i]!,\n                    {\n                        method: method.method as MethodName,\n                        path: method.url,\n                    },\n                ));\n            }\n        }\n\n        const handler = defineCoreHandler({\n            method: method.method as MethodName,\n            path: method.url,\n            fn: async (event) => {\n                const args = await buildDecoratorMethodArguments(\n                    { event },\n                    meta.parameters[propertyKey] ?? [],\n                );\n\n                return controller[propertyKey](...args);\n            },\n        });\n\n        childApp.use(handler);\n    }\n\n    return childApp;\n}\n\nexport function mountController(\n    router: IApp,\n    input: (ClassType | Record<string, any>),\n) {\n    let controller : Record<string, any>;\n\n    if (isObject(input)) {\n        controller = input;\n    } else {\n        controller = new (input as ClassType)();\n    }\n\n    const meta = useDecoratorMeta(controller);\n\n    if (Array.isArray(meta.url)) {\n        const childApp = buildControllerApp(controller, meta);\n        // `router.use(url, childApp)` snapshots `childApp.routes` per\n        // call (flatten-on-use), so mounting the same child under\n        // multiple paths needs no clone — each mount gets its own\n        // copy of the routes with the right prefix.\n        for (const url of meta.url) {\n            router.use(url, childApp);\n        }\n    } else {\n        router.use(meta.url, buildControllerApp(controller, meta));\n    }\n}\n\nexport function mountControllers(\n    router: IApp,\n    input: (ClassType | Record<string, any>)[],\n) {\n    for (const element of input) {\n        mountController(router, element);\n    }\n}\n","import type { Plugin } from 'routup';\nimport { mountControllers } from './mount';\nimport type { Options } from './type';\n\nexport function decorators(options: Options) : Plugin {\n    return {\n        name: 'decorators',\n        install: (router) => {\n            mountControllers(\n                router,\n                options.controllers,\n            );\n        },\n    };\n}\n","import type { ClassType } from '../type';\nimport { useDecoratorMeta } from '../utils';\n\nexport function createClassDecorator(\n    url: string | string[],\n    middlewares?: ClassType[],\n) : ClassDecorator {\n    return (target: any) : void => {\n        const meta = useDecoratorMeta(target.prototype);\n\n        meta.url = url;\n        meta.middlewares = [];\n\n        if (Array.isArray(middlewares)) {\n            meta.middlewares = [\n                ...meta.middlewares,\n                ...middlewares,\n            ];\n        }\n    };\n}\n\nexport function DController(\n    url: string | string[],\n    middlewares?: ClassType[],\n) : ClassDecorator {\n    return createClassDecorator(url, middlewares);\n}\n","export function DDeprecated() : any {\n    return () => {};\n}\nexport function DDescription<T>(name: string | number, description?: string, example?: T): any {\n    return () => { };\n}\n\nexport function DHidden() {\n    return () => {};\n}\n\nexport function DExample<T>(example: any) {\n    return (...args: any) => {};\n}\n\nexport function DSecurity(roles?: string | string[], name?: string) {\n    return (...args: any) => { };\n}\n\nexport function DTags(...names: string[]) {\n    return (...args: any) => { };\n}\n","import {\n    decorators,\n} from './module';\n\nexport * from './class';\nexport * from './mixed';\nexport * from './method';\nexport * from './module';\nexport * from './mount';\nexport * from './parameter';\nexport * from './type';\nexport * from './utils';\n\nexport default decorators;\n"],"mappings":";;;;;AAAA,IAAY,gBAAL,yBAAA,eAAA;CACH,cAAA,aAAA;CACA,cAAA,cAAA;CACA,cAAA,aAAA;CACA,cAAA,UAAA;CACA,cAAA,WAAA;CACA,cAAA,YAAA;CAEA,cAAA,YAAA;CACA,cAAA,WAAA;CACA,cAAA,UAAA;;AACJ,EAAA,CAAA,CAAA;;;ACXA,SAAgB,SAAS,MAA6C;CAClE,OACI,CAAC,CAAC,QACF,OAAO,SAAS,YAChB,CAAC,MAAM,QAAQ,IAAI;AAE3B;;;ACCA,SAAgB,uBAAuB,OAA4C;CAC/E,OAAO,SAAS,KAAK,KACjB,OAAQ,MAAc,QAAQ;AACtC;AAEA,SAAgB,0BACZ,MACA,SACF;CACE,OAAO,kBAAkB;EACrB,GAAG;EACH,KAAK,UAAU;GACX,MAAM,SAAS,IAAK,KAAmB;GAEvC,IAAI,uBAAuB,MAAM,GAC7B,OAAO,OAAO,IAAI,KAAK;;GAI3B,OAAO,OAAO,KAAK;EACvB;CACJ,CAAC;AACL;;;AC3BA,MAAM,SAAS,OAAO,IAAI,kCAAkC;AAE5D,SAAgB,iBAAiB,QAA6C;CAC1E,IAAI,UAAU,QACV,OAAQ,OAAe;CAG3B,OAAgB,UAAU;EACtB,KAAK;EACL,aAAa,CAAC;EACd,SAAS,CAAC;EACV,YAAY,CAAC;CACjB;CAEA,OAAQ,OAAe;AAC3B;;;ACdA,SAAgB,yBACZ,MACA,UACkB;CAClB,QACI,QACA,aACA,mBACC;;EAED,IAAI,OAAO,gBAAgB,UACvB;EAGJ,MAAM,OAAO,iBAAiB,MAAM;EACpC,IAAI,OAAO,KAAK,WAAW,iBAAiB,aACxC,KAAK,WAAW,eAAe,CAAC;EAGpC,KAAK,WAAW,aAAa,KAAK;G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