UNPKG

@tsed/schema

Version:
76 lines (75 loc) 2.4 kB
import { decorateMethodsOf, DecoratorTypes, UnsupportedDecoratorType } from "@tsed/core"; import { JsonEntityStore } from "../../domain/JsonEntityStore.js"; import { JsonParameter } from "../../domain/JsonParameter.js"; import { JsonSchema } from "../../domain/JsonSchema.js"; /** * Add a input parameter. * * ::: warning * Don't use decorator with Ts.ED application to decorate parameters. Use @@BodyParams@@, @@PathParams@@, etc... instead. * But you can use this decorator on Method, to add extra in parameters like Authorization header. * * ```typescript * @Controller("/") * class MyController { * @Get("/") * @In("header").Type(String).Name("Authorization").Required() * method() { * } * } * ``` * ::: * * @param inType * @decorator * @swagger * @schema * @input * @operation */ export function In(inType) { const jsonParameter = new JsonParameter(); const schema = {}; const decorator = (target, propertyKey, index) => { const store = JsonEntityStore.from(target, propertyKey, index); switch (store.decoratorType) { case DecoratorTypes.PARAM: store.parameter.in(inType); break; case DecoratorTypes.METHOD: jsonParameter.in(inType); store.operation.addParameter(-1, jsonParameter); jsonParameter.schema(JsonSchema.from(schema)); break; case DecoratorTypes.CLASS: decorateMethodsOf(target, decorator); break; default: throw new UnsupportedDecoratorType(In, [target, propertyKey, index]); } }; decorator.Type = (type) => { schema.type = type; return decorator; }; decorator.Name = (name) => { jsonParameter.name(name); return decorator; }; decorator.Description = (description) => { jsonParameter.description(description); return decorator; }; decorator.Required = (required = true) => { jsonParameter.required(required); return decorator; }; decorator.Pattern = (pattern) => { return decorator.Schema({ pattern: pattern.toString() }); }; decorator.Schema = (_schema) => { Object.assign(schema, _schema.toJSON ? _schema.toJSON() : _schema); return decorator; }; return decorator; }