-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaseController.ts
125 lines (111 loc) · 3.03 KB
/
BaseController.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
import express from 'express'
import {
EndResult,
JSONResult,
RedirectResult,
RenderResult,
SendFileResult,
SendResult,
SendStatusResult,
RenderResultCallback,
SendFileResultCallback
} from './results'
import { OutgoingHttpHeaders } from 'http'
interface HttpContext {
req: express.Request
res: express.Response
}
interface Context {
req: express.Request
res: express.Response
inject<S>(key: string): S
}
export class BaseController {
context?: Context
/* istanbul ignore next */
get httpContext(): HttpContext | undefined {
// tslint:disable-next-line:no-console
console.warn(
'BaseController#httpContext will be deprecated from v1.0.0. Please use BaseController#context.'
)
if (this.context == null) return
return {
req: this.context.req,
res: this.context.res
}
}
/* istanbul ignore next */
get injector(): ((key: string) => any) | undefined {
// tslint:disable-next-line:no-console
console.warn(
'BaseController#injector will be deprecated from v1.0.0. Please use BaseController#context.inject.'
)
if (this.context == null) return
return this.context.inject
}
/* istanbul ignore next */
set injector(injector: ((key: string) => any) | undefined) {
// tslint:disable-next-line:no-console
console.warn(
'BaseController#injector will be deprecated from v1.0.0. Please use BaseController#context.inject.'
)
if (this.context != null && injector != null) {
this.context.inject = injector
}
}
/* istanbul ignore next */
inject<S>(key: string): S {
// tslint:disable-next-line:no-console
console.warn(
'BaseController#inject will be deprecated from v1.0.0. Please use BaseController#context.inject.'
)
if (this.injector == null) throw new Error('Injector is not set.')
return this.injector(key)
}
end<D>(
data: D,
encoding?: string,
status?: number,
headers?: OutgoingHttpHeaders
): EndResult<D> {
return new EndResult(data, encoding, status, headers)
}
json<D>(
data: D,
status?: number,
headers?: OutgoingHttpHeaders
): JSONResult<D> {
return new JSONResult(data, status, headers)
}
redirect(location: string, status?: number, headers?: OutgoingHttpHeaders) {
return new RedirectResult(location, status, headers)
}
render<D>(
view: string,
locals?: D,
callback?: RenderResultCallback,
status?: number,
headers?: OutgoingHttpHeaders
): RenderResult<D> {
return new RenderResult(view, locals, callback, status, headers)
}
sendFile(
filePath: string,
options?: any,
callback?: SendFileResultCallback,
status?: number,
headers?: OutgoingHttpHeaders
) {
return new SendFileResult(filePath, options, callback, status, headers)
}
send<D>(
data: D,
status?: number,
headers?: OutgoingHttpHeaders
): SendResult<D> {
return new SendResult(data, status, headers)
}
sendStatus(status: number, headers?: OutgoingHttpHeaders) {
return new SendStatusResult(status, headers)
}
}