express-ext 0.1.22 → 0.1.25
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/GenericController.js +6 -2
- package/lib/GenericSearchController.js +2 -2
- package/lib/LoadController.js +36 -0
- package/lib/LowCodeController.js +2 -2
- package/package.json +1 -1
- package/src/GenericController.ts +6 -2
- package/src/GenericSearchController.ts +3 -3
- package/src/LoadController.ts +35 -3
- package/src/LowCodeController.ts +3 -3
package/lib/GenericController.js
CHANGED
|
@@ -164,12 +164,12 @@ function getStatusCode(errs) {
|
|
|
164
164
|
}
|
|
165
165
|
exports.getStatusCode = getStatusCode;
|
|
166
166
|
function useBuild(c, generate) {
|
|
167
|
-
var b = new Builder(generate, c.id ? c.id : '', c.payload ? c.payload : '', c.user ? c.user : '', c.updatedBy ? c.updatedBy : '', c.updatedAt ? c.updatedAt : '', c.createdBy ? c.createdBy : '', c.createdAt ? c.createdAt : '');
|
|
167
|
+
var b = new Builder(generate, c.id ? c.id : '', c.payload ? c.payload : '', c.user ? c.user : '', c.updatedBy ? c.updatedBy : '', c.updatedAt ? c.updatedAt : '', c.createdBy ? c.createdBy : '', c.createdAt ? c.createdAt : '', c.version ? c.version : '');
|
|
168
168
|
return b.build;
|
|
169
169
|
}
|
|
170
170
|
exports.useBuild = useBuild;
|
|
171
171
|
var Builder = (function () {
|
|
172
|
-
function Builder(generate, id, payload, user, updatedBy, updatedAt, createdBy, createdAt) {
|
|
172
|
+
function Builder(generate, id, payload, user, updatedBy, updatedAt, createdBy, createdAt, version) {
|
|
173
173
|
this.generate = generate;
|
|
174
174
|
this.id = id;
|
|
175
175
|
this.payload = payload;
|
|
@@ -178,6 +178,7 @@ var Builder = (function () {
|
|
|
178
178
|
this.updatedAt = updatedAt;
|
|
179
179
|
this.createdBy = createdBy;
|
|
180
180
|
this.createdAt = createdAt;
|
|
181
|
+
this.version = version;
|
|
181
182
|
this.build = this.build.bind(this);
|
|
182
183
|
}
|
|
183
184
|
Builder.prototype.build = function (res, obj, isCreate, isPatch) {
|
|
@@ -210,6 +211,9 @@ var Builder = (function () {
|
|
|
210
211
|
o[this.createdBy] = usr;
|
|
211
212
|
}
|
|
212
213
|
}
|
|
214
|
+
if (this.version.length > 0) {
|
|
215
|
+
o[this.version] = 1;
|
|
216
|
+
}
|
|
213
217
|
}
|
|
214
218
|
else if (isPatch) {
|
|
215
219
|
var keys = Object.keys(o);
|
|
@@ -19,8 +19,8 @@ var search_1 = require("./search");
|
|
|
19
19
|
var search_func_1 = require("./search_func");
|
|
20
20
|
var GenericSearchController = (function (_super) {
|
|
21
21
|
__extends(GenericSearchController, _super);
|
|
22
|
-
function GenericSearchController(log, find, service, config, validate, dates, numbers) {
|
|
23
|
-
var _this = _super.call(this, log, service, config, validate) || this;
|
|
22
|
+
function GenericSearchController(log, find, service, config, validate, build, dates, numbers) {
|
|
23
|
+
var _this = _super.call(this, log, service, config, validate, build) || this;
|
|
24
24
|
_this.find = find;
|
|
25
25
|
_this.search = _this.search.bind(_this);
|
|
26
26
|
_this.config = search_1.initializeConfig(config);
|
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/lib/LowCodeController.js
CHANGED
|
@@ -19,8 +19,8 @@ var search_1 = require("./search");
|
|
|
19
19
|
var search_func_1 = require("./search_func");
|
|
20
20
|
var Controller = (function (_super) {
|
|
21
21
|
__extends(Controller, _super);
|
|
22
|
-
function Controller(log, lowCodeService, config, validate, dates, numbers) {
|
|
23
|
-
var _this = _super.call(this, log, lowCodeService, config, validate) || this;
|
|
22
|
+
function Controller(log, lowCodeService, config, validate, build, dates, numbers) {
|
|
23
|
+
var _this = _super.call(this, log, lowCodeService, config, validate, build) || this;
|
|
24
24
|
_this.lowCodeService = lowCodeService;
|
|
25
25
|
_this.search = _this.search.bind(_this);
|
|
26
26
|
_this.config = search_1.initializeConfig(config);
|
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -151,13 +151,14 @@ export interface ModelConfig {
|
|
|
151
151
|
updatedAt?: string;
|
|
152
152
|
createdBy?: string;
|
|
153
153
|
createdAt?: string;
|
|
154
|
+
version?: string;
|
|
154
155
|
}
|
|
155
156
|
export function useBuild<T>(c: ModelConfig, generate?: (() => string)): Build<T> {
|
|
156
|
-
const b = new Builder<T>(generate, c.id ? c.id : '', c.payload ? c.payload : '', c.user ? c.user : '', c.updatedBy ? c.updatedBy : '', c.updatedAt ? c.updatedAt : '', c.createdBy ? c.createdBy : '', c.createdAt ? c.createdAt : '');
|
|
157
|
+
const b = new Builder<T>(generate, c.id ? c.id : '', c.payload ? c.payload : '', c.user ? c.user : '', c.updatedBy ? c.updatedBy : '', c.updatedAt ? c.updatedAt : '', c.createdBy ? c.createdBy : '', c.createdAt ? c.createdAt : '', c.version ? c.version : '');
|
|
157
158
|
return b.build;
|
|
158
159
|
}
|
|
159
160
|
export class Builder<T> {
|
|
160
|
-
constructor(public generate: (() => string)|undefined, public id: string, public payload: string, public user: string, public updatedBy: string, public updatedAt: string, public createdBy: string, public createdAt: string) {
|
|
161
|
+
constructor(public generate: (() => string)|undefined, public id: string, public payload: string, public user: string, public updatedBy: string, public updatedAt: string, public createdBy: string, public createdAt: string, public version: string) {
|
|
161
162
|
this.build = this.build.bind(this);
|
|
162
163
|
}
|
|
163
164
|
build(res: Response, obj: T, isCreate?: boolean, isPatch?: boolean): void {
|
|
@@ -189,6 +190,9 @@ export class Builder<T> {
|
|
|
189
190
|
o[this.createdBy] = usr;
|
|
190
191
|
}
|
|
191
192
|
}
|
|
193
|
+
if (this.version.length > 0) {
|
|
194
|
+
o[this.version] = 1;
|
|
195
|
+
}
|
|
192
196
|
} else if (isPatch) {
|
|
193
197
|
const keys = Object.keys(o);
|
|
194
198
|
if (keys.length === 0) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {ResultInfo, StatusConfig} from './edit';
|
|
3
|
-
import {GenericController, GenericService} from './GenericController';
|
|
3
|
+
import {Build, GenericController, GenericService} from './GenericController';
|
|
4
4
|
import {handleError, Log} from './http';
|
|
5
5
|
import {ErrorMessage} from './metadata';
|
|
6
6
|
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
@@ -16,8 +16,8 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
|
|
|
16
16
|
fields?: string;
|
|
17
17
|
excluding?: string;
|
|
18
18
|
array?: string[];
|
|
19
|
-
constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
|
|
20
|
-
super(log, service, config, validate);
|
|
19
|
+
constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, build?: Build<T>, dates?: string[], numbers?: string[]) {
|
|
20
|
+
super(log, service, config, validate, build);
|
|
21
21
|
this.search = this.search.bind(this);
|
|
22
22
|
this.config = initializeConfig(config);
|
|
23
23
|
if (this.config) {
|
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
|
|
|
@@ -8,13 +8,13 @@ export interface ViewService<T, ID> {
|
|
|
8
8
|
load(id: ID, ctx?: any): Promise<T|null>;
|
|
9
9
|
}
|
|
10
10
|
export type Load<T, ID> = ((id: ID, ctx?: any) => Promise<T|null>);
|
|
11
|
-
function getViewFunc<T, ID>(viewService: ViewService<T, ID> |
|
|
11
|
+
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>): (id: ID, ctx?: any) => Promise<T|null> {
|
|
12
12
|
if (typeof viewService === 'function') {
|
|
13
13
|
return viewService;
|
|
14
14
|
}
|
|
15
15
|
return viewService.load;
|
|
16
16
|
}
|
|
17
|
-
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> |
|
|
17
|
+
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes|Attribute[]|string[]): Attribute[] | undefined {
|
|
18
18
|
if (keys) {
|
|
19
19
|
if (Array.isArray(keys)) {
|
|
20
20
|
if (keys.length > 0) {
|
|
@@ -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
|
+
}
|
package/src/LowCodeController.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {ResultInfo, StatusConfig} from './edit';
|
|
3
|
-
import {GenericController, GenericService} from './GenericController';
|
|
3
|
+
import {Build, GenericController, GenericService} from './GenericController';
|
|
4
4
|
import {handleError, Log} from './http';
|
|
5
5
|
import {ErrorMessage} from './metadata';
|
|
6
6
|
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
@@ -19,8 +19,8 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
|
|
|
19
19
|
fields?: string;
|
|
20
20
|
excluding?: string;
|
|
21
21
|
array?: string[];
|
|
22
|
-
constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
|
|
23
|
-
super(log, lowCodeService, config, validate);
|
|
22
|
+
constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, build?: Build<T>, dates?: string[], numbers?: string[]) {
|
|
23
|
+
super(log, lowCodeService, config, validate, build);
|
|
24
24
|
this.search = this.search.bind(this);
|
|
25
25
|
this.config = initializeConfig(config);
|
|
26
26
|
if (this.config) {
|