@nestia/core
Version:
Super-fast validation decorators of NestJS
94 lines • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SwaggerExample = void 0;
/**
* Attach example values to Swagger documents.
*
* `SwaggerExample` is a namespace of decorators that attach example values
* to controller methods (request/response bodies, parameters), so that
* `@nestia/sdk`'s Swagger generator can populate the `example` / `examples`
* fields of the generated OpenAPI document.
*
* The decorators only affect Swagger document generation. They do not change
* runtime behavior, validation, or SDK function signatures.
*
* @example
*
* ```typescript
* import core from "@nestia/core";
* import { Controller } from "@nestjs/common";
* import typia from "typia";
*
* @Controller("bbs/articles")
* export class BbsArticlesController {
* // Single response example.
* @core.SwaggerExample.Response(typia.random<IBbsArticle>())
* @core.TypedRoute.Post()
* public async create(
* // Multiple named parameter examples plus a default one.
* @core.SwaggerExample.Parameter(typia.random<IBbsArticle.ICreate>())
* @core.SwaggerExample.Parameter("x", typia.random<IBbsArticle.ICreate>())
* @core.SwaggerExample.Parameter("y", typia.random<IBbsArticle.ICreate>())
* @core.TypedBody()
* input: IBbsArticle.ICreate,
* ): Promise<IBbsArticle> {
* // ...
* }
* }
* ```
*
* @author Jeongho Nam - https://github.com/samchon
*/
var SwaggerExample;
(function (SwaggerExample) {
function Response(...args) {
return function SwaggerExampleResponse(_target, _propertyKey, descriptor) {
emplaceValue(emplaceOfResponse(descriptor))(args);
return descriptor;
};
}
SwaggerExample.Response = Response;
function Parameter(...args) {
return function SwaggerExampleParameter(target, propertyKey, index) {
emplaceValue(emplaceOfParameter(target, propertyKey !== null && propertyKey !== void 0 ? propertyKey : "", index))(args);
};
}
SwaggerExample.Parameter = Parameter;
})(SwaggerExample || (exports.SwaggerExample = SwaggerExample = {}));
const emplaceValue = (data) => (args) => {
var _a;
if (args.length === 1)
data.example = args[0];
else {
const key = args[0];
const value = args[1];
(_a = data.examples) !== null && _a !== void 0 ? _a : (data.examples = {});
data.examples[key] = value;
}
};
const emplaceOfResponse = (descriptor) => {
const oldbie = Reflect.getMetadata("nestia/SwaggerExample/Response", descriptor.value);
if (oldbie !== undefined)
return oldbie;
const newbie = {};
Reflect.defineMetadata("nestia/SwaggerExample/Response", newbie, descriptor.value);
return newbie;
};
const emplaceOfParameter = (target, propertyKey, index) => {
const array = emplaceArrayOfParameters(target, propertyKey);
const oldibe = array.find((e) => e.index === index);
if (oldibe !== undefined)
return oldibe;
const data = { index };
array.push(data);
return data;
};
const emplaceArrayOfParameters = (target, propertyKey) => {
const array = Reflect.getMetadata("nestia/SwaggerExample/Parameters", target, propertyKey);
if (array !== undefined)
return array;
const newbie = [];
Reflect.defineMetadata("nestia/SwaggerExample/Parameters", newbie, target, propertyKey);
return newbie;
};
//# sourceMappingURL=SwaggerExample.js.map