nicot 1.0.7 → 1.0.10
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/README.md +124 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,3 +1,127 @@
|
|
|
1
1
|
# nicot
|
|
2
2
|
|
|
3
3
|
Nest.js interacting with class-validator + OpenAPI + TypeORM for Nest.js Restful API development.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
In your Nest.js project, run the following command:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @nestjs/swagger typeorm @nestjs/typeorm class-validator class-transformer nicot
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Entity
|
|
14
|
+
|
|
15
|
+
Those decorators would all decorate the following, with the SAME settings.
|
|
16
|
+
|
|
17
|
+
- TypeORM `@Entity()` settings.
|
|
18
|
+
- class-validator validation settings.
|
|
19
|
+
- `@nestjs/swagger` `@ApiProperty()` settings.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
@Entity()
|
|
23
|
+
export class User extends IdBase() {
|
|
24
|
+
@Index()
|
|
25
|
+
@StringColumn(5, {
|
|
26
|
+
required: true,
|
|
27
|
+
description: 'User name',
|
|
28
|
+
})
|
|
29
|
+
name: string;
|
|
30
|
+
|
|
31
|
+
@IntColumn('int', { unsigned: true, description: 'User age', default: 20 })
|
|
32
|
+
age: number;
|
|
33
|
+
|
|
34
|
+
@EnumColumn(Gender, { description: 'User gender' })
|
|
35
|
+
gender: Gender;
|
|
36
|
+
|
|
37
|
+
@NotColumn()
|
|
38
|
+
somethingElse: any; // Would not come from client input, and would not go into OpenAPI document.
|
|
39
|
+
}
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
There are also other following decorators to control accessibility:
|
|
43
|
+
|
|
44
|
+
- `@NotWritable()` Can only come from GET requests.
|
|
45
|
+
- `@NotChangeable()` Cannot be changed by PATCH requests.
|
|
46
|
+
|
|
47
|
+
## CrudService
|
|
48
|
+
|
|
49
|
+
Creates a service for database operation in one word.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
@Injectable()
|
|
53
|
+
export class UserService extends CrudService(User) {
|
|
54
|
+
constructor(@InjectDataSource() db: DataSource) {
|
|
55
|
+
super(db.getRepository(User));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Controller decorators
|
|
61
|
+
|
|
62
|
+
Would also register proper OpenAPI documentation for the controller.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const dec = new RestfulFactory(User);
|
|
66
|
+
class UpdateUserDto extends dec.updateDto {} // to extract type and class
|
|
67
|
+
|
|
68
|
+
@Controller('user')
|
|
69
|
+
export class UserController {
|
|
70
|
+
constructor(private userService: UserService) {}
|
|
71
|
+
|
|
72
|
+
@dec.create() // POST /
|
|
73
|
+
create(@dec.createParam() user: User) {
|
|
74
|
+
return this.userService.create(user);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@dec.findOne() // GET /:id
|
|
78
|
+
findOne(@dec.idParam() id: number) {
|
|
79
|
+
return this.userService.findOne(id);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
@dec.findAll() // GET /
|
|
83
|
+
findAll(@dec.findAllParam() user: UpdateUserDto) {
|
|
84
|
+
return this.userService.findAll(user);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
@dec.update() // PATH /:id
|
|
88
|
+
update(@dec.idParam() id: number, @dec.updateParam() user: UpdateUserDto) {
|
|
89
|
+
return this.userService.update(id, user);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
@dec.delete() // DELETE /:id
|
|
93
|
+
delete(@dec.idParam() id: number) {
|
|
94
|
+
return this.userService.delete(id);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Return message
|
|
100
|
+
|
|
101
|
+
Return data of all APIs are in the following format, with proper OpenAPI documentation:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
export interface ReturnMessage<T> {
|
|
105
|
+
statusCode: number;
|
|
106
|
+
message: string;
|
|
107
|
+
success: boolean;
|
|
108
|
+
data: T;
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
You may also create a Dto class like this by the following way:
|
|
113
|
+
|
|
114
|
+
```ts
|
|
115
|
+
export class UserReturnMessage extends ReturnMessageDto(User) {}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
With result into the following class, also with proper OpenAPI documentation:
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
export class UserReturnMessage {
|
|
122
|
+
statusCode: number;
|
|
123
|
+
message: string;
|
|
124
|
+
success: boolean;
|
|
125
|
+
data: User;
|
|
126
|
+
}
|
|
127
|
+
```
|
package/package.json
CHANGED