web_api_base 2.7.3 → 3.2.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/dist/Application.js +81 -7
- package/dist/Application.js.map +1 -1
- package/dist/controllers/base/ControllerBase.d.ts +7 -4
- package/dist/controllers/base/ControllerBase.js +28 -4
- package/dist/controllers/base/ControllerBase.js.map +1 -1
- package/dist/decorators/controllers/ControllerDecorators.d.ts +23 -19
- package/dist/decorators/controllers/ControllerDecorators.js +67 -49
- package/dist/decorators/controllers/ControllerDecorators.js.map +1 -1
- package/dist/decorators/validations/ValidationDecorators.d.ts +50 -0
- package/dist/decorators/validations/ValidationDecorators.js +181 -0
- package/dist/decorators/validations/ValidationDecorators.js.map +1 -0
- package/dist/index.d.ts +14 -4
- package/dist/index.js +62 -4
- package/dist/index.js.map +1 -1
- package/dist/metadata/OwnMetaDataContainer.d.ts +12 -0
- package/dist/metadata/OwnMetaDataContainer.js +33 -0
- package/dist/metadata/OwnMetaDataContainer.js.map +1 -0
- package/dist/temp/Application.d.ts +5 -0
- package/dist/temp/Application.js +27 -0
- package/dist/temp/Application.js.map +1 -0
- package/dist/temp/Index.d.ts +1 -0
- package/dist/temp/Index.js +8 -0
- package/dist/temp/Index.js.map +1 -0
- package/dist/temp/controllers/StatusController.d.ts +8 -0
- package/dist/temp/controllers/StatusController.js +58 -0
- package/dist/temp/controllers/StatusController.js.map +1 -0
- package/dist/temp/service/SampleService.d.ts +9 -0
- package/dist/temp/service/SampleService.js +19 -0
- package/dist/temp/service/SampleService.js.map +1 -0
- package/package.json +1 -1
- package/readme.md +223 -7
package/readme.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
web_api_base is a npm packaged that allows to create web-apis like MVC of .NET
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
# Installation
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
@@ -11,7 +11,7 @@ npm install web_api_base
|
|
|
11
11
|
```
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
# Usage
|
|
15
15
|
|
|
16
16
|
First of all we need implement the abstract class __Application__.
|
|
17
17
|
After that, we need to create some controllers and they must inherit the abstract class __ControllerBase__.
|
|
@@ -26,14 +26,14 @@ npx create-controller
|
|
|
26
26
|
|
|
27
27
|
```typescript
|
|
28
28
|
|
|
29
|
-
import { ControllerBase, Route,
|
|
29
|
+
import { ControllerBase, Route, GET } from "web_api_base";
|
|
30
30
|
|
|
31
31
|
|
|
32
32
|
@Route()
|
|
33
33
|
export default class SampleController extends ControllerBase
|
|
34
34
|
{
|
|
35
35
|
|
|
36
|
-
@
|
|
36
|
+
@GET()
|
|
37
37
|
public Hello() : void
|
|
38
38
|
{
|
|
39
39
|
this.OK({message: "Hello Word!"})
|
|
@@ -78,7 +78,7 @@ import Application from './Application';
|
|
|
78
78
|
new Application().StartAsync();
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
# Dependecy Injection
|
|
82
82
|
Consider this abstraction of a service and some imnplementations
|
|
83
83
|
|
|
84
84
|
### ./services/SampleService.ts
|
|
@@ -110,7 +110,7 @@ We can use the DI service like this
|
|
|
110
110
|
|
|
111
111
|
```typescript
|
|
112
112
|
|
|
113
|
-
import { ControllerBase, Route,
|
|
113
|
+
import { ControllerBase, Route, GET, Inject } from "web_api_base";
|
|
114
114
|
import {SampleServiceAbstract } from '../services/SampleService.ts';
|
|
115
115
|
|
|
116
116
|
@Route()
|
|
@@ -125,7 +125,7 @@ export default class SampleController extends ControllerBase
|
|
|
125
125
|
this.SomeDepency = someDependecy ;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
@
|
|
128
|
+
@GET()
|
|
129
129
|
public Hello() : void
|
|
130
130
|
{
|
|
131
131
|
this.OK({message: "Hello Word!"})
|
|
@@ -166,6 +166,222 @@ export default class App extends Application
|
|
|
166
166
|
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
+
|
|
170
|
+
# HTTP Verbs decorators
|
|
171
|
+
|
|
172
|
+
### @GET()
|
|
173
|
+
Create a GET endpoint
|
|
174
|
+
|
|
175
|
+
### @PUT()
|
|
176
|
+
Create a PUT endpoint
|
|
177
|
+
|
|
178
|
+
### @POST()
|
|
179
|
+
Create a POST endpoint
|
|
180
|
+
|
|
181
|
+
### @DELETE()
|
|
182
|
+
Create a DELETE endpoint
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
# Model Bind decorators
|
|
186
|
+
|
|
187
|
+
### @FromBody()
|
|
188
|
+
Extract a method parameter type instance from body of request
|
|
189
|
+
|
|
190
|
+
```json
|
|
191
|
+
{
|
|
192
|
+
"Name": "Adriano Marino Balera",
|
|
193
|
+
"Email": "adriano@gmail.com",
|
|
194
|
+
"Age" : 30
|
|
195
|
+
}
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
@POST()
|
|
200
|
+
public async Insert(@FromBody()user : User) : Promise<void>
|
|
201
|
+
{
|
|
202
|
+
this.OK(await this._service.AddAsync(user));
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
In the example above, the __model binding system__ will cast the body in a intance of type __User__.
|
|
206
|
+
|
|
207
|
+
We can extract some part of body using named FromBody args: __@FromBody('user')__. The __model binding system__ will use the 'user' property of body json.
|
|
208
|
+
|
|
209
|
+
```json
|
|
210
|
+
{
|
|
211
|
+
"user" :
|
|
212
|
+
{
|
|
213
|
+
"Name": "Adriano Marino Balera",
|
|
214
|
+
"Email": "adriano@gmail.com",
|
|
215
|
+
"Age" : 30
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
### @FromQuery()
|
|
220
|
+
Extract the method parameter from query string of request
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
@GET()
|
|
224
|
+
public async GetById(@FromQuery()id : number) : Promise<void>
|
|
225
|
+
{
|
|
226
|
+
this.OK(await this._service.GetByIdAsync(id));
|
|
227
|
+
}
|
|
228
|
+
```
|
|
229
|
+
In the example above, the __model binding system__ will get the first query argument of request.
|
|
230
|
+
We can also determine the name of parameter: __@FromQuery('id')__.
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
## Sample of a complete controller
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
|
|
238
|
+
import { ControllerBase, Route, POST, PUT, DELETE, GET, Inject, Validate, FromBody, FromQuery } from "web_api_base";
|
|
239
|
+
import AbstractUserService from "../core/abstractions/AbstractUserService";
|
|
240
|
+
import User from "../core/entities/User";
|
|
241
|
+
|
|
242
|
+
@Validate()
|
|
243
|
+
@Route('/v1/users/')
|
|
244
|
+
export default class UserController extends ControllerBase
|
|
245
|
+
{
|
|
246
|
+
@Inject()
|
|
247
|
+
private _service : AbstractUserService;
|
|
248
|
+
|
|
249
|
+
constructor(service : AbstractUserService)
|
|
250
|
+
{
|
|
251
|
+
super();
|
|
252
|
+
this._service = service;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
@GET("list")
|
|
256
|
+
public async GetAll() : Promise<void>
|
|
257
|
+
{
|
|
258
|
+
this.OK(await this._service.GetAllAsync());
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
@GET("permissions")
|
|
262
|
+
public async GetAllPermissions() : Promise<void>
|
|
263
|
+
{
|
|
264
|
+
this.OK(await this._service.GetAllPermissions());
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
@GET()
|
|
268
|
+
public async GetById(@FromQuery("id")id : number) : Promise<void>
|
|
269
|
+
{
|
|
270
|
+
this.OK(await this._service.GetByIdAsync(id));
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
@POST()
|
|
274
|
+
public async Insert(@FromBody()user : User) : Promise<void>
|
|
275
|
+
{
|
|
276
|
+
this.OK(await this._service.AddAsync(user));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
@PUT()
|
|
280
|
+
public async Update(@FromBody()user : User, ) : Promise<void>
|
|
281
|
+
{
|
|
282
|
+
if(user.Id == undefined || user.Id <= 0)
|
|
283
|
+
return this.BadRequest({ Message : "The ID must be greater than 0"});
|
|
284
|
+
|
|
285
|
+
this.OK(await this._service.UpdateAsync(user));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
@DELETE()
|
|
289
|
+
public async Delete(@FromQuery()id : number) : Promise<void>
|
|
290
|
+
{
|
|
291
|
+
let del = await this._service.GetByIdAsync(id);
|
|
292
|
+
|
|
293
|
+
if(!del)
|
|
294
|
+
return this.NotFound();
|
|
295
|
+
|
|
296
|
+
this.OK(await this._service.DeleteAsync(del));
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
# Validation decorators
|
|
304
|
+
|
|
305
|
+
### @Validate()
|
|
306
|
+
Say that all arguments from model bind will be validated before injected on the controller action.
|
|
307
|
+
This decorator must be used in the controller declaration.
|
|
308
|
+
```typescript
|
|
309
|
+
@Validate()
|
|
310
|
+
@Route('v1/users/')
|
|
311
|
+
export default class UserController extends ControllerBase
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
### @Required()
|
|
315
|
+
Determine whether a property of a class is required
|
|
316
|
+
|
|
317
|
+
### @Max(max : number)
|
|
318
|
+
Determine the maximun value of a number property
|
|
319
|
+
|
|
320
|
+
### @Min(min: number)
|
|
321
|
+
Determine the minimun value of a number property
|
|
322
|
+
|
|
323
|
+
### @MaxLenght(max : number)
|
|
324
|
+
Determine the maximun number of characters of a string
|
|
325
|
+
|
|
326
|
+
### @MaxLenght(min : number)
|
|
327
|
+
Determine the minumun number of characters of a string
|
|
328
|
+
|
|
329
|
+
### @Regex(exp : RegExp)
|
|
330
|
+
Determine is a string field match determined pattern
|
|
331
|
+
|
|
332
|
+
### @Rule<T>(action : (arg : T) => boolean)
|
|
333
|
+
Check whether some property respect some validation rule
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
### Sample of a complete object
|
|
337
|
+
|
|
338
|
+
```typescript
|
|
339
|
+
|
|
340
|
+
import {Required, MaxLenght, MinLenght, Rule, Max, Min, Regex} from 'web_api_base';
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
export default class ValidatedObject
|
|
344
|
+
{
|
|
345
|
+
@Max(10)
|
|
346
|
+
public MaxValue : number;
|
|
347
|
+
|
|
348
|
+
@Min(10)
|
|
349
|
+
public MinValue : number;
|
|
350
|
+
|
|
351
|
+
@Min(10)
|
|
352
|
+
@Max(20)
|
|
353
|
+
public Range: number;
|
|
354
|
+
|
|
355
|
+
@Regex(/^[^\s@]+@[^\s@]+\.[^\s@]+$/)
|
|
356
|
+
public RegExp : string;
|
|
357
|
+
|
|
358
|
+
@Required()
|
|
359
|
+
public Required : string;
|
|
360
|
+
|
|
361
|
+
@MaxLenght(20)
|
|
362
|
+
public MaxLenght : string;
|
|
363
|
+
|
|
364
|
+
@MinLenght(10)
|
|
365
|
+
public MinLenght : string;
|
|
366
|
+
|
|
367
|
+
@Rule<string[]>(p => p.length > 5)
|
|
368
|
+
public Permissions : string[];
|
|
369
|
+
|
|
370
|
+
constructor()
|
|
371
|
+
{
|
|
372
|
+
this.MaxValue = -1;
|
|
373
|
+
this.MinValue = -1;
|
|
374
|
+
this.Range = -1;
|
|
375
|
+
this.Required = "";
|
|
376
|
+
this.MaxLenght = "";
|
|
377
|
+
this.MinLenght = "";
|
|
378
|
+
this.RegExp = "";
|
|
379
|
+
this.Permissions = [];
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
|
|
169
385
|
## Contributing
|
|
170
386
|
|
|
171
387
|
Pull requests are welcome. For major changes, please open an issue first
|