UNPKG

@gabliam/web-core

Version:
98 lines (97 loc) 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Next = exports.Cookies = exports.RequestHeaders = exports.RequestBody = exports.QueryParam = exports.RequestParam = exports.Response = exports.Request = exports.Context = exports.ExecContext = exports.makeWebParamDecorator = void 0; const core_1 = require("@gabliam/core"); const lodash_1 = require("lodash"); const constants_1 = require("../constants"); /** * Function for create you own param decorator * @usageNotes * * You must passed a type and the handler * Type is mandatory, this param is used for introspection of parameters (swagger by ex) * * Sample to extract the user in query * * ```typescript * const User = makeWebParamDecorator('user', (args, ctx) => ctx.request.user) * * @Controller('/') * class SampleController { * @Get('/') * hello(@User() user: UserModel) { * return 'Hello'; * } * } * ``` */ const makeWebParamDecorator = (type, handler) => (0, core_1.makeParamDecorator)(constants_1.METADATA_KEY.controllerParameter, (...args) => ({ args, handler, type })); exports.makeWebParamDecorator = makeWebParamDecorator; exports.ExecContext = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.EXEC_CONTEXT, (_, __, ___, execCtx) => execCtx); exports.Context = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.CONTEXT, ([path], ctx) => { if (path) { return (0, lodash_1.get)(ctx, path); } return ctx; }); exports.Request = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.REQUEST, ([path], ctx) => { if (path) { return (0, lodash_1.get)(ctx.request, path); } return ctx.request; }); exports.Response = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.RESPONSE, ([path], ctx) => { if (path) { return (0, lodash_1.get)(ctx.response, path); } return ctx.response; }); exports.RequestParam = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.PARAMS, ([path], ctx, type) => { let res = ctx.request.params; if (path) { res = (0, lodash_1.get)(ctx.request.params, path); } if (res && type && type.name === 'Number') { try { // parseFloat for compatibility with integer and float res = Number.parseFloat(res); // eslint-disable-next-line no-empty } catch (_a) { } } return res; }); exports.QueryParam = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.QUERY, ([path], ctx, type) => { let res = ctx.request.query; if (path) { res = (0, lodash_1.get)(ctx.request.query, path); } if (res && type && type.name === 'Number') { try { // parseFloat for compatibility with integer and float res = Number.parseFloat(res); // eslint-disable-next-line no-empty } catch (_a) { } } return res; }); exports.RequestBody = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.BODY, ([path], ctx) => { if (path) { return (0, lodash_1.get)(ctx.request.body, path); } return ctx.request.body; }); exports.RequestHeaders = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.HEADERS, ([path], ctx) => { if (path) { return (0, lodash_1.get)(ctx.request.headers, path); } return ctx.request.headers; }); exports.Cookies = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.COOKIES, ([path], ctx) => { if (path) { return ctx.cookies.get(path); } return ctx.cookies; }); exports.Next = (0, exports.makeWebParamDecorator)(constants_1.PARAMETER_TYPE.NEXT, (_, __, ___, ____, next) => next);