@galeh/chuka 1.0.6 → 1.0.8
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 +77 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Purpose
|
|
2
|
+
This package aims at making the API programming much easier, using the expressjs as underlying library, it creates fast API's. It adds leverages expressjs by adding strong types, dependency injection and profound validation.
|
|
3
|
+
|
|
4
|
+
# Example
|
|
5
|
+
|
|
6
|
+
```typescript
|
|
7
|
+
// main.ts
|
|
8
|
+
const app = createApp({
|
|
9
|
+
dependencies: [
|
|
10
|
+
{
|
|
11
|
+
provide: 'catservice',
|
|
12
|
+
useClass: CatsService
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
routes: [
|
|
16
|
+
{
|
|
17
|
+
path: '/cats',
|
|
18
|
+
controller: CatsController
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
middlewares: [
|
|
22
|
+
json()
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
app.use((error: any, req: any, res: any, next: any) => {
|
|
27
|
+
res.status(400).json(error);
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
app.listen(8080, () => {
|
|
31
|
+
console.log('running application!');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// example for a controller
|
|
38
|
+
|
|
39
|
+
@injectable()
|
|
40
|
+
export class CatsController extends Controller {
|
|
41
|
+
constructor(@inject('catservice') service: CatsServiceInterface) {
|
|
42
|
+
super();
|
|
43
|
+
|
|
44
|
+
const intercepted = this.middleware(
|
|
45
|
+
createLogger()
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
intercepted.middleware(
|
|
49
|
+
bodyValidator<CatModel>({
|
|
50
|
+
name: and(isDefined(), isString()),
|
|
51
|
+
country: isNumber(),
|
|
52
|
+
age: custom(model => Promise.resolve(!!(model.age && model.age > 2))),
|
|
53
|
+
parents: {
|
|
54
|
+
name: isString(),
|
|
55
|
+
parents: {
|
|
56
|
+
country: isString(),
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
).post('/', async (req, res) => {{
|
|
61
|
+
const allcats = await service.add(req.body.name);
|
|
62
|
+
res.send(allcats);
|
|
63
|
+
}});
|
|
64
|
+
|
|
65
|
+
intercepted.get('/', async (req, res) => {{
|
|
66
|
+
const allcats = await service.findAll();
|
|
67
|
+
res.send(allcats);
|
|
68
|
+
}});
|
|
69
|
+
|
|
70
|
+
intercepted.get('/:id', async (req, res) => {
|
|
71
|
+
const onecat = await service.findOne(+req.params.id);
|
|
72
|
+
res.send(onecat);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
```
|