denwa-nest-shared 1.0.78 → 1.0.79
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
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [1.0.79](https://github.com/Denwa799/nest-shared/compare/v1.0.78...v1.0.79) (2026-04-26)
|
|
6
|
+
|
|
5
7
|
### [1.0.78](https://github.com/Denwa799/nest-shared/compare/v1.0.77...v1.0.78) (2026-03-28)
|
|
6
8
|
|
|
7
9
|
### [1.0.77](https://github.com/Denwa799/nest-shared/compare/v1.0.76...v1.0.77) (2026-01-23)
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package.json",
|
|
3
3
|
"name": "denwa-nest-shared",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.79",
|
|
5
|
+
"skills": "skills",
|
|
5
6
|
"types": "./dist/index.d.ts",
|
|
6
7
|
"main": "./cjs/index.js",
|
|
7
8
|
"module": "./dist/index.js",
|
|
@@ -11,6 +12,8 @@
|
|
|
11
12
|
"files": [
|
|
12
13
|
"cjs",
|
|
13
14
|
"dist",
|
|
15
|
+
"skills",
|
|
16
|
+
"!skills/_artifacts",
|
|
14
17
|
"CHANGELOG.md",
|
|
15
18
|
"README.md"
|
|
16
19
|
],
|
|
@@ -24,7 +27,9 @@
|
|
|
24
27
|
"release:minor": "npm run build && standard-version minor",
|
|
25
28
|
"release:patch": "npm run build && standard-version patch"
|
|
26
29
|
},
|
|
27
|
-
"keywords": [
|
|
30
|
+
"keywords": [
|
|
31
|
+
"tanstack-intent"
|
|
32
|
+
],
|
|
28
33
|
"license": "ISC",
|
|
29
34
|
"description": "",
|
|
30
35
|
"repository": {
|
|
@@ -57,7 +62,8 @@
|
|
|
57
62
|
"ts-morph": "^25.0.1",
|
|
58
63
|
"tsconfig-paths": "^4.2.0",
|
|
59
64
|
"typescript": "^5.7.3",
|
|
60
|
-
"typescript-eslint": "^8.24.0"
|
|
65
|
+
"typescript-eslint": "^8.24.0",
|
|
66
|
+
"@tanstack/intent": "latest"
|
|
61
67
|
},
|
|
62
68
|
"dependencies": {
|
|
63
69
|
"@nestjs/common": "^11.0.9",
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: auth-security
|
|
3
|
+
description: >
|
|
4
|
+
Implementing security and RBAC using Roles, Public, and identity decorators.
|
|
5
|
+
Handles GraphQL and REST contexts uniformly. Includes field-level role checking.
|
|
6
|
+
type: core
|
|
7
|
+
library: denwa-nest-shared
|
|
8
|
+
library_version: "1.0.78"
|
|
9
|
+
sources:
|
|
10
|
+
- "Denwa799/nest-shared:src/decorators.ts"
|
|
11
|
+
- "Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts"
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# RBAC & Identity Decorators
|
|
15
|
+
|
|
16
|
+
Use this skill to protect routes and mutations, and to extract user information regardless of whether the request came from a GraphQL resolver or a standard REST controller.
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
Protect a resolver with roles and extract the current user ID.
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Resolver, Mutation, Args } from '@nestjs/graphql';
|
|
24
|
+
import { Roles, GetCurrentUserId } from 'denwa-nest-shared';
|
|
25
|
+
|
|
26
|
+
@Resolver()
|
|
27
|
+
export class UserResolver {
|
|
28
|
+
@Roles('admin', 'manager')
|
|
29
|
+
@Mutation(() => Boolean)
|
|
30
|
+
async deleteUser(
|
|
31
|
+
@Args('id') id: string,
|
|
32
|
+
@GetCurrentUserId() userId: string
|
|
33
|
+
) {
|
|
34
|
+
// userId is extracted correctly for both GQL and REST
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Core Patterns
|
|
41
|
+
|
|
42
|
+
### Public Routes
|
|
43
|
+
Bypass global authentication guards for specific handlers.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { Public } from 'denwa-nest-shared';
|
|
47
|
+
|
|
48
|
+
@Public()
|
|
49
|
+
@Get('health')
|
|
50
|
+
getHealth() {
|
|
51
|
+
return 'ok';
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Full User Object Extraction
|
|
56
|
+
Extract the complete user object from the request context.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
import { GetCurrentUser } from 'denwa-nest-shared';
|
|
60
|
+
|
|
61
|
+
@Get('profile')
|
|
62
|
+
getProfile(@GetCurrentUser() user: any) {
|
|
63
|
+
return user;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Field-Level Role Protection
|
|
68
|
+
Apply restrictions to specific GraphQL fields within a request.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
|
|
72
|
+
import { CheckFieldsRoles, FieldsRolesInterceptor } from 'denwa-nest-shared';
|
|
73
|
+
import { UseInterceptors } from '@nestjs/common';
|
|
74
|
+
|
|
75
|
+
@Resolver(() => User)
|
|
76
|
+
@UseInterceptors(FieldsRolesInterceptor)
|
|
77
|
+
export class UserResolver {
|
|
78
|
+
@CheckFieldsRoles({ email: ['admin'] })
|
|
79
|
+
@ResolveField(() => String)
|
|
80
|
+
async email(@Parent() user: User) {
|
|
81
|
+
return user.email;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Common Mistakes
|
|
87
|
+
|
|
88
|
+
### HIGH Direct Access to req.user
|
|
89
|
+
Wrong:
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
@Mutation()
|
|
93
|
+
async update(@Context() ctx) {
|
|
94
|
+
const user = ctx.req.user; // Potential failure if context not normalized
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Correct:
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
@Mutation()
|
|
102
|
+
async update(@GetCurrentUser() user: User) {
|
|
103
|
+
// Safe extraction across GQL and REST
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Directly accessing the request object can fail in mixed environments. The identity decorators handle the underlying context normalization automatically.
|
|
108
|
+
|
|
109
|
+
Source: Denwa799/nest-shared:src/decorators.ts:121
|
|
110
|
+
|
|
111
|
+
### MEDIUM Listing super-admin in Roles
|
|
112
|
+
Wrong:
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
@Roles('admin', 'super-admin')
|
|
116
|
+
@Mutation()
|
|
117
|
+
async delete() {}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Correct:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
@Roles('admin')
|
|
124
|
+
@Mutation()
|
|
125
|
+
async delete() {}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The library's `FieldsRolesInterceptor` and standard role guards typically bypass all restrictions for `super-admin` or `super_admin`. You do not need to specify them explicitly in projects.
|
|
129
|
+
|
|
130
|
+
Source: maintainer interview
|
|
131
|
+
|
|
132
|
+
### LOW Forgetting FieldsRolesInterceptor
|
|
133
|
+
Wrong:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
@CheckFieldsRoles({ salary: ['admin'] })
|
|
137
|
+
@ResolveField()
|
|
138
|
+
async salary() {}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Correct:
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
@UseInterceptors(FieldsRolesInterceptor)
|
|
145
|
+
@CheckFieldsRoles({ salary: ['admin'] })
|
|
146
|
+
@ResolveField()
|
|
147
|
+
async salary() {}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The `@CheckFieldsRoles` decorator only stores metadata; it requires the `FieldsRolesInterceptor` to be active on the class or handler to actually enforce the restriction.
|
|
151
|
+
|
|
152
|
+
Source: Denwa799/nest-shared:src/interceptors/fields-roles.interceptor.ts
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: data-access-typeorm
|
|
3
|
+
description: >
|
|
4
|
+
Generating complex TypeORM FindOptions using the createQueryData helper.
|
|
5
|
+
Standardizes filtering, ILike searching, range mapping (fromToFields), and pagination.
|
|
6
|
+
type: core
|
|
7
|
+
library: denwa-nest-shared
|
|
8
|
+
library_version: "1.0.78"
|
|
9
|
+
sources:
|
|
10
|
+
- "Denwa799/nest-shared:src/lib.ts"
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# TypeORM Query Generation
|
|
14
|
+
|
|
15
|
+
Use this skill to transform frontend filter and search parameters into standard TypeORM `FindOptions`. This is the preferred way to build all data-fetching logic in projects using this library.
|
|
16
|
+
|
|
17
|
+
## Setup
|
|
18
|
+
|
|
19
|
+
Initialize a repository query using `createQueryData`.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { createQueryData } from 'denwa-nest-shared';
|
|
23
|
+
import { Repository } from 'typeorm';
|
|
24
|
+
|
|
25
|
+
async function findUsers(repository: Repository<User>, args: GetUsersArgs) {
|
|
26
|
+
const queryData = createQueryData({
|
|
27
|
+
filter: args.filter, // Object with direct matches
|
|
28
|
+
search: args.search, // Object for ILike search
|
|
29
|
+
paginate: args.paginate, // { page, limit }
|
|
30
|
+
sort: args.sort, // { sortField, sortOrder }
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return await repository.findAndCount(queryData);
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Core Patterns
|
|
38
|
+
|
|
39
|
+
### Range Mapping (fromToFields)
|
|
40
|
+
Map specific "start" and "end" fields to a single column range (e.g. `priceStart` and `priceEnd` map to `price`).
|
|
41
|
+
|
|
42
|
+
```typescript
|
|
43
|
+
const queryData = createQueryData({
|
|
44
|
+
filter: { minPrice: 100, maxPrice: 500 },
|
|
45
|
+
fromToFields: [
|
|
46
|
+
{
|
|
47
|
+
fromFieldName: 'minPrice',
|
|
48
|
+
toFieldName: 'maxPrice',
|
|
49
|
+
originalFieldName: 'price'
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
});
|
|
53
|
+
// Result includes: where: { price: And(MoreThanOrEqual(100), LessThanOrEqual(500)) }
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Array Overlap Filtering
|
|
57
|
+
Search for entities where an array column contains any of the provided values.
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const queryData = createQueryData({
|
|
61
|
+
filter: { categoryIds: [1, 2, 3] },
|
|
62
|
+
oneEntryArrayFields: ['categoryIds']
|
|
63
|
+
});
|
|
64
|
+
// Uses ArrayOverlap for those fields
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### Combined Search and Filter
|
|
68
|
+
`createQueryData` supports both strict filtering and fuzzy searching.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const queryData = createQueryData({
|
|
72
|
+
filter: { isActive: true },
|
|
73
|
+
search: { firstName: 'John', lastName: 'Doe' },
|
|
74
|
+
searchType: 'or' // Default: searches for firstName OR lastName matching the terms
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Common Mistakes
|
|
79
|
+
|
|
80
|
+
### CRITICAL Manually Building QueryBuilders
|
|
81
|
+
Wrong:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
const qb = repository.createQueryBuilder('user');
|
|
85
|
+
if (filter.name) {
|
|
86
|
+
qb.andWhere('user.name ILIKE :name', { name: `%${filter.name}%` });
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Correct:
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
const queryData = createQueryData({ search: { name: filter.name } });
|
|
94
|
+
const results = await repository.find(queryData);
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Falling back to raw QueryBuilders for standard filtering and searching avoids the library's optimized range, overlap, and pagination logic. Always use `createQueryData`.
|
|
98
|
+
|
|
99
|
+
Source: maintainer interview
|
|
100
|
+
|
|
101
|
+
### HIGH Incorrect fromToFields Mapping
|
|
102
|
+
Wrong:
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
createQueryData({
|
|
106
|
+
filter: { ageFrom: 18, ageTo: 99 },
|
|
107
|
+
fromToFields: [{
|
|
108
|
+
fromFieldName: 'ageFrom',
|
|
109
|
+
toFieldName: 'ageTo',
|
|
110
|
+
originalFieldName: 'id' // Mismatched field name
|
|
111
|
+
}]
|
|
112
|
+
})
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Correct:
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
createQueryData({
|
|
119
|
+
filter: { ageFrom: 18, ageTo: 99 },
|
|
120
|
+
fromToFields: [{
|
|
121
|
+
fromFieldName: 'ageFrom',
|
|
122
|
+
toFieldName: 'ageTo',
|
|
123
|
+
originalFieldName: 'age'
|
|
124
|
+
}]
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The `originalFieldName` must exactly match the property name on your Entity.
|
|
129
|
+
|
|
130
|
+
Source: Denwa799/nest-shared:src/lib.ts:454
|
|
131
|
+
|
|
132
|
+
### MEDIUM Ignoring searchType Default
|
|
133
|
+
Wrong:
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
// Assuming this will search for users who match BOTH critera
|
|
137
|
+
const queryData = createQueryData({
|
|
138
|
+
search: { firstName: 'John', email: 'test@' }
|
|
139
|
+
});
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Correct:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
const queryData = createQueryData({
|
|
146
|
+
search: { firstName: 'John', email: 'test@' },
|
|
147
|
+
searchType: 'and'
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
By default, `searchType` is `'or'`. If you need strict matching across multiple search fields, you must explicitly set it to `'and'`.
|
|
152
|
+
|
|
153
|
+
Source: Denwa799/nest-shared:src/lib.ts:479
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: file-management
|
|
3
|
+
description: >
|
|
4
|
+
Managing file and image metadata stored as JSON strings in the database.
|
|
5
|
+
Standardizes the lifecycle of temporary uploads, entity sync, and S3 metadata formatting.
|
|
6
|
+
type: core
|
|
7
|
+
library: denwa-nest-shared
|
|
8
|
+
library_version: "1.0.78"
|
|
9
|
+
sources:
|
|
10
|
+
- "Denwa799/nest-shared:src/lib.ts"
|
|
11
|
+
- "Denwa799/nest-shared:src/dto.ts"
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# File & Image JSON Storage
|
|
15
|
+
|
|
16
|
+
Use this skill to manage file metadata that is stored as stringified JSON in your database. This is used to track S3 locations, alt tags, and file states.
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
Synchronize temporary uploads with an entity.
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { prepareTempImages, updateEntityImages } from 'denwa-nest-shared';
|
|
24
|
+
|
|
25
|
+
async function updateProductGallery(product, newImagesDto: TempImagesDto) {
|
|
26
|
+
// 1. Prepare metadata for images that were uploaded to temporary storage
|
|
27
|
+
const prepared = prepareTempImages(newImagesDto.tempImages, 'products-gallery');
|
|
28
|
+
|
|
29
|
+
// 2. Reconcile with existing entity images (adds new, keeps existing, removes deleted)
|
|
30
|
+
product.images = updateEntityImages(product.images, prepared);
|
|
31
|
+
|
|
32
|
+
await repository.save(product);
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Core Patterns
|
|
37
|
+
|
|
38
|
+
### Manual Image Formatting
|
|
39
|
+
Format a raw S3 result or external URL into the library-standard image JSON structure.
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
import { prepareImages } from 'denwa-nest-shared';
|
|
43
|
+
|
|
44
|
+
const images = prepareImages([
|
|
45
|
+
{
|
|
46
|
+
name: 'photo.jpg',
|
|
47
|
+
url: 'https://s3.aws.com/bucket/photo.jpg'
|
|
48
|
+
}
|
|
49
|
+
], 'avatar');
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### File Meta Retrieval
|
|
53
|
+
The library assumes that your database columns store these as strings. Ensure you handle the serialized state correctly in your repositories.
|
|
54
|
+
|
|
55
|
+
```typescript
|
|
56
|
+
// Typical DTO for accepting image updates
|
|
57
|
+
import { AllImagesDto } from 'denwa-nest-shared';
|
|
58
|
+
|
|
59
|
+
export class UpdateProfileDto {
|
|
60
|
+
@ApiValidationProperty({ type: 'string', optional: true })
|
|
61
|
+
avatar?: string; // Standardized image JSON string
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Common Mistakes
|
|
66
|
+
|
|
67
|
+
### HIGH Storing Objects instead of Strings
|
|
68
|
+
Wrong:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
product.images = [ { url: '...', name: '...' } ]; // DB driver might fail or save as object
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Correct:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
product.images = JSON.stringify([ { url: '...', name: '...' } ]);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The library's internal helpers and TypeORM configuration for these columns often expect a stringified JSON. While some drivers handle object auto-serialization, manually ensuring string format prevents hydration issues.
|
|
81
|
+
|
|
82
|
+
Source: Denwa799/nest-shared:src/lib.ts:741
|
|
83
|
+
|
|
84
|
+
### MEDIUM Mismatched Folder Keys
|
|
85
|
+
Wrong:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const prepared = prepareTempImages(temp, 'products');
|
|
89
|
+
product.images = updateEntityImages(product.images, prepared); // Folder key mismatch logic
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Correct:
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
const folder = 'gallery';
|
|
96
|
+
const prepared = prepareTempImages(temp, folder);
|
|
97
|
+
product.images = updateEntityImages(product.images, prepared);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
While the `folder` key is primarily for S3 organization, inconsistent use during update cycles can lead to metadata being assigned to the wrong entity scope.
|
|
101
|
+
|
|
102
|
+
Source: Denwa799/nest-shared:src/lib.ts:762
|
|
103
|
+
|
|
104
|
+
## References
|
|
105
|
+
|
|
106
|
+
- [Image DTO Reference](Denwa799/nest-shared:src/dto.ts#L250)
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: schema-definition
|
|
3
|
+
description: >
|
|
4
|
+
Defining properties in DTOs and Entities using ApiValidationProperty to unify GraphQL, Swagger, and class-validation.
|
|
5
|
+
Includes Paginate, DeleteDto, IdsDto, and OkRequestDto for standardizing request/response shapes.
|
|
6
|
+
type: core
|
|
7
|
+
library: denwa-nest-shared
|
|
8
|
+
library_version: "1.0.78"
|
|
9
|
+
sources:
|
|
10
|
+
- "Denwa799/nest-shared:src/decorators.ts"
|
|
11
|
+
- "Denwa799/nest-shared:src/dto.ts"
|
|
12
|
+
---
|
|
13
|
+
|
|
14
|
+
# Unified Schema Definition
|
|
15
|
+
|
|
16
|
+
Use this skill to define data structures that are automatically compatible with NestJS GraphQL (Apollo), Swagger (OpenAPI), and class-validator using a single decorator.
|
|
17
|
+
|
|
18
|
+
## Setup
|
|
19
|
+
|
|
20
|
+
Define a standard DTO or Entity using `ApiValidationProperty`.
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { InputType } from '@nestjs/graphql';
|
|
24
|
+
import { ApiValidationProperty } from 'denwa-nest-shared';
|
|
25
|
+
|
|
26
|
+
@InputType()
|
|
27
|
+
export class CreateUserDto {
|
|
28
|
+
@ApiValidationProperty({
|
|
29
|
+
type: 'string',
|
|
30
|
+
description: 'User display name',
|
|
31
|
+
example: 'John Doe',
|
|
32
|
+
minLength: 2
|
|
33
|
+
})
|
|
34
|
+
name: string;
|
|
35
|
+
|
|
36
|
+
@ApiValidationProperty({
|
|
37
|
+
type: 'string',
|
|
38
|
+
isEmail: true,
|
|
39
|
+
description: 'Login email'
|
|
40
|
+
})
|
|
41
|
+
email: string;
|
|
42
|
+
|
|
43
|
+
@ApiValidationProperty({
|
|
44
|
+
type: 'boolean',
|
|
45
|
+
default: true,
|
|
46
|
+
optional: true
|
|
47
|
+
})
|
|
48
|
+
isActive?: boolean;
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Core Patterns
|
|
53
|
+
|
|
54
|
+
### Defining Enum Properties
|
|
55
|
+
Map TypeScript enums to GraphQL and Swagger simultaneously.
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
import { ApiValidationProperty } from 'denwa-nest-shared';
|
|
59
|
+
|
|
60
|
+
enum UserRole {
|
|
61
|
+
ADMIN = 'admin',
|
|
62
|
+
USER = 'user'
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export class UserDto {
|
|
66
|
+
@ApiValidationProperty({
|
|
67
|
+
type: 'string',
|
|
68
|
+
enumObject: UserRole,
|
|
69
|
+
swaggerType: 'UserRole'
|
|
70
|
+
})
|
|
71
|
+
role: UserRole;
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Pagination Request Shapes
|
|
76
|
+
Use the built-in `Paginate` class to standardize list query parameters.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
import { ArgsType } from '@nestjs/graphql';
|
|
80
|
+
import { Paginate, ApiValidationProperty } from 'denwa-nest-shared';
|
|
81
|
+
|
|
82
|
+
@ArgsType()
|
|
83
|
+
export class GetUsersArgs extends Paginate {
|
|
84
|
+
@ApiValidationProperty({ type: 'string', optional: true })
|
|
85
|
+
search?: string;
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Numeric and Date Types
|
|
90
|
+
Leverage internal type mapping for correct decorator generation.
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import { ApiValidationProperty } from 'denwa-nest-shared';
|
|
94
|
+
|
|
95
|
+
export class StatsDto {
|
|
96
|
+
@ApiValidationProperty({ type: 'int', min: 0 })
|
|
97
|
+
count: number;
|
|
98
|
+
|
|
99
|
+
@ApiValidationProperty({ type: 'float', max: 100 })
|
|
100
|
+
score: number;
|
|
101
|
+
|
|
102
|
+
@ApiValidationProperty({ type: 'date', optional: true })
|
|
103
|
+
lastLogin?: Date;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Common Mistakes
|
|
108
|
+
|
|
109
|
+
### CRITICAL Using Separate Decorators
|
|
110
|
+
Wrong:
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
@Field()
|
|
114
|
+
@ApiProperty()
|
|
115
|
+
@IsString()
|
|
116
|
+
@MinLength(2)
|
|
117
|
+
name: string;
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Correct:
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
@ApiValidationProperty({ type: 'string', minLength: 2 })
|
|
124
|
+
name: string;
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Using separate decorators misses the unified type mapping and default formatting provided by the library.
|
|
128
|
+
|
|
129
|
+
Source: maintainer interview
|
|
130
|
+
|
|
131
|
+
### HIGH Mismatched Validation for Type
|
|
132
|
+
Wrong:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
@ApiValidationProperty({ type: 'boolean', min: 10 })
|
|
136
|
+
isActive: boolean;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Correct:
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
@ApiValidationProperty({ type: 'int', min: 10 })
|
|
143
|
+
count: number;
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Validation parameters like `min`/`max` only apply to numeric types. Boolean types do not support these constraints.
|
|
147
|
+
|
|
148
|
+
Source: Denwa799/nest-shared:src/decorators.ts:160
|
|
149
|
+
|
|
150
|
+
### MEDIUM Misusing Optional with Default
|
|
151
|
+
Wrong:
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
@ApiValidationProperty({ type: 'string', optional: false, default: 'Guest' })
|
|
155
|
+
name: string;
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Correct:
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
@ApiValidationProperty({ type: 'string', optional: true, default: 'Guest' })
|
|
162
|
+
name: string;
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
If a property has a `default` value, it should typically be marked as `optional: true` so it registers correctly in the documentation as a non-required field for the consumer.
|
|
166
|
+
|
|
167
|
+
Source: Denwa799/nest-shared:src/decorators.ts:182
|