denwa-nest-shared 1.2.4 → 1.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/README.md +96 -0
- package/cjs/decorators.js +25 -27
- package/cjs/decorators.js.map +1 -1
- package/cjs/dto.js +10 -3
- package/cjs/dto.js.map +1 -1
- package/cjs/interceptors/fields-roles.interceptor.js +9 -3
- package/cjs/interceptors/fields-roles.interceptor.js.map +1 -1
- package/cjs/lib.js +79 -35
- package/cjs/lib.js.map +1 -1
- package/cjs/openapi-axios/interpolate-params.js +1 -12
- package/cjs/openapi-axios/interpolate-params.js.map +1 -1
- package/cjs/openapi-axios/types/serializer.d.ts +1 -0
- package/cjs/openapi-axios/types/serializer.js +1 -0
- package/cjs/openapi-axios/types/serializer.js.map +1 -1
- package/cjs/opensearch-core.js +1 -1
- package/cjs/opensearch-core.js.map +1 -1
- package/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/cjs/validation/validation.exception.d.ts +2 -2
- package/cjs/validation/validation.exception.js +1 -1
- package/cjs/validation/validation.exception.js.map +1 -1
- package/cjs/validation/validation.pipe.d.ts +2 -0
- package/cjs/validation/validation.pipe.js +27 -11
- package/cjs/validation/validation.pipe.js.map +1 -1
- package/dist/decorators.js +25 -27
- package/dist/decorators.js.map +1 -1
- package/dist/dto.js +11 -4
- package/dist/dto.js.map +1 -1
- package/dist/interceptors/fields-roles.interceptor.js +9 -3
- package/dist/interceptors/fields-roles.interceptor.js.map +1 -1
- package/dist/lib.js +79 -35
- package/dist/lib.js.map +1 -1
- package/dist/openapi-axios/interpolate-params.js +1 -12
- package/dist/openapi-axios/interpolate-params.js.map +1 -1
- package/dist/openapi-axios/types/serializer.d.ts +1 -0
- package/dist/openapi-axios/types/serializer.js +1 -0
- package/dist/openapi-axios/types/serializer.js.map +1 -1
- package/dist/opensearch-core.js +1 -1
- package/dist/opensearch-core.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/validation/validation.exception.d.ts +2 -2
- package/dist/validation/validation.exception.js +1 -1
- package/dist/validation/validation.exception.js.map +1 -1
- package/dist/validation/validation.pipe.d.ts +2 -0
- package/dist/validation/validation.pipe.js +27 -11
- package/dist/validation/validation.pipe.js.map +1 -1
- package/package.json +16 -6
- package/skills/auth-security/SKILL.md +3 -3
- package/skills/data-access-typeorm/SKILL.md +22 -4
- package/skills/file-management/SKILL.md +27 -21
- package/skills/schema-definition/SKILL.md +21 -16
|
@@ -17,7 +17,7 @@ let ValidationException = class ValidationException extends HttpException {
|
|
|
17
17
|
};
|
|
18
18
|
ValidationException = __decorate([
|
|
19
19
|
Injectable(),
|
|
20
|
-
__metadata("design:paramtypes", [
|
|
20
|
+
__metadata("design:paramtypes", [Object])
|
|
21
21
|
], ValidationException);
|
|
22
22
|
export { ValidationException };
|
|
23
23
|
//# sourceMappingURL=validation.exception.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.exception.js","sourceRoot":"","sources":["../../src/validation/validation.exception.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGhE,IAAM,mBAAmB,GAAzB,MAAM,mBAAoB,SAAQ,aAAa;IACpD,QAAQ,
|
|
1
|
+
{"version":3,"file":"validation.exception.js","sourceRoot":"","sources":["../../src/validation/validation.exception.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGhE,IAAM,mBAAmB,GAAzB,MAAM,mBAAoB,SAAQ,aAAa;IACpD,QAAQ,CAAU;IAElB,YAAY,QAAyB;QACnC,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC,WAAW,CAAC,CAAC;QACxC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF,CAAA;AAPY,mBAAmB;IAD/B,UAAU,EAAE;;GACA,mBAAmB,CAO/B","sourcesContent":["import { HttpException, HttpStatus, Injectable } from '@nestjs/common';\n\n@Injectable()\nexport class ValidationException extends HttpException {\n messages: unknown;\n\n constructor(response: string | object) {\n super(response, HttpStatus.BAD_REQUEST);\n this.messages = response;\n }\n}\n"]}
|
|
@@ -10,29 +10,45 @@ import { validate } from 'class-validator';
|
|
|
10
10
|
import { ValidationException } from './validation.exception';
|
|
11
11
|
let ValidationPipe = class ValidationPipe {
|
|
12
12
|
async transform(value, metadata) {
|
|
13
|
-
if (!metadata.metatype ||
|
|
13
|
+
if (!metadata.metatype ||
|
|
14
|
+
!this.toValidate(metadata.metatype) ||
|
|
15
|
+
(value && typeof value === 'object' && 'fieldNodes' in value)) {
|
|
14
16
|
return value;
|
|
15
17
|
}
|
|
16
|
-
const obj = plainToClass(metadata.metatype, value);
|
|
17
|
-
if (typeof obj !== 'object'
|
|
18
|
+
const obj = plainToClass(metadata.metatype, value ?? {});
|
|
19
|
+
if (!obj || typeof obj !== 'object')
|
|
18
20
|
return value;
|
|
19
|
-
const
|
|
20
|
-
const errors = validateErrors.filter((item) => item.value !== undefined);
|
|
21
|
+
const errors = await validate(obj);
|
|
21
22
|
if (errors.length) {
|
|
22
|
-
const messages =
|
|
23
|
-
if (!err.constraints)
|
|
24
|
-
return `${err.property}`;
|
|
25
|
-
return `${err.property} - ${Object.values(err.constraints).join(', ')}`;
|
|
26
|
-
});
|
|
23
|
+
const messages = this.formatErrors(errors);
|
|
27
24
|
const response = {
|
|
28
25
|
statusCode: HttpStatus.BAD_REQUEST,
|
|
29
26
|
error: 'Bad request',
|
|
30
27
|
messages,
|
|
31
28
|
};
|
|
32
|
-
throw new ValidationException(
|
|
29
|
+
throw new ValidationException(response);
|
|
33
30
|
}
|
|
34
31
|
return value;
|
|
35
32
|
}
|
|
33
|
+
toValidate(metatype) {
|
|
34
|
+
const types = [String, Boolean, Number, Array, Object];
|
|
35
|
+
return !types.includes(metatype);
|
|
36
|
+
}
|
|
37
|
+
formatErrors(errors) {
|
|
38
|
+
const messages = [];
|
|
39
|
+
for (const err of errors) {
|
|
40
|
+
if (err.constraints) {
|
|
41
|
+
messages.push(`${err.property} - ${Object.values(err.constraints).join(', ')}`);
|
|
42
|
+
}
|
|
43
|
+
else if (err.children && err.children.length > 0) {
|
|
44
|
+
messages.push(...this.formatErrors(err.children).map((subMsg) => `${err.property}.${subMsg}`));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
messages.push(err.property);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return messages;
|
|
51
|
+
}
|
|
36
52
|
};
|
|
37
53
|
ValidationPipe = __decorate([
|
|
38
54
|
Injectable()
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.pipe.js","sourceRoot":"","sources":["../../src/validation/validation.pipe.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAoB,UAAU,EAAE,UAAU,EAAiB,MAAM,gBAAgB,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,
|
|
1
|
+
{"version":3,"file":"validation.pipe.js","sourceRoot":"","sources":["../../src/validation/validation.pipe.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAoB,UAAU,EAAE,UAAU,EAAiB,MAAM,gBAAgB,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAmB,MAAM,iBAAiB,CAAC;AAI5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,wBAAwB,CAAC;AAGtD,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,KAAK,CAAC,SAAS,CAAC,KAAc,EAAE,QAA0B;QACxD,IACE,CAAC,QAAQ,CAAC,QAAQ;YAClB,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,QAAQ,CAAC;YACnC,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,YAAY,IAAI,KAAK,CAAC,EAC7D,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,MAAM,GAAG,GAAW,YAAY,CAAC,QAAQ,CAAC,QAAQ,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC;QACjE,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ;YAAE,OAAO,KAAK,CAAC;QAElD,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,GAAG,CAAC,CAAC;QAEnC,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC;YAClB,MAAM,QAAQ,GAAG,IAAI,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;YAE3C,MAAM,QAAQ,GAAsC;gBAClD,UAAU,EAAE,UAAU,CAAC,WAAW;gBAClC,KAAK,EAAE,aAAa;gBACpB,QAAQ;aACT,CAAC;YAEF,MAAM,IAAI,mBAAmB,CAAC,QAAQ,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAGO,UAAU,CAAC,QAAkB;QAEnC,MAAM,KAAK,GAAe,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;QACnE,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACnC,CAAC;IAEO,YAAY,CAAC,MAAyB;QAC5C,MAAM,QAAQ,GAAa,EAAE,CAAC;QAC9B,KAAK,MAAM,GAAG,IAAI,MAAM,EAAE,CAAC;YACzB,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC;gBACpB,QAAQ,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC,QAAQ,MAAM,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;YAClF,CAAC;iBAAM,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACnD,QAAQ,CAAC,IAAI,CACX,GAAG,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,QAAQ,IAAI,MAAM,EAAE,CAAC,CAChF,CAAC;YACJ,CAAC;iBAAM,CAAC;gBACN,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;YAC9B,CAAC;QACH,CAAC;QACD,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF,CAAA;AAnDY,cAAc;IAD1B,UAAU,EAAE;GACA,cAAc,CAmD1B","sourcesContent":["import { ArgumentMetadata, HttpStatus, Injectable, PipeTransform } from '@nestjs/common';\nimport { plainToClass } from 'class-transformer';\nimport { validate, ValidationError } from 'class-validator';\n\nimport { IResponse } from '../types';\n\nimport { ValidationException } from './validation.exception';\n\n@Injectable()\nexport class ValidationPipe implements PipeTransform<unknown> {\n async transform(value: unknown, metadata: ArgumentMetadata): Promise<unknown> {\n if (\n !metadata.metatype ||\n !this.toValidate(metadata.metatype) ||\n (value && typeof value === 'object' && 'fieldNodes' in value)\n ) {\n return value;\n }\n\n const obj: object = plainToClass(metadata.metatype, value ?? {});\n if (!obj || typeof obj !== 'object') return value;\n\n const errors = await validate(obj);\n\n if (errors.length) {\n const messages = this.formatErrors(errors);\n\n const response: IResponse<HttpStatus.BAD_REQUEST> = {\n statusCode: HttpStatus.BAD_REQUEST,\n error: 'Bad request',\n messages,\n };\n\n throw new ValidationException(response);\n }\n return value;\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n private toValidate(metatype: Function): boolean {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type\n const types: Function[] = [String, Boolean, Number, Array, Object];\n return !types.includes(metatype);\n }\n\n private formatErrors(errors: ValidationError[]): string[] {\n const messages: string[] = [];\n for (const err of errors) {\n if (err.constraints) {\n messages.push(`${err.property} - ${Object.values(err.constraints).join(', ')}`);\n } else if (err.children && err.children.length > 0) {\n messages.push(\n ...this.formatErrors(err.children).map((subMsg) => `${err.property}.${subMsg}`),\n );\n } else {\n messages.push(err.property);\n }\n }\n return messages;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "denwa-nest-shared",
|
|
4
|
-
"version": "1.2.
|
|
4
|
+
"version": "1.2.5",
|
|
5
5
|
"skills": "skills",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"main": "./cjs/index.js",
|
|
@@ -18,12 +18,13 @@
|
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
|
+
"test": "vitest run",
|
|
21
22
|
"build": "npm run build:esm && npm run build:cjs",
|
|
22
23
|
"build:esm": "rimraf ./dist && tsc -p tsconfig.build.json",
|
|
23
24
|
"build:cjs": "rimraf ./cjs && tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > ./cjs/package.json",
|
|
24
25
|
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
|
|
25
26
|
"format": "prettier --write \"src/**/*.ts\" ",
|
|
26
|
-
"prepare-push": "npm run lint && npm run format && npm run build",
|
|
27
|
+
"prepare-push": "npm run lint && npm run format && npm test && npm run build",
|
|
27
28
|
"release:minor": "npm run build && standard-version minor",
|
|
28
29
|
"release:patch": "npm run build && standard-version patch"
|
|
29
30
|
},
|
|
@@ -31,7 +32,7 @@
|
|
|
31
32
|
"tanstack-intent"
|
|
32
33
|
],
|
|
33
34
|
"license": "ISC",
|
|
34
|
-
"description": "",
|
|
35
|
+
"description": "Shared NestJS module for DTO validation, RBAC, TypeORM query building, and OpenAPI client",
|
|
35
36
|
"repository": {
|
|
36
37
|
"type": "git",
|
|
37
38
|
"url": "https://github.com/Denwa799/nest-shared.git"
|
|
@@ -65,13 +66,15 @@
|
|
|
65
66
|
"globals": "^17.6.0",
|
|
66
67
|
"graphql": "^16.14.0",
|
|
67
68
|
"prettier": "^3.8.3",
|
|
69
|
+
"reflect-metadata": "^0.2.2",
|
|
68
70
|
"rimraf": "^6.1.3",
|
|
71
|
+
"rxjs": "^7.8.2",
|
|
69
72
|
"standard-version": "^9.5.0",
|
|
70
|
-
"ts-morph": "^28.0.0",
|
|
71
73
|
"tsconfig-paths": "^4.2.0",
|
|
72
|
-
"typeorm": "^
|
|
74
|
+
"typeorm": "^0.3.20",
|
|
73
75
|
"typescript": "^5",
|
|
74
|
-
"typescript-eslint": "^8.60.0"
|
|
76
|
+
"typescript-eslint": "^8.60.0",
|
|
77
|
+
"vitest": "^5.0.0"
|
|
75
78
|
},
|
|
76
79
|
"dependencies": {
|
|
77
80
|
"axios": "^1.16.1",
|
|
@@ -85,9 +88,16 @@
|
|
|
85
88
|
"@nestjs/common": "^10.0.0 || ^11.0.0",
|
|
86
89
|
"@nestjs/graphql": "^12.0.0 || ^13.0.0",
|
|
87
90
|
"@nestjs/swagger": "^7.0.0 || ^11.0.0",
|
|
91
|
+
"@opensearch-project/opensearch": "^2.0.0 || ^3.0.0",
|
|
88
92
|
"class-transformer": "^0.5.1",
|
|
89
93
|
"class-validator": "^0.14.0 || ^0.15.0",
|
|
90
94
|
"graphql": "^15.0.0 || ^16.0.0",
|
|
95
|
+
"rxjs": "^7.0.0 || ^8.0.0",
|
|
91
96
|
"typeorm": "^0.3.0"
|
|
97
|
+
},
|
|
98
|
+
"peerDependenciesMeta": {
|
|
99
|
+
"@opensearch-project/opensearch": {
|
|
100
|
+
"optional": true
|
|
101
|
+
}
|
|
92
102
|
}
|
|
93
103
|
}
|
|
@@ -25,7 +25,7 @@ import { Roles, GetCurrentUserId } from 'denwa-nest-shared';
|
|
|
25
25
|
|
|
26
26
|
@Resolver()
|
|
27
27
|
export class UserResolver {
|
|
28
|
-
@Roles('admin', 'manager')
|
|
28
|
+
@Roles(['admin', 'manager'])
|
|
29
29
|
@Mutation(() => Boolean)
|
|
30
30
|
async deleteUser(@Args('id') id: string, @GetCurrentUserId() userId: string) {
|
|
31
31
|
// userId is extracted correctly for both GQL and REST
|
|
@@ -114,7 +114,7 @@ Source: Denwa799/nest-shared:src/decorators.ts:121
|
|
|
114
114
|
Wrong:
|
|
115
115
|
|
|
116
116
|
```typescript
|
|
117
|
-
@Roles('admin', 'super-admin')
|
|
117
|
+
@Roles(['admin', 'super-admin'])
|
|
118
118
|
@Mutation()
|
|
119
119
|
async delete() {}
|
|
120
120
|
```
|
|
@@ -122,7 +122,7 @@ async delete() {}
|
|
|
122
122
|
Correct:
|
|
123
123
|
|
|
124
124
|
```typescript
|
|
125
|
-
@Roles('admin')
|
|
125
|
+
@Roles(['admin'])
|
|
126
126
|
@Mutation()
|
|
127
127
|
async delete() {}
|
|
128
128
|
```
|
|
@@ -24,10 +24,13 @@ import { Repository } from 'typeorm';
|
|
|
24
24
|
|
|
25
25
|
async function findUsers(repository: Repository<User>, args: GetUsersArgs) {
|
|
26
26
|
const queryData = createQueryData({
|
|
27
|
+
page: args.page ?? 1,
|
|
28
|
+
limit: args.limit ?? 20,
|
|
29
|
+
sortField: args.sortField ?? 'createdAt',
|
|
30
|
+
sortOrder: args.sortOrder ?? 'desc',
|
|
31
|
+
filterType: 'and',
|
|
27
32
|
filter: args.filter, // Object with direct matches
|
|
28
|
-
search: args.search, //
|
|
29
|
-
paginate: args.paginate, // { page, limit }
|
|
30
|
-
sort: args.sort, // { sortField, sortOrder }
|
|
33
|
+
search: args.search, // Multi-field string or object for ILike search
|
|
31
34
|
});
|
|
32
35
|
|
|
33
36
|
return await repository.findAndCount(queryData);
|
|
@@ -42,6 +45,11 @@ Map specific "start" and "end" fields to a single column range (e.g. `priceStart
|
|
|
42
45
|
|
|
43
46
|
```typescript
|
|
44
47
|
const queryData = createQueryData({
|
|
48
|
+
page: 1,
|
|
49
|
+
limit: 20,
|
|
50
|
+
sortField: 'createdAt',
|
|
51
|
+
sortOrder: 'desc',
|
|
52
|
+
filterType: 'and',
|
|
45
53
|
filter: { minPrice: 100, maxPrice: 500 },
|
|
46
54
|
fromToFields: [
|
|
47
55
|
{
|
|
@@ -60,8 +68,13 @@ Search for entities where an array column contains any of the provided values.
|
|
|
60
68
|
|
|
61
69
|
```typescript
|
|
62
70
|
const queryData = createQueryData({
|
|
71
|
+
page: 1,
|
|
72
|
+
limit: 20,
|
|
73
|
+
sortField: 'createdAt',
|
|
74
|
+
sortOrder: 'desc',
|
|
75
|
+
filterType: 'and',
|
|
63
76
|
filter: { categoryIds: [1, 2, 3] },
|
|
64
|
-
oneEntryArrayFields: ['categoryIds'],
|
|
77
|
+
oneEntryArrayFields: [{ key: 'categoryIds', originalKey: 'categories' }],
|
|
65
78
|
});
|
|
66
79
|
// Uses ArrayOverlap for those fields
|
|
67
80
|
```
|
|
@@ -72,6 +85,11 @@ const queryData = createQueryData({
|
|
|
72
85
|
|
|
73
86
|
```typescript
|
|
74
87
|
const queryData = createQueryData({
|
|
88
|
+
page: 1,
|
|
89
|
+
limit: 20,
|
|
90
|
+
sortField: 'createdAt',
|
|
91
|
+
sortOrder: 'desc',
|
|
92
|
+
filterType: 'and',
|
|
75
93
|
filter: { isActive: true },
|
|
76
94
|
search: { firstName: 'John', lastName: 'Doe' },
|
|
77
95
|
searchType: 'or', // Default: searches for firstName OR lastName matching the terms
|
|
@@ -20,37 +20,43 @@ Use this skill to manage file metadata that is stored as stringified JSON in you
|
|
|
20
20
|
Synchronize temporary uploads with an entity.
|
|
21
21
|
|
|
22
22
|
```typescript
|
|
23
|
-
import {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
import {
|
|
24
|
+
prepareTempImages,
|
|
25
|
+
updateEntityImages,
|
|
26
|
+
ImageFileDto,
|
|
27
|
+
TempImagesDto,
|
|
28
|
+
AllImagesDto,
|
|
29
|
+
} from 'denwa-nest-shared';
|
|
30
|
+
|
|
31
|
+
async function updateProductGallery(
|
|
32
|
+
product: { images: string | null },
|
|
33
|
+
filesFromUploadService: ImageFileDto[],
|
|
34
|
+
allImages: AllImagesDto[],
|
|
35
|
+
tempImages: TempImagesDto[],
|
|
36
|
+
) {
|
|
37
|
+
// 1. Map uploaded microservice files with alt metadata
|
|
38
|
+
const prepared = prepareTempImages(filesFromUploadService, tempImages);
|
|
39
|
+
|
|
40
|
+
// 2. Reconcile with existing entity images (returns newImages and deletedImages)
|
|
41
|
+
const { newImages, deletedImages } = updateEntityImages(product.images, allImages, prepared);
|
|
42
|
+
product.images = newImages ? JSON.stringify(newImages) : null;
|
|
31
43
|
|
|
32
44
|
await repository.save(product);
|
|
45
|
+
return { product, deletedImages };
|
|
33
46
|
}
|
|
34
47
|
```
|
|
35
48
|
|
|
36
49
|
## Core Patterns
|
|
37
50
|
|
|
38
|
-
###
|
|
51
|
+
### Image JSON Parsing & Validation
|
|
39
52
|
|
|
40
|
-
|
|
53
|
+
Parse and validate a stored image JSON string against Zod schema.
|
|
41
54
|
|
|
42
55
|
```typescript
|
|
43
56
|
import { prepareImages } from 'denwa-nest-shared';
|
|
44
57
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
{
|
|
48
|
-
name: 'photo.jpg',
|
|
49
|
-
url: 'https://s3.aws.com/bucket/photo.jpg',
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
'avatar',
|
|
53
|
-
);
|
|
58
|
+
// Parses and validates a JSON string of IPreparedImage[]
|
|
59
|
+
const images = prepareImages(product.imagesJsonString);
|
|
54
60
|
```
|
|
55
61
|
|
|
56
62
|
### File Meta Retrieval
|
|
@@ -59,10 +65,10 @@ The library assumes that your database columns store these as strings. Ensure yo
|
|
|
59
65
|
|
|
60
66
|
```typescript
|
|
61
67
|
// Typical DTO for accepting image updates
|
|
62
|
-
import {
|
|
68
|
+
import { ApiValidationProperty } from 'denwa-nest-shared';
|
|
63
69
|
|
|
64
70
|
export class UpdateProfileDto {
|
|
65
|
-
@ApiValidationProperty({ type: 'string',
|
|
71
|
+
@ApiValidationProperty({ type: 'string', isOptional: true })
|
|
66
72
|
avatar?: string; // Standardized image JSON string
|
|
67
73
|
}
|
|
68
74
|
```
|
|
@@ -29,7 +29,7 @@ export class CreateUserDto {
|
|
|
29
29
|
type: 'string',
|
|
30
30
|
description: 'User display name',
|
|
31
31
|
example: 'John Doe',
|
|
32
|
-
|
|
32
|
+
min: 2,
|
|
33
33
|
})
|
|
34
34
|
name: string;
|
|
35
35
|
|
|
@@ -42,8 +42,7 @@ export class CreateUserDto {
|
|
|
42
42
|
|
|
43
43
|
@ApiValidationProperty({
|
|
44
44
|
type: 'boolean',
|
|
45
|
-
|
|
46
|
-
optional: true,
|
|
45
|
+
isOptional: true,
|
|
47
46
|
})
|
|
48
47
|
isActive?: boolean;
|
|
49
48
|
}
|
|
@@ -75,15 +74,21 @@ export class UserDto {
|
|
|
75
74
|
|
|
76
75
|
### Pagination Request Shapes
|
|
77
76
|
|
|
78
|
-
|
|
77
|
+
Define pagination and filter arguments using standard types:
|
|
79
78
|
|
|
80
79
|
```typescript
|
|
81
80
|
import { ArgsType } from '@nestjs/graphql';
|
|
82
|
-
import {
|
|
81
|
+
import { ApiValidationProperty } from 'denwa-nest-shared';
|
|
83
82
|
|
|
84
83
|
@ArgsType()
|
|
85
|
-
export class GetUsersArgs
|
|
86
|
-
@ApiValidationProperty({ type: '
|
|
84
|
+
export class GetUsersArgs {
|
|
85
|
+
@ApiValidationProperty({ type: 'int', isOptional: true })
|
|
86
|
+
page?: number;
|
|
87
|
+
|
|
88
|
+
@ApiValidationProperty({ type: 'int', isOptional: true })
|
|
89
|
+
limit?: number;
|
|
90
|
+
|
|
91
|
+
@ApiValidationProperty({ type: 'string', isOptional: true })
|
|
87
92
|
search?: string;
|
|
88
93
|
}
|
|
89
94
|
```
|
|
@@ -102,7 +107,7 @@ export class StatsDto {
|
|
|
102
107
|
@ApiValidationProperty({ type: 'float', max: 100 })
|
|
103
108
|
score: number;
|
|
104
109
|
|
|
105
|
-
@ApiValidationProperty({ type: 'date',
|
|
110
|
+
@ApiValidationProperty({ type: 'date', isOptional: true })
|
|
106
111
|
lastLogin?: Date;
|
|
107
112
|
}
|
|
108
113
|
```
|
|
@@ -124,7 +129,7 @@ name: string;
|
|
|
124
129
|
Correct:
|
|
125
130
|
|
|
126
131
|
```typescript
|
|
127
|
-
@ApiValidationProperty({ type: 'string',
|
|
132
|
+
@ApiValidationProperty({ type: 'string', min: 2 })
|
|
128
133
|
name: string;
|
|
129
134
|
```
|
|
130
135
|
|
|
@@ -152,22 +157,22 @@ Validation parameters like `min`/`max` only apply to numeric types. Boolean type
|
|
|
152
157
|
|
|
153
158
|
Source: Denwa799/nest-shared:src/decorators.ts:160
|
|
154
159
|
|
|
155
|
-
### MEDIUM
|
|
160
|
+
### MEDIUM Using optional instead of isOptional
|
|
156
161
|
|
|
157
162
|
Wrong:
|
|
158
163
|
|
|
159
164
|
```typescript
|
|
160
|
-
@ApiValidationProperty({ type: 'string', optional:
|
|
161
|
-
name
|
|
165
|
+
@ApiValidationProperty({ type: 'string', optional: true })
|
|
166
|
+
name?: string;
|
|
162
167
|
```
|
|
163
168
|
|
|
164
169
|
Correct:
|
|
165
170
|
|
|
166
171
|
```typescript
|
|
167
|
-
@ApiValidationProperty({ type: 'string',
|
|
168
|
-
name
|
|
172
|
+
@ApiValidationProperty({ type: 'string', isOptional: true })
|
|
173
|
+
name?: string;
|
|
169
174
|
```
|
|
170
175
|
|
|
171
|
-
|
|
176
|
+
The property name in `ApiValidationProperty` is `isOptional`, not `optional`. Using `optional` will be ignored and the field will be treated as required.
|
|
172
177
|
|
|
173
|
-
Source: Denwa799/nest-shared:src/decorators.ts:
|
|
178
|
+
Source: Denwa799/nest-shared:src/decorators.ts:58
|