express-ext 0.4.0 → 0.4.2

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