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/src/index.ts CHANGED
@@ -1,17 +1,17 @@
1
- import { NextFunction, Request, Response } from 'express';
2
- import { GenericController } from './GenericController';
3
- import { GenericSearchController } from './GenericSearchController';
4
- import { HealthController } from './HealthController';
5
- import { handleError, Log } from './http';
6
- import { LoadController } from './LoadController';
7
- import { LoadSearchController } from './LoadSearchController';
8
- import { LogController } from './LogController';
9
- import { Controller, Service } from './LowCodeController';
10
- import { ErrorMessage } from './metadata';
11
- import { StringMap } from './resources';
12
- import { SearchController } from './SearchController';
1
+ import { NextFunction, Request, Response } from "express"
2
+ import { GenericController } from "./GenericController"
3
+ import { GenericSearchController } from "./GenericSearchController"
4
+ import { HealthController } from "./HealthController"
5
+ import { handleError, Log } from "./http"
6
+ import { LoadController } from "./LoadController"
7
+ import { LoadSearchController } from "./LoadSearchController"
8
+ import { LogController } from "./LogController"
9
+ import { Controller, Service } from "./LowCodeController"
10
+ import { ErrorMessage } from "./metadata"
11
+ import { StringMap } from "./resources"
12
+ import { SearchController } from "./SearchController"
13
13
 
14
- export { HealthController as HealthHandler, LoadController as LoadHandler, LogController as LogHandler, LoadController as ViewHandler };
14
+ export { HealthController as HealthHandler, LoadController as LoadHandler, LogController as LogHandler, LoadController as ViewHandler }
15
15
  // export {LoadController as ViewController};
16
16
 
17
17
  export {
@@ -21,363 +21,363 @@ export {
21
21
  LoadSearchController as LoadSearchHandler,
22
22
  Service as LowCodeService,
23
23
  SearchController as SearchHandler,
24
- };
24
+ }
25
25
 
26
- export * from './client';
27
- export * from './edit';
28
- export * from './GenericController';
29
- export * from './GenericSearchController';
30
- export * from './health';
31
- export * from './HealthController';
32
- export * from './http';
33
- export * from './LoadController';
34
- export * from './LoadSearchController';
35
- export * from './log';
36
- export * from './LogController';
37
- export * from './LowCodeController';
38
- export * from './metadata';
39
- export * from './resources';
40
- export * from './search';
41
- export * from './SearchController';
42
- export * from './view';
26
+ export * from "./client"
27
+ export * from "./edit"
28
+ export * from "./GenericController"
29
+ export * from "./GenericSearchController"
30
+ export * from "./health"
31
+ export * from "./HealthController"
32
+ export * from "./http"
33
+ export * from "./LoadController"
34
+ export * from "./LoadSearchController"
35
+ export * from "./log"
36
+ export * from "./LogController"
37
+ export * from "./LowCodeController"
38
+ export * from "./metadata"
39
+ export * from "./resources"
40
+ export * from "./search"
41
+ export * from "./SearchController"
42
+ export * from "./view"
43
43
 
44
44
  export interface AccessConfig {
45
- origin?: string | string[];
46
- credentials?: string | string[];
47
- methods?: string | string[];
48
- headers: number | string | ReadonlyArray<string>;
45
+ origin?: string | string[]
46
+ credentials?: string | string[]
47
+ methods?: string | string[]
48
+ headers: number | string | ReadonlyArray<string>
49
49
  }
50
- export type AccessControlAllowConfig = AccessConfig;
50
+ export type AccessControlAllowConfig = AccessConfig
51
51
  export function allow(access: AccessConfig): (req: Request, res: Response, next: NextFunction) => void {
52
- const ao = access.origin;
53
- if (typeof ao === 'string') {
52
+ const ao = access.origin
53
+ if (typeof ao === "string") {
54
54
  return (req: Request, res: Response, next: NextFunction) => {
55
- res.header('Access-Control-Allow-Origin', access.origin);
56
- res.header('Access-Control-Allow-Credentials', access.credentials);
57
- res.header('Access-Control-Allow-Methods', access.methods);
58
- res.setHeader('Access-Control-Allow-Headers', access.headers);
59
- next();
60
- };
55
+ res.header("Access-Control-Allow-Origin", access.origin)
56
+ res.header("Access-Control-Allow-Credentials", access.credentials)
57
+ res.header("Access-Control-Allow-Methods", access.methods)
58
+ res.setHeader("Access-Control-Allow-Headers", access.headers)
59
+ next()
60
+ }
61
61
  } else if (Array.isArray(ao) && ao.length > 0) {
62
62
  return (req: Request, res: Response, next: NextFunction) => {
63
- const origin = req.headers.origin;
63
+ const origin = req.headers.origin
64
64
  if (origin) {
65
65
  if (ao.includes(origin)) {
66
- res.setHeader('Access-Control-Allow-Origin', origin);
66
+ res.setHeader("Access-Control-Allow-Origin", origin)
67
67
  }
68
68
  }
69
- res.header('Access-Control-Allow-Credentials', access.credentials);
70
- res.header('Access-Control-Allow-Methods', access.methods);
71
- res.setHeader('Access-Control-Allow-Headers', access.headers);
72
- next();
73
- };
69
+ res.header("Access-Control-Allow-Credentials", access.credentials)
70
+ res.header("Access-Control-Allow-Methods", access.methods)
71
+ res.setHeader("Access-Control-Allow-Headers", access.headers)
72
+ next()
73
+ }
74
74
  }
75
75
  return (req: Request, res: Response, next: NextFunction) => {
76
- res.header('Access-Control-Allow-Credentials', access.credentials);
77
- res.header('Access-Control-Allow-Methods', access.methods);
78
- res.setHeader('Access-Control-Allow-Headers', access.headers);
79
- next();
80
- };
76
+ res.header("Access-Control-Allow-Credentials", access.credentials)
77
+ res.header("Access-Control-Allow-Methods", access.methods)
78
+ res.setHeader("Access-Control-Allow-Headers", access.headers)
79
+ next()
80
+ }
81
81
  }
82
82
  export interface SavedService<T> {
83
- load(id: string): Promise<T[]>;
84
- save(id: string, itemId: string): Promise<number>;
85
- remove(id: string, itemId: string): Promise<number>;
83
+ load(id: string): Promise<T[]>
84
+ save(id: string, itemId: string): Promise<number>
85
+ remove(id: string, itemId: string): Promise<number>
86
86
  }
87
87
  export class SavedController<T> {
88
88
  constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
89
- this.id = id && id.length > 0 ? id : 'id';
90
- this.save = this.save.bind(this);
91
- this.remove = this.remove.bind(this);
92
- this.load = this.load.bind(this);
89
+ this.id = id && id.length > 0 ? id : "id"
90
+ this.save = this.save.bind(this)
91
+ this.remove = this.remove.bind(this)
92
+ this.load = this.load.bind(this)
93
93
  }
94
- id: string;
94
+ id: string
95
95
  save(req: Request, res: Response) {
96
- const id = req.params[this.id];
97
- const itemId = req.params[this.item];
96
+ const id = req.params[this.id]
97
+ const itemId = req.params[this.item]
98
98
  if (!id || id.length === 0) {
99
- res.status(400).end(`'${this.id}' cannot be empty`);
100
- return;
99
+ res.status(400).end(`'${this.id}' cannot be empty`)
100
+ return
101
101
  }
102
102
  if (!itemId || itemId.length === 0) {
103
- res.status(400).end(`'${this.item}' cannot be empty`);
104
- return;
103
+ res.status(400).end(`'${this.item}' cannot be empty`)
104
+ return
105
105
  }
106
106
  this.service
107
107
  .save(id, itemId)
108
108
  .then((data) => {
109
- res.status(200).json(data).end();
109
+ res.status(200).json(data).end()
110
110
  })
111
- .catch((err) => handleError(err, res, this.log));
111
+ .catch((err) => handleError(err, res, this.log))
112
112
  }
113
113
  remove(req: Request, res: Response) {
114
- const id = req.params[this.id];
115
- const itemId = req.params[this.item];
114
+ const id = req.params[this.id]
115
+ const itemId = req.params[this.item]
116
116
  if (!id || id.length === 0) {
117
- res.status(400).end(`'${this.id}' cannot be empty`);
118
- return;
117
+ res.status(400).end(`'${this.id}' cannot be empty`)
118
+ return
119
119
  }
120
120
  if (!itemId || itemId.length === 0) {
121
- res.status(400).end(`'${this.item}' cannot be empty`);
122
- return;
121
+ res.status(400).end(`'${this.item}' cannot be empty`)
122
+ return
123
123
  }
124
124
  this.service
125
125
  .remove(id, itemId)
126
126
  .then((data) => {
127
- res.status(200).json(data).end();
127
+ res.status(200).json(data).end()
128
128
  })
129
- .catch((err) => handleError(err, res, this.log));
129
+ .catch((err) => handleError(err, res, this.log))
130
130
  }
131
131
  load(req: Request, res: Response) {
132
- const id = req.params[this.id];
132
+ const id = req.params[this.id]
133
133
  if (!id || id.length === 0) {
134
- res.status(400).end(`'${this.id}' cannot be empty`);
135
- return;
134
+ res.status(400).end(`'${this.id}' cannot be empty`)
135
+ return
136
136
  }
137
137
  this.service
138
138
  .load(id)
139
139
  .then((data) => {
140
- res.status(200).json(data).end();
140
+ res.status(200).json(data).end()
141
141
  })
142
- .catch((err) => handleError(err, res, this.log));
142
+ .catch((err) => handleError(err, res, this.log))
143
143
  }
144
144
  }
145
145
  export interface FollowService {
146
- follow(id: string, target: string): Promise<number | undefined>;
147
- unfollow(id: string, target: string): Promise<number>;
148
- checkFollow(id: string, target: string): Promise<number>;
146
+ follow(id: string, target: string): Promise<number | undefined>
147
+ unfollow(id: string, target: string): Promise<number>
148
+ checkFollow(id: string, target: string): Promise<number>
149
149
  }
150
150
  // tslint:disable-next-line:max-classes-per-file
151
151
  export class FollowController {
152
152
  constructor(public log: Log, public service: FollowService, public target: string, id: string) {
153
- this.id = id && id.length > 0 ? id : 'id';
154
- this.follow = this.follow.bind(this);
155
- this.unfollow = this.unfollow.bind(this);
156
- this.checkFollow = this.checkFollow.bind(this);
153
+ this.id = id && id.length > 0 ? id : "id"
154
+ this.follow = this.follow.bind(this)
155
+ this.unfollow = this.unfollow.bind(this)
156
+ this.checkFollow = this.checkFollow.bind(this)
157
157
  }
158
- id: string;
158
+ id: string
159
159
  follow(req: Request, res: Response): void {
160
- const id = req.params.id;
161
- const target = req.params.target;
160
+ const id = req.params.id
161
+ const target = req.params.target
162
162
  if (!id || id.length === 0) {
163
- res.status(400).end(`'${this.id}' cannot be empty`);
164
- return;
163
+ res.status(400).end(`'${this.id}' cannot be empty`)
164
+ return
165
165
  }
166
166
  if (!target || target.length === 0) {
167
- res.status(400).end(`'${this.target}' cannot be empty`);
168
- return;
167
+ res.status(400).end(`'${this.target}' cannot be empty`)
168
+ return
169
169
  }
170
170
  this.service
171
171
  .follow(id, target)
172
172
  .then((count) => {
173
- return res.status(200).json(count).end();
173
+ return res.status(200).json(count).end()
174
174
  })
175
- .catch((err) => handleError(err, res, this.log));
175
+ .catch((err) => handleError(err, res, this.log))
176
176
  }
177
177
  unfollow(req: Request, res: Response): void {
178
- const id = req.params.id;
179
- const target = req.params.target;
178
+ const id = req.params.id
179
+ const target = req.params.target
180
180
  if (!id || id.length === 0) {
181
- res.status(400).end(`'${this.id}' cannot be empty`);
182
- return;
181
+ res.status(400).end(`'${this.id}' cannot be empty`)
182
+ return
183
183
  }
184
184
  if (!target || target.length === 0) {
185
- res.status(400).end(`'${this.target}' cannot be empty`);
186
- return;
185
+ res.status(400).end(`'${this.target}' cannot be empty`)
186
+ return
187
187
  }
188
188
  this.service
189
189
  .unfollow(id, target)
190
190
  .then((count) => {
191
- return res.status(200).json(count).end();
191
+ return res.status(200).json(count).end()
192
192
  })
193
- .catch((err) => handleError(err, res, this.log));
193
+ .catch((err) => handleError(err, res, this.log))
194
194
  }
195
195
  checkFollow(req: Request, res: Response): void {
196
- const id = req.params.id;
197
- const target = req.params.target;
196
+ const id = req.params.id
197
+ const target = req.params.target
198
198
  if (!id || id.length === 0) {
199
- res.status(400).end(`'${this.id}' cannot be empty`);
200
- return;
199
+ res.status(400).end(`'${this.id}' cannot be empty`)
200
+ return
201
201
  }
202
202
  if (!target || target.length === 0) {
203
- res.status(400).end(`'${this.target}' cannot be empty`);
204
- return;
203
+ res.status(400).end(`'${this.target}' cannot be empty`)
204
+ return
205
205
  }
206
206
  this.service
207
207
  .checkFollow(id, target)
208
208
  .then((count) => {
209
- return res.status(200).json(count).end();
209
+ return res.status(200).json(count).end()
210
210
  })
211
- .catch((err) => handleError(err, res, this.log));
211
+ .catch((err) => handleError(err, res, this.log))
212
212
  }
213
213
  }
214
214
  export interface ReactService {
215
- react(id: string, author: string, reaction: string): Promise<number>;
216
- unreact(id: string, author: string, reaction: string): Promise<number>;
217
- checkReaction(id: string, author: string): Promise<number>;
215
+ react(id: string, author: string, reaction: string): Promise<number>
216
+ unreact(id: string, author: string, reaction: string): Promise<number>
217
+ checkReaction(id: string, author: string): Promise<number>
218
218
  }
219
219
  // tslint:disable-next-line:max-classes-per-file
220
220
  export class UserReactionController {
221
221
  constructor(public log: Log, public service: ReactService, public author: string, id: string, public reaction: string) {
222
- this.id = id && id.length > 0 ? id : 'id';
223
- this.react = this.react.bind(this);
224
- this.unreact = this.unreact.bind(this);
225
- this.checkReaction = this.checkReaction.bind(this);
222
+ this.id = id && id.length > 0 ? id : "id"
223
+ this.react = this.react.bind(this)
224
+ this.unreact = this.unreact.bind(this)
225
+ this.checkReaction = this.checkReaction.bind(this)
226
226
  }
227
- id: string;
227
+ id: string
228
228
  react(req: Request, res: Response): void {
229
- const id = req.params.id;
230
- const author = req.params.author;
231
- const reaction = req.params.reaction;
229
+ const id = req.params.id
230
+ const author = req.params.author
231
+ const reaction = req.params.reaction
232
232
  if (!id || id.length === 0) {
233
- res.status(400).end(`'${this.id}' cannot be empty`);
234
- return;
233
+ res.status(400).end(`'${this.id}' cannot be empty`)
234
+ return
235
235
  }
236
236
  if (!author || author.length === 0) {
237
- res.status(400).end(`'${this.author}' cannot be empty`);
238
- return;
237
+ res.status(400).end(`'${this.author}' cannot be empty`)
238
+ return
239
239
  }
240
240
  if (!reaction || reaction.length === 0) {
241
- res.status(400).end(`'${this.reaction}' cannot be empty`);
242
- return;
241
+ res.status(400).end(`'${this.reaction}' cannot be empty`)
242
+ return
243
243
  }
244
244
  this.service
245
245
  .react(id, author, reaction)
246
246
  .then((count) => {
247
- return res.status(200).json(count).end();
247
+ return res.status(200).json(count).end()
248
248
  })
249
- .catch((err) => handleError(err, res, this.log));
249
+ .catch((err) => handleError(err, res, this.log))
250
250
  }
251
251
  unreact(req: Request, res: Response): void {
252
- const id = req.params.id;
253
- const author = req.params.author;
254
- const reaction = req.params.reaction;
252
+ const id = req.params.id
253
+ const author = req.params.author
254
+ const reaction = req.params.reaction
255
255
  if (!id || id.length === 0) {
256
- res.status(400).end(`'${this.id}' cannot be empty`);
257
- return;
256
+ res.status(400).end(`'${this.id}' cannot be empty`)
257
+ return
258
258
  }
259
259
  if (!author || author.length === 0) {
260
- res.status(400).end(`'${this.author}' cannot be empty`);
261
- return;
260
+ res.status(400).end(`'${this.author}' cannot be empty`)
261
+ return
262
262
  }
263
263
  if (!reaction || reaction.length === 0) {
264
- res.status(400).end(`'${this.reaction}' cannot be empty`);
265
- return;
264
+ res.status(400).end(`'${this.reaction}' cannot be empty`)
265
+ return
266
266
  }
267
267
  this.service
268
268
  .unreact(id, author, reaction)
269
269
  .then((count) => {
270
- return res.status(200).json(count).end();
270
+ return res.status(200).json(count).end()
271
271
  })
272
- .catch((err) => handleError(err, res, this.log));
272
+ .catch((err) => handleError(err, res, this.log))
273
273
  }
274
274
  checkReaction(req: Request, res: Response): void {
275
- const id = req.params.id;
276
- const author = req.params.author;
275
+ const id = req.params.id
276
+ const author = req.params.author
277
277
  if (!id || id.length === 0) {
278
- res.status(400).end(`'${this.id}' cannot be empty`);
279
- return;
278
+ res.status(400).end(`'${this.id}' cannot be empty`)
279
+ return
280
280
  }
281
281
  if (!author || author.length === 0) {
282
- res.status(400).end(`'${this.author}' cannot be empty`);
283
- return;
282
+ res.status(400).end(`'${this.author}' cannot be empty`)
283
+ return
284
284
  }
285
285
  this.service
286
286
  .checkReaction(id, author)
287
287
  .then((count) => {
288
- return res.status(200).json(count).end();
288
+ return res.status(200).json(count).end()
289
289
  })
290
- .catch((err) => handleError(err, res, this.log));
290
+ .catch((err) => handleError(err, res, this.log))
291
291
  }
292
292
  }
293
- export const ReactController = UserReactionController;
294
- export const ReactionController = UserReactionController;
293
+ export const ReactController = UserReactionController
294
+ export const ReactionController = UserReactionController
295
295
 
296
296
  export function checked(s: string[] | string | undefined, v: string): boolean | undefined {
297
297
  if (s) {
298
298
  if (Array.isArray(s)) {
299
- return s.includes(v);
299
+ return s.includes(v)
300
300
  } else {
301
- return s === v;
301
+ return s === v
302
302
  }
303
303
  }
304
- return false;
304
+ return false
305
305
  }
306
306
  export function addSeconds(date: Date, number: number): Date {
307
- const d = new Date(date);
308
- d.setSeconds(d.getSeconds() + number);
309
- return d;
307
+ const d = new Date(date)
308
+ d.setSeconds(d.getSeconds() + number)
309
+ return d
310
310
  }
311
311
  export function addMinutes(date: Date, number: number): Date {
312
- const d = new Date(date);
313
- d.setMinutes(d.getMinutes() + number);
314
- return d;
312
+ const d = new Date(date)
313
+ d.setMinutes(d.getMinutes() + number)
314
+ return d
315
315
  }
316
316
  export function addDays(d: Date, n: number): Date {
317
- const newDate = new Date(d);
318
- newDate.setDate(newDate.getDate() + n);
319
- return newDate;
317
+ const newDate = new Date(d)
318
+ newDate.setDate(newDate.getDate() + n)
319
+ return newDate
320
320
  }
321
321
  export interface ErrorMap {
322
- [key: string]: ErrorMessage;
322
+ [key: string]: ErrorMessage
323
323
  }
324
324
  export function toMap(errors: ErrorMessage[]): ErrorMap {
325
- const errorMap: ErrorMap = {};
325
+ const errorMap: ErrorMap = {}
326
326
  if (!errors) {
327
- return errorMap;
327
+ return errorMap
328
328
  }
329
329
  for (let i = 0; i < errors.length; i++) {
330
- errors[i].invalid = 'invalid';
331
- errorMap[errors[i].field] = errors[i];
330
+ errors[i].invalid = "invalid"
331
+ errorMap[errors[i].field] = errors[i]
332
332
  }
333
- return errorMap;
333
+ return errorMap
334
334
  }
335
335
  const map: StringMap = {
336
- '&': '&amp;',
337
- '<': '&lt;',
338
- '>': '&gt;',
339
- '"': '&quot;',
340
- "'": '&#39;',
341
- '`': '&#96;',
342
- };
336
+ "&": "&amp;",
337
+ "<": "&lt;",
338
+ ">": "&gt;",
339
+ '"': "&quot;",
340
+ "'": "&#39;",
341
+ "`": "&#96;",
342
+ }
343
343
  function escapeHTML(input: string): string {
344
344
  return input.replace(/[&<>"'`]/g, function (char) {
345
- return map[char];
346
- });
345
+ return map[char]
346
+ })
347
347
  }
348
348
 
349
- const s = 'string';
350
- const o = 'object';
349
+ const s = "string"
350
+ const o = "object"
351
351
  export function escape(obj: any): any {
352
352
  if (!obj || typeof obj !== s) {
353
- return obj;
353
+ return obj
354
354
  }
355
- const keys = Object.keys(obj);
355
+ const keys = Object.keys(obj)
356
356
  for (const key of keys) {
357
- const v = obj[key];
357
+ const v = obj[key]
358
358
  if (typeof v === s) {
359
- obj[key] = escapeHTML(v);
359
+ obj[key] = escapeHTML(v)
360
360
  } else if (Array.isArray(v) && v.length > 0) {
361
- const v1 = v[0];
361
+ const v1 = v[0]
362
362
  if (typeof v1 === o && !(v1 instanceof Date)) {
363
363
  for (const item of v) {
364
- escape(item);
364
+ escape(item)
365
365
  }
366
366
  }
367
367
  } else if (typeof v === o && !(v instanceof Date)) {
368
- escape(obj[key]);
368
+ escape(obj[key])
369
369
  }
370
370
  }
371
- return obj;
371
+ return obj
372
372
  }
373
373
  export function escapeArray<T>(arrs: T[]): T[] {
374
374
  if (!arrs) {
375
- return arrs;
375
+ return arrs
376
376
  }
377
377
  if (arrs.length > 0) {
378
378
  for (const obj of arrs) {
379
- escape(obj);
379
+ escape(obj)
380
380
  }
381
381
  }
382
- return arrs;
382
+ return arrs
383
383
  }