express-ext 0.4.0 → 0.4.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/lib/GenericSearchController.js +38 -41
- package/lib/LoadSearchController.js +73 -80
- package/lib/LowCodeController.js +61 -68
- package/lib/SearchController.js +26 -29
- package/lib/resources.js +58 -56
- package/lib/search.js +348 -477
- package/package.json +1 -1
- package/src/GenericController.ts +101 -101
- package/src/GenericSearchController.ts +25 -30
- package/src/HealthController.ts +8 -8
- package/src/LoadController.ts +55 -48
- package/src/LoadSearchController.ts +63 -72
- package/src/LogController.ts +86 -84
- package/src/LowCodeController.ts +44 -54
- package/src/SearchController.ts +19 -23
- package/src/client.ts +46 -46
- package/src/edit.ts +45 -45
- package/src/health.ts +26 -26
- package/src/http.ts +139 -139
- package/src/index.ts +197 -197
- package/src/log.ts +155 -151
- package/src/metadata.ts +44 -27
- package/src/resources.ts +71 -69
- package/src/search.ts +342 -502
- package/src/view.ts +33 -33
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { Request, Response } from
|
|
2
|
-
import { checkId, create, isTypeError, update } from
|
|
3
|
-
import { handleError, Log } from
|
|
4
|
-
import { LoadController } from
|
|
5
|
-
import { Attribute, Attributes, ErrorMessage } from
|
|
6
|
-
import { resources, StringMap } from
|
|
7
|
-
import { buildAndCheckId, buildId } from
|
|
1
|
+
import { Request, Response } from "express"
|
|
2
|
+
import { checkId, create, isTypeError, update } from "./edit"
|
|
3
|
+
import { handleError, Log } from "./http"
|
|
4
|
+
import { LoadController } from "./LoadController"
|
|
5
|
+
import { Attribute, Attributes, ErrorMessage } from "./metadata"
|
|
6
|
+
import { resources, StringMap } from "./resources"
|
|
7
|
+
import { buildAndCheckId, buildId } from "./view"
|
|
8
8
|
|
|
9
|
-
export type Build<T> = (res: Response, obj: T, isCreate?: boolean, isPatch?: boolean) => void
|
|
10
|
-
export type Validate<T> = (obj: T, resource?: StringMap, patch?: boolean) => Promise<ErrorMessage[]
|
|
11
|
-
export type Save<T> = (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]
|
|
9
|
+
export type Build<T> = (res: Response, obj: T, isCreate?: boolean, isPatch?: boolean) => void
|
|
10
|
+
export type Validate<T> = (obj: T, resource?: StringMap, patch?: boolean) => Promise<ErrorMessage[]>
|
|
11
|
+
export type Save<T> = (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]>
|
|
12
12
|
export interface GenericService<T, ID, R> {
|
|
13
|
-
metadata?(): Attributes | undefined
|
|
14
|
-
load(id: ID, ctx?: any): Promise<T | null
|
|
15
|
-
create(obj: T, ctx?: any): Promise<R
|
|
16
|
-
update(obj: T, ctx?: any): Promise<R
|
|
17
|
-
patch?(obj: Partial<T>, ctx?: any): Promise<R
|
|
18
|
-
delete?(id: ID, ctx?: any): Promise<number
|
|
13
|
+
metadata?(): Attributes | undefined
|
|
14
|
+
load(id: ID, ctx?: any): Promise<T | null>
|
|
15
|
+
create(obj: T, ctx?: any): Promise<R>
|
|
16
|
+
update(obj: T, ctx?: any): Promise<R>
|
|
17
|
+
patch?(obj: Partial<T>, ctx?: any): Promise<R>
|
|
18
|
+
delete?(id: ID, ctx?: any): Promise<number>
|
|
19
19
|
}
|
|
20
20
|
export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
21
|
-
metadata?: Attributes
|
|
22
|
-
returnNumber?: boolean
|
|
21
|
+
metadata?: Attributes
|
|
22
|
+
returnNumber?: boolean
|
|
23
23
|
constructor(
|
|
24
24
|
log: Log,
|
|
25
25
|
public service: GenericService<T, ID, number | T | ErrorMessage[]>,
|
|
@@ -27,50 +27,50 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
|
27
27
|
public validate?: Validate<T>,
|
|
28
28
|
returnNumber?: boolean,
|
|
29
29
|
) {
|
|
30
|
-
super(log, service)
|
|
31
|
-
this.returnNumber = returnNumber
|
|
30
|
+
super(log, service)
|
|
31
|
+
this.returnNumber = returnNumber
|
|
32
32
|
if (service.metadata) {
|
|
33
|
-
const m = service.metadata()
|
|
33
|
+
const m = service.metadata()
|
|
34
34
|
if (m) {
|
|
35
|
-
this.metadata = m
|
|
35
|
+
this.metadata = m
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
this.create = this.create.bind(this)
|
|
39
|
-
this.update = this.update.bind(this)
|
|
40
|
-
this.patch = this.patch.bind(this)
|
|
41
|
-
this.delete = this.delete.bind(this)
|
|
38
|
+
this.create = this.create.bind(this)
|
|
39
|
+
this.update = this.update.bind(this)
|
|
40
|
+
this.patch = this.patch.bind(this)
|
|
41
|
+
this.delete = this.delete.bind(this)
|
|
42
42
|
if (!validate && resources.createValidator && this.metadata) {
|
|
43
|
-
const v = resources.createValidator(this.metadata)
|
|
44
|
-
this.validate = v.validate
|
|
43
|
+
const v = resources.createValidator(this.metadata)
|
|
44
|
+
this.validate = v.validate
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
create(req: Request, res: Response): void {
|
|
48
|
-
validateAndCreate(req, res, this.service.create, this.log, this.validate, this.build)
|
|
48
|
+
validateAndCreate(req, res, this.service.create, this.log, this.validate, this.build)
|
|
49
49
|
}
|
|
50
50
|
update(req: Request, res: Response): void {
|
|
51
|
-
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update)
|
|
51
|
+
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update)
|
|
52
52
|
if (id) {
|
|
53
|
-
validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, undefined, this.build)
|
|
53
|
+
validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, undefined, this.build)
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
patch(req: Request, res: Response): void {
|
|
57
|
-
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch)
|
|
57
|
+
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch)
|
|
58
58
|
if (id && this.service.patch) {
|
|
59
|
-
validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, undefined, this.build)
|
|
59
|
+
validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, undefined, this.build)
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
62
|
delete(req: Request, res: Response): void {
|
|
63
|
-
const id = buildAndCheckId<ID>(req, res, this.keys)
|
|
63
|
+
const id = buildAndCheckId<ID>(req, res, this.keys)
|
|
64
64
|
if (id) {
|
|
65
65
|
if (!this.service.delete) {
|
|
66
|
-
res.status(405).end(
|
|
66
|
+
res.status(405).end("Method Not Allowed")
|
|
67
67
|
} else {
|
|
68
68
|
this.service
|
|
69
69
|
.delete(id)
|
|
70
70
|
.then((count) => {
|
|
71
|
-
res.status(getDeleteStatus(count)).json(count).end()
|
|
71
|
+
res.status(getDeleteStatus(count)).json(count).end()
|
|
72
72
|
})
|
|
73
|
-
.catch((err) => handleError(err, res, this.log))
|
|
73
|
+
.catch((err) => handleError(err, res, this.log))
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
}
|
|
@@ -84,25 +84,25 @@ export function validateAndCreate<T>(
|
|
|
84
84
|
build?: Build<T>,
|
|
85
85
|
returnNumber?: boolean,
|
|
86
86
|
): void {
|
|
87
|
-
const obj = req.body
|
|
88
|
-
if (!obj || obj ===
|
|
89
|
-
res.status(400).end(
|
|
87
|
+
const obj = req.body
|
|
88
|
+
if (!obj || obj === "") {
|
|
89
|
+
res.status(400).end("The request body cannot be empty.")
|
|
90
90
|
} else {
|
|
91
91
|
if (validate) {
|
|
92
92
|
validate(obj)
|
|
93
93
|
.then((errors) => {
|
|
94
94
|
if (errors && errors.length > 0) {
|
|
95
|
-
res.status(getStatusCode(errors)).json(errors).end()
|
|
95
|
+
res.status(getStatusCode(errors)).json(errors).end()
|
|
96
96
|
} else {
|
|
97
97
|
if (build) {
|
|
98
|
-
build(res, obj, true)
|
|
98
|
+
build(res, obj, true)
|
|
99
99
|
}
|
|
100
|
-
create(res, obj, save, log, returnNumber)
|
|
100
|
+
create(res, obj, save, log, returnNumber)
|
|
101
101
|
}
|
|
102
102
|
})
|
|
103
|
-
.catch((err) => handleError(err, res, log))
|
|
103
|
+
.catch((err) => handleError(err, res, log))
|
|
104
104
|
} else {
|
|
105
|
-
create(res, obj, save, log, returnNumber)
|
|
105
|
+
create(res, obj, save, log, returnNumber)
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
}
|
|
@@ -121,76 +121,76 @@ export function validateAndUpdate<T>(
|
|
|
121
121
|
validate(obj, resource, isPatch)
|
|
122
122
|
.then((errors) => {
|
|
123
123
|
if (errors && errors.length > 0) {
|
|
124
|
-
res.status(getStatusCode(errors)).json(errors).end()
|
|
124
|
+
res.status(getStatusCode(errors)).json(errors).end()
|
|
125
125
|
} else {
|
|
126
126
|
if (build) {
|
|
127
|
-
build(res, obj, false, isPatch)
|
|
127
|
+
build(res, obj, false, isPatch)
|
|
128
128
|
}
|
|
129
|
-
update(res, obj, save, log, returnNumber)
|
|
129
|
+
update(res, obj, save, log, returnNumber)
|
|
130
130
|
}
|
|
131
131
|
})
|
|
132
|
-
.catch((err) => handleError(err, res, log))
|
|
132
|
+
.catch((err) => handleError(err, res, log))
|
|
133
133
|
} else {
|
|
134
|
-
update(res, obj, save, log, returnNumber)
|
|
134
|
+
update(res, obj, save, log, returnNumber)
|
|
135
135
|
}
|
|
136
136
|
}
|
|
137
137
|
export function buildAndCheckIdWithBody<T, ID, R>(req: Request, res: Response, keys?: Attribute[], patch?: (obj: T, ctx?: any) => Promise<R>): ID | undefined {
|
|
138
|
-
const obj = req.body
|
|
139
|
-
if (!obj || obj ===
|
|
140
|
-
res.status(400).end(
|
|
141
|
-
return undefined
|
|
138
|
+
const obj = req.body
|
|
139
|
+
if (!obj || obj === "") {
|
|
140
|
+
res.status(400).end("The request body cannot be empty.")
|
|
141
|
+
return undefined
|
|
142
142
|
}
|
|
143
143
|
if (!patch) {
|
|
144
|
-
res.status(405).end(
|
|
145
|
-
return undefined
|
|
144
|
+
res.status(405).end("Method Not Allowed")
|
|
145
|
+
return undefined
|
|
146
146
|
}
|
|
147
|
-
const id = buildId<ID>(req, keys)
|
|
147
|
+
const id = buildId<ID>(req, keys)
|
|
148
148
|
if (!id) {
|
|
149
|
-
res.status(400).end(
|
|
150
|
-
return undefined
|
|
149
|
+
res.status(400).end("Invalid parameters")
|
|
150
|
+
return undefined
|
|
151
151
|
}
|
|
152
|
-
const ok = checkId<T, ID>(obj, id, keys)
|
|
152
|
+
const ok = checkId<T, ID>(obj, id, keys)
|
|
153
153
|
if (!ok) {
|
|
154
|
-
res.status(400).end(
|
|
155
|
-
return undefined
|
|
154
|
+
res.status(400).end("body and url are not matched")
|
|
155
|
+
return undefined
|
|
156
156
|
}
|
|
157
|
-
return id
|
|
157
|
+
return id
|
|
158
158
|
}
|
|
159
159
|
export function getDeleteStatus(count: number): number {
|
|
160
160
|
if (count > 0) {
|
|
161
|
-
return 200
|
|
161
|
+
return 200
|
|
162
162
|
} else if (count === 0) {
|
|
163
|
-
return 404
|
|
163
|
+
return 404
|
|
164
164
|
} else {
|
|
165
|
-
return 409
|
|
165
|
+
return 409
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
export function getStatusCode(errs: ErrorMessage[]): number {
|
|
169
|
-
return isTypeError(errs) ? 400 : 422
|
|
169
|
+
return isTypeError(errs) ? 400 : 422
|
|
170
170
|
}
|
|
171
171
|
export interface ModelConfig {
|
|
172
|
-
id?: string
|
|
173
|
-
payload?: string
|
|
174
|
-
user?: string
|
|
175
|
-
updatedBy?: string
|
|
176
|
-
updatedAt?: string
|
|
177
|
-
createdBy?: string
|
|
178
|
-
createdAt?: string
|
|
179
|
-
version?: string
|
|
172
|
+
id?: string
|
|
173
|
+
payload?: string
|
|
174
|
+
user?: string
|
|
175
|
+
updatedBy?: string
|
|
176
|
+
updatedAt?: string
|
|
177
|
+
createdBy?: string
|
|
178
|
+
createdAt?: string
|
|
179
|
+
version?: string
|
|
180
180
|
}
|
|
181
181
|
export function useBuild<T>(c: ModelConfig, generate?: () => string): Build<T> {
|
|
182
182
|
const b = new Builder<T>(
|
|
183
183
|
generate,
|
|
184
|
-
c.id ? c.id :
|
|
185
|
-
c.payload ? c.payload :
|
|
186
|
-
c.user ? c.user :
|
|
187
|
-
c.updatedBy ? c.updatedBy :
|
|
188
|
-
c.updatedAt ? c.updatedAt :
|
|
189
|
-
c.createdBy ? c.createdBy :
|
|
190
|
-
c.createdAt ? c.createdAt :
|
|
191
|
-
c.version ? c.version :
|
|
192
|
-
)
|
|
193
|
-
return b.build
|
|
184
|
+
c.id ? c.id : "",
|
|
185
|
+
c.payload ? c.payload : "",
|
|
186
|
+
c.user ? c.user : "",
|
|
187
|
+
c.updatedBy ? c.updatedBy : "",
|
|
188
|
+
c.updatedAt ? c.updatedAt : "",
|
|
189
|
+
c.createdBy ? c.createdBy : "",
|
|
190
|
+
c.createdAt ? c.createdAt : "",
|
|
191
|
+
c.version ? c.version : "",
|
|
192
|
+
)
|
|
193
|
+
return b.build
|
|
194
194
|
}
|
|
195
195
|
// tslint:disable-next-line:max-classes-per-file
|
|
196
196
|
export class Builder<T> {
|
|
@@ -205,52 +205,52 @@ export class Builder<T> {
|
|
|
205
205
|
public createdAt: string,
|
|
206
206
|
public version: string,
|
|
207
207
|
) {
|
|
208
|
-
this.build = this.build.bind(this)
|
|
208
|
+
this.build = this.build.bind(this)
|
|
209
209
|
}
|
|
210
210
|
build(res: Response, obj: T, isCreate?: boolean, isPatch?: boolean): void {
|
|
211
|
-
const o: any = obj
|
|
212
|
-
let usr =
|
|
211
|
+
const o: any = obj
|
|
212
|
+
let usr = ""
|
|
213
213
|
if (this.user.length > 0) {
|
|
214
214
|
if (this.payload.length > 0) {
|
|
215
|
-
const payload = res.locals[this.payload]
|
|
215
|
+
const payload = res.locals[this.payload]
|
|
216
216
|
if (payload) {
|
|
217
|
-
usr = payload[this.user]
|
|
217
|
+
usr = payload[this.user]
|
|
218
218
|
}
|
|
219
219
|
} else {
|
|
220
|
-
usr = res.locals[this.user]
|
|
220
|
+
usr = res.locals[this.user]
|
|
221
221
|
}
|
|
222
222
|
}
|
|
223
223
|
if (!usr) {
|
|
224
|
-
usr =
|
|
224
|
+
usr = ""
|
|
225
225
|
}
|
|
226
|
-
const now = new Date()
|
|
226
|
+
const now = new Date()
|
|
227
227
|
if (isCreate) {
|
|
228
228
|
if (this.generate && this.id.length > 0) {
|
|
229
|
-
o[this.id] = this.generate()
|
|
229
|
+
o[this.id] = this.generate()
|
|
230
230
|
}
|
|
231
231
|
if (usr.length > 0) {
|
|
232
232
|
if (this.createdAt.length > 0) {
|
|
233
|
-
o[this.createdAt] = now
|
|
233
|
+
o[this.createdAt] = now
|
|
234
234
|
}
|
|
235
235
|
if (this.createdBy.length > 0) {
|
|
236
|
-
o[this.createdBy] = usr
|
|
236
|
+
o[this.createdBy] = usr
|
|
237
237
|
}
|
|
238
238
|
}
|
|
239
239
|
if (this.version.length > 0) {
|
|
240
|
-
o[this.version] = 1
|
|
240
|
+
o[this.version] = 1
|
|
241
241
|
}
|
|
242
242
|
} else if (isPatch) {
|
|
243
|
-
const keys = Object.keys(o)
|
|
243
|
+
const keys = Object.keys(o)
|
|
244
244
|
if (keys.length === 0) {
|
|
245
|
-
return
|
|
245
|
+
return
|
|
246
246
|
}
|
|
247
247
|
}
|
|
248
248
|
if (usr.length > 0) {
|
|
249
249
|
if (this.updatedAt.length > 0) {
|
|
250
|
-
o[this.updatedAt] = now
|
|
250
|
+
o[this.updatedAt] = now
|
|
251
251
|
}
|
|
252
252
|
if (this.updatedBy.length > 0) {
|
|
253
|
-
o[this.updatedBy] = usr
|
|
253
|
+
o[this.updatedBy] = usr
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
256
|
}
|
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { Request, Response } from
|
|
2
|
-
import { Build, GenericController, GenericService } from
|
|
3
|
-
import { handleError, Log } from
|
|
4
|
-
import { ErrorMessage } from
|
|
5
|
-
import { StringMap } from
|
|
6
|
-
import { buildArray, Filter, format, fromRequest, getMetadataFunc, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult } from
|
|
1
|
+
import { Request, Response } from "express"
|
|
2
|
+
import { Build, GenericController, GenericService } from "./GenericController"
|
|
3
|
+
import { handleError, Log } from "./http"
|
|
4
|
+
import { ErrorMessage } from "./metadata"
|
|
5
|
+
import { resources, StringMap } from "./resources"
|
|
6
|
+
import { buildArray, Filter, format, fromRequest, getMetadataFunc, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult } from "./search"
|
|
7
7
|
|
|
8
8
|
export class GenericSearchController<T, ID, S extends Filter> extends GenericController<T, ID> {
|
|
9
|
-
config?: SearchConfig
|
|
10
|
-
csv?: boolean
|
|
11
|
-
dates?: string[]
|
|
12
|
-
numbers?: string[]
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
array?: string[];
|
|
9
|
+
config?: SearchConfig
|
|
10
|
+
csv?: boolean
|
|
11
|
+
dates?: string[]
|
|
12
|
+
numbers?: string[]
|
|
13
|
+
excluding?: string
|
|
14
|
+
array?: string[]
|
|
16
15
|
constructor(
|
|
17
16
|
log: Log,
|
|
18
|
-
public find: (s: S, limit
|
|
17
|
+
public find: (s: S, limit: number, page?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
|
|
19
18
|
service: GenericService<T, ID, number | ErrorMessage[]>,
|
|
20
19
|
config?: SearchConfig,
|
|
21
20
|
build?: Build<T>,
|
|
@@ -23,29 +22,25 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
|
|
|
23
22
|
dates?: string[],
|
|
24
23
|
numbers?: string[],
|
|
25
24
|
) {
|
|
26
|
-
super(log, service, build, validate)
|
|
27
|
-
this.search = this.search.bind(this)
|
|
28
|
-
this.config = initializeConfig(config)
|
|
25
|
+
super(log, service, build, validate)
|
|
26
|
+
this.search = this.search.bind(this)
|
|
27
|
+
this.config = initializeConfig(config)
|
|
29
28
|
if (this.config) {
|
|
30
|
-
this.csv = this.config.csv
|
|
31
|
-
this.
|
|
32
|
-
this.excluding = this.config.excluding;
|
|
29
|
+
this.csv = this.config.csv
|
|
30
|
+
this.excluding = this.config.excluding
|
|
33
31
|
}
|
|
34
|
-
|
|
35
|
-
this.fields = 'fields';
|
|
36
|
-
}
|
|
37
|
-
const m = getMetadataFunc(service, dates, numbers);
|
|
32
|
+
const m = getMetadataFunc(service, dates, numbers)
|
|
38
33
|
if (m) {
|
|
39
|
-
this.dates = m.dates
|
|
40
|
-
this.numbers = m.numbers
|
|
34
|
+
this.dates = m.dates
|
|
35
|
+
this.numbers = m.numbers
|
|
41
36
|
}
|
|
42
37
|
}
|
|
43
38
|
search(req: Request, res: Response) {
|
|
44
|
-
const s = fromRequest<S>(req, buildArray(this.array,
|
|
45
|
-
const l = getParameters(s, this.config)
|
|
46
|
-
const s2 = format(s, this.dates, this.numbers)
|
|
39
|
+
const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
|
|
40
|
+
const l = getParameters(s, this.config)
|
|
41
|
+
const s2 = format(s, this.dates, this.numbers)
|
|
47
42
|
this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
|
|
48
43
|
.then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
|
|
49
|
-
.catch((err) => handleError(err, res, this.log))
|
|
44
|
+
.catch((err) => handleError(err, res, this.log))
|
|
50
45
|
}
|
|
51
46
|
}
|
package/src/HealthController.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import {Request, Response} from
|
|
2
|
-
import {health, HealthChecker} from
|
|
1
|
+
import { Request, Response } from "express"
|
|
2
|
+
import { health, HealthChecker } from "./health"
|
|
3
3
|
|
|
4
4
|
export class HealthController {
|
|
5
5
|
constructor(protected checkers: HealthChecker[]) {
|
|
6
|
-
this.check = this.check.bind(this)
|
|
6
|
+
this.check = this.check.bind(this)
|
|
7
7
|
}
|
|
8
8
|
check(req: Request, res: Response) {
|
|
9
|
-
health(this.checkers).then(r => {
|
|
10
|
-
if (r.status ===
|
|
11
|
-
return res.status(200).json(r).end()
|
|
9
|
+
health(this.checkers).then((r) => {
|
|
10
|
+
if (r.status === "UP") {
|
|
11
|
+
return res.status(200).json(r).end()
|
|
12
12
|
} else {
|
|
13
|
-
return res.status(500).json(r).end()
|
|
13
|
+
return res.status(500).json(r).end()
|
|
14
14
|
}
|
|
15
|
-
})
|
|
15
|
+
})
|
|
16
16
|
}
|
|
17
17
|
}
|
package/src/LoadController.ts
CHANGED
|
@@ -1,89 +1,96 @@
|
|
|
1
|
-
import {Request, Response} from
|
|
2
|
-
import {attrs, handleError, Log, minimize, queryNumber, respondModel} from
|
|
3
|
-
import {Attribute, Attributes} from
|
|
4
|
-
import {buildAndCheckId, buildKeys} from
|
|
1
|
+
import { Request, Response } from "express"
|
|
2
|
+
import { attrs, handleError, Log, minimize, queryNumber, respondModel } from "./http"
|
|
3
|
+
import { Attribute, Attributes } from "./metadata"
|
|
4
|
+
import { buildAndCheckId, buildKeys } from "./view"
|
|
5
5
|
|
|
6
6
|
export interface ViewService<T, ID> {
|
|
7
|
-
metadata?(): Attributes|undefined
|
|
8
|
-
load(id: ID, ctx?: any): Promise<T|null
|
|
7
|
+
metadata?(): Attributes | undefined
|
|
8
|
+
load(id: ID, ctx?: any): Promise<T | null>
|
|
9
9
|
}
|
|
10
|
-
export type Load<T, ID> = (
|
|
11
|
-
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>): (id: ID, ctx?: any) => Promise<T|null> {
|
|
12
|
-
if (typeof viewService ===
|
|
13
|
-
return viewService
|
|
10
|
+
export type Load<T, ID> = (id: ID, ctx?: any) => Promise<T | null>
|
|
11
|
+
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>): (id: ID, ctx?: any) => Promise<T | null> {
|
|
12
|
+
if (typeof viewService === "function") {
|
|
13
|
+
return viewService
|
|
14
14
|
}
|
|
15
|
-
return viewService.load
|
|
15
|
+
return viewService.load
|
|
16
16
|
}
|
|
17
|
-
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes|Attribute[]|string[]): Attribute[] | undefined {
|
|
17
|
+
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes | Attribute[] | string[]): Attribute[] | undefined {
|
|
18
18
|
if (keys) {
|
|
19
19
|
if (Array.isArray(keys)) {
|
|
20
20
|
if (keys.length > 0) {
|
|
21
|
-
if (typeof keys[0] ===
|
|
22
|
-
return attrs(keys as string[])
|
|
21
|
+
if (typeof keys[0] === "string") {
|
|
22
|
+
return attrs(keys as string[])
|
|
23
23
|
} else {
|
|
24
|
-
return keys as Attribute[]
|
|
24
|
+
return keys as Attribute[]
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
-
return undefined
|
|
27
|
+
return undefined
|
|
28
28
|
} else {
|
|
29
|
-
return buildKeys(keys as Attributes)
|
|
29
|
+
return buildKeys(keys as Attributes)
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
if (typeof viewService !==
|
|
33
|
-
const metadata = viewService.metadata()
|
|
32
|
+
if (typeof viewService !== "function" && viewService.metadata) {
|
|
33
|
+
const metadata = viewService.metadata()
|
|
34
34
|
if (metadata) {
|
|
35
|
-
return buildKeys(metadata)
|
|
35
|
+
return buildKeys(metadata)
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
-
return undefined
|
|
38
|
+
return undefined
|
|
39
39
|
}
|
|
40
40
|
export class LoadController<T, ID> {
|
|
41
|
-
protected keys?: Attribute[]
|
|
42
|
-
protected view: Load<T, ID
|
|
43
|
-
constructor(protected log: Log, viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes|Attribute[]|string[]) {
|
|
44
|
-
this.load = this.load.bind(this)
|
|
45
|
-
this.view = getViewFunc(viewService)
|
|
46
|
-
this.keys = getKeysFunc(viewService, keys)
|
|
41
|
+
protected keys?: Attribute[]
|
|
42
|
+
protected view: Load<T, ID>
|
|
43
|
+
constructor(protected log: Log, viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes | Attribute[] | string[]) {
|
|
44
|
+
this.load = this.load.bind(this)
|
|
45
|
+
this.view = getViewFunc(viewService)
|
|
46
|
+
this.keys = getKeysFunc(viewService, keys)
|
|
47
47
|
}
|
|
48
48
|
load(req: Request, res: Response): void {
|
|
49
|
-
const id = buildAndCheckId<ID>(req, res, this.keys)
|
|
49
|
+
const id = buildAndCheckId<ID>(req, res, this.keys)
|
|
50
50
|
if (id) {
|
|
51
51
|
this.view(id)
|
|
52
|
-
.then(obj => respondModel(minimize(obj), res))
|
|
53
|
-
.catch(err => handleError(err, res, this.log))
|
|
52
|
+
.then((obj) => respondModel(minimize(obj), res))
|
|
53
|
+
.catch((err) => handleError(err, res, this.log))
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
// tslint:disable-next-line:max-classes-per-file
|
|
58
58
|
export class ItemController<T> {
|
|
59
|
-
constructor(
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
59
|
+
constructor(
|
|
60
|
+
protected log: Log,
|
|
61
|
+
private loadData: (keyword: string, max?: number) => Promise<T>,
|
|
62
|
+
name?: string,
|
|
63
|
+
protected param?: boolean,
|
|
64
|
+
max?: number,
|
|
65
|
+
maxName?: string,
|
|
66
|
+
) {
|
|
67
|
+
this.name = name && name.length > 0 ? name : "keyword"
|
|
68
|
+
this.max = max && max > 0 ? max : 20
|
|
69
|
+
this.maxName = maxName && maxName.length > 0 ? maxName : "max"
|
|
70
|
+
this.load = this.load.bind(this)
|
|
71
|
+
this.query = this.query.bind(this)
|
|
65
72
|
}
|
|
66
|
-
name: string
|
|
67
|
-
max: number
|
|
68
|
-
maxName: string
|
|
73
|
+
name: string
|
|
74
|
+
max: number
|
|
75
|
+
maxName: string
|
|
69
76
|
query(req: Request, res: Response) {
|
|
70
|
-
return this.load(req, res)
|
|
77
|
+
return this.load(req, res)
|
|
71
78
|
}
|
|
72
79
|
load(req: Request, res: Response) {
|
|
73
|
-
const v = this.param ? req.params[this.name] : req.query[this.name]
|
|
80
|
+
const v = this.param ? req.params[this.name] : req.query[this.name]
|
|
74
81
|
if (!v) {
|
|
75
|
-
res.status(400).end(`'${this.name}' cannot be empty`)
|
|
82
|
+
res.status(400).end(`'${this.name}' cannot be empty`)
|
|
76
83
|
} else {
|
|
77
|
-
const s = v.toString()
|
|
84
|
+
const s = v.toString()
|
|
78
85
|
if (s.length === 0) {
|
|
79
|
-
res.status(400).end(`'${this.name}' cannot be empty`)
|
|
86
|
+
res.status(400).end(`'${this.name}' cannot be empty`)
|
|
80
87
|
} else {
|
|
81
|
-
const max = queryNumber(req, this.maxName, this.max)
|
|
88
|
+
const max = queryNumber(req, this.maxName, this.max)
|
|
82
89
|
this.loadData(s, max)
|
|
83
|
-
.then(result => respondModel(minimize(result), res))
|
|
84
|
-
.catch(err => handleError(err, res, this.log))
|
|
90
|
+
.then((result) => respondModel(minimize(result), res))
|
|
91
|
+
.catch((err) => handleError(err, res, this.log))
|
|
85
92
|
}
|
|
86
93
|
}
|
|
87
94
|
}
|
|
88
95
|
}
|
|
89
|
-
export {ItemController as ItemHandler}
|
|
96
|
+
export { ItemController as ItemHandler }
|