express-ext 0.1.27 → 0.1.30

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,12 +67,13 @@ var LoadSearchController = (function (_super) {
67
67
  return LoadSearchController;
68
68
  }(LoadController_1.LoadController));
69
69
  exports.LoadSearchController = LoadSearchController;
70
- var ViewController = (function (_super) {
71
- __extends(ViewController, _super);
72
- function ViewController(log, query, config, dates, numbers) {
70
+ var QueryController = (function (_super) {
71
+ __extends(QueryController, _super);
72
+ function QueryController(log, query, config, dates, numbers, array) {
73
73
  var _this = _super.call(this, log, query) || this;
74
74
  _this.query = query;
75
75
  _this.search = _this.search.bind(_this);
76
+ _this.array = array;
76
77
  if (config) {
77
78
  if (typeof config === 'boolean') {
78
79
  _this.csv = config;
@@ -96,7 +97,7 @@ var ViewController = (function (_super) {
96
97
  }
97
98
  return _this;
98
99
  }
99
- ViewController.prototype.search = function (req, res) {
100
+ QueryController.prototype.search = function (req, res) {
100
101
  var _this = this;
101
102
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
102
103
  var l = search_1.getParameters(s, this.config);
@@ -105,6 +106,7 @@ var ViewController = (function (_super) {
105
106
  .then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
106
107
  .catch(function (err) { return http_1.handleError(err, res, _this.log); });
107
108
  };
108
- return ViewController;
109
+ return QueryController;
109
110
  }(LoadController_1.LoadController));
110
- exports.ViewController = ViewController;
111
+ exports.QueryController = QueryController;
112
+ exports.QueryHandler = QueryController;
@@ -51,6 +51,7 @@ var LowcodeController = (function (_super) {
51
51
  return LowcodeController;
52
52
  }(GenericController_1.GenericController));
53
53
  exports.LowcodeController = LowcodeController;
54
+ exports.LowcodeHandler = LowcodeController;
54
55
  var Controller = (function (_super) {
55
56
  __extends(Controller, _super);
56
57
  function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
package/lib/index.js CHANGED
@@ -9,17 +9,16 @@ var GenericSearchController_1 = require("./GenericSearchController");
9
9
  exports.GenericSearchHandler = GenericSearchController_1.GenericSearchController;
10
10
  var HealthController_1 = require("./HealthController");
11
11
  exports.HealthHandler = HealthController_1.HealthController;
12
+ var http_1 = require("./http");
12
13
  var LoadController_1 = require("./LoadController");
13
14
  exports.LoadHandler = LoadController_1.LoadController;
14
15
  exports.ViewHandler = LoadController_1.LoadController;
15
- exports.ViewController = LoadController_1.LoadController;
16
16
  var LoadSearchController_1 = require("./LoadSearchController");
17
17
  exports.LoadSearchHandler = LoadSearchController_1.LoadSearchController;
18
18
  var LogController_1 = require("./LogController");
19
19
  exports.LogHandler = LogController_1.LogController;
20
20
  var LowCodeController_1 = require("./LowCodeController");
21
- exports.LowCodeHandler = LowCodeController_1.Controller;
22
- exports.LowCodeController = LowCodeController_1.Controller;
21
+ exports.Handler = LowCodeController_1.Controller;
23
22
  var SearchController_1 = require("./SearchController");
24
23
  exports.SearchHandler = SearchController_1.SearchController;
25
24
  __export(require("./health"));
@@ -72,3 +71,123 @@ function allow(access) {
72
71
  };
73
72
  }
74
73
  exports.allow = allow;
74
+ var SavedController = (function () {
75
+ function SavedController(log, service, item, id) {
76
+ this.log = log;
77
+ this.service = service;
78
+ this.item = item;
79
+ this.id = (id && id.length > 0 ? id : 'id');
80
+ this.save = this.save.bind(this);
81
+ this.unsave = this.unsave.bind(this);
82
+ this.load = this.load.bind(this);
83
+ }
84
+ SavedController.prototype.save = function (req, res) {
85
+ var _this = this;
86
+ var id = req.params[this.id];
87
+ var itemId = req.params[this.item];
88
+ if (!id || id.length === 0) {
89
+ res.status(400).end("'" + this.id + "' cannot be empty");
90
+ return;
91
+ }
92
+ if (!itemId || itemId.length === 0) {
93
+ res.status(400).end("'" + this.item + "' cannot be empty");
94
+ return;
95
+ }
96
+ this.service.save(id, itemId).then(function (data) {
97
+ res.status(200).json(data).end();
98
+ })
99
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
100
+ };
101
+ SavedController.prototype.unsave = function (req, res) {
102
+ var _this = this;
103
+ var id = req.params[this.id];
104
+ var itemId = req.params[this.item];
105
+ if (!id || id.length === 0) {
106
+ res.status(400).end("'" + this.id + "' cannot be empty");
107
+ return;
108
+ }
109
+ if (!itemId || itemId.length === 0) {
110
+ res.status(400).end("'" + this.item + "' cannot be empty");
111
+ return;
112
+ }
113
+ this.service.unsave(id, itemId).then(function (data) {
114
+ res.status(200).json(data).end();
115
+ })
116
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
117
+ };
118
+ SavedController.prototype.load = function (req, res) {
119
+ var _this = this;
120
+ var id = req.params[this.id];
121
+ if (!id || id.length === 0) {
122
+ res.status(400).end("'" + this.id + "' cannot be empty");
123
+ return;
124
+ }
125
+ this.service.load(id).then(function (data) {
126
+ res.status(200).json(data).send();
127
+ })
128
+ .catch(function (err) { return http_1.handleError(err, res, _this.log); });
129
+ };
130
+ return SavedController;
131
+ }());
132
+ exports.SavedController = SavedController;
133
+ var FollowController = (function () {
134
+ function FollowController(log, service, target, id) {
135
+ this.log = log;
136
+ this.service = service;
137
+ this.target = target;
138
+ this.id = (id && id.length > 0 ? id : 'id');
139
+ this.follow = this.follow.bind(this);
140
+ this.unfollow = this.unfollow.bind(this);
141
+ this.checkfollow = this.checkfollow.bind(this);
142
+ }
143
+ FollowController.prototype.follow = function (req, res) {
144
+ var _this = this;
145
+ var id = req.params.id;
146
+ var target = req.params.target;
147
+ if (!id || id.length === 0) {
148
+ res.status(400).end("'" + this.id + "' cannot be empty");
149
+ return;
150
+ }
151
+ if (!target || target.length === 0) {
152
+ res.status(400).end("'" + this.target + "' cannot be empty");
153
+ return;
154
+ }
155
+ this.service.follow(id, target).then(function (count) {
156
+ return res.status(200).json(count).end();
157
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
158
+ };
159
+ FollowController.prototype.unfollow = function (req, res) {
160
+ var _this = this;
161
+ var id = req.params.id;
162
+ var target = req.params.target;
163
+ if (!id || id.length === 0) {
164
+ res.status(400).end("'" + this.id + "' cannot be empty");
165
+ return;
166
+ }
167
+ if (!target || target.length === 0) {
168
+ res.status(400).end("'" + this.target + "' cannot be empty");
169
+ return;
170
+ }
171
+ this.service.unfollow(id, target).then(function (count) {
172
+ return res.status(200).json(count).end();
173
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
174
+ };
175
+ FollowController.prototype.checkfollow = function (req, res) {
176
+ var _this = this;
177
+ var id = req.params.id;
178
+ var target = req.params.target;
179
+ if (!id || id.length === 0) {
180
+ res.status(400).end("'" + this.id + "' cannot be empty");
181
+ return;
182
+ }
183
+ if (!target || target.length === 0) {
184
+ res.status(400).end("'" + this.target + "' cannot be empty");
185
+ return;
186
+ }
187
+ this.service.checkFollow(id, target).then(function (count) {
188
+ return res.status(200).json(count).end();
189
+ }).catch(function (err) { return http_1.handleError(err, res, _this.log); });
190
+ };
191
+ return FollowController;
192
+ }());
193
+ exports.FollowController = FollowController;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.27",
3
+ "version": "0.1.30",
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};
@@ -67,7 +67,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
67
67
  .catch(err => handleError(err, res, this.log));
68
68
  }
69
69
  }
70
- export class ViewController<T, ID, S extends Filter> extends LoadController<T, ID> {
70
+ export class QueryController<T, ID, S extends Filter> extends LoadController<T, ID> {
71
71
  config?: SearchConfig;
72
72
  csv?: boolean;
73
73
  dates?: string[];
@@ -75,9 +75,10 @@ export class ViewController<T, ID, S extends Filter> extends LoadController<T, I
75
75
  fields?: string;
76
76
  excluding?: string;
77
77
  array?: string[];
78
- constructor(log: Log, protected query: Query<T, ID, S>, config?: SearchConfig|boolean, dates?: string[], numbers?: string[]) {
78
+ constructor(log: Log, protected query: Query<T, ID, S>, config?: SearchConfig|boolean, dates?: string[], numbers?: string[], array?: string[]) {
79
79
  super(log, query);
80
80
  this.search = this.search.bind(this);
81
+ this.array = array;
81
82
  if (config) {
82
83
  if (typeof config === 'boolean') {
83
84
  this.csv = config;
@@ -108,3 +109,4 @@ export class ViewController<T, ID, S extends Filter> extends LoadController<T, I
108
109
  .catch(err => handleError(err, res, this.log));
109
110
  }
110
111
  }
112
+ export {QueryController as QueryHandler};
@@ -46,6 +46,7 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
46
46
  .catch(err => handleError(err, res, this.log));
47
47
  }
48
48
  }
49
+ export {LowcodeController as LowcodeHandler};
49
50
  export class Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
50
51
  config?: SearchConfig;
51
52
  csv?: boolean;
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ import {NextFunction, Request, Response} from 'express';
2
2
  import {GenericController} from './GenericController';
3
3
  import {GenericSearchController} from './GenericSearchController';
4
4
  import {HealthController} from './HealthController';
5
+ import {handleError, Log} from './http';
5
6
  import {LoadController} from './LoadController';
6
7
  import {LoadSearchController} from './LoadSearchController';
7
8
  import {LogController} from './LogController';
@@ -13,15 +14,13 @@ export {HealthController as HealthHandler};
13
14
  export {LogController as LogHandler};
14
15
  export {LoadController as LoadHandler};
15
16
  export {LoadController as ViewHandler};
16
- export {LoadController as ViewController};
17
+ // export {LoadController as ViewController};
17
18
 
18
19
  export {GenericController as GenericHandler};
19
20
  export {SearchController as SearchHandler};
20
21
  export {LoadSearchController as LoadSearchHandler};
21
22
  export {GenericSearchController as GenericSearchHandler};
22
- export {Controller as LowCodeHandler};
23
- // export {Controller as Handler};
24
- export {Controller as LowCodeController};
23
+ export {Controller as Handler};
25
24
  export {Service as LowCodeService};
26
25
 
27
26
  export * from './health';
@@ -81,3 +80,120 @@ export function allow(access: AccessConfig): (req: Request, res: Response, next:
81
80
  next();
82
81
  };
83
82
  }
83
+ export interface SavedService<T> {
84
+ load(id: string): Promise<T[]>;
85
+ save(id: string, itemId: string): Promise<number>;
86
+ unsave(id: string, itemId: string): Promise<number>;
87
+ }
88
+ export class SavedController<T> {
89
+ constructor(public log: (msg: string) => void, public service: SavedService<T>, public item: string, id?: string) {
90
+ this.id = (id && id.length > 0 ? id : 'id');
91
+ this.save = this.save.bind(this);
92
+ this.unsave = this.unsave.bind(this);
93
+ this.load = this.load.bind(this);
94
+ }
95
+ id: string;
96
+ save(req: Request, res: Response) {
97
+ const id = req.params[this.id];
98
+ const itemId = req.params[this.item];
99
+ if (!id || id.length === 0) {
100
+ res.status(400).end(`'${this.id}' cannot be empty`);
101
+ return;
102
+ }
103
+ if (!itemId || itemId.length === 0) {
104
+ res.status(400).end(`'${this.item}' cannot be empty`);
105
+ return;
106
+ }
107
+ this.service.save(id, itemId).then(data => {
108
+ res.status(200).json(data).end();
109
+ })
110
+ .catch(err => handleError(err, res, this.log));
111
+ }
112
+ unsave(req: Request, res: Response) {
113
+ const id = req.params[this.id];
114
+ const itemId = req.params[this.item];
115
+ if (!id || id.length === 0) {
116
+ res.status(400).end(`'${this.id}' cannot be empty`);
117
+ return;
118
+ }
119
+ if (!itemId || itemId.length === 0) {
120
+ res.status(400).end(`'${this.item}' cannot be empty`);
121
+ return;
122
+ }
123
+ this.service.unsave(id, itemId).then(data => {
124
+ res.status(200).json(data).end();
125
+ })
126
+ .catch(err => handleError(err, res, this.log));
127
+ }
128
+ load(req: Request, res: Response) {
129
+ const id = req.params[this.id];
130
+ if (!id || id.length === 0) {
131
+ res.status(400).end(`'${this.id}' cannot be empty`);
132
+ return;
133
+ }
134
+ this.service.load(id).then(data => {
135
+ res.status(200).json(data).send();
136
+ })
137
+ .catch(err => handleError(err, res, this.log));
138
+ }
139
+ }
140
+ export interface FollowService {
141
+ follow(id: string, target: string): Promise<number | undefined>;
142
+ unfollow(id: string, target: string): Promise<number>;
143
+ checkFollow(id: string, target: string): Promise<number>;
144
+ }
145
+ // tslint:disable-next-line:max-classes-per-file
146
+ export class FollowController {
147
+ constructor(public log: Log, public service: FollowService, public target: string, id: string) {
148
+ this.id = (id && id.length > 0 ? id : 'id');
149
+ this.follow = this.follow.bind(this);
150
+ this.unfollow = this.unfollow.bind(this);
151
+ this.checkfollow = this.checkfollow.bind(this);
152
+ }
153
+ id: string;
154
+ follow(req: Request, res: Response): void {
155
+ const id = req.params.id;
156
+ const target = req.params.target;
157
+ if (!id || id.length === 0) {
158
+ res.status(400).end(`'${this.id}' cannot be empty`);
159
+ return;
160
+ }
161
+ if (!target || target.length === 0) {
162
+ res.status(400).end(`'${this.target}' cannot be empty`);
163
+ return;
164
+ }
165
+ this.service.follow(id, target).then(count => {
166
+ return res.status(200).json(count).end();
167
+ }).catch(err => handleError(err, res, this.log));
168
+ }
169
+ unfollow(req: Request, res: Response): void {
170
+ const id = req.params.id;
171
+ const target = req.params.target;
172
+ if (!id || id.length === 0) {
173
+ res.status(400).end(`'${this.id}' cannot be empty`);
174
+ return;
175
+ }
176
+ if (!target || target.length === 0) {
177
+ res.status(400).end(`'${this.target}' cannot be empty`);
178
+ return;
179
+ }
180
+ this.service.unfollow(id, target).then(count => {
181
+ return res.status(200).json(count).end();
182
+ }).catch(err => handleError(err, res, this.log));
183
+ }
184
+ checkfollow(req: Request, res: Response): void {
185
+ const id = req.params.id;
186
+ const target = req.params.target;
187
+ if (!id || id.length === 0) {
188
+ res.status(400).end(`'${this.id}' cannot be empty`);
189
+ return;
190
+ }
191
+ if (!target || target.length === 0) {
192
+ res.status(400).end(`'${this.target}' cannot be empty`);
193
+ return;
194
+ }
195
+ this.service.checkFollow(id, target).then(count => {
196
+ return res.status(200).json(count).end();
197
+ }).catch(err => handleError(err, res, this.log));
198
+ }
199
+ }