express-ext 0.1.17 → 0.1.20

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.
@@ -80,21 +80,23 @@ exports.GenericController = GenericController;
80
80
  function validateAndCreate(req, res, status, save, log, validate) {
81
81
  var obj = req.body;
82
82
  if (!obj || obj === '') {
83
- return res.status(400).end('The request body cannot be empty.');
84
- }
85
- if (validate) {
86
- validate(obj).then(function (errors) {
87
- if (errors && errors.length > 0) {
88
- var r = { status: status.validation_error, errors: errors };
89
- res.status(getStatusCode(errors)).json(r).end();
90
- }
91
- else {
92
- edit_1.create(res, status, obj, save, log);
93
- }
94
- }).catch(function (err) { return http_1.handleError(err, res, log); });
83
+ res.status(400).end('The request body cannot be empty.');
95
84
  }
96
85
  else {
97
- edit_1.create(res, status, obj, save, log);
86
+ if (validate) {
87
+ validate(obj).then(function (errors) {
88
+ if (errors && errors.length > 0) {
89
+ var r = { status: status.validation_error, errors: errors };
90
+ res.status(getStatusCode(errors)).json(r).end();
91
+ }
92
+ else {
93
+ edit_1.create(res, status, obj, save, log);
94
+ }
95
+ }).catch(function (err) { return http_1.handleError(err, res, log); });
96
+ }
97
+ else {
98
+ edit_1.create(res, status, obj, save, log);
99
+ }
98
100
  }
99
101
  }
100
102
  exports.validateAndCreate = validateAndCreate;
@@ -17,6 +17,15 @@ var http_1 = require("./http");
17
17
  var LoadController_1 = require("./LoadController");
18
18
  var search_1 = require("./search");
19
19
  var search_func_1 = require("./search_func");
20
+ function useSearchController(log, find, viewService, array, dates, numbers, keys, config) {
21
+ var c = new LoadSearchController(log, find, viewService, keys, config, dates, numbers);
22
+ c.array = array;
23
+ return c;
24
+ }
25
+ exports.useSearchController = useSearchController;
26
+ exports.useSearchHandler = useSearchController;
27
+ exports.createSearchController = useSearchController;
28
+ exports.createSearchHandler = useSearchController;
20
29
  var LoadSearchController = (function (_super) {
21
30
  __extends(LoadSearchController, _super);
22
31
  function LoadSearchController(log, find, viewService, keys, config, dates, numbers) {
@@ -17,9 +17,9 @@ var GenericController_1 = require("./GenericController");
17
17
  var http_1 = require("./http");
18
18
  var search_1 = require("./search");
19
19
  var search_func_1 = require("./search_func");
20
- var LowCodeController = (function (_super) {
21
- __extends(LowCodeController, _super);
22
- function LowCodeController(log, lowCodeService, config, validate, dates, numbers) {
20
+ var Controller = (function (_super) {
21
+ __extends(Controller, _super);
22
+ function Controller(log, lowCodeService, config, validate, dates, numbers) {
23
23
  var _this = _super.call(this, log, lowCodeService, config, validate) || this;
24
24
  _this.lowCodeService = lowCodeService;
25
25
  _this.search = _this.search.bind(_this);
@@ -39,7 +39,7 @@ var LowCodeController = (function (_super) {
39
39
  }
40
40
  return _this;
41
41
  }
42
- LowCodeController.prototype.search = function (req, res) {
42
+ Controller.prototype.search = function (req, res) {
43
43
  var _this = this;
44
44
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
45
45
  var l = search_1.getParameters(s, this.config);
@@ -48,6 +48,6 @@ var LowCodeController = (function (_super) {
48
48
  .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
49
49
  .catch(function (err) { return http_1.handleError(err, res, _this.log); });
50
50
  };
51
- return LowCodeController;
51
+ return Controller;
52
52
  }(GenericController_1.GenericController));
53
- exports.LowCodeController = LowCodeController;
53
+ exports.Controller = Controller;
package/lib/http.js CHANGED
@@ -10,6 +10,7 @@ function handleError(err, res, log) {
10
10
  }
11
11
  }
12
12
  exports.handleError = handleError;
13
+ exports.error = handleError;
13
14
  function toString(v) {
14
15
  if (typeof v === 'string') {
15
16
  return v;
@@ -174,6 +175,7 @@ function param(req, res, name) {
174
175
  return v;
175
176
  }
176
177
  exports.param = param;
178
+ exports.getParam = param;
177
179
  function params(req, name, d, split) {
178
180
  var v = req.params[name];
179
181
  if (!v || v.length === 0) {
@@ -185,6 +187,7 @@ function params(req, name, d, split) {
185
187
  return v.split(split);
186
188
  }
187
189
  exports.params = params;
190
+ exports.getParams = params;
188
191
  function getRequiredParameters(req, res, name, split) {
189
192
  var v = req.params[name];
190
193
  if (!v || v.length === 0) {
package/lib/index.js CHANGED
@@ -1,3 +1 @@
1
- "use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}
2
- Object.defineProperty(exports,"__esModule",{value:!0});var GenericController_1=require("./GenericController");exports.GenericHandler=GenericController_1.GenericController;var GenericSearchController_1=require("./GenericSearchController");exports.GenericSearchHandler=GenericSearchController_1.GenericSearchController;var HealthController_1=require("./HealthController");exports.HealthHandler=HealthController_1.HealthController;var LoadController_1=require("./LoadController");exports.LoadHandler=LoadController_1.LoadController;exports.ViewHandler=LoadController_1.LoadController;exports.ViewController=LoadController_1.LoadController;var LoadSearchController_1=require("./LoadSearchController");exports.LoadSearchHandler=LoadSearchController_1.LoadSearchController;var LogController_1=require("./LogController");exports.LogHandler=LogController_1.LogController;var LowCodeController_1=require("./LowCodeController");exports.LowCodeHandler=LowCodeController_1.LowCodeController;exports.Handler=LowCodeController_1.LowCodeController;exports.Controller=LowCodeController_1.LowCodeController;var SearchController_1=require("./SearchController");exports.SearchHandler=SearchController_1.SearchController;__export(require("./health"));__export(require("./HealthController"));__export(require("./LogController"));__export(require("./log"));__export(require("./http"));__export(require("./view"));__export(require("./LoadController"));__export(require("./search_func"));__export(require("./search"));__export(require("./SearchController"));__export(require("./LoadSearchController"));__export(require("./resources"));__export(require("./edit"));__export(require("./GenericController"));__export(require("./GenericSearchController"));__export(require("./LowCodeController"));function allow(access){return function(req,res,next){res.header('Access-Control-Allow-Origin',access.origin);res.header('Access-Control-Allow-Credentials',access.credentials);res.header('Access-Control-Allow-Methods',access.methods);res.setHeader('Access-Control-Allow-Headers',access.headers);next()}}
3
- exports.allow=allow
1
+ "use strict";function __export(r){for(var e in r)exports.hasOwnProperty(e)||(exports[e]=r[e])}Object.defineProperty(exports,"__esModule",{value:!0});var GenericController_1=require("./GenericController");exports.GenericHandler=GenericController_1.GenericController;var GenericSearchController_1=require("./GenericSearchController");exports.GenericSearchHandler=GenericSearchController_1.GenericSearchController;var HealthController_1=require("./HealthController");exports.HealthHandler=HealthController_1.HealthController;var LoadController_1=require("./LoadController");exports.LoadHandler=LoadController_1.LoadController,exports.ViewHandler=LoadController_1.LoadController,exports.ViewController=LoadController_1.LoadController;var LoadSearchController_1=require("./LoadSearchController");exports.LoadSearchHandler=LoadSearchController_1.LoadSearchController;var LogController_1=require("./LogController");exports.LogHandler=LogController_1.LogController;var LowCodeController_1=require("./LowCodeController");exports.LowCodeHandler=LowCodeController_1.Controller,exports.LowCodeController=LowCodeController_1.Controller;var SearchController_1=require("./SearchController");function allow(r){return function(e,o,l){o.header("Access-Control-Allow-Origin",r.origin),o.header("Access-Control-Allow-Credentials",r.credentials),o.header("Access-Control-Allow-Methods",r.methods),o.setHeader("Access-Control-Allow-Headers",r.headers),l()}}exports.SearchHandler=SearchController_1.SearchController,__export(require("./health")),__export(require("./HealthController")),__export(require("./LogController")),__export(require("./log")),__export(require("./http")),__export(require("./view")),__export(require("./LoadController")),__export(require("./search_func")),__export(require("./search")),__export(require("./SearchController")),__export(require("./LoadSearchController")),__export(require("./resources")),__export(require("./edit")),__export(require("./GenericController")),__export(require("./GenericSearchController")),__export(require("./LowCodeController")),exports.allow=allow;
package/lib/resources.js CHANGED
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var fs = require("fs");
4
+ var http = require("http");
5
+ var https = require("https");
4
6
  var resources = (function () {
5
7
  function resources() {
6
8
  }
@@ -17,7 +19,7 @@ var TypeChecker = (function () {
17
19
  TypeChecker.prototype.check = function (req, res, next) {
18
20
  var obj = req.body;
19
21
  if (!obj || obj === '') {
20
- return res.status(400).end('The request body cannot be empty');
22
+ res.status(400).end('The request body cannot be empty');
21
23
  }
22
24
  else {
23
25
  var errors = resources.check(obj, this.attributes, this.allowUndefined);
@@ -53,3 +55,35 @@ function loadTemplates(ok, buildTemplates, correct, files) {
53
55
  return buildTemplates(mappers, correct);
54
56
  }
55
57
  exports.loadTemplates = loadTemplates;
58
+ function start(a, s) {
59
+ process.on('uncaughtException', function (err) {
60
+ console.log(err);
61
+ });
62
+ if (s.https) {
63
+ if (s.options) {
64
+ https.createServer(s.options, a).listen(s.port, function () {
65
+ console.log('Use https and start server at port ' + s.port);
66
+ });
67
+ }
68
+ else if (s.key && s.cert && s.key.length > 0 && s.cert.length > 0) {
69
+ var options = {
70
+ key: fs.readFileSync(s.key),
71
+ cert: fs.readFileSync(s.cert)
72
+ };
73
+ https.createServer(options, a).listen(s.port, function () {
74
+ console.log('Use https and start server at port ' + s.port);
75
+ });
76
+ }
77
+ else {
78
+ http.createServer(a).listen(s.port, function () {
79
+ console.log('Start server at port ' + s.port);
80
+ });
81
+ }
82
+ }
83
+ else {
84
+ http.createServer(a).listen(s.port, function () {
85
+ console.log('Start server at port ' + s.port);
86
+ });
87
+ }
88
+ }
89
+ exports.start = start;
package/lib/search.js CHANGED
@@ -139,28 +139,40 @@ function inArray(s, arr) {
139
139
  return false;
140
140
  }
141
141
  exports.inArray = inArray;
142
- function setValue(obj, path, value) {
143
- var paths = path.split('.');
144
- if (paths.length === 1) {
145
- obj[path] = value;
142
+ function setValue(o, key, value) {
143
+ var obj = o;
144
+ var replaceKey = key.replace(/\[/g, '.[').replace(/\.\./g, '.');
145
+ if (replaceKey.indexOf('.') === 0) {
146
+ replaceKey = replaceKey.slice(1, replaceKey.length);
147
+ }
148
+ var keys = replaceKey.split('.');
149
+ var firstKey = keys.shift();
150
+ if (!firstKey) {
151
+ return;
152
+ }
153
+ var isArrayKey = /\[([0-9]+)\]/.test(firstKey);
154
+ if (keys.length > 0) {
155
+ var firstKeyValue = obj[firstKey] || {};
156
+ var returnValue = setValue(firstKeyValue, keys.join('.'), value);
157
+ return setKey(obj, isArrayKey, firstKey, returnValue);
158
+ }
159
+ return setKey(obj, isArrayKey, firstKey, value);
160
+ }
161
+ exports.setValue = setValue;
162
+ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
163
+ if (_isArrayKey) {
164
+ if (_object.length > _key) {
165
+ _object[_key] = _nextValue;
166
+ }
167
+ else {
168
+ _object.push(_nextValue);
169
+ }
146
170
  }
147
171
  else {
148
- var o = obj;
149
- var l = paths.length - 1;
150
- for (var i = 0; i < l - 1; i++) {
151
- var p = paths[i];
152
- if (p in o) {
153
- o = o[p];
154
- }
155
- else {
156
- o[p] = {};
157
- o = o[p];
158
- }
159
- }
160
- o[paths[paths.length - 1]] = value;
172
+ _object[_key] = _nextValue;
161
173
  }
162
- }
163
- exports.setValue = setValue;
174
+ return _object;
175
+ };
164
176
  function getParameters(obj, config) {
165
177
  var o = obj;
166
178
  if (!config) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.17",
3
+ "version": "0.1.20",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -36,25 +36,25 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
36
36
  this.validate = v.validate;
37
37
  }
38
38
  }
39
- create(req: Request, res: Response) {
39
+ create(req: Request, res: Response): void {
40
40
  return this.insert(req, res);
41
41
  }
42
- insert(req: Request, res: Response) {
42
+ insert(req: Request, res: Response): void {
43
43
  validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate);
44
44
  }
45
- update(req: Request, res: Response) {
45
+ update(req: Request, res: Response): void {
46
46
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update);
47
47
  if (id) {
48
48
  validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate);
49
49
  }
50
50
  }
51
- patch(req: Request, res: Response) {
51
+ patch(req: Request, res: Response): void {
52
52
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch);
53
53
  if (id && this.service.patch) {
54
54
  validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate);
55
55
  }
56
56
  }
57
- delete(req: Request, res: Response) {
57
+ delete(req: Request, res: Response): void {
58
58
  const id = buildAndCheckId<ID>(req, res, this.keys);
59
59
  if (id) {
60
60
  if (!this.service.delete) {
@@ -70,19 +70,20 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
70
70
  export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: Log, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
71
71
  const obj = req.body;
72
72
  if (!obj || obj === '') {
73
- return res.status(400).end('The request body cannot be empty.');
74
- }
75
- if (validate) {
76
- validate(obj).then(errors => {
77
- if (errors && errors.length > 0) {
78
- const r: ResultInfo<T> = {status: status.validation_error, errors};
79
- res.status(getStatusCode(errors)).json(r).end();
80
- } else {
81
- create(res, status, obj, save, log);
82
- }
83
- }).catch(err => handleError(err, res, log));
73
+ res.status(400).end('The request body cannot be empty.');
84
74
  } else {
85
- create(res, status, obj, save, log);
75
+ if (validate) {
76
+ validate(obj).then(errors => {
77
+ if (errors && errors.length > 0) {
78
+ const r: ResultInfo<T> = {status: status.validation_error, errors};
79
+ res.status(getStatusCode(errors)).json(r).end();
80
+ } else {
81
+ create(res, status, obj, save, log);
82
+ }
83
+ }).catch(err => handleError(err, res, log));
84
+ } else {
85
+ create(res, status, obj, save, log);
86
+ }
86
87
  }
87
88
  }
88
89
  export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: Log, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
@@ -44,7 +44,7 @@ export class LoadController<T, ID> {
44
44
  this.view = getViewFunc(viewService);
45
45
  this.keys = getKeysFunc(viewService, keys);
46
46
  }
47
- load(req: Request, res: Response) {
47
+ load(req: Request, res: Response): void {
48
48
  const id = buildAndCheckId<ID>(req, res, this.keys);
49
49
  if (id) {
50
50
  this.view(id)
@@ -5,6 +5,22 @@ import {Attribute, Attributes} from './metadata';
5
5
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
6
6
  import {getMetadataFunc} from './search_func';
7
7
 
8
+ export interface Search {
9
+ search(req: Request, res: Response): void;
10
+ load(req: Request, res: Response): void;
11
+ }
12
+ export interface SearchManager {
13
+ search(req: Request, res: Response): void;
14
+ load(req: Request, res: Response): void;
15
+ }
16
+ export function useSearchController<T, ID, S extends Filter>(log: Log, find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), array?: string[], dates?: string[], numbers?: string[], keys?: Attributes|Attribute[]|string[], config?: SearchConfig|boolean): Search {
17
+ const c = new LoadSearchController(log, find, viewService, keys, config, dates, numbers);
18
+ c.array = array;
19
+ return c;
20
+ }
21
+ export const useSearchHandler = useSearchController;
22
+ export const createSearchController = useSearchController;
23
+ export const createSearchHandler = useSearchController;
8
24
  export class LoadSearchController<T, ID, S extends Filter> extends LoadController<T, ID> {
9
25
  config?: SearchConfig;
10
26
  csv?: boolean;
@@ -37,7 +53,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
37
53
  this.numbers = m.numbers;
38
54
  }
39
55
  }
40
- search(req: Request, res: Response) {
56
+ search(req: Request, res: Response): void {
41
57
  const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
42
58
  const l = getParameters(s, this.config);
43
59
  const s2 = format(s, this.dates, this.numbers);
@@ -11,7 +11,7 @@ export interface LowCodeConfig extends StatusConfig, SearchConfig {
11
11
  export interface Service<T, ID, R, S extends Filter> extends GenericService<T, ID, R> {
12
12
  search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
13
13
  }
14
- export class LowCodeController<T, ID, S extends Filter> extends GenericController<T, ID> {
14
+ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
15
15
  config?: SearchConfig;
16
16
  csv?: boolean;
17
17
  dates?: string[];
package/src/http.ts CHANGED
@@ -11,6 +11,7 @@ export function handleError(err: any, res: Response, log?: (msg: string) => void
11
11
  res.status(500).end(toString(err));
12
12
  }
13
13
  }
14
+ export const error = handleError;
14
15
  export function toString(v: any): string {
15
16
  if (typeof v === 'string') {
16
17
  return v;
@@ -158,6 +159,7 @@ export function param(req: Request, res: Response, name: string): string|undefin
158
159
  }
159
160
  return v;
160
161
  }
162
+ export const getParam = param;
161
163
  export function params(req: Request, name: string, d?: string[], split?: string): string[]|undefined {
162
164
  const v = req.params[name];
163
165
  if (!v || v.length === 0) {
@@ -168,6 +170,7 @@ export function params(req: Request, name: string, d?: string[], split?: string)
168
170
  }
169
171
  return v.split(split);
170
172
  }
173
+ export const getParams = params;
171
174
  export function getRequiredParameters(req: Request, res: Response, name: string, split?: string): string[]|undefined {
172
175
  const v = req.params[name];
173
176
  if (!v || v.length === 0) {
package/src/index.ts CHANGED
@@ -5,7 +5,7 @@ import {HealthController} from './HealthController';
5
5
  import {LoadController} from './LoadController';
6
6
  import {LoadSearchController} from './LoadSearchController';
7
7
  import {LogController} from './LogController';
8
- import {LowCodeController} from './LowCodeController';
8
+ import {Controller} from './LowCodeController';
9
9
  import {Service} from './LowCodeController';
10
10
  import {SearchController} from './SearchController';
11
11
 
@@ -19,9 +19,9 @@ export {GenericController as GenericHandler};
19
19
  export {SearchController as SearchHandler};
20
20
  export {LoadSearchController as LoadSearchHandler};
21
21
  export {GenericSearchController as GenericSearchHandler};
22
- export {LowCodeController as LowCodeHandler};
23
- export {LowCodeController as Handler};
24
- export {LowCodeController as Controller};
22
+ export {Controller as LowCodeHandler};
23
+ // export {Controller as Handler};
24
+ export {Controller as LowCodeController};
25
25
  export {Service as LowCodeService};
26
26
 
27
27
  export * from './health';
package/src/resources.ts CHANGED
@@ -1,5 +1,7 @@
1
- import { NextFunction, Request, Response } from 'express';
1
+ import { Application, NextFunction, Request, Response } from 'express';
2
2
  import * as fs from 'fs';
3
+ import * as http from 'http';
4
+ import * as https from 'https';
3
5
  import { Attributes, ErrorMessage } from './metadata';
4
6
 
5
7
  // tslint:disable-next-line:class-name
@@ -21,7 +23,7 @@ export class TypeChecker {
21
23
  check(req: Request, res: Response, next: NextFunction): void {
22
24
  const obj = req.body;
23
25
  if (!obj || (obj as any) === '') {
24
- return res.status(400).end('The request body cannot be empty');
26
+ res.status(400).end('The request body cannot be empty');
25
27
  } else {
26
28
  const errors = resources.check(obj, this.attributes, this.allowUndefined);
27
29
  if (errors.length > 0) {
@@ -37,6 +39,14 @@ export function check(attributes: Attributes, allowUndefined?: boolean): Handler
37
39
  const x = new TypeChecker(attributes, allowUndefined);
38
40
  return x.check;
39
41
  }
42
+ export interface Parameter {
43
+ name: string;
44
+ type: string;
45
+ }
46
+ export interface StringFormat {
47
+ texts: string[];
48
+ parameters: Parameter[];
49
+ }
40
50
  export interface Template {
41
51
  name?: string | null;
42
52
  text: string;
@@ -48,6 +58,11 @@ export interface TemplateNode {
48
58
  property: string | null;
49
59
  encode?: string | null;
50
60
  value: string | null;
61
+ format: StringFormat;
62
+ array?: string | null;
63
+ separator?: string | null;
64
+ suffix?: string | null;
65
+ prefix?: string | null;
51
66
  }
52
67
  export function loadTemplates(ok: boolean|undefined, buildTemplates: (streams: string[], correct?: (stream: string) => string) => Map<string, Template>, correct?: (stream: string) => string, files?: string[]): Map<string, Template>|undefined {
53
68
  if (!ok) {
@@ -63,3 +78,38 @@ export function loadTemplates(ok: boolean|undefined, buildTemplates: (streams: s
63
78
  }
64
79
  return buildTemplates(mappers, correct);
65
80
  }
81
+ export interface Server {
82
+ port: number;
83
+ https?: boolean;
84
+ options?: any;
85
+ key?: string;
86
+ cert?: string;
87
+ }
88
+ export function start(a: Application, s: Server): void {
89
+ process.on('uncaughtException', (err) => {
90
+ console.log(err);
91
+ });
92
+ if (s.https) {
93
+ if (s.options) {
94
+ https.createServer(s.options, a).listen(s.port, () => {
95
+ console.log('Use https and start server at port ' + s.port);
96
+ });
97
+ } else if (s.key && s.cert && s.key.length > 0 && s.cert.length > 0) {
98
+ const options = {
99
+ key: fs.readFileSync(s.key),
100
+ cert: fs.readFileSync(s.cert)
101
+ };
102
+ https.createServer(options, a).listen(s.port, () => {
103
+ console.log('Use https and start server at port ' + s.port);
104
+ });
105
+ } else {
106
+ http.createServer(a).listen(s.port, () => {
107
+ console.log('Start server at port ' + s.port);
108
+ });
109
+ }
110
+ } else {
111
+ http.createServer(a).listen(s.port, () => {
112
+ console.log('Start server at port ' + s.port);
113
+ });
114
+ }
115
+ }
package/src/search.ts CHANGED
@@ -137,17 +137,17 @@ export function fromUrl<S>(req: Request, arr?: string[]): S {
137
137
  }
138
138
  */
139
139
  const s: any = {};
140
- const obj = req.query;
141
- const keys = Object.keys(obj);
142
- for (const key of keys) {
143
- if (inArray(key, arr)) {
144
- const x = (obj[key] as string).split(',');
145
- setValue(s, key, x);
146
- } else {
147
- setValue(s, key, obj[key] as string);
148
- }
140
+ const obj = req.query;
141
+ const keys = Object.keys(obj);
142
+ for (const key of keys) {
143
+ if (inArray(key, arr)) {
144
+ const x = (obj[key] as string).split(',');
145
+ setValue(s, key, x);
146
+ } else {
147
+ setValue(s, key, obj[key] as string);
149
148
  }
150
- return s;
149
+ }
150
+ return s;
151
151
  }
152
152
  export function inArray(s: string, arr?: string[]): boolean {
153
153
  if (!arr || arr.length === 0) {
@@ -179,25 +179,37 @@ export function setValue<T>(obj: T, path: string, value: string): void {
179
179
  }
180
180
  }
181
181
  */
182
- export function setValue<T, V>(obj: T, path: string, value: V): void {
183
- const paths = path.split('.');
184
- if (paths.length === 1) {
185
- (obj as any)[path] = value;
186
- } else {
187
- let o: any = obj;
188
- const l = paths.length - 1;
189
- for (let i = 0; i < l - 1; i++) {
190
- const p = paths[i];
191
- if (p in o) {
192
- o = o[p];
193
- } else {
194
- o[p] = {};
195
- o = o[p];
196
- }
182
+ export function setValue<T, V>(o: T, key: string, value: V): any {
183
+ const obj: any = o;
184
+ let replaceKey = key.replace(/\[/g, '.[').replace(/\.\./g, '.');
185
+ if (replaceKey.indexOf('.') === 0) {
186
+ replaceKey = replaceKey.slice(1, replaceKey.length);
187
+ }
188
+ const keys = replaceKey.split('.');
189
+ const firstKey = keys.shift();
190
+ if (!firstKey) {
191
+ return;
192
+ }
193
+ const isArrayKey = /\[([0-9]+)\]/.test(firstKey);
194
+ if (keys.length > 0) {
195
+ const firstKeyValue = obj[firstKey] || {};
196
+ const returnValue = setValue(firstKeyValue, keys.join('.'), value);
197
+ return setKey(obj, isArrayKey, firstKey, returnValue);
198
+ }
199
+ return setKey(obj, isArrayKey, firstKey, value);
200
+ }
201
+ const setKey = (_object: any, _isArrayKey: boolean, _key: string, _nextValue: any) => {
202
+ if (_isArrayKey) {
203
+ if (_object.length > _key) {
204
+ _object[_key] = _nextValue;
205
+ } else {
206
+ _object.push(_nextValue);
197
207
  }
198
- o[paths[paths.length - 1]] = value;
208
+ } else {
209
+ _object[_key] = _nextValue;
199
210
  }
200
- }
211
+ return _object;
212
+ };
201
213
  export interface Limit {
202
214
  limit?: number;
203
215
  skip?: number;
@@ -370,7 +382,8 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
370
382
  return r;
371
383
  }
372
384
  }
373
- export function deletePageInfo(obj: any, arr?: (string | undefined)[]): void {
385
+ // tslint:disable-next-line:array-type
386
+ export function deletePageInfo(obj: any, arr?: Array<string | undefined>): void {
374
387
  if (!arr || arr.length === 0) {
375
388
  delete obj['limit'];
376
389
  delete obj['firstLimit'];