express-ext 0.1.23 → 0.1.24
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/lib/LoadController.js +36 -0
- package/package.json +1 -1
- package/src/LoadController.ts +33 -1
package/lib/LoadController.js
CHANGED
|
@@ -52,3 +52,39 @@ 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) {
|
|
57
|
+
this.log = log;
|
|
58
|
+
this.loadData = loadData;
|
|
59
|
+
this.param = param;
|
|
60
|
+
this.name = (name && name.length > 0 ? name : 'keyword');
|
|
61
|
+
this.max = (max && max > 0 ? max : 20);
|
|
62
|
+
this.maxName = (maxName && maxName.length > 0 ? maxName : 'max');
|
|
63
|
+
this.load = this.load.bind(this);
|
|
64
|
+
this.query = this.query.bind(this);
|
|
65
|
+
}
|
|
66
|
+
QueryController.prototype.query = function (req, res) {
|
|
67
|
+
return this.load(req, res);
|
|
68
|
+
};
|
|
69
|
+
QueryController.prototype.load = function (req, res) {
|
|
70
|
+
var _this = this;
|
|
71
|
+
var v = this.param ? req.params[this.name] : req.query[this.name];
|
|
72
|
+
if (!v) {
|
|
73
|
+
res.status(400).end("'" + this.name + "' cannot be empty");
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
var s = v.toString();
|
|
77
|
+
if (s.length === 0) {
|
|
78
|
+
res.status(400).end("'" + this.name + "' cannot be empty");
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
var max = http_1.queryNumber(req, this.maxName, this.max);
|
|
82
|
+
this.loadData(s, max)
|
|
83
|
+
.then(function (result) { return http_1.respondModel(http_1.minimize(result), res); })
|
|
84
|
+
.catch(function (err) { return http_1.handleError(err, res, _this.log); });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return QueryController;
|
|
89
|
+
}());
|
|
90
|
+
exports.QueryController = QueryController;
|
package/package.json
CHANGED
package/src/LoadController.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
-
import {attrs, handleError, Log, minimize, respondModel} from './http';
|
|
2
|
+
import {attrs, handleError, Log, minimize, queryNumber, respondModel} from './http';
|
|
3
3
|
import {Attribute, Attributes} from './metadata';
|
|
4
4
|
import {buildAndCheckId, buildKeys} from './view';
|
|
5
5
|
|
|
@@ -54,3 +54,35 @@ export class LoadController<T, ID> {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
58
|
+
export class QueryController<T> {
|
|
59
|
+
constructor(protected log: Log, private loadData: (keyword: string, max?: number) => Promise<T>, name?: string, protected param?: boolean, max?: number, maxName?: string) {
|
|
60
|
+
this.name = (name && name.length > 0 ? name : 'keyword');
|
|
61
|
+
this.max = (max && max > 0 ? max : 20);
|
|
62
|
+
this.maxName = (maxName && maxName.length > 0 ? maxName : 'max');
|
|
63
|
+
this.load = this.load.bind(this);
|
|
64
|
+
this.query = this.query.bind(this);
|
|
65
|
+
}
|
|
66
|
+
name: string;
|
|
67
|
+
max: number;
|
|
68
|
+
maxName: string;
|
|
69
|
+
query(req: Request, res: Response) {
|
|
70
|
+
return this.load(req, res);
|
|
71
|
+
}
|
|
72
|
+
load(req: Request, res: Response) {
|
|
73
|
+
const v = this.param ? req.params[this.name] : req.query[this.name];
|
|
74
|
+
if (!v) {
|
|
75
|
+
res.status(400).end(`'${this.name}' cannot be empty`);
|
|
76
|
+
} else {
|
|
77
|
+
const s = v.toString();
|
|
78
|
+
if (s.length === 0) {
|
|
79
|
+
res.status(400).end(`'${this.name}' cannot be empty`);
|
|
80
|
+
} else {
|
|
81
|
+
const max = queryNumber(req, this.maxName, this.max);
|
|
82
|
+
this.loadData(s, max)
|
|
83
|
+
.then(result => respondModel(minimize(result), res))
|
|
84
|
+
.catch(err => handleError(err, res, this.log));
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|