express-ext 0.1.19 → 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.
- package/lib/GenericController.js +15 -13
- package/lib/LoadSearchController.js +9 -0
- package/lib/search.js +31 -19
- package/package.json +1 -1
- package/src/GenericController.ts +18 -17
- package/src/LoadController.ts +1 -1
- package/src/LoadSearchController.ts +17 -1
- package/src/search.ts +41 -28
package/lib/GenericController.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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) {
|
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(
|
|
143
|
-
var
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
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
|
-
|
|
174
|
+
return _object;
|
|
175
|
+
};
|
|
164
176
|
function getParameters(obj, config) {
|
|
165
177
|
var o = obj;
|
|
166
178
|
if (!config) {
|
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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 {
|
package/src/LoadController.ts
CHANGED
|
@@ -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);
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
-
|
|
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>(
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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'];
|