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/client.ts CHANGED
@@ -1,69 +1,69 @@
1
- import * as http from 'http';
2
- import * as https from 'https';
3
- import { AnyMap, HealthChecker } from './health';
1
+ import * as http from "http"
2
+ import * as https from "https"
3
+ import { AnyMap, HealthChecker } from "./health"
4
4
 
5
5
  function getHealthSecure(url: string, timeout: number): Promise<AnyMap> {
6
6
  return new Promise((resolve) => {
7
- https.get(url, { rejectUnauthorized: false }, (res: any) => {
8
- let data = '';
9
- res.on('data', (d: any) => {
10
- data += d;
11
- });
12
- res.on('end', () => {
13
- resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage });
14
- });
15
- }).on('error', (e: any) => {
16
- return { statusCode: 500, statusMessage: e };
17
- });
18
- setTimeout(() => resolve({ statusCode: 408, statusMessage: 'Time out' }), timeout);
19
- });
7
+ https
8
+ .get(url, { rejectUnauthorized: false }, (res: any) => {
9
+ let data = ""
10
+ res.on("data", (d: any) => {
11
+ data += d
12
+ })
13
+ res.on("end", () => {
14
+ resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage })
15
+ })
16
+ })
17
+ .on("error", (e: any) => {
18
+ return { statusCode: 500, statusMessage: e }
19
+ })
20
+ setTimeout(() => resolve({ statusCode: 408, statusMessage: "Time out" }), timeout)
21
+ })
20
22
  }
21
23
  function getHealth(url: string, timeout: number): Promise<AnyMap> {
22
24
  return new Promise((resolve) => {
23
- http.get(url, (res: any) => {
24
- let data = '';
25
- res.on('data', (d: any) => {
26
- data += d;
27
- });
28
- res.on('end', () => {
29
- resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage });
30
- });
31
- }).on('error', (e: any) => {
32
- return { statusCode: 500, statusMessage: e };
33
- });
34
- setTimeout(() => resolve({ statusCode: 408, statusMessage: 'Time out' }), timeout);
35
- });
25
+ http
26
+ .get(url, (res: any) => {
27
+ let data = ""
28
+ res.on("data", (d: any) => {
29
+ data += d
30
+ })
31
+ res.on("end", () => {
32
+ resolve({ statusCode: res.statusCode, data, statusMessage: res.statusMessage })
33
+ })
34
+ })
35
+ .on("error", (e: any) => {
36
+ return { statusCode: 500, statusMessage: e }
37
+ })
38
+ setTimeout(() => resolve({ statusCode: 408, statusMessage: "Time out" }), timeout)
39
+ })
36
40
  }
37
41
  export class ClientChecker implements HealthChecker {
38
- timeout: number;
42
+ timeout: number
39
43
  constructor(private service: string, private url: string, timeout: number) {
40
- this.timeout = (timeout ? timeout : 4200);
41
- this.check = this.check.bind(this);
42
- this.name = this.name.bind(this);
43
- this.build = this.build.bind(this);
44
+ this.timeout = timeout ? timeout : 4200
45
+ this.check = this.check.bind(this)
46
+ this.name = this.name.bind(this)
47
+ this.build = this.build.bind(this)
44
48
  }
45
49
  check(): Promise<AnyMap> {
46
- let obj = {} as AnyMap;
47
- if (this.url.startsWith('https://')) {
48
- return getHealthSecure(this.url, this.timeout).then(
49
- r => obj = r
50
- );
50
+ let obj = {} as AnyMap
51
+ if (this.url.startsWith("https://")) {
52
+ return getHealthSecure(this.url, this.timeout).then((r) => (obj = r))
51
53
  } else {
52
- return getHealth(this.url, this.timeout).then(
53
- r => obj = r
54
- );
54
+ return getHealth(this.url, this.timeout).then((r) => (obj = r))
55
55
  }
56
56
  }
57
57
  name(): string {
58
- return this.service;
58
+ return this.service
59
59
  }
60
60
  build(data: AnyMap, err: any): AnyMap {
61
61
  if (err) {
62
62
  if (!data) {
63
- data = {} as AnyMap;
63
+ data = {} as AnyMap
64
64
  }
65
- data['error'] = err;
65
+ data["error"] = err
66
66
  }
67
- return data;
67
+ return data
68
68
  }
69
69
  }
package/src/edit.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { Response } from 'express';
2
- import { handleError } from './http';
3
- import { Attribute, ErrorMessage } from './metadata';
1
+ import { Response } from "express"
2
+ import { handleError } from "./http"
3
+ import { Attribute, ErrorMessage } from "./metadata"
4
4
  /*
5
5
  export interface StatusConfig {
6
6
  duplicate_key: number|string;
@@ -32,35 +32,35 @@ export function initializeStatus(s?: StatusConfig): StatusConfig {
32
32
  }
33
33
  */
34
34
  export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
35
- const n: string = keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id';
36
- const o: any = obj;
37
- const i: any = id;
35
+ const n: string = keys && keys.length === 1 && keys[0].name ? keys[0].name : "id"
36
+ const o: any = obj
37
+ const i: any = id
38
38
  if (!keys || keys.length === 1) {
39
- const v = o[n];
39
+ const v = o[n]
40
40
  if (!v) {
41
- o[n] = i;
42
- return true;
41
+ o[n] = i
42
+ return true
43
43
  }
44
44
  // tslint:disable-next-line:triple-equals
45
45
  if (v != i) {
46
- return false;
46
+ return false
47
47
  }
48
- return true;
48
+ return true
49
49
  }
50
- const ks = Object.keys(i);
50
+ const ks = Object.keys(i)
51
51
  for (const k of ks) {
52
- const v = o[k];
52
+ const v = o[k]
53
53
  if (!v) {
54
- o[k] = i[k];
54
+ o[k] = i[k]
55
55
  } else {
56
56
  // tslint:disable-next-line:triple-equals
57
57
  if (v != i[k]) {
58
- return false;
58
+ return false
59
59
  }
60
60
  }
61
- o[k] = i[k];
61
+ o[k] = i[k]
62
62
  }
63
- return true;
63
+ return true
64
64
  }
65
65
  export function create<T>(
66
66
  res: Response,
@@ -71,22 +71,22 @@ export function create<T>(
71
71
  ): void {
72
72
  insert(obj)
73
73
  .then((result) => {
74
- if (typeof result === 'number') {
74
+ if (typeof result === "number") {
75
75
  if (result >= 1) {
76
76
  res
77
77
  .status(201)
78
78
  .json(returnNumber ? result : obj)
79
- .end();
79
+ .end()
80
80
  } else {
81
- res.status(409).json(result).end();
81
+ res.status(409).json(result).end()
82
82
  }
83
83
  } else if (Array.isArray(result)) {
84
- res.status(422).json(result).end();
84
+ res.status(422).json(result).end()
85
85
  } else {
86
- res.status(201).json(result).end();
86
+ res.status(201).json(result).end()
87
87
  }
88
88
  })
89
- .catch((err) => handleError(err, res, log));
89
+ .catch((err) => handleError(err, res, log))
90
90
  }
91
91
  export function update<T>(
92
92
  res: Response,
@@ -97,46 +97,46 @@ export function update<T>(
97
97
  ): void {
98
98
  save(obj)
99
99
  .then((result) => {
100
- if (typeof result === 'number') {
100
+ if (typeof result === "number") {
101
101
  if (result >= 1) {
102
102
  res
103
103
  .status(200)
104
104
  .json(returnNumber ? result : obj)
105
- .end();
105
+ .end()
106
106
  } else if (result === 0) {
107
- res.status(404).json(result).end();
107
+ res.status(404).json(result).end()
108
108
  } else {
109
- res.status(409).json(result).end();
109
+ res.status(409).json(result).end()
110
110
  }
111
111
  } else if (Array.isArray(result)) {
112
- res.status(422).json(result).end();
112
+ res.status(422).json(result).end()
113
113
  } else {
114
- res.status(200).json(result).end();
114
+ res.status(200).json(result).end()
115
115
  }
116
116
  })
117
- .catch((err) => handleError(err, res, log));
117
+ .catch((err) => handleError(err, res, log))
118
118
  }
119
119
  export function isTypeError(errs: ErrorMessage[]): boolean {
120
120
  if (!errs) {
121
- return false;
121
+ return false
122
122
  }
123
123
  for (const err of errs) {
124
- const c = err.code;
124
+ const c = err.code
125
125
  if (
126
- c === 'type' ||
127
- c === 'string' ||
128
- c === 'number' ||
129
- c === 'date' ||
130
- c === 'boolean' ||
131
- c === 'strings' ||
132
- c === 'numbers' ||
133
- c === 'integers' ||
134
- c === 'dates' ||
135
- c === 'datetimes' ||
136
- c === 'booleans'
126
+ c === "type" ||
127
+ c === "string" ||
128
+ c === "number" ||
129
+ c === "date" ||
130
+ c === "boolean" ||
131
+ c === "strings" ||
132
+ c === "numbers" ||
133
+ c === "integers" ||
134
+ c === "dates" ||
135
+ c === "datetimes" ||
136
+ c === "booleans"
137
137
  ) {
138
- return true;
138
+ return true
139
139
  }
140
140
  }
141
- return false;
141
+ return false
142
142
  }
package/src/health.ts CHANGED
@@ -1,50 +1,50 @@
1
- export type HealthStatus = 'UP' | 'DOWN';
1
+ export type HealthStatus = "UP" | "DOWN"
2
2
  export interface HealthMap {
3
- [key: string]: Health;
3
+ [key: string]: Health
4
4
  }
5
5
  export interface Health {
6
- status: HealthStatus;
7
- data?: AnyMap;
8
- details?: HealthMap;
6
+ status: HealthStatus
7
+ data?: AnyMap
8
+ details?: HealthMap
9
9
  }
10
10
  export interface AnyMap {
11
- [key: string]: any;
11
+ [key: string]: any
12
12
  }
13
13
  export interface HealthChecker {
14
- name(): string;
15
- build(data: AnyMap, error: any): AnyMap;
16
- check(): Promise<AnyMap>;
14
+ name(): string
15
+ build(data: AnyMap, error: any): AnyMap
16
+ check(): Promise<AnyMap>
17
17
  }
18
18
 
19
19
  export async function health(checkers: HealthChecker[]): Promise<Health> {
20
- const p: Health = { status: 'UP' };
21
- const total = checkers.length - 1;
22
- let count = 0;
23
- p.details = {} as HealthMap;
20
+ const p: Health = { status: "UP" }
21
+ const total = checkers.length - 1
22
+ let count = 0
23
+ p.details = {} as HealthMap
24
24
  for (const checker of checkers) {
25
- const sub: Health = {status: 'UP'};
25
+ const sub: Health = { status: "UP" }
26
26
  try {
27
- const r = await checker.check();
27
+ const r = await checker.check()
28
28
  if (r && Object.keys(r).length > 0) {
29
- sub.data = r;
29
+ sub.data = r
30
30
  }
31
- p.details[checker.name()] = sub;
31
+ p.details[checker.name()] = sub
32
32
  if (count >= total) {
33
- return p;
33
+ return p
34
34
  } else {
35
- count++;
35
+ count++
36
36
  }
37
37
  } catch (err) {
38
- sub.status = 'DOWN';
39
- p.status = 'DOWN';
40
- sub.data = checker.build({} as AnyMap, err);
41
- p.details[checker.name()] = sub;
38
+ sub.status = "DOWN"
39
+ p.status = "DOWN"
40
+ sub.data = checker.build({} as AnyMap, err)
41
+ p.details[checker.name()] = sub
42
42
  if (count >= total) {
43
- return p;
43
+ return p
44
44
  } else {
45
- count++;
45
+ count++
46
46
  }
47
47
  }
48
48
  }
49
- return p;
49
+ return p
50
50
  }