Skip to content

Commit 543097a

Browse files
committed
fix(json-api-nestjs): replace deprecated faker methods, enhance zod schemas with transformations and update constants with METHOD_NAME. change name for method function
1 parent 3c6531c commit 543097a

File tree

9 files changed

+133
-24
lines changed

9 files changed

+133
-24
lines changed

libs/json-api/json-api-nestjs/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export {
3232
DEFAULT_PAGE_SIZE,
3333
DEFAULT_QUERY_PAGE,
3434
CURRENT_ENTITY,
35+
METHOD_NAME
3536
} from './lib/constants';
3637
export {
3738
OrmService,
@@ -41,6 +42,7 @@ export {
4142
FindOneRowEntity,
4243
RunInTransaction,
4344
EntityParamMap,
45+
MethodName
4446
} from './lib/modules/mixin/types';
4547
export {
4648
PatchData,

libs/json-api/json-api-nestjs/src/lib/constants/constants.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { MethodName } from '../modules/mixin/types';
2+
13
export const JSON_API_CONTROLLER_POSTFIX = 'JsonApiController';
24
export const JSON_API_MODULE_POSTFIX = 'JsonApiModule';
35
export const DEFAULT_CONNECTION_NAME = 'default';
@@ -8,3 +10,16 @@ export const DESC = 'DESC';
810
export const ASC = 'ASC';
911

1012
export const SORT_TYPE = [DESC, ASC] as const;
13+
export const METHOD_NAME: {
14+
[P in MethodName]: P;
15+
} = {
16+
getAll: 'getAll',
17+
getOne: 'getOne',
18+
deleteOne: 'deleteOne',
19+
deleteRelationship: 'deleteRelationship',
20+
postOne: 'postOne',
21+
getRelationship: 'getRelationship',
22+
patchOne: 'patchOne',
23+
patchRelationship: 'patchRelationship',
24+
postRelationship: 'postRelationship',
25+
};

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { Body, Param, Query, RequestMethod } from '@nestjs/common';
22

33
import { BindingsConfig } from '../types';
44
import { JsonBaseController } from '../controllers/json-base.controller';
5-
import { PARAMS_RELATION_NAME, PARAMS_RESOURCE_ID } from '../../../constants';
5+
import {
6+
METHOD_NAME,
7+
PARAMS_RELATION_NAME,
8+
PARAMS_RESOURCE_ID,
9+
} from '../../../constants';
610

711
import {
812
queryInputMixin,
@@ -19,7 +23,7 @@ import {
1923
} from '../pipe';
2024

2125
const Bindings: BindingsConfig = {
22-
getAll: {
26+
[METHOD_NAME.getAll]: {
2327
method: RequestMethod.GET,
2428
name: 'getAll',
2529
path: '/',
@@ -36,7 +40,7 @@ const Bindings: BindingsConfig = {
3640
},
3741
],
3842
},
39-
getOne: {
43+
[METHOD_NAME.getOne]: {
4044
method: RequestMethod.GET,
4145
name: 'getOne',
4246
path: `:${PARAMS_RESOURCE_ID}`,
@@ -58,7 +62,7 @@ const Bindings: BindingsConfig = {
5862
},
5963
],
6064
},
61-
deleteOne: {
65+
[METHOD_NAME.deleteOne]: {
6266
method: RequestMethod.DELETE,
6367
name: 'deleteOne',
6468
path: `:${PARAMS_RESOURCE_ID}`,
@@ -71,7 +75,7 @@ const Bindings: BindingsConfig = {
7175
},
7276
],
7377
},
74-
postOne: {
78+
[METHOD_NAME.postOne]: {
7579
method: RequestMethod.POST,
7680
name: 'postOne',
7781
path: '/',
@@ -83,7 +87,7 @@ const Bindings: BindingsConfig = {
8387
},
8488
],
8589
},
86-
patchOne: {
90+
[METHOD_NAME.patchOne]: {
8791
method: RequestMethod.PATCH,
8892
name: 'patchOne',
8993
path: `:${PARAMS_RESOURCE_ID}`,
@@ -100,7 +104,7 @@ const Bindings: BindingsConfig = {
100104
},
101105
],
102106
},
103-
getRelationship: {
107+
[METHOD_NAME.getRelationship]: {
104108
path: `:${PARAMS_RESOURCE_ID}/relationships/:${PARAMS_RELATION_NAME}`,
105109
name: 'getRelationship',
106110
method: RequestMethod.GET,
@@ -118,7 +122,7 @@ const Bindings: BindingsConfig = {
118122
},
119123
],
120124
},
121-
postRelationship: {
125+
[METHOD_NAME.postRelationship]: {
122126
path: `:${PARAMS_RESOURCE_ID}/relationships/:${PARAMS_RELATION_NAME}`,
123127
name: 'postRelationship',
124128
method: RequestMethod.POST,
@@ -140,7 +144,7 @@ const Bindings: BindingsConfig = {
140144
},
141145
],
142146
},
143-
deleteRelationship: {
147+
[METHOD_NAME.deleteRelationship]: {
144148
path: `:${PARAMS_RESOURCE_ID}/relationships/:${PARAMS_RELATION_NAME}`,
145149
name: 'deleteRelationship',
146150
method: RequestMethod.DELETE,
@@ -162,7 +166,7 @@ const Bindings: BindingsConfig = {
162166
},
163167
],
164168
},
165-
patchRelationship: {
169+
[METHOD_NAME.patchRelationship]: {
166170
path: `:${PARAMS_RESOURCE_ID}/relationships/:${PARAMS_RELATION_NAME}`,
167171
name: 'patchRelationship',
168172
method: RequestMethod.PATCH,

libs/json-api/json-api-nestjs/src/lib/modules/mixin/helpers/bind-controller.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,22 @@ export function bindController(
4141

4242
if (!Object.prototype.hasOwnProperty.call(controller.prototype, name)) {
4343
// need uniq descriptor for correct work swagger
44+
45+
const func = function (this: typeof controller.prototype,
46+
...arg: Parameters<typeof implementation>
47+
): ReturnType<typeof implementation> {
48+
return this.constructor.__proto__.prototype[name].call(this, ...arg);
49+
};
50+
51+
Object.defineProperty(func, 'name', {
52+
value: name,
53+
writable: false,
54+
enumerable: false,
55+
configurable: true
56+
});
57+
4458
Reflect.defineProperty(controller.prototype, name, {
45-
value: function (
46-
...arg: Parameters<typeof implementation>
47-
): ReturnType<typeof implementation> {
48-
return this.constructor.__proto__.prototype[name].call(this, ...arg);
49-
},
59+
value: func,
5060
writable: true,
5161
enumerable: false,
5262
configurable: true,

libs/json-api/json-api-nestjs/src/lib/modules/mixin/helpers/create-controller.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { createController } from './create-controller';
88
import { JsonBaseController } from '../controllers/json-base.controller';
99
import {
1010
JSON_API_CONTROLLER_POSTFIX,
11+
JSON_API_DECORATOR_ENTITY,
1112
ORM_SERVICE,
1213
ORM_SERVICE_PROPS,
1314
} from '../../../constants';
@@ -52,12 +53,15 @@ describe('createController', () => {
5253

5354
expect(Reflect.getMetadata(CONTROLLER_WATERMARK, result)).toBe(true);
5455
expect(Reflect.getMetadata(PATH_METADATA, result)).toBe('users');
56+
expect(Reflect.getMetadata(JSON_API_DECORATOR_ENTITY, result)).toBe(Users);
5557

5658
expect(Reflect.getMetadata(CONTROLLER_WATERMARK, result2)).toBe(true);
5759
expect(Reflect.getMetadata(PATH_METADATA, result2)).toBe('users');
60+
expect(Reflect.getMetadata(JSON_API_DECORATOR_ENTITY, result)).toBe(Users);
5861

5962
expect(Reflect.getMetadata(CONTROLLER_WATERMARK, result3)).toBe(true);
6063
expect(Reflect.getMetadata(PATH_METADATA, result3)).toBe(overrideRoute);
64+
expect(Reflect.getMetadata(JSON_API_DECORATOR_ENTITY, result)).toBe(Users);
6165
});
6266

6367
it('Check inject typeorm, service', () => {

libs/json-api/json-api-nestjs/src/lib/modules/mixin/helpers/create-controller.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import { Controller, Inject, UseInterceptors } from '@nestjs/common';
22
import { kebabCase } from 'change-case-commonjs';
33
import { ModuleMixinOptions, NestController } from '../../../types';
44
import { DecoratorOptions } from '../types';
5-
import { getProviderName, nameIt } from './utils';
5+
import { entityForClass, getProviderName, nameIt } from './utils';
66
import { JsonBaseController } from '../controllers';
77
import {
88
JSON_API_DECORATOR_OPTIONS,
99
ORM_SERVICE_PROPS,
1010
ORM_SERVICE,
1111
JSON_API_CONTROLLER_POSTFIX,
12+
JSON_API_DECORATOR_ENTITY,
1213
} from '../../../constants';
1314
import { ErrorInterceptors, LogTimeInterceptors } from '../interceptors';
1415
import { getEntityName } from '@klerick/json-api-nestjs-shared';
@@ -24,6 +25,9 @@ export function createController(
2425
JsonBaseController
2526
);
2627

28+
if(!entityForClass(controllerClass)){
29+
Reflect.defineMetadata(JSON_API_DECORATOR_ENTITY, entity, controllerClass)
30+
}
2731
const entityName = getEntityName(entity);
2832

2933
if (

libs/json-api/json-api-nestjs/src/lib/modules/mixin/service/json-api-transformer.service.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('JsonApiTransformerService - extractAttributes', () => {
5555
userObject.firstName = faker.person.firstName();
5656
userObject.lastName = faker.person.lastName();
5757
userObject.isActive = null;
58-
userObject.login = faker.internet.userName({
58+
userObject.login = faker.internet.username({
5959
lastName: userObject.lastName,
6060
firstName: userObject.firstName,
6161
});
@@ -331,7 +331,7 @@ describe('JsonApiTransformerService - extractAttributes', () => {
331331

332332
userObject.manager = {
333333
id: 1,
334-
login: faker.internet.userName({
334+
login: faker.internet.username({
335335
lastName: faker.person.lastName(),
336336
firstName: faker.person.firstName(),
337337
}),

libs/json-api/json-api-nestjs/src/lib/modules/mixin/zod/zod-query-schema/filter.spec.ts

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,17 @@ describe('Check "filter" zod schema', () => {
2020
relation: null,
2121
};
2222
const result = schema.parse(check1);
23-
expect(result).toEqual(check1);
23+
24+
expect(result).toEqual({
25+
...check1,
26+
target: {
27+
...check1.target,
28+
id: {
29+
gte: 1213,
30+
ne: 12,
31+
},
32+
},
33+
});
2434
});
2535

2636
it('Valid schema - check2', () => {
@@ -42,7 +52,15 @@ describe('Check "filter" zod schema', () => {
4252
},
4353
};
4454
const result = schema.parse(check2);
45-
expect(result).toEqual(check2);
55+
expect(result).toEqual({
56+
...check2,
57+
target: {
58+
...check2.target,
59+
id: {
60+
gte: 1213,
61+
}
62+
}
63+
});
4664
});
4765

4866
it('Valid schema - check3', () => {
@@ -71,7 +89,17 @@ describe('Check "filter" zod schema', () => {
7189
},
7290
};
7391
const result = schema.parse(check4);
74-
expect(result).toEqual(check4);
92+
expect(result).toEqual({
93+
...check4,
94+
relation: {
95+
...check4.relation,
96+
comments: {
97+
id: {
98+
lte: 123,
99+
},
100+
}
101+
}
102+
});
75103
});
76104

77105
it('Valid schema - check5', () => {
@@ -108,7 +136,16 @@ describe('Check "filter" zod schema', () => {
108136
relation: null,
109137
};
110138
const result = schema.parse(check6);
111-
expect(result).toEqual(check6);
139+
expect(result).toEqual({
140+
...check6,
141+
target: {
142+
...check6.target,
143+
id:{
144+
gte: 1213,
145+
ne: 123,
146+
}
147+
},
148+
});
112149
});
113150

114151
it('Valid schema - check7', () => {
@@ -121,7 +158,12 @@ describe('Check "filter" zod schema', () => {
121158
relation: null,
122159
};
123160
const result = schema.parse(check7);
124-
expect(result).toEqual(check7);
161+
expect(result).toEqual({
162+
...check7,
163+
target: {
164+
...check7.target,
165+
},
166+
});
125167
});
126168

127169
it('Valid schema - check8', () => {
@@ -134,7 +176,15 @@ describe('Check "filter" zod schema', () => {
134176
relation: null,
135177
};
136178
const result = schema.parse(check8);
137-
expect(result).toEqual(check8);
179+
expect(result).toEqual({
180+
...check8,
181+
target: {
182+
...check8.target,
183+
createdAt: {
184+
eq: new Date('2023-12-08T09:40:58.020Z'),
185+
},
186+
},
187+
});
138188
});
139189

140190
it('Valid schema - check9', () => {
@@ -170,6 +220,10 @@ describe('Check "filter" zod schema', () => {
170220
...check,
171221
target: {
172222
...check.target,
223+
id: {
224+
gte: 1213,
225+
ne: 123,
226+
},
173227
addresses: {
174228
eq: 'null',
175229
},

libs/json-api/json-api-nestjs/src/lib/modules/mixin/zod/zod-query-schema/filter.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ function getZodRulesForField(type: TypeField = TypeField.string) {
6161
.refine(stringMustBe(type), {
6262
error: `String should be as ${type}`,
6363
})
64+
.transform((r) => {
65+
if (r === 'null' || r === null) {
66+
return r;
67+
}
68+
69+
switch (type) {
70+
case TypeField.boolean:
71+
return Boolean(r);
72+
case TypeField.number:
73+
return Number(r);
74+
case TypeField.date:
75+
return new Date(r);
76+
}
77+
78+
return r;
79+
})
6480
.optional(),
6581
}),
6682
{} as {

0 commit comments

Comments
 (0)