Skip to content

Commit

Permalink
feat: define swagger for createMany
Browse files Browse the repository at this point in the history
  • Loading branch information
jiho-kr committed Aug 19, 2023
1 parent a39e9e6 commit 14c3791
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 27 deletions.
20 changes: 19 additions & 1 deletion spec/exclude-swagger/exclude-swagger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,25 @@ describe('exclude swagger by route', () => {
requestBody: {
description: 'CreateBaseDto',
required: true,
content: { 'application/json': { schema: { $ref: '#/components/schemas/CreateBaseBodyDto' } } },
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/CreateBaseBodyDto',
anyOf: [
{
$ref: '#/components/schemas/CreateBaseBodyDto',
},
{
items: {
$ref: '#/components/schemas/CreateBaseBodyDto',
},
type: 'array',
},
],
type: 'object',
},
},
},
},
});
});
Expand Down
91 changes: 65 additions & 26 deletions src/lib/crud.route.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ import {
import { CrudLogger } from './provider/crud-logger';
import { CrudReadManyRequest } from './request';

type ParameterDecorators =
| {
name?: string;
description?: string;
required: boolean;
in: string;
items?: { type: string };
type: unknown;
isArray?: boolean;
}
| {
name?: any;
description?: string;
required: boolean;
in: string;
type: unknown;
schema: {
[type: string]: unknown;
};
};

export class CrudRouteFactory {
private crudLogger: CrudLogger;
private entity: {
Expand Down Expand Up @@ -244,15 +265,13 @@ export class CrudRouteFactory {
}

private defineParameterSwagger(method: Method, params: string[], target: Object) {
const parameterDecorators: Array<{
name?: string;
description?: string;
required: boolean;
in: string;
items?: { type: string };
type: unknown;
isArray?: boolean;
}> = params.map((param) => ({ name: param, required: true, in: 'path', type: String, isArray: false }));
const parameterDecorators: ParameterDecorators[] = params.map((param) => ({
name: param,
required: true,
in: 'path',
type: String,
isArray: false,
}));

if (method === Method.READ_MANY) {
parameterDecorators.push(
Expand Down Expand Up @@ -285,23 +304,43 @@ export class CrudRouteFactory {
});
}
if (CRUD_POLICY[method].useBody) {
parameterDecorators.push({
description: [capitalizeFirstLetter(method), capitalizeFirstLetter(this.tableName), 'Dto'].join(''),
required: true,
in: 'body',
type: (() => {
if (method === Method.SEARCH) {
return RequestSearchDto;
}
const routeConfig = this.crudOptions.routes?.[method];
if (routeConfig?.swagger && 'body' in routeConfig.swagger) {
return this.generalTypeGuard(routeConfig.swagger['body']!, method, 'body');
}

return CreateRequestDto(this.crudOptions.entity, method);
})(),
isArray: false,
});
const bodyType = (() => {
if (method === Method.SEARCH) {
return RequestSearchDto;
}
const routeConfig = this.crudOptions.routes?.[method];
if (routeConfig?.swagger && 'body' in routeConfig.swagger) {
return this.generalTypeGuard(routeConfig.swagger['body']!, method, 'body');
}

return CreateRequestDto(this.crudOptions.entity, method);
})();

parameterDecorators.push(
method === Method.CREATE
? {
description: [capitalizeFirstLetter(method), capitalizeFirstLetter(this.tableName), 'Dto'].join(''),
required: true,
in: 'body',
type: bodyType,
schema: {
type: 'object',
anyOf: [
{
$ref: `#/components/schemas/${bodyType.name}`,
},
{ type: 'array', items: { $ref: `#/components/schemas/${bodyType.name}` } },
],
},
}
: {
description: [capitalizeFirstLetter(method), capitalizeFirstLetter(this.tableName), 'Dto'].join(''),
required: true,
in: 'body',
type: bodyType,
isArray: false,
},
);
}
Reflect.defineMetadata(DECORATORS.API_PARAMETERS, parameterDecorators, target);
}
Expand Down

0 comments on commit 14c3791

Please sign in to comment.