express-ext 0.5.18 → 0.6.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.
@@ -1,6 +1,6 @@
1
1
  import { Request, Response } from "express"
2
2
  import { checkId, create, getStatusCode, update } from "./edit"
3
- import { handleError, Log } from "./http"
3
+ import { handleError } from "./http"
4
4
  import { LoadController } from "./LoadController"
5
5
  import { Attribute, Attributes, ErrorMessage } from "./metadata"
6
6
  import { resources, StringMap } from "./resources"
@@ -21,13 +21,12 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
21
21
  metadata?: Attributes
22
22
  returnNumber?: boolean
23
23
  constructor(
24
- log: Log,
25
24
  public service: GenericService<T, ID, number | T | ErrorMessage[]>,
26
25
  public build?: Build<T>,
27
26
  public validate?: Validate<T>,
28
27
  returnNumber?: boolean,
29
28
  ) {
30
- super(log, service)
29
+ super(service)
31
30
  this.returnNumber = returnNumber
32
31
  if (service.metadata) {
33
32
  const m = service.metadata()
@@ -45,18 +44,18 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
45
44
  }
46
45
  }
47
46
  create(req: Request, res: Response): void {
48
- validateAndCreate(req, res, this.service.create, this.log, this.validate, this.build)
47
+ validateAndCreate(req, res, this.service.create, this.validate, this.build)
49
48
  }
50
49
  update(req: Request, res: Response): void {
51
50
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update)
52
51
  if (id) {
53
- validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, undefined, this.build)
52
+ validateAndUpdate(res, req.body, false, this.service.update, this.validate, undefined, this.build)
54
53
  }
55
54
  }
56
55
  patch(req: Request, res: Response): void {
57
56
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch)
58
57
  if (id && this.service.patch) {
59
- validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, undefined, this.build)
58
+ validateAndUpdate(res, req.body, true, this.service.patch, this.validate, undefined, this.build)
60
59
  }
61
60
  }
62
61
  delete(req: Request, res: Response): void {
@@ -70,20 +69,12 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
70
69
  .then((count) => {
71
70
  res.status(getDeleteStatus(count)).json(count).end()
72
71
  })
73
- .catch((err) => handleError(err, res, this.log))
72
+ .catch((err) => handleError(err, res))
74
73
  }
75
74
  }
76
75
  }
77
76
  }
78
- export function validateAndCreate<T>(
79
- req: Request,
80
- res: Response,
81
- save: Save<T>,
82
- log: Log,
83
- validate?: Validate<T>,
84
- build?: Build<T>,
85
- returnNumber?: boolean,
86
- ): void {
77
+ export function validateAndCreate<T>(req: Request, res: Response, save: Save<T>, validate?: Validate<T>, build?: Build<T>, returnNumber?: boolean): void {
87
78
  const obj = req.body
88
79
  if (!obj || obj === "") {
89
80
  res.status(400).end("The request body cannot be empty.")
@@ -97,12 +88,12 @@ export function validateAndCreate<T>(
97
88
  if (build) {
98
89
  build(res, obj, true)
99
90
  }
100
- create(res, obj, save, log, returnNumber)
91
+ create(res, obj, save, returnNumber)
101
92
  }
102
93
  })
103
- .catch((err) => handleError(err, res, log))
94
+ .catch((err) => handleError(err, res))
104
95
  } else {
105
- create(res, obj, save, log, returnNumber)
96
+ create(res, obj, save, returnNumber)
106
97
  }
107
98
  }
108
99
  }
@@ -111,7 +102,6 @@ export function validateAndUpdate<T>(
111
102
  obj: T,
112
103
  isPatch: boolean,
113
104
  save: Save<T>,
114
- log: Log,
115
105
  validate?: Validate<T>,
116
106
  resource?: StringMap,
117
107
  build?: Build<T>,
@@ -126,12 +116,12 @@ export function validateAndUpdate<T>(
126
116
  if (build) {
127
117
  build(res, obj, false, isPatch)
128
118
  }
129
- update(res, obj, save, log, returnNumber)
119
+ update(res, obj, save, returnNumber)
130
120
  }
131
121
  })
132
- .catch((err) => handleError(err, res, log))
122
+ .catch((err) => handleError(err, res))
133
123
  } else {
134
- update(res, obj, save, log, returnNumber)
124
+ update(res, obj, save, returnNumber)
135
125
  }
136
126
  }
137
127
  export function buildAndCheckIdWithBody<T, ID, R>(req: Request, res: Response, keys?: Attribute[], patch?: (obj: T, ctx?: any) => Promise<R>): ID | undefined {
@@ -1,6 +1,6 @@
1
1
  import { Request, Response } from "express"
2
2
  import { Build, GenericController, GenericService } from "./GenericController"
3
- import { handleError, Log } from "./http"
3
+ import { handleError } from "./http"
4
4
  import { ErrorMessage } from "./metadata"
5
5
  import { resources, StringMap } from "./resources"
6
6
  import { buildArray, Filter, format, fromRequest, getMetadataFunc, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult } from "./search"
@@ -13,7 +13,6 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
13
13
  excluding?: string
14
14
  array?: string[]
15
15
  constructor(
16
- log: Log,
17
16
  public find: (s: S, limit: number, page?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
18
17
  service: GenericService<T, ID, number | ErrorMessage[]>,
19
18
  config?: SearchConfig,
@@ -22,7 +21,7 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
22
21
  dates?: string[],
23
22
  numbers?: string[],
24
23
  ) {
25
- super(log, service, build, validate)
24
+ super(service, build, validate)
26
25
  this.search = this.search.bind(this)
27
26
  this.config = initializeConfig(config)
28
27
  if (this.config) {
@@ -41,6 +40,6 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
41
40
  const s2 = format(s, this.dates, this.numbers)
42
41
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
43
42
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
44
- .catch((err) => handleError(err, res, this.log))
43
+ .catch((err) => handleError(err, res))
45
44
  }
46
45
  }
@@ -1,5 +1,5 @@
1
1
  import { Request, Response } from "express"
2
- import { attrs, handleError, Log, minimize, queryNumber, respondModel } from "./http"
2
+ import { attrs, handleError, minimize, queryNumber, respondModel } from "./http"
3
3
  import { Attribute, Attributes } from "./metadata"
4
4
  import { buildAndCheckId, buildKeys } from "./view"
5
5
 
@@ -40,7 +40,7 @@ function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>, keys?
40
40
  export class LoadController<T, ID> {
41
41
  protected keys?: Attribute[]
42
42
  protected view: Load<T, ID>
43
- constructor(protected log: Log, viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes | Attribute[] | string[]) {
43
+ constructor(viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes | Attribute[] | string[]) {
44
44
  this.load = this.load.bind(this)
45
45
  this.view = getViewFunc(viewService)
46
46
  this.keys = getKeysFunc(viewService, keys)
@@ -50,14 +50,13 @@ export class LoadController<T, ID> {
50
50
  if (id) {
51
51
  this.view(id)
52
52
  .then((obj) => respondModel(minimize(obj), res))
53
- .catch((err) => handleError(err, res, this.log))
53
+ .catch((err) => handleError(err, res))
54
54
  }
55
55
  }
56
56
  }
57
57
  // tslint:disable-next-line:max-classes-per-file
58
58
  export class ItemController<T> {
59
59
  constructor(
60
- protected log: Log,
61
60
  private loadData: (keyword: string, max?: number) => Promise<T>,
62
61
  name?: string,
63
62
  protected param?: boolean,
@@ -88,7 +87,7 @@ export class ItemController<T> {
88
87
  const max = queryNumber(req, this.maxName, this.max)
89
88
  this.loadData(s, max)
90
89
  .then((result) => respondModel(minimize(result), res))
91
- .catch((err) => handleError(err, res, this.log))
90
+ .catch((err) => handleError(err, res))
92
91
  }
93
92
  }
94
93
  }
@@ -1,5 +1,5 @@
1
1
  import { Request, Response } from "express"
2
- import { handleError, Log } from "./http"
2
+ import { handleError } from "./http"
3
3
  import { LoadController, ViewService } from "./LoadController"
4
4
  import { Attribute, Attributes } from "./metadata"
5
5
  import { resources } from "./resources"
@@ -19,7 +19,6 @@ export interface SearchManager {
19
19
  load(req: Request, res: Response): void
20
20
  }
21
21
  export function useSearchController<T, ID, S extends Filter>(
22
- log: Log,
23
22
  find: (s: S, limit: number, page?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
24
23
  viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>),
25
24
  array?: string[],
@@ -28,7 +27,7 @@ export function useSearchController<T, ID, S extends Filter>(
28
27
  keys?: Attributes | Attribute[] | string[],
29
28
  config?: SearchConfig | boolean,
30
29
  ): Search {
31
- const c = new LoadSearchController(log, find, viewService, keys, config, dates, numbers)
30
+ const c = new LoadSearchController(find, viewService, keys, config, dates, numbers)
32
31
  c.array = array
33
32
  return c
34
33
  }
@@ -43,7 +42,6 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
43
42
  excluding?: string
44
43
  array?: string[]
45
44
  constructor(
46
- log: Log,
47
45
  public find: (s: S, limit: number, page?: number | string, fields?: string[]) => Promise<SearchResult<T>>,
48
46
  viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>),
49
47
  keys?: Attributes | Attribute[] | string[],
@@ -51,7 +49,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
51
49
  dates?: string[],
52
50
  numbers?: string[],
53
51
  ) {
54
- super(log, viewService, keys)
52
+ super(viewService, keys)
55
53
  this.search = this.search.bind(this)
56
54
  if (config) {
57
55
  if (typeof config === "boolean") {
@@ -76,7 +74,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
76
74
  const s2 = format(s, this.dates, this.numbers)
77
75
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
78
76
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
79
- .catch((err) => handleError(err, res, this.log))
77
+ .catch((err) => handleError(err, res))
80
78
  }
81
79
  }
82
80
  export class QueryController<T, ID, S extends Filter> extends LoadController<T, ID> {
@@ -86,8 +84,14 @@ export class QueryController<T, ID, S extends Filter> extends LoadController<T,
86
84
  numbers?: string[]
87
85
  excluding?: string
88
86
  array?: string[]
89
- constructor(log: Log, protected query: Query<T, ID, S>, config?: SearchConfig | boolean, dates?: string[], numbers?: string[], array?: string[]) {
90
- super(log, query)
87
+ constructor(
88
+ protected query: Query<T, ID, S>,
89
+ config?: SearchConfig | boolean,
90
+ dates?: string[],
91
+ numbers?: string[],
92
+ array?: string[],
93
+ ) {
94
+ super(query)
91
95
  this.search = this.search.bind(this)
92
96
  this.array = array
93
97
  if (config) {
@@ -114,7 +118,7 @@ export class QueryController<T, ID, S extends Filter> extends LoadController<T,
114
118
  this.query
115
119
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
116
120
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
117
- .catch((err) => handleError(err, res, this.log))
121
+ .catch((err) => handleError(err, res))
118
122
  }
119
123
  }
120
124
  export { QueryController as QueryHandler }
@@ -16,7 +16,6 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
16
16
  excluding?: string
17
17
  array?: string[]
18
18
  constructor(
19
- log: Log,
20
19
  public lowCodeService: Service<T, ID, number | ErrorMessage[], S>,
21
20
  config?: SearchConfig,
22
21
  build?: Build<T>,
@@ -24,7 +23,7 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
24
23
  dates?: string[],
25
24
  numbers?: string[],
26
25
  ) {
27
- super(log, lowCodeService, build, validate)
26
+ super(lowCodeService, build, validate)
28
27
  this.search = this.search.bind(this)
29
28
  this.config = initializeConfig(config)
30
29
  if (this.config) {
@@ -44,7 +43,7 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
44
43
  this.lowCodeService
45
44
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
46
45
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
47
- .catch((err) => handleError(err, res, this.log))
46
+ .catch((err) => handleError(err, res))
48
47
  }
49
48
  }
50
49
  export { LowcodeController as LowcodeHandler }
@@ -64,7 +63,7 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
64
63
  dates?: string[],
65
64
  numbers?: string[],
66
65
  ) {
67
- super(log, lowCodeService, build, validate)
66
+ super(lowCodeService, build, validate)
68
67
  this.search = this.search.bind(this)
69
68
  this.config = initializeConfig(config)
70
69
  if (this.config) {
@@ -84,6 +83,6 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
84
83
  this.lowCodeService
85
84
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
86
85
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
87
- .catch((err) => handleError(err, res, this.log))
86
+ .catch((err) => handleError(err, res))
88
87
  }
89
88
  }
@@ -34,6 +34,6 @@ export class SearchController<T, S extends Filter> {
34
34
  const s2 = format(s, this.dates, this.numbers)
35
35
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
36
36
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
37
- .catch((err) => handleError(err, res, this.log))
37
+ .catch((err) => handleError(err, res))
38
38
  }
39
39
  }
package/src/edit.ts CHANGED
@@ -62,13 +62,7 @@ export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
62
62
  }
63
63
  return true
64
64
  }
65
- export function create<T>(
66
- res: Response,
67
- obj: T,
68
- insert: (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]>,
69
- log: (msg: string, ctx?: any) => void,
70
- returnNumber?: boolean,
71
- ): void {
65
+ export function create<T>(res: Response, obj: T, insert: (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]>, returnNumber?: boolean): void {
72
66
  insert(obj)
73
67
  .then((result) => {
74
68
  if (typeof result === "number") {
@@ -89,15 +83,9 @@ export function create<T>(
89
83
  .end()
90
84
  }
91
85
  })
92
- .catch((err) => handleError(err, res, log))
86
+ .catch((err) => handleError(err, res))
93
87
  }
94
- export function update<T>(
95
- res: Response,
96
- obj: T,
97
- save: (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]>,
98
- log: (msg: string, ctx?: any) => void,
99
- returnNumber?: boolean,
100
- ): void {
88
+ export function update<T>(res: Response, obj: T, save: (obj: T, ctx?: any) => Promise<number | T | ErrorMessage[]>, returnNumber?: boolean): void {
101
89
  save(obj)
102
90
  .then((result) => {
103
91
  if (typeof result === "number") {
@@ -120,7 +108,7 @@ export function update<T>(
120
108
  .end()
121
109
  }
122
110
  })
123
- .catch((err) => handleError(err, res, log))
111
+ .catch((err) => handleError(err, res))
124
112
  }
125
113
  export function isTypeError(errs: ErrorMessage[]): boolean {
126
114
  if (!errs) {
package/src/http.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Request, Response } from "express"
2
2
  import { Attribute } from "./metadata"
3
+ import { resources } from "./resources"
3
4
 
4
5
  export type Log = (msg: string) => void
5
6
  export type LogFunc = Log
@@ -17,12 +18,17 @@ Object.defineProperty(Error.prototype, "toJSON", {
17
18
  configurable: true,
18
19
  writable: true,
19
20
  })
20
- export function handleError(err: any, res: Response, log?: (msg: string) => void) {
21
- if (log) {
22
- log(toString(err))
21
+ export function handleError(err: any, res: Response) {
22
+ if (resources.log) {
23
+ resources.log(toString(err))
23
24
  res.status(500).end("Internal Server Error")
24
25
  } else {
25
- res.status(500).end(toString(err))
26
+ if (resources.returnServerError) {
27
+ res.status(500).end(toString(err))
28
+ } else {
29
+ console.error(toString(err))
30
+ res.status(500).end("Internal Server Error")
31
+ }
26
32
  }
27
33
  }
28
34
  export const error = handleError
package/src/index.ts CHANGED
@@ -2,7 +2,7 @@ import { Request, Response } from "express"
2
2
  import { GenericController } from "./GenericController"
3
3
  import { GenericSearchController } from "./GenericSearchController"
4
4
  import { HealthController } from "./HealthController"
5
- import { handleError, Log, query } from "./http"
5
+ import { handleError, query } from "./http"
6
6
  import { LoadController } from "./LoadController"
7
7
  import { LoadSearchController } from "./LoadSearchController"
8
8
  import { LogController } from "./LogController"
@@ -49,7 +49,6 @@ export interface SavedService {
49
49
  export class SavedController {
50
50
  constructor(
51
51
  protected savedService: SavedService,
52
- protected log: (msg: string) => void,
53
52
  id?: string,
54
53
  userId?: string,
55
54
  ) {
@@ -77,7 +76,7 @@ export class SavedController {
77
76
  const status = result > 0 ? 200 : result === 0 ? 409 : 422
78
77
  res.status(status).json(result).end()
79
78
  })
80
- .catch((err) => handleError(err, res, this.log))
79
+ .catch((err) => handleError(err, res))
81
80
  }
82
81
  remove(req: Request, res: Response) {
83
82
  const userId: string = res.locals[this.userId]
@@ -96,7 +95,7 @@ export class SavedController {
96
95
  const status = result > 0 ? 200 : 410
97
96
  res.status(status).json(result).end()
98
97
  })
99
- .catch((err) => handleError(err, res, this.log))
98
+ .catch((err) => handleError(err, res))
100
99
  }
101
100
  }
102
101
  export interface FollowService {
@@ -107,7 +106,6 @@ export interface FollowService {
107
106
  export class FollowController {
108
107
  constructor(
109
108
  protected service: FollowService,
110
- protected log: Log,
111
109
  id?: string,
112
110
  userId?: string,
113
111
  ) {
@@ -135,7 +133,7 @@ export class FollowController {
135
133
  const status = result > 0 ? 200 : 409
136
134
  res.status(status).json(result).end()
137
135
  })
138
- .catch((err) => handleError(err, res, this.log))
136
+ .catch((err) => handleError(err, res))
139
137
  }
140
138
  unfollow(req: Request, res: Response) {
141
139
  const userId: string = res.locals[this.userId]
@@ -154,7 +152,7 @@ export class FollowController {
154
152
  const status = result > 0 ? 200 : 410
155
153
  res.status(status).json(result).end()
156
154
  })
157
- .catch((err) => handleError(err, res, this.log))
155
+ .catch((err) => handleError(err, res))
158
156
  }
159
157
  }
160
158
  export interface ReactService {
@@ -165,11 +163,10 @@ export interface ReactService {
165
163
  // tslint:disable-next-line:max-classes-per-file
166
164
  export class UserReactionController {
167
165
  constructor(
168
- public log: Log,
169
- public service: ReactService,
170
- public author: string,
166
+ protected service: ReactService,
167
+ protected author: string,
171
168
  id: string,
172
- public reaction: string,
169
+ protected reaction: string,
173
170
  ) {
174
171
  this.id = id && id.length > 0 ? id : "id"
175
172
  this.react = this.react.bind(this)
@@ -198,7 +195,7 @@ export class UserReactionController {
198
195
  .then((count) => {
199
196
  res.status(200).json(count).end()
200
197
  })
201
- .catch((err) => handleError(err, res, this.log))
198
+ .catch((err) => handleError(err, res))
202
199
  }
203
200
  unreact(req: Request, res: Response) {
204
201
  const id = req.params.id as string
@@ -221,7 +218,7 @@ export class UserReactionController {
221
218
  .then((count) => {
222
219
  res.status(200).json(count).end()
223
220
  })
224
- .catch((err) => handleError(err, res, this.log))
221
+ .catch((err) => handleError(err, res))
225
222
  }
226
223
  checkReaction(req: Request, res: Response) {
227
224
  const id = req.params.id as string
@@ -239,7 +236,7 @@ export class UserReactionController {
239
236
  .then((count) => {
240
237
  res.status(200).json(count).end()
241
238
  })
242
- .catch((err) => handleError(err, res, this.log))
239
+ .catch((err) => handleError(err, res))
243
240
  }
244
241
  }
245
242
  export const ReactController = UserReactionController
@@ -326,7 +323,7 @@ export function respond<T>(res: Response, result: number | T | ErrorMessage[], r
326
323
  .end()
327
324
  }
328
325
  }
329
- export function save<T>(isEdit: boolean, res: Response, obj: T, service: SaveService<T>, log: (msg: string, ctx?: any) => void, returnNumber?: boolean) {
326
+ export function save<T>(isEdit: boolean, res: Response, obj: T, service: SaveService<T>, returnNumber?: boolean) {
330
327
  if (!isEdit) {
331
328
  service
332
329
  .create(obj)
@@ -339,7 +336,7 @@ export function save<T>(isEdit: boolean, res: Response, obj: T, service: SaveSer
339
336
  res.status(201).json(obj).end()
340
337
  }
341
338
  })
342
- .catch((err) => handleError(err, res, log))
339
+ .catch((err) => handleError(err, res))
343
340
  } else {
344
341
  service
345
342
  .update(obj)
@@ -357,7 +354,7 @@ export function save<T>(isEdit: boolean, res: Response, obj: T, service: SaveSer
357
354
  .end()
358
355
  }
359
356
  })
360
- .catch((err) => handleError(err, res, log))
357
+ .catch((err) => handleError(err, res))
361
358
  }
362
359
  }
363
360
  const map: StringMap = {
package/src/resources.ts CHANGED
@@ -21,6 +21,8 @@ export class resources {
21
21
  static fields = "fields"
22
22
  static partial = "partial"
23
23
  static subPartial = "sub"
24
+ static log?: (msg: string) => void
25
+ static returnServerError?: boolean
24
26
  static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>
25
27
  static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[]
26
28
  static encoding?: BufferEncoding = "utf-8"