express-ext 0.3.9 → 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/resources.ts CHANGED
@@ -1,82 +1,84 @@
1
- import { Application, NextFunction, Request, Response } from 'express';
2
- import * as fs from 'fs';
3
- import * as http from 'http';
4
- import * as https from 'https';
5
- import { Attributes, ErrorMessage } from './metadata';
1
+ import { Application, NextFunction, Request, Response } from "express"
2
+ import * as fs from "fs"
3
+ import * as http from "http"
4
+ import * as https from "https"
5
+ import { Attributes, ErrorMessage } from "./metadata"
6
6
 
7
7
  export interface StringMap {
8
- [key: string]: string;
8
+ [key: string]: string
9
9
  }
10
10
  // tslint:disable-next-line:class-name
11
11
  export class resources {
12
- static limits = [12, 24, 60, 100, 120, 180, 300, 600];
13
- static pages = 'pages';
14
- static page = 'page';
15
- static limit = 'limit';
16
- static defaultLimit = 12;
17
- static sort = 'sort';
18
- static partial = 'partial';
19
- static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>;
20
- static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[];
21
- static encoding?: BufferEncoding = 'utf-8';
12
+ static limits = [12, 24, 60, 100, 120, 180, 300, 600]
13
+ static pages = "pages"
14
+ static page = "page"
15
+ static nextPageToken = "next"
16
+ static limit = "limit"
17
+ static defaultLimit = 12
18
+ static sort = "sort"
19
+ static fields = "fields"
20
+ static partial = "partial"
21
+ static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>
22
+ static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[]
23
+ static encoding?: BufferEncoding = "utf-8"
22
24
  }
23
25
  export function getView(req: Request, view: string): string {
24
- const partial = req.query[resources.partial];
25
- return partial == 'true' ? resources.pages + '/' + view : view;
26
+ const partial = req.query[resources.partial]
27
+ return partial == "true" ? resources.pages + "/" + view : view
26
28
  }
27
29
 
28
30
  export interface Validator<T> {
29
- validate(obj: T, resource?: StringMap, patch?: boolean): Promise<ErrorMessage[]>;
31
+ validate(obj: T, resource?: StringMap, patch?: boolean): Promise<ErrorMessage[]>
30
32
  }
31
33
 
32
34
  // tslint:disable-next-line:max-classes-per-file
33
35
  export class TypeChecker {
34
36
  constructor(public attributes: Attributes, public allowUndefined?: boolean) {
35
- this.check = this.check.bind(this);
37
+ this.check = this.check.bind(this)
36
38
  }
37
39
  check(req: Request, res: Response, next: NextFunction): void {
38
- const obj = req.body;
39
- if (!obj || (obj as any) === '') {
40
- res.status(400).end('The request body cannot be empty');
40
+ const obj = req.body
41
+ if (!obj || (obj as any) === "") {
42
+ res.status(400).end("The request body cannot be empty")
41
43
  } else {
42
- const errors = resources.check(obj, this.attributes, this.allowUndefined);
44
+ const errors = resources.check(obj, this.attributes, this.allowUndefined)
43
45
  if (errors.length > 0) {
44
- res.status(400).json(errors).end();
46
+ res.status(400).json(errors).end()
45
47
  } else {
46
- next();
48
+ next()
47
49
  }
48
50
  }
49
51
  }
50
52
  }
51
- export type Handler = (req: Request, res: Response, next: NextFunction) => void;
53
+ export type Handler = (req: Request, res: Response, next: NextFunction) => void
52
54
  export function check(attributes: Attributes, allowUndefined?: boolean): Handler {
53
- const x = new TypeChecker(attributes, allowUndefined);
54
- return x.check;
55
+ const x = new TypeChecker(attributes, allowUndefined)
56
+ return x.check
55
57
  }
56
58
  export interface Parameter {
57
- name: string;
58
- type: string;
59
+ name: string
60
+ type: string
59
61
  }
60
62
  export interface StringFormat {
61
- texts: string[];
62
- parameters: Parameter[];
63
+ texts: string[]
64
+ parameters: Parameter[]
63
65
  }
64
66
  export interface Template {
65
- name?: string | null;
66
- text: string;
67
- templates: TemplateNode[];
67
+ name?: string | null
68
+ text: string
69
+ templates: TemplateNode[]
68
70
  }
69
71
  export interface TemplateNode {
70
- type: string;
71
- text: string;
72
- property: string | null;
73
- encode?: string | null;
74
- value: string | null;
75
- format: StringFormat;
76
- array?: string | null;
77
- separator?: string | null;
78
- suffix?: string | null;
79
- prefix?: string | null;
72
+ type: string
73
+ text: string
74
+ property: string | null
75
+ encode?: string | null
76
+ value: string | null
77
+ format: StringFormat
78
+ array?: string | null
79
+ separator?: string | null
80
+ suffix?: string | null
81
+ prefix?: string | null
80
82
  }
81
83
  export function loadTemplates(
82
84
  ok: boolean | undefined,
@@ -85,50 +87,50 @@ export function loadTemplates(
85
87
  files?: string[],
86
88
  ): Map<string, Template> | undefined {
87
89
  if (!ok) {
88
- return undefined;
90
+ return undefined
89
91
  }
90
92
  if (!files) {
91
- files = ['./src/query.xml'];
93
+ files = ["./src/query.xml"]
92
94
  }
93
- const mappers: string[] = [];
95
+ const mappers: string[] = []
94
96
  for (const file of files) {
95
- const mapper = fs.readFileSync(file, 'utf8');
96
- mappers.push(mapper);
97
+ const mapper = fs.readFileSync(file, "utf8")
98
+ mappers.push(mapper)
97
99
  }
98
- return buildTemplates(mappers, correct);
100
+ return buildTemplates(mappers, correct)
99
101
  }
100
102
  export interface Server {
101
- port: number;
102
- https?: boolean;
103
- options?: any;
104
- key?: string;
105
- cert?: string;
103
+ port: number
104
+ https?: boolean
105
+ options?: any
106
+ key?: string
107
+ cert?: string
106
108
  }
107
109
  export function start(a: Application, s: Server): void {
108
- process.on('uncaughtException', (err) => {
109
- console.log(err);
110
- });
110
+ process.on("uncaughtException", (err) => {
111
+ console.log(err)
112
+ })
111
113
  if (s.https) {
112
114
  if (s.options) {
113
115
  https.createServer(s.options, a).listen(s.port, () => {
114
- console.log('Use https and start server at port ' + s.port);
115
- });
116
+ console.log("Use https and start server at port " + s.port)
117
+ })
116
118
  } else if (s.key && s.cert && s.key.length > 0 && s.cert.length > 0) {
117
119
  const options = {
118
120
  key: fs.readFileSync(s.key),
119
121
  cert: fs.readFileSync(s.cert),
120
- };
122
+ }
121
123
  https.createServer(options, a).listen(s.port, () => {
122
- console.log('Use https and start server at port ' + s.port);
123
- });
124
+ console.log("Use https and start server at port " + s.port)
125
+ })
124
126
  } else {
125
127
  http.createServer(a).listen(s.port, () => {
126
- console.log('Start server at port ' + s.port);
127
- });
128
+ console.log("Start server at port " + s.port)
129
+ })
128
130
  }
129
131
  } else {
130
132
  http.createServer(a).listen(s.port, () => {
131
- console.log('Start server at port ' + s.port);
132
- });
133
+ console.log("Start server at port " + s.port)
134
+ })
133
135
  }
134
136
  }