-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcontroller.ts
141 lines (135 loc) · 4.27 KB
/
controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { Request, Response } from 'express';
import { inject } from 'inversify';
import {
BaseHttpController,
controller,
httpGet,
httpPost,
request,
requestParam,
response
} from 'inversify-express-utils';
import { timeout } from 'rxjs/operators';
import SERVICE_IDENTIFIER from '../../../common/constants/identifiers';
import { ILogger, IMetrics } from '../../../common/interfaces';
import { authMiddleware } from '../../../common/middleware/auth-middleware';
import { IExample } from '../../interfaces';
import { HttpError, Quote } from '../../models';
import { ErrorResponseBuilder, HttpStatus } from '../../services';
/**
* Examples Controller
* JWT Auth middleware as well as logging middleware added
*/
@controller(
'/examples',
authMiddleware({ role: 'admin' } as any),
SERVICE_IDENTIFIER.LOGGER_MIDDLEWARE
)
class ExampleController extends BaseHttpController {
public exampleService: IExample;
public loggerService: ILogger;
public metricsService: IMetrics;
public constructor(
@inject(SERVICE_IDENTIFIER.EXAMPLE) exampleService: IExample,
@inject(SERVICE_IDENTIFIER.LOGGER) loggerService: ILogger,
@inject(SERVICE_IDENTIFIER.METRICS) metricsService: IMetrics
) {
super();
this.exampleService = exampleService;
this.loggerService = loggerService;
this.metricsService = metricsService;
}
/**
* Get all items in the examples collection
* @param req request
* @param res response
*/
@httpGet('/')
public async all(@request() req: Request, @response() res: Response) {
return this.exampleService.all();
}
/**
* Get Posts by ID
* @param id post ID
* @param req request
* @param res response
*/
@httpGet('/:id')
public async byPostsByID(
@requestParam('id') id: number,
@request() req: Request,
@response() res: Response
) {
this.loggerService.info(req.originalUrl);
return new Promise((resolve, reject) => {
this.exampleService
.byPostsByID(id)
.pipe(timeout(+process.env.API_TIME_OUT))
.subscribe(
result => {
this.loggerService.info(result.data as Quote);
this.loggerService.info(result.timings);
this.loggerService.logAPITrace(req, res, HttpStatus.OK);
this.metricsService.logAPIMetrics(req, res, HttpStatus.OK);
resolve(result.data);
},
err => {
const error: HttpError = err as HttpError;
const resp = new ErrorResponseBuilder()
.setTitle(error.name)
.setStatus(HttpStatus.NOT_FOUND)
.setDetail(error.stack)
.setMessage(error.message)
.setSource(req.url)
.build();
this.loggerService.logAPITrace(req, res, HttpStatus.NOT_FOUND);
this.metricsService.logAPIMetrics(req, res, HttpStatus.NOT_FOUND);
reject(resp);
}
);
});
}
/**
* Check by ID
* @param req request
* @param res response
*/
public byId(req: Request, res: Response): void {
this.exampleService.byId(+req.params.id).then(
r => {
if (r) {
res.json(r);
this.loggerService.logAPITrace(req, res, HttpStatus.OK);
this.metricsService.logAPIMetrics(req, res, HttpStatus.OK);
} else {
res.status(HttpStatus.NOT_FOUND).end();
this.loggerService.logAPITrace(req, res, HttpStatus.NOT_FOUND);
this.metricsService.logAPIMetrics(req, res, HttpStatus.NOT_FOUND);
}
},
error => {
const resp = new ErrorResponseBuilder()
.setTitle(error.name)
.setStatus(HttpStatus.NOT_FOUND)
.setDetail(error.stack)
.setMessage(error.message)
.setSource(req.url)
.build();
res.status(HttpStatus.NOT_FOUND).json(resp);
this.loggerService.logAPITrace(req, res, HttpStatus.NOT_FOUND);
this.metricsService.logAPIMetrics(req, res, HttpStatus.NOT_FOUND);
}
);
}
/**
* Create request sample
* Add a new element to the in memory Sample object
* @param req request
* @param res response
*/
@httpPost('/')
public async create(@request() req: Request, @response() res: Response) {
return this.exampleService.create(req.body.name);
}
}
export default ExampleController;