express-ext 0.1.26 → 0.1.29

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.
@@ -52,8 +52,8 @@ var LoadController = (function () {
52
52
  return LoadController;
53
53
  }());
54
54
  exports.LoadController = LoadController;
55
- var QueryController = (function () {
56
- function QueryController(log, loadData, name, param, max, maxName) {
55
+ var ItemController = (function () {
56
+ function ItemController(log, loadData, name, param, max, maxName) {
57
57
  this.log = log;
58
58
  this.loadData = loadData;
59
59
  this.param = param;
@@ -63,10 +63,10 @@ var QueryController = (function () {
63
63
  this.load = this.load.bind(this);
64
64
  this.query = this.query.bind(this);
65
65
  }
66
- QueryController.prototype.query = function (req, res) {
66
+ ItemController.prototype.query = function (req, res) {
67
67
  return this.load(req, res);
68
68
  };
69
- QueryController.prototype.load = function (req, res) {
69
+ ItemController.prototype.load = function (req, res) {
70
70
  var _this = this;
71
71
  var v = this.param ? req.params[this.name] : req.query[this.name];
72
72
  if (!v) {
@@ -85,6 +85,7 @@ var QueryController = (function () {
85
85
  }
86
86
  }
87
87
  };
88
- return QueryController;
88
+ return ItemController;
89
89
  }());
90
- exports.QueryController = QueryController;
90
+ exports.ItemController = ItemController;
91
+ exports.ItemHandler = ItemController;
@@ -67,3 +67,46 @@ var LoadSearchController = (function (_super) {
67
67
  return LoadSearchController;
68
68
  }(LoadController_1.LoadController));
69
69
  exports.LoadSearchController = LoadSearchController;
70
+ var QueryController = (function (_super) {
71
+ __extends(QueryController, _super);
72
+ function QueryController(log, query, config, dates, numbers, array) {
73
+ var _this = _super.call(this, log, query) || this;
74
+ _this.query = query;
75
+ _this.search = _this.search.bind(_this);
76
+ _this.array = array;
77
+ if (config) {
78
+ if (typeof config === 'boolean') {
79
+ _this.csv = config;
80
+ }
81
+ else {
82
+ _this.config = search_1.initializeConfig(config);
83
+ if (_this.config) {
84
+ _this.csv = _this.config.csv;
85
+ _this.fields = _this.config.fields;
86
+ _this.excluding = _this.config.excluding;
87
+ }
88
+ }
89
+ }
90
+ if (!_this.fields || _this.fields.length === 0) {
91
+ _this.fields = 'fields';
92
+ }
93
+ var m = search_func_1.getMetadataFunc(query, dates, numbers);
94
+ if (m) {
95
+ _this.dates = m.dates;
96
+ _this.numbers = m.numbers;
97
+ }
98
+ return _this;
99
+ }
100
+ QueryController.prototype.search = function (req, res) {
101
+ var _this = this;
102
+ var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
103
+ var l = search_1.getParameters(s, this.config);
104
+ var s2 = search_1.format(s, this.dates, this.numbers);
105
+ this.query.search(s2, l.limit, l.skipOrRefId, l.fields)
106
+ .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
107
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
108
+ };
109
+ return QueryController;
110
+ }(LoadController_1.LoadController));
111
+ exports.QueryController = QueryController;
112
+ exports.QueryHandler = QueryController;
@@ -17,9 +17,44 @@ 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, build, validate, dates, numbers) {
23
+ var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
24
+ _this.lowCodeService = lowCodeService;
25
+ _this.search = _this.search.bind(_this);
26
+ _this.config = search_1.initializeConfig(config);
27
+ if (_this.config) {
28
+ _this.csv = _this.config.csv;
29
+ _this.fields = _this.config.fields;
30
+ _this.excluding = _this.config.excluding;
31
+ }
32
+ if (!_this.fields || _this.fields.length === 0) {
33
+ _this.fields = 'fields';
34
+ }
35
+ var m = search_func_1.getMetadataFunc(lowCodeService, dates, numbers);
36
+ if (m) {
37
+ _this.dates = m.dates;
38
+ _this.numbers = m.numbers;
39
+ }
40
+ return _this;
41
+ }
42
+ LowcodeController.prototype.search = function (req, res) {
43
+ var _this = this;
44
+ var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
45
+ var l = search_1.getParameters(s, this.config);
46
+ var s2 = search_1.format(s, this.dates, this.numbers);
47
+ this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
48
+ .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
49
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
50
+ };
51
+ return LowcodeController;
52
+ }(GenericController_1.GenericController));
53
+ exports.LowcodeController = LowcodeController;
54
+ exports.LowcodeHandler = LowcodeController;
20
55
  var Controller = (function (_super) {
21
56
  __extends(Controller, _super);
22
- function Controller(log, lowCodeService, config, build, validate, dates, numbers) {
57
+ function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
23
58
  var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
24
59
  _this.lowCodeService = lowCodeService;
25
60
  _this.search = _this.search.bind(_this);
package/lib/index.js CHANGED
@@ -12,14 +12,12 @@ exports.HealthHandler = HealthController_1.HealthController;
12
12
  var LoadController_1 = require("./LoadController");
13
13
  exports.LoadHandler = LoadController_1.LoadController;
14
14
  exports.ViewHandler = LoadController_1.LoadController;
15
- exports.ViewController = LoadController_1.LoadController;
16
15
  var LoadSearchController_1 = require("./LoadSearchController");
17
16
  exports.LoadSearchHandler = LoadSearchController_1.LoadSearchController;
18
17
  var LogController_1 = require("./LogController");
19
18
  exports.LogHandler = LogController_1.LogController;
20
19
  var LowCodeController_1 = require("./LowCodeController");
21
- exports.LowCodeHandler = LowCodeController_1.Controller;
22
- exports.LowCodeController = LowCodeController_1.Controller;
20
+ exports.Handler = LowCodeController_1.Controller;
23
21
  var SearchController_1 = require("./SearchController");
24
22
  exports.SearchHandler = SearchController_1.SearchController;
25
23
  __export(require("./health"));
@@ -72,3 +70,30 @@ function allow(access) {
72
70
  };
73
71
  }
74
72
  exports.allow = allow;
73
+ var SavedController = (function () {
74
+ function SavedController(log, service, item, id) {
75
+ this.log = log;
76
+ this.service = service;
77
+ this.item = item;
78
+ this.id = (id && id.length > 0 ? id : 'id');
79
+ this.save = this.save.bind(this);
80
+ this.load = this.load.bind(this);
81
+ }
82
+ SavedController.prototype.save = function (req, res) {
83
+ var id = req.params[this.id];
84
+ var itemId = req.params[this.item];
85
+ this.service.save(id, itemId).then(function (data) {
86
+ res.status(200).json(data).end();
87
+ })
88
+ .catch(function (err) { return console.log(err); });
89
+ };
90
+ SavedController.prototype.load = function (req, res) {
91
+ var id = req.params[this.id];
92
+ this.service.load(id).then(function (data) {
93
+ res.status(200).json(data).send();
94
+ })
95
+ .catch(function (err) { return console.log(err); });
96
+ };
97
+ return SavedController;
98
+ }());
99
+ exports.SavedController = SavedController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.26",
3
+ "version": "0.1.29",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -55,7 +55,7 @@ export class LoadController<T, ID> {
55
55
  }
56
56
  }
57
57
  // tslint:disable-next-line:max-classes-per-file
58
- export class QueryController<T> {
58
+ export class ItemController<T> {
59
59
  constructor(protected log: Log, private loadData: (keyword: string, max?: number) => Promise<T>, name?: string, protected param?: boolean, max?: number, maxName?: string) {
60
60
  this.name = (name && name.length > 0 ? name : 'keyword');
61
61
  this.max = (max && max > 0 ? max : 20);
@@ -86,3 +86,4 @@ export class QueryController<T> {
86
86
  }
87
87
  }
88
88
  }
89
+ export {ItemController as ItemHandler};
@@ -9,6 +9,11 @@ export interface Search {
9
9
  search(req: Request, res: Response): void;
10
10
  load(req: Request, res: Response): void;
11
11
  }
12
+ export interface Query<T, ID, S> extends ViewService<T, ID> {
13
+ search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
14
+ metadata?(): Attributes|undefined;
15
+ load(id: ID, ctx?: any): Promise<T|null>;
16
+ }
12
17
  export interface SearchManager {
13
18
  search(req: Request, res: Response): void;
14
19
  load(req: Request, res: Response): void;
@@ -62,3 +67,46 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
62
67
  .catch(err => handleError(err, res, this.log));
63
68
  }
64
69
  }
70
+ export class QueryController<T, ID, S extends Filter> extends LoadController<T, ID> {
71
+ config?: SearchConfig;
72
+ csv?: boolean;
73
+ dates?: string[];
74
+ numbers?: string[];
75
+ fields?: string;
76
+ excluding?: string;
77
+ array?: string[];
78
+ constructor(log: Log, protected query: Query<T, ID, S>, config?: SearchConfig|boolean, dates?: string[], numbers?: string[], array?: string[]) {
79
+ super(log, query);
80
+ this.search = this.search.bind(this);
81
+ this.array = array;
82
+ if (config) {
83
+ if (typeof config === 'boolean') {
84
+ this.csv = config;
85
+ } else {
86
+ this.config = initializeConfig(config);
87
+ if (this.config) {
88
+ this.csv = this.config.csv;
89
+ this.fields = this.config.fields;
90
+ this.excluding = this.config.excluding;
91
+ }
92
+ }
93
+ }
94
+ if (!this.fields || this.fields.length === 0) {
95
+ this.fields = 'fields';
96
+ }
97
+ const m = getMetadataFunc(query, dates, numbers);
98
+ if (m) {
99
+ this.dates = m.dates;
100
+ this.numbers = m.numbers;
101
+ }
102
+ }
103
+ search(req: Request, res: Response): void {
104
+ const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
105
+ const l = getParameters(s, this.config);
106
+ const s2 = format(s, this.dates, this.numbers);
107
+ this.query.search(s2, l.limit, l.skipOrRefId, l.fields)
108
+ .then(result => jsonResult(res, result, this.csv, l.fields, this.config))
109
+ .catch(err => handleError(err, res, this.log));
110
+ }
111
+ }
112
+ export {QueryController as QueryHandler};
@@ -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 Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
14
+ export class LowcodeController<T, ID, S extends Filter> extends GenericController<T, ID> {
15
15
  config?: SearchConfig;
16
16
  csv?: boolean;
17
17
  dates?: string[];
@@ -46,3 +46,39 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
46
46
  .catch(err => handleError(err, res, this.log));
47
47
  }
48
48
  }
49
+ export {LowcodeController as LowcodeHandler};
50
+ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
51
+ config?: SearchConfig;
52
+ csv?: boolean;
53
+ dates?: string[];
54
+ numbers?: string[];
55
+ fields?: string;
56
+ excluding?: string;
57
+ array?: string[];
58
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: LowCodeConfig, dates?: string[], numbers?: string[]) {
59
+ super(log, lowCodeService, config, build, validate);
60
+ this.search = this.search.bind(this);
61
+ this.config = initializeConfig(config);
62
+ if (this.config) {
63
+ this.csv = this.config.csv;
64
+ this.fields = this.config.fields;
65
+ this.excluding = this.config.excluding;
66
+ }
67
+ if (!this.fields || this.fields.length === 0) {
68
+ this.fields = 'fields';
69
+ }
70
+ const m = getMetadataFunc(lowCodeService, dates, numbers);
71
+ if (m) {
72
+ this.dates = m.dates;
73
+ this.numbers = m.numbers;
74
+ }
75
+ }
76
+ search(req: Request, res: Response) {
77
+ const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
78
+ const l = getParameters(s, this.config);
79
+ const s2 = format(s, this.dates, this.numbers);
80
+ this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
81
+ .then(result => jsonResult(res, result, this.csv, l.fields, this.config))
82
+ .catch(err => handleError(err, res, this.log));
83
+ }
84
+ }
package/src/index.ts CHANGED
@@ -13,15 +13,13 @@ export {HealthController as HealthHandler};
13
13
  export {LogController as LogHandler};
14
14
  export {LoadController as LoadHandler};
15
15
  export {LoadController as ViewHandler};
16
- export {LoadController as ViewController};
16
+ // export {LoadController as ViewController};
17
17
 
18
18
  export {GenericController as GenericHandler};
19
19
  export {SearchController as SearchHandler};
20
20
  export {LoadSearchController as LoadSearchHandler};
21
21
  export {GenericSearchController as GenericSearchHandler};
22
- export {Controller as LowCodeHandler};
23
- // export {Controller as Handler};
24
- export {Controller as LowCodeController};
22
+ export {Controller as Handler};
25
23
  export {Service as LowCodeService};
26
24
 
27
25
  export * from './health';
@@ -81,3 +79,30 @@ export function allow(access: AccessConfig): (req: Request, res: Response, next:
81
79
  next();
82
80
  };
83
81
  }
82
+ export interface SavedService<T> {
83
+ load(id: string): Promise<T[]>;
84
+ save(id: string, itemId: string): Promise<number>;
85
+ }
86
+ export class SavedController<T> {
87
+ constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
88
+ this.id = (id && id.length > 0 ? id : 'id');
89
+ this.save = this.save.bind(this);
90
+ this.load = this.load.bind(this);
91
+ }
92
+ id: string;
93
+ save(req: Request, res: Response) {
94
+ const id = req.params[this.id];
95
+ const itemId = req.params[this.item];
96
+ this.service.save(id, itemId).then(data => {
97
+ res.status(200).json(data).end();
98
+ })
99
+ .catch(err => console.log(err));
100
+ }
101
+ load(req: Request, res: Response) {
102
+ const id = req.params[this.id];
103
+ this.service.load(id).then(data => {
104
+ res.status(200).json(data).send();
105
+ })
106
+ .catch(err => console.log(err));
107
+ }
108
+ }