Skip to content

Commit 30b3988

Browse files
committed
refactor(json-api-nestjs): some refactoring swagger generator
1 parent f41fe6a commit 30b3988

File tree

25 files changed

+1404
-28
lines changed

25 files changed

+1404
-28
lines changed

.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ DB_LOGGING=1
33

44
DB_USERNAME="postgres"
55
DB_PASSWORD="postgres"
6-
DB_NAME="json-api-db"
6+
#DB_NAME="json-api-db"
7+
DB_NAME="postgres"
78
DB_PORT=5432
89
DB_TYPE=postgres
910

libs/json-api/json-api-nestjs-sdk/src/lib/service/json-api-utils.service.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createEntityInstance } from '@klerick/json-api-nestjs-shared';
2+
13
import {
24
Attributes,
35
Entity as EntityObject,
@@ -260,8 +262,7 @@ export class JsonApiUtilsService {
260262
}
261263

262264
createEntityInstance<E>(name: string): E {
263-
const entityName = kebabToCamel(name);
264-
return Function('return new class ' + entityName + '{}')();
265+
return createEntityInstance<E>(name);
265266
}
266267

267268
private findIncludeEntity<E, R extends MainData<EntityRelation<E>>>(

libs/json-api/json-api-nestjs-shared/src/lib/utils/object-utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { kebabToCamel } from './string-utils';
2+
13
export const ObjectTyped = {
24
keys: Object.keys as <T extends {}>(yourObject: T) => Array<keyof T>,
35
values: Object.values as <U extends {}>(yourObject: U) => Array<U[keyof U]>,
@@ -12,3 +14,8 @@ export const ObjectTyped = {
1214
export function isObject(item: unknown): item is object {
1315
return typeof item === 'object' && !Array.isArray(item) && item !== null;
1416
}
17+
18+
export function createEntityInstance<E>(name: string): E {
19+
const entityName = kebabToCamel(name);
20+
return Function('return new class ' + entityName + '{}')();
21+
}

libs/json-api/json-api-nestjs/src/lib/modules/mixin/mixin.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
ZodInputPatchRelationshipSchema,
2323
ZodInputPostRelationshipSchema,
2424
} from './factory';
25+
import { SwaggerBindService } from './swagger';
2526

2627
export class MixinModule {
2728
static forRoot(options: MixinOptions): DynamicModule {
@@ -75,6 +76,7 @@ export class MixinModule {
7576
ZodQuerySchema(),
7677
ZodPatchSchema(),
7778
ZodPostSchema(),
79+
SwaggerBindService,
7880
ZodInputPatchRelationshipSchema,
7981
ZodInputPostRelationshipSchema,
8082
],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { ApiProperty } from '@nestjs/swagger';
2+
import { FilterOperand as FilterOperandType } from '@klerick/json-api-nestjs-shared';
3+
4+
const title = 'is equal to the conditional of query';
5+
6+
export const OperandsMapTitle = {
7+
[FilterOperandType.in]: `${title} "WHERE 'attribute_name' IN ('value1', 'value2')"`,
8+
[FilterOperandType.nin]: `${title} "WHERE 'attribute_name' NOT IN ('value1', 'value1')"`,
9+
[FilterOperandType.eq]: `${title} "WHERE 'attribute_name' = 'value1'`,
10+
[FilterOperandType.ne]: `${title} "WHERE 'attribute_name' <> 'value1'`,
11+
[FilterOperandType.gt]: `${title} "WHERE 'attribute_name' > 'value1'`,
12+
[FilterOperandType.gte]: `${title} "WHERE 'attribute_name' >= 'value1'`,
13+
[FilterOperandType.like]: `${title} "WHERE 'attribute_name' ILIKE %value1%`,
14+
[FilterOperandType.lt]: `${title} "WHERE 'attribute_name' < 'value1'`,
15+
[FilterOperandType.lte]: `${title} "WHERE 'attribute_name' <= 'value1'`,
16+
[FilterOperandType.regexp]: `${title} "WHERE 'attribute_name' ~* value1`,
17+
[FilterOperandType.some]: `${title} "WHERE 'attribute_name' && [value1]`,
18+
};
19+
20+
export class FilterOperand {
21+
@ApiProperty({
22+
title: OperandsMapTitle[FilterOperandType.in],
23+
required: false,
24+
type: 'array',
25+
items: {
26+
type: 'string',
27+
},
28+
})
29+
[FilterOperandType.in]!: string[];
30+
31+
@ApiProperty({
32+
title: OperandsMapTitle[FilterOperandType.nin],
33+
required: false,
34+
type: 'array',
35+
items: {
36+
type: 'string',
37+
},
38+
})
39+
[FilterOperandType.nin]!: string[];
40+
41+
@ApiProperty({
42+
title: OperandsMapTitle[FilterOperandType.eq],
43+
required: false,
44+
})
45+
[FilterOperandType.eq]!: string;
46+
@ApiProperty({
47+
title: OperandsMapTitle[FilterOperandType.ne],
48+
required: false,
49+
})
50+
[FilterOperandType.ne]!: string;
51+
52+
@ApiProperty({
53+
title: OperandsMapTitle[FilterOperandType.gte],
54+
required: false,
55+
})
56+
[FilterOperandType.gte]!: string;
57+
@ApiProperty({
58+
title: OperandsMapTitle[FilterOperandType.gt],
59+
required: false,
60+
})
61+
[FilterOperandType.gt]!: string;
62+
63+
@ApiProperty({
64+
title: OperandsMapTitle[FilterOperandType.lt],
65+
required: false,
66+
})
67+
[FilterOperandType.lt]!: string;
68+
@ApiProperty({
69+
title: OperandsMapTitle[FilterOperandType.lte],
70+
required: false,
71+
})
72+
[FilterOperandType.lte]!: string;
73+
74+
@ApiProperty({
75+
title: OperandsMapTitle[FilterOperandType.regexp],
76+
required: false,
77+
})
78+
[FilterOperandType.regexp]!: string;
79+
@ApiProperty({
80+
title: OperandsMapTitle[FilterOperandType.some],
81+
required: false,
82+
})
83+
[FilterOperandType.some]!: string;
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './swagger-bind.service';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { EntityClass, EntityTarget, ObjectLiteral } from '../../../../types';
2+
import { Type } from '@nestjs/common';
3+
import { EntityProps, TypeField, ZodParams } from '../../types';
4+
import { ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
5+
import { errorSchema } from '../utils';
6+
7+
export function deleteOne<E extends ObjectLiteral>(
8+
controller: Type<any>,
9+
descriptor: PropertyDescriptor,
10+
entity: EntityClass<E>,
11+
zodParams: ZodParams<E, EntityProps<E>, string>,
12+
methodName: string
13+
) {
14+
const entityName = entity.name;
15+
16+
const { typeId } = zodParams;
17+
18+
ApiParam({
19+
name: 'id',
20+
required: true,
21+
type: typeId === TypeField.number ? 'integer' : 'string',
22+
description: `ID of resource "${entityName}"`,
23+
})(controller, methodName, descriptor);
24+
25+
ApiOperation({
26+
summary: `Delete item of resource "${entityName}"`,
27+
operationId: `${controller.constructor.name}_${methodName}`,
28+
})(controller, methodName, descriptor);
29+
30+
ApiResponse({
31+
status: 404,
32+
description: `Item of resource "${entityName}" not found`,
33+
schema: errorSchema,
34+
})(controller, methodName, descriptor);
35+
36+
ApiResponse({
37+
status: 204,
38+
description: `Item of resource "${entityName}" has been deleted`,
39+
})(controller, methodName, descriptor);
40+
41+
ApiResponse({
42+
status: 400,
43+
description: 'Wrong query parameters',
44+
schema: errorSchema,
45+
})(controller, methodName, descriptor);
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { EntityClass, ObjectLiteral } from '../../../../types';
2+
import { ApiBody, ApiOperation, ApiParam, ApiResponse } from '@nestjs/swagger';
3+
import { Type } from '@nestjs/common';
4+
5+
import { EntityProps, TypeField, ZodParams } from '../../types';
6+
import { zodPatchRelationship } from '../../zod';
7+
import { errorSchema } from '../utils';
8+
import { generateSchema } from '@anatine/zod-openapi';
9+
import {
10+
ReferenceObject,
11+
SchemaObject,
12+
} from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
13+
14+
export function deleteRelationship<E extends ObjectLiteral>(
15+
controller: Type<any>,
16+
descriptor: PropertyDescriptor,
17+
entity: EntityClass<E>,
18+
zodParams: ZodParams<E, EntityProps<E>, string>,
19+
methodName: string
20+
) {
21+
const entityName = entity.name;
22+
23+
const {
24+
entityFieldsStructure: { relations },
25+
typeId,
26+
} = zodParams;
27+
28+
ApiOperation({
29+
summary: `Delete list of relation for resource "${entityName}"`,
30+
operationId: `${controller.name}_${methodName}`,
31+
})(controller, methodName, descriptor);
32+
33+
ApiParam({
34+
name: 'id',
35+
required: true,
36+
type: typeId === TypeField.number ? 'integer' : 'string',
37+
description: `ID of resource "${entityName}"`,
38+
})(controller, methodName, descriptor);
39+
40+
ApiParam({
41+
name: 'relName',
42+
required: true,
43+
type: 'string',
44+
enum: relations,
45+
description: `Relation name of resource "${entityName}"`,
46+
})(controller, methodName, descriptor);
47+
48+
ApiBody({
49+
description: `Json api schema for delete "${entityName}" item`,
50+
schema: generateSchema(zodPatchRelationship) as
51+
| SchemaObject
52+
| ReferenceObject,
53+
required: true,
54+
})(controller, methodName, descriptor);
55+
56+
ApiResponse({
57+
status: 400,
58+
description: 'Wrong url parameters',
59+
schema: errorSchema,
60+
})(controller, methodName, descriptor);
61+
62+
ApiResponse({
63+
status: 422,
64+
description: 'Incorrect type for relation',
65+
schema: errorSchema,
66+
})(controller, methodName, descriptor);
67+
68+
ApiResponse({
69+
status: 404,
70+
description: 'Resource not found ',
71+
schema: errorSchema,
72+
})(controller, methodName, descriptor);
73+
74+
ApiResponse({
75+
status: 204,
76+
description: `Item/s of relation for "${entityName}" has been deleted`,
77+
})(controller, methodName, descriptor);
78+
}

0 commit comments

Comments
 (0)