-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.ts
More file actions
231 lines (188 loc) · 5.72 KB
/
Copy pathsample.ts
File metadata and controls
231 lines (188 loc) · 5.72 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import { Body, Controller, Delete, FromParams, FromQuery, Get, Post } from "../decorators";
import Express from 'express';
import { addController } from "../controller";
import bodyParser from "body-parser";
import { NextFunction, Request, Response, Router } from "express";
import { container as globalContainer, inject, injectable, Lifecycle } from 'tsyringe';
import { createApp } from "../application";
import { User, UserNotFoundError, UserRepository } from "./user-repository";
const container = globalContainer.createChildContainer();
const app = createApp(container);
const UserCtrlNameSym = Symbol('user-controller-name');
@injectable()
class CalcService {
add(a: number, b: number): number {
return a + b;
}
sub(a: number, b: number): number {
return a - b;
}
mult(a: number, b: number): number {
return a * b;
}
div(a: number, b: number): number {
return a / b;
}
}
container.register<CalcService>(CalcService, CalcService, {lifecycle: Lifecycle.Singleton});
container.register<string>(UserCtrlNameSym, {useValue: 'user-controller'});
container.registerSingleton(UserRepository);
app.use(bodyParser.json());
interface UserDTO {
name: string;
age: number;
}
interface UserEntryDTO extends UserDTO {
id: string;
}
interface StatusResponse {
status: string;
msg: string;
}
class HttpError extends Error {
constructor(public statusCode: number, msg: string) {
super(msg);
}
}
class HttpNotFoundError extends HttpError {
constructor(msg: string) {
super(404, msg);
}
}
class HttpInternalServerError extends HttpError {
constructor(msg: string) {
super(501, msg);
}
}
const users: {[id: string]: UserDTO} = {
'000001': {
name: 'John',
age: 43,
},
'000002': {
name: 'Max',
age: 15,
},
'000003': {
name: 'George',
age: 35,
},
};
function delayAsync(millies: number): Promise<void> {
return new Promise<void>((resolve) => {
setTimeout(() => resolve(), millies);
});
}
type ToHttpError = (err: any) => HttpError;
@Controller
class UserController {
private transformErrorMap: {[className: string]: ToHttpError} = {
[UserNotFoundError.name]: (err) => {
const notFoundError = err as UserNotFoundError;
return new HttpNotFoundError(notFoundError.message);
},
};
constructor(
@inject(UserCtrlNameSym) private ctrlName: string,
private userRepo: UserRepository,
) {}
@Get('/')
async getUsers(): Promise<User[]> {
try {
await delayAsync(4000);
return this.userRepo.getUsers();
} catch (err) {
throw this.transformError(err);
}
}
@Get('/hello')
async getLog(@FromQuery('name') name: string): Promise<string> {
try {
await delayAsync(2000);
return `hello ${name} from ${this.ctrlName}`;
} catch (err) {
throw this.transformError(err);
}
}
@Get('/:id')
getUser(@FromParams('id') userId: string): User {
try {
return this.userRepo.getUser(userId);
} catch (err) {
throw this.transformError(err);
}
}
@Post('/:id')
postUser(@FromParams('id') userId: string, @Body user: UserDTO): User {
try {
this.userRepo.createOrUpdateUser(userId, user);
return {id: userId, ...user};
} catch(err) {
throw this.transformError(err);
}
}
@Delete('/:id')
deleteUser(@FromParams('id') userId: string): StatusResponse {
try {
this.userRepo.removeUser(userId);
return {
status: 'success',
msg: `deleted user with id ${userId}`,
};
} catch(err) {
throw this.transformError(err);
}
}
transformError(err: any): HttpError {
const transform = this.transformErrorMap[err.constructor.name];
if (transform != null) {
return transform(err);
}
if (err instanceof Error) {
return new HttpInternalServerError(err.message);
}
return new HttpInternalServerError('unexpected error');
}
}
interface CalcResultDTO {
result: number;
}
@Controller
class CalculatorController {
constructor(private calcService: CalcService) {}
@Get('/add')
async add(@FromQuery('a') a: string, @FromQuery('b') b: string): Promise<CalcResultDTO> {
await delayAsync(1000);
return { result: this.calcService.add(+a, +b)};
}
@Get('/sub')
async sub(@FromQuery('a') a: string, @FromQuery('b') b: string): Promise<CalcResultDTO> {
await delayAsync(1000);
return { result: this.calcService.sub(+a, +b)};
}
@Get('/mult')
async mult(@FromQuery('a') a: string, @FromQuery('b') b: string): Promise<CalcResultDTO> {
await delayAsync(1000);
return { result: this.calcService.mult(+a, +b)};
}
@Get('/div')
async div(@FromQuery('a') a: string, @FromQuery('b') b: string): Promise<CalcResultDTO> {
await delayAsync(1000);
return { result: this.calcService.div(+a, +b)};
}
}
function addApiControllers(router: Router) {
addController(router, '/user', UserController);
addController(router, '/calc', CalculatorController);
}
const apiRouter = Router();
addApiControllers(apiRouter);
app.use('/api', apiRouter);
app.use((err: any, _req: Request, resp: Response, next: NextFunction) => {
if (err instanceof HttpError) {
resp.status(err.statusCode).send({ status: 'ERROR', msg: err.message})
} else {
next(err);
}
});
app.listen(3000, () => console.log('server listening on port 3000'));