resting-squirrel-controller 2.5.0 → 2.6.1

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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2018 zabkwak
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2018 zabkwak
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,225 +1,225 @@
1
- # resting-squirrel-controller
2
- Controller for defining endpoints in [resting-squirrel](https://www.npmjs.com/package/resting-squirrel).
3
- Definitions of endpoints can be done with extending the `Controller` class and using decorators.
4
-
5
- ## Installation
6
- ```bash
7
- npm install resting-squirrel-controller --save
8
- ```
9
-
10
- ## Usage
11
- ### Javascript
12
- TBD
13
- ### Typescript
14
- #### DTO
15
- ```typescript
16
-
17
- import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
18
- import Controller from 'resting-squirrel-controller';
19
- import RSDto, { IRSDto } from 'resting-squirrel-dto'; // peer dependency
20
-
21
- class TestRequestDto implements IRSDto {
22
-
23
- @RequestDto.integer
24
- @RequestDto.required
25
- public id: number;
26
- }
27
-
28
- class TestResponseDto implements IRSDto {
29
-
30
- @ResponseDto.integer
31
- public id: number;
32
-
33
- @ResponseDto.string
34
- public status: string;
35
- }
36
-
37
- class TestDto implements IRSDto {
38
-
39
- @RSDto.integer
40
- @RSDto.required
41
- public id: number;
42
-
43
- @RSDto.string
44
- @RSDto.response
45
- public status: string;
46
- }
47
-
48
- @Controller.v(0)
49
- class TestController extends Controller {
50
-
51
- @Controller.get('/test')
52
- @Controller.dto(TestDto)
53
- @Controller.auth(RouteAuth.REQUIRED)
54
- public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
55
- return { status: 'get', id: req.query.id };
56
- }
57
-
58
- @Controller.put('/test')
59
- @Controller.params(TestRequestDto)
60
- @Controller.auth(RouteAuth.REQUIRED)
61
- @Controller.response(TestResponseDto)
62
- public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
63
- return { status: 'put', id: req.body.id };
64
- }
65
-
66
- @Controller.post('/test/:id')
67
- @Controller.params(TestRequestDto)
68
- @Controller.auth(RouteAuth.REQUIRED)
69
- @Controller.response(TestResponseDto)
70
- @Controller.args([new Field('id', Type.integer)])
71
- public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
72
- return { status: 'post', id: req.body.id };
73
- }
74
-
75
- @Controller.delete('/test/:id')
76
- @Controller.params(TestRequestDto)
77
- @Controller.auth(RouteAuth.REQUIRED)
78
- @Controller.emptyResponse
79
- @Controller.args([new Field('id', Type.integer)])
80
- public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
81
- return null;
82
- }
83
- }
84
-
85
- const app = rs();
86
-
87
- new TestController(app).register();
88
-
89
- app.start();
90
-
91
- ```
92
-
93
- #### DTO Legacy
94
- ```typescript
95
-
96
- import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
97
- import Controller from 'resting-squirrel-controller';
98
- import RSDto, { RequestDto, ResponseDto } from 'resting-squirrel-dto'; // peer dependency
99
-
100
- class TestRequestDto extends RequestDto {
101
-
102
- @RequestDto.integer
103
- @RequestDto.required
104
- public id: number;
105
- }
106
-
107
- class TestResponseDto extends RequestDto {
108
-
109
- @ResponseDto.integer
110
- public id: number;
111
-
112
- @ResponseDto.string
113
- public status: string;
114
- }
115
-
116
- class TestDto extends RSDto {
117
-
118
- @RSDto.integer
119
- @RSDto.required
120
- public id: number;
121
-
122
- @RSDto.string
123
- @RSDto.response
124
- public status: string;
125
- }
126
-
127
- @Controller.v(0)
128
- class TestController extends Controller {
129
-
130
- @Controller.get('/test')
131
- @Controller.dto(TestDto)
132
- @Controller.auth(RouteAuth.REQUIRED)
133
- public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
134
- return { status: 'get', id: req.query.id };
135
- }
136
-
137
- @Controller.put('/test')
138
- @Controller.params(TestRequestDto)
139
- @Controller.auth(RouteAuth.REQUIRED)
140
- @Controller.response(TestResponseDto)
141
- public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
142
- return { status: 'put', id: req.body.id };
143
- }
144
-
145
- @Controller.post('/test/:id')
146
- @Controller.params(TestRequestDto)
147
- @Controller.auth(RouteAuth.REQUIRED)
148
- @Controller.response(TestResponseDto)
149
- @Controller.args([new Field('id', Type.integer)])
150
- public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
151
- return { status: 'post', id: req.body.id };
152
- }
153
-
154
- @Controller.delete('/test/:id')
155
- @Controller.params(TestRequestDto)
156
- @Controller.auth(RouteAuth.REQUIRED)
157
- @Controller.emptyResponse
158
- @Controller.args([new Field('id', Type.integer)])
159
- public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
160
- return null;
161
- }
162
- }
163
-
164
- const app = rs();
165
-
166
- new TestController(app).register();
167
-
168
- app.start();
169
-
170
- ```
171
-
172
- ## Classes
173
- ### Connector
174
- #### Methods
175
- ##### `static registerDirectory(app: Application, directory: string): Promise<void>`
176
- Registers the directory with controllers to the `resting-squirrel` application.
177
- ##### `register(): void`
178
- Registers the controller to the `resting-squirrel` application.
179
- #### Decorators
180
- ##### Class
181
- Decorators for the `Controller` class.
182
- ###### `version(version: number)`
183
- Sets the version of all endpoint in the `Controller`.
184
- ###### `v(version: number)`
185
- Alias for `version` decorator.
186
- ###### `controllerOptions(options: IRouteOptions)`
187
- Class decorator to set some of route options to all endpoints.
188
- ##### Method
189
- Decorators for the `Controller` methods defining endpoint.
190
- ###### `put(route: string)`
191
- The endpoint is executed with `PUT` method.
192
- ###### `get(route: string)`
193
- The endpoint is executed with `GET` method.
194
- ###### `post(route: string)`
195
- The endpoint is executed with `POST` method.
196
- ###### `delete(route: string)`
197
- The endpoint is executed with `DELETE` method.
198
- ###### `deprecated`
199
- Marks the endpoint as deprecated.
200
- ###### `options(options: IRouteOptions)`
201
- Sets the options to the endpoint.
202
- ###### `option<K extends keyof IRouteOptions>(option: K, value: IRouteOptions[K])`
203
- Sets specific option to the endpoint.
204
- ###### `auth(auth: RouteAuth)`
205
- ###### `dto(dto: typeof BaseDto)`
206
- ###### `params(params: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof RequestDto)`
207
- ###### `response(response: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof ResponseDto)`
208
- ###### `errors(errors: Array<ErrorField>)`
209
- ###### `description(description: string)`
210
- ###### `hideDocs`
211
- Sets the `hideDocs` option to `true`.
212
- ###### `args(args: Array<Field> | typeof ArgsDto)`
213
- ###### `requireApiKey(requireApiKey: boolean)`
214
- ###### `excludeApiKeys(excludeApiKeys: (() => Promise<Array<string>>) | Array<string>))`
215
- ###### `timeout(timeout: number)`
216
- ###### `<IProps = {[key: string]: any}>props(props: IProps)`
217
- ###### `emptyResponse`
218
- Sets the endpoint as empty. It returns 204 status code.
219
-
220
- ## Migration to v2
221
- There are no breaking changes in the v2 except the peer dependency on the `resting-squirrel-dto` module.
222
-
223
- ## TODO
224
- ### v3
1
+ # resting-squirrel-controller
2
+ Controller for defining endpoints in [resting-squirrel](https://www.npmjs.com/package/resting-squirrel).
3
+ Definitions of endpoints can be done with extending the `Controller` class and using decorators.
4
+
5
+ ## Installation
6
+ ```bash
7
+ npm install resting-squirrel-controller --save
8
+ ```
9
+
10
+ ## Usage
11
+ ### Javascript
12
+ TBD
13
+ ### Typescript
14
+ #### DTO
15
+ ```typescript
16
+
17
+ import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
18
+ import Controller from 'resting-squirrel-controller';
19
+ import RSDto, { IRSDto } from 'resting-squirrel-dto'; // peer dependency
20
+
21
+ class TestRequestDto implements IRSDto {
22
+
23
+ @RequestDto.integer
24
+ @RequestDto.required
25
+ public id: number;
26
+ }
27
+
28
+ class TestResponseDto implements IRSDto {
29
+
30
+ @ResponseDto.integer
31
+ public id: number;
32
+
33
+ @ResponseDto.string
34
+ public status: string;
35
+ }
36
+
37
+ class TestDto implements IRSDto {
38
+
39
+ @RSDto.integer
40
+ @RSDto.required
41
+ public id: number;
42
+
43
+ @RSDto.string
44
+ @RSDto.response
45
+ public status: string;
46
+ }
47
+
48
+ @Controller.v(0)
49
+ class TestController extends Controller {
50
+
51
+ @Controller.get('/test')
52
+ @Controller.dto(TestDto)
53
+ @Controller.auth(RouteAuth.REQUIRED)
54
+ public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
55
+ return { status: 'get', id: req.query.id };
56
+ }
57
+
58
+ @Controller.put('/test')
59
+ @Controller.params(TestRequestDto)
60
+ @Controller.auth(RouteAuth.REQUIRED)
61
+ @Controller.response(TestResponseDto)
62
+ public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
63
+ return { status: 'put', id: req.body.id };
64
+ }
65
+
66
+ @Controller.post('/test/:id')
67
+ @Controller.params(TestRequestDto)
68
+ @Controller.auth(RouteAuth.REQUIRED)
69
+ @Controller.response(TestResponseDto)
70
+ @Controller.args([new Field('id', Type.integer)])
71
+ public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
72
+ return { status: 'post', id: req.body.id };
73
+ }
74
+
75
+ @Controller.delete('/test/:id')
76
+ @Controller.params(TestRequestDto)
77
+ @Controller.auth(RouteAuth.REQUIRED)
78
+ @Controller.emptyResponse
79
+ @Controller.args([new Field('id', Type.integer)])
80
+ public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
81
+ return null;
82
+ }
83
+ }
84
+
85
+ const app = rs();
86
+
87
+ new TestController(app).register();
88
+
89
+ app.start();
90
+
91
+ ```
92
+
93
+ #### DTO Legacy
94
+ ```typescript
95
+
96
+ import rs, { Field, IRequest, RouteAuth, Type } from 'resting-squirrel'; // peer dependency
97
+ import Controller from 'resting-squirrel-controller';
98
+ import RSDto, { RequestDto, ResponseDto } from 'resting-squirrel-dto'; // peer dependency
99
+
100
+ class TestRequestDto extends RequestDto {
101
+
102
+ @RequestDto.integer
103
+ @RequestDto.required
104
+ public id: number;
105
+ }
106
+
107
+ class TestResponseDto extends RequestDto {
108
+
109
+ @ResponseDto.integer
110
+ public id: number;
111
+
112
+ @ResponseDto.string
113
+ public status: string;
114
+ }
115
+
116
+ class TestDto extends RSDto {
117
+
118
+ @RSDto.integer
119
+ @RSDto.required
120
+ public id: number;
121
+
122
+ @RSDto.string
123
+ @RSDto.response
124
+ public status: string;
125
+ }
126
+
127
+ @Controller.v(0)
128
+ class TestController extends Controller {
129
+
130
+ @Controller.get('/test')
131
+ @Controller.dto(TestDto)
132
+ @Controller.auth(RouteAuth.REQUIRED)
133
+ public async getTest(req: IRequest<{}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
134
+ return { status: 'get', id: req.query.id };
135
+ }
136
+
137
+ @Controller.put('/test')
138
+ @Controller.params(TestRequestDto)
139
+ @Controller.auth(RouteAuth.REQUIRED)
140
+ @Controller.response(TestResponseDto)
141
+ public async createTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
142
+ return { status: 'put', id: req.body.id };
143
+ }
144
+
145
+ @Controller.post('/test/:id')
146
+ @Controller.params(TestRequestDto)
147
+ @Controller.auth(RouteAuth.REQUIRED)
148
+ @Controller.response(TestResponseDto)
149
+ @Controller.args([new Field('id', Type.integer)])
150
+ public async updateTest(req: IRequest<{}, {}, TestRequestDto>): Promise<Partial<TestResponseDto>> {
151
+ return { status: 'post', id: req.body.id };
152
+ }
153
+
154
+ @Controller.delete('/test/:id')
155
+ @Controller.params(TestRequestDto)
156
+ @Controller.auth(RouteAuth.REQUIRED)
157
+ @Controller.emptyResponse
158
+ @Controller.args([new Field('id', Type.integer)])
159
+ public async deleteTest(req: IRequest<{}, {}, TestRequestDto>): Promise<null> {
160
+ return null;
161
+ }
162
+ }
163
+
164
+ const app = rs();
165
+
166
+ new TestController(app).register();
167
+
168
+ app.start();
169
+
170
+ ```
171
+
172
+ ## Classes
173
+ ### Connector
174
+ #### Methods
175
+ ##### `static registerDirectory(app: Application, directory: string): Promise<void>`
176
+ Registers the directory with controllers to the `resting-squirrel` application.
177
+ ##### `register(): void`
178
+ Registers the controller to the `resting-squirrel` application.
179
+ #### Decorators
180
+ ##### Class
181
+ Decorators for the `Controller` class.
182
+ ###### `version(version: number)`
183
+ Sets the version of all endpoint in the `Controller`.
184
+ ###### `v(version: number)`
185
+ Alias for `version` decorator.
186
+ ###### `controllerOptions(options: IRouteOptions)`
187
+ Class decorator to set some of route options to all endpoints.
188
+ ##### Method
189
+ Decorators for the `Controller` methods defining endpoint.
190
+ ###### `put(route: string)`
191
+ The endpoint is executed with `PUT` method.
192
+ ###### `get(route: string)`
193
+ The endpoint is executed with `GET` method.
194
+ ###### `post(route: string)`
195
+ The endpoint is executed with `POST` method.
196
+ ###### `delete(route: string)`
197
+ The endpoint is executed with `DELETE` method.
198
+ ###### `deprecated`
199
+ Marks the endpoint as deprecated.
200
+ ###### `options(options: IRouteOptions)`
201
+ Sets the options to the endpoint.
202
+ ###### `option<K extends keyof IRouteOptions>(option: K, value: IRouteOptions[K])`
203
+ Sets specific option to the endpoint.
204
+ ###### `auth(auth: RouteAuth)`
205
+ ###### `dto(dto: typeof BaseDto)`
206
+ ###### `params(params: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof RequestDto)`
207
+ ###### `response(response: (new (...args: any[]) => IRSDto) | typeof BaseDto | typeof ResponseDto)`
208
+ ###### `errors(errors: Array<ErrorField>)`
209
+ ###### `description(description: string)`
210
+ ###### `hideDocs`
211
+ Sets the `hideDocs` option to `true`.
212
+ ###### `args(args: Array<Field> | typeof ArgsDto)`
213
+ ###### `requireApiKey(requireApiKey: boolean)`
214
+ ###### `excludeApiKeys(excludeApiKeys: (() => Promise<Array<string>>) | Array<string>))`
215
+ ###### `timeout(timeout: number)`
216
+ ###### `<IProps = {[key: string]: any}>props(props: IProps)`
217
+ ###### `emptyResponse`
218
+ Sets the endpoint as empty. It returns 204 status code.
219
+
220
+ ## Migration to v2
221
+ There are no breaking changes in the v2 except the peer dependency on the `resting-squirrel-dto` module.
222
+
223
+ ## TODO
224
+ ### v3
225
225
  - Replace deprecated static endpoint decorators with controller decorators.