UNPKG

@micro.ts/core

Version:

Microservice framework with Typescript

180 lines (179 loc) 7.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContainerInject = exports.CurrentUser = exports.Connection = exports.Broker = exports.Method = exports.RawRequest = exports.Request = exports.Header = exports.Headers = exports.QueryParam = exports.Query = exports.Params = exports.Param = exports.BodyParam = exports.Body = void 0; const ParamMetadataTypes_1 = require("./types/ParamMetadataTypes"); const BaseDecorators_1 = require("./BaseDecorators"); /** * Injects the Action request body to the handler * @param options options to validate the parameter */ function Body(options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Body, bodyOptions: options || {} }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Body = Body; /** * Injects a single value using the given key on the Action request body root level * @param name Key of the body object to inject * @param options Specify if the field is required */ function BodyParam(name, options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.BodyField, bodyParamOptions: options || {}, name }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.BodyParam = BodyParam; /** * Injects a single value using the given key on the Action request params root level * @param name Key of the param object to inject * @param options Specify if the field is required */ function Param(name, options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.ParamField, singleParamOptions: options || {}, name }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Param = Param; /** * Inject params value of the Action object on the handler * @param options Specify if params object needs to be validated */ function Params(options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Params, paramOptions: options || {} }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Params = Params; /** * Inject query object of the Action object on the handler * @param options Specify if the query object needs to be validated, or required if it should absolutely exist */ function Query(options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Query, queryOptions: options || {} }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Query = Query; function QueryParam(name, options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.QueryField, queryParamOptions: options || {}, name }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.QueryParam = QueryParam; function Headers(options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Header, headerOptions: options || {} }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Headers = Headers; function Header(name, options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.HeaderField, headerParamOptions: options || {}, name }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Header = Header; function Request() { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Request }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Request = Request; function RawRequest() { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.RawRequest }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.RawRequest = RawRequest; function Method() { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Method }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Method = Method; function Broker() { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Broker }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Broker = Broker; /** * Injects the broker connection in the method parameter it decorates */ function Connection() { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.Connection }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.Connection = Connection; /** * currentUserChecker function is called and its result gets injected on the method parameter * @param options */ function CurrentUser(options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.User, currentUserOptions: options || {} }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.CurrentUser = CurrentUser; /** * Specifies that the current handler parameter will be injected from the DI container * @param name * @param options */ function ContainerInject(name, options) { return (target, propertyKey, parameterIndex) => { const newOptions = { decoratorType: ParamMetadataTypes_1.ParamDecoratorType.ContainerInject, containerInjectOptions: options || {}, name }; (0, BaseDecorators_1.registerParamMetadata)(target, propertyKey, parameterIndex, newOptions); }; } exports.ContainerInject = ContainerInject;