express-ext 0.1.21 → 0.1.22
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 +79 -6
- package/package.json +1 -1
- package/src/GenericController.ts +78 -7
- package/src/LoadController.ts +3 -2
package/lib/GenericController.js
CHANGED
|
@@ -20,10 +20,11 @@ var resources_1 = require("./resources");
|
|
|
20
20
|
var view_1 = require("./view");
|
|
21
21
|
var GenericController = (function (_super) {
|
|
22
22
|
__extends(GenericController, _super);
|
|
23
|
-
function GenericController(log, service, status, validate) {
|
|
23
|
+
function GenericController(log, service, status, validate, build) {
|
|
24
24
|
var _this = _super.call(this, log, service) || this;
|
|
25
25
|
_this.service = service;
|
|
26
26
|
_this.validate = validate;
|
|
27
|
+
_this.build = build;
|
|
27
28
|
_this.status = edit_1.initializeStatus(status);
|
|
28
29
|
if (service.metadata) {
|
|
29
30
|
var m = service.metadata();
|
|
@@ -46,18 +47,18 @@ var GenericController = (function (_super) {
|
|
|
46
47
|
return this.insert(req, res);
|
|
47
48
|
};
|
|
48
49
|
GenericController.prototype.insert = function (req, res) {
|
|
49
|
-
validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate);
|
|
50
|
+
validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
|
|
50
51
|
};
|
|
51
52
|
GenericController.prototype.update = function (req, res) {
|
|
52
53
|
var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.update);
|
|
53
54
|
if (id) {
|
|
54
|
-
validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate);
|
|
55
|
+
validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
|
|
55
56
|
}
|
|
56
57
|
};
|
|
57
58
|
GenericController.prototype.patch = function (req, res) {
|
|
58
59
|
var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.patch);
|
|
59
60
|
if (id && this.service.patch) {
|
|
60
|
-
validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate);
|
|
61
|
+
validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
|
|
61
62
|
}
|
|
62
63
|
};
|
|
63
64
|
GenericController.prototype.delete = function (req, res) {
|
|
@@ -77,7 +78,7 @@ var GenericController = (function (_super) {
|
|
|
77
78
|
return GenericController;
|
|
78
79
|
}(LoadController_1.LoadController));
|
|
79
80
|
exports.GenericController = GenericController;
|
|
80
|
-
function validateAndCreate(req, res, status, save, log, validate) {
|
|
81
|
+
function validateAndCreate(req, res, status, save, log, validate, build) {
|
|
81
82
|
var obj = req.body;
|
|
82
83
|
if (!obj || obj === '') {
|
|
83
84
|
res.status(400).end('The request body cannot be empty.');
|
|
@@ -90,6 +91,9 @@ function validateAndCreate(req, res, status, save, log, validate) {
|
|
|
90
91
|
res.status(getStatusCode(errors)).json(r).end();
|
|
91
92
|
}
|
|
92
93
|
else {
|
|
94
|
+
if (build) {
|
|
95
|
+
build(res, obj, true);
|
|
96
|
+
}
|
|
93
97
|
edit_1.create(res, status, obj, save, log);
|
|
94
98
|
}
|
|
95
99
|
}).catch(function (err) { return http_1.handleError(err, res, log); });
|
|
@@ -100,7 +104,7 @@ function validateAndCreate(req, res, status, save, log, validate) {
|
|
|
100
104
|
}
|
|
101
105
|
}
|
|
102
106
|
exports.validateAndCreate = validateAndCreate;
|
|
103
|
-
function validateAndUpdate(res, status, obj, isPatch, save, log, validate) {
|
|
107
|
+
function validateAndUpdate(res, status, obj, isPatch, save, log, validate, build) {
|
|
104
108
|
if (validate) {
|
|
105
109
|
validate(obj, isPatch).then(function (errors) {
|
|
106
110
|
if (errors && errors.length > 0) {
|
|
@@ -108,6 +112,9 @@ function validateAndUpdate(res, status, obj, isPatch, save, log, validate) {
|
|
|
108
112
|
res.status(getStatusCode(errors)).json(r).end();
|
|
109
113
|
}
|
|
110
114
|
else {
|
|
115
|
+
if (build) {
|
|
116
|
+
build(res, obj, false, isPatch);
|
|
117
|
+
}
|
|
111
118
|
edit_1.update(res, status, obj, save, log);
|
|
112
119
|
}
|
|
113
120
|
}).catch(function (err) { return http_1.handleError(err, res, log); });
|
|
@@ -156,3 +163,69 @@ function getStatusCode(errs) {
|
|
|
156
163
|
return (edit_1.isTypeError(errs) ? 400 : 422);
|
|
157
164
|
}
|
|
158
165
|
exports.getStatusCode = getStatusCode;
|
|
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 : '');
|
|
168
|
+
return b.build;
|
|
169
|
+
}
|
|
170
|
+
exports.useBuild = useBuild;
|
|
171
|
+
var Builder = (function () {
|
|
172
|
+
function Builder(generate, id, payload, user, updatedBy, updatedAt, createdBy, createdAt) {
|
|
173
|
+
this.generate = generate;
|
|
174
|
+
this.id = id;
|
|
175
|
+
this.payload = payload;
|
|
176
|
+
this.user = user;
|
|
177
|
+
this.updatedBy = updatedBy;
|
|
178
|
+
this.updatedAt = updatedAt;
|
|
179
|
+
this.createdBy = createdBy;
|
|
180
|
+
this.createdAt = createdAt;
|
|
181
|
+
this.build = this.build.bind(this);
|
|
182
|
+
}
|
|
183
|
+
Builder.prototype.build = function (res, obj, isCreate, isPatch) {
|
|
184
|
+
var o = obj;
|
|
185
|
+
var usr = '';
|
|
186
|
+
if (this.user.length > 0) {
|
|
187
|
+
if (this.payload.length > 0) {
|
|
188
|
+
var payload = res.locals[this.payload];
|
|
189
|
+
if (payload) {
|
|
190
|
+
usr = payload[this.user];
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
usr = res.locals[this.user];
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!usr) {
|
|
198
|
+
usr = '';
|
|
199
|
+
}
|
|
200
|
+
var now = new Date();
|
|
201
|
+
if (isCreate) {
|
|
202
|
+
if (this.generate && this.id.length > 0) {
|
|
203
|
+
o[this.id] = this.generate();
|
|
204
|
+
}
|
|
205
|
+
if (usr.length > 0) {
|
|
206
|
+
if (this.createdAt.length > 0) {
|
|
207
|
+
o[this.createdAt] = now;
|
|
208
|
+
}
|
|
209
|
+
if (this.createdBy.length > 0) {
|
|
210
|
+
o[this.createdBy] = usr;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
else if (isPatch) {
|
|
215
|
+
var keys = Object.keys(o);
|
|
216
|
+
if (keys.length === 0) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (usr.length > 0) {
|
|
221
|
+
if (this.updatedAt.length > 0) {
|
|
222
|
+
o[this.updatedAt] = now;
|
|
223
|
+
}
|
|
224
|
+
if (this.updatedBy.length > 0) {
|
|
225
|
+
o[this.updatedBy] = usr;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
return Builder;
|
|
230
|
+
}());
|
|
231
|
+
exports.Builder = Builder;
|
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -6,18 +6,21 @@ import {Attribute, Attributes, ErrorMessage} from './metadata';
|
|
|
6
6
|
import {resources} from './resources';
|
|
7
7
|
import {buildAndCheckId, buildId} from './view';
|
|
8
8
|
|
|
9
|
+
export type Build<T> = (res: Response, obj: T, isCreate?: boolean, isPatch?: boolean) => void;
|
|
10
|
+
export type Validate<T> = (obj: T, patch?: boolean) => Promise<ErrorMessage[]>;
|
|
11
|
+
export type Save<T> = (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>;
|
|
9
12
|
export interface GenericService<T, ID, R> {
|
|
10
13
|
metadata?(): Attributes|undefined;
|
|
11
14
|
load(id: ID, ctx?: any): Promise<T|null>;
|
|
12
15
|
insert(obj: T, ctx?: any): Promise<R>;
|
|
13
16
|
update(obj: T, ctx?: any): Promise<R>;
|
|
14
|
-
patch?(obj: T
|
|
17
|
+
patch?(obj: Partial<T>, ctx?: any): Promise<R>;
|
|
15
18
|
delete?(id: ID, ctx?: any): Promise<number>;
|
|
16
19
|
}
|
|
17
20
|
export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
18
21
|
status: StatusConfig;
|
|
19
22
|
metadata?: Attributes;
|
|
20
|
-
constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?:
|
|
23
|
+
constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?: Validate<T>, public build?: Build<T>) {
|
|
21
24
|
super(log, service);
|
|
22
25
|
this.status = initializeStatus(status);
|
|
23
26
|
if (service.metadata) {
|
|
@@ -40,18 +43,18 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
|
40
43
|
return this.insert(req, res);
|
|
41
44
|
}
|
|
42
45
|
insert(req: Request, res: Response): void {
|
|
43
|
-
validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate);
|
|
46
|
+
validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
|
|
44
47
|
}
|
|
45
48
|
update(req: Request, res: Response): void {
|
|
46
49
|
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update);
|
|
47
50
|
if (id) {
|
|
48
|
-
validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate);
|
|
51
|
+
validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
|
|
49
52
|
}
|
|
50
53
|
}
|
|
51
54
|
patch(req: Request, res: Response): void {
|
|
52
55
|
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch);
|
|
53
56
|
if (id && this.service.patch) {
|
|
54
|
-
validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate);
|
|
57
|
+
validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
60
|
delete(req: Request, res: Response): void {
|
|
@@ -67,7 +70,7 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
|
67
70
|
}
|
|
68
71
|
}
|
|
69
72
|
}
|
|
70
|
-
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save:
|
|
73
|
+
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>): void {
|
|
71
74
|
const obj = req.body;
|
|
72
75
|
if (!obj || obj === '') {
|
|
73
76
|
res.status(400).end('The request body cannot be empty.');
|
|
@@ -78,6 +81,9 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
|
|
|
78
81
|
const r: ResultInfo<T> = {status: status.validation_error, errors};
|
|
79
82
|
res.status(getStatusCode(errors)).json(r).end();
|
|
80
83
|
} else {
|
|
84
|
+
if (build) {
|
|
85
|
+
build(res, obj, true);
|
|
86
|
+
}
|
|
81
87
|
create(res, status, obj, save, log);
|
|
82
88
|
}
|
|
83
89
|
}).catch(err => handleError(err, res, log));
|
|
@@ -86,13 +92,16 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
|
|
|
86
92
|
}
|
|
87
93
|
}
|
|
88
94
|
}
|
|
89
|
-
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save:
|
|
95
|
+
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>): void {
|
|
90
96
|
if (validate) {
|
|
91
97
|
validate(obj, isPatch).then(errors => {
|
|
92
98
|
if (errors && errors.length > 0) {
|
|
93
99
|
const r: ResultInfo<T> = {status: status.validation_error, errors};
|
|
94
100
|
res.status(getStatusCode(errors)).json(r).end();
|
|
95
101
|
} else {
|
|
102
|
+
if (build) {
|
|
103
|
+
build(res, obj, false, isPatch);
|
|
104
|
+
}
|
|
96
105
|
update(res, status, obj, save, log);
|
|
97
106
|
}
|
|
98
107
|
}).catch(err => handleError(err, res, log));
|
|
@@ -134,3 +143,65 @@ export function getDeleteStatus(count: number): number {
|
|
|
134
143
|
export function getStatusCode(errs: ErrorMessage[]): number {
|
|
135
144
|
return (isTypeError(errs) ? 400 : 422);
|
|
136
145
|
}
|
|
146
|
+
export interface ModelConfig {
|
|
147
|
+
id?: string;
|
|
148
|
+
payload?: string;
|
|
149
|
+
user?: string;
|
|
150
|
+
updatedBy?: string;
|
|
151
|
+
updatedAt?: string;
|
|
152
|
+
createdBy?: string;
|
|
153
|
+
createdAt?: string;
|
|
154
|
+
}
|
|
155
|
+
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
|
+
return b.build;
|
|
158
|
+
}
|
|
159
|
+
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
|
+
this.build = this.build.bind(this);
|
|
162
|
+
}
|
|
163
|
+
build(res: Response, obj: T, isCreate?: boolean, isPatch?: boolean): void {
|
|
164
|
+
let o: any = obj;
|
|
165
|
+
let usr = '';
|
|
166
|
+
if (this.user.length > 0) {
|
|
167
|
+
if (this.payload.length > 0) {
|
|
168
|
+
const payload = res.locals[this.payload];
|
|
169
|
+
if (payload) {
|
|
170
|
+
usr = payload[this.user];
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
usr = res.locals[this.user];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (!usr) {
|
|
177
|
+
usr = '';
|
|
178
|
+
}
|
|
179
|
+
const now = new Date();
|
|
180
|
+
if (isCreate) {
|
|
181
|
+
if (this.generate && this.id.length > 0) {
|
|
182
|
+
o[this.id] = this.generate();
|
|
183
|
+
}
|
|
184
|
+
if (usr.length > 0) {
|
|
185
|
+
if (this.createdAt.length > 0) {
|
|
186
|
+
o[this.createdAt] = now;
|
|
187
|
+
}
|
|
188
|
+
if (this.createdBy.length > 0) {
|
|
189
|
+
o[this.createdBy] = usr;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} else if (isPatch) {
|
|
193
|
+
const keys = Object.keys(o);
|
|
194
|
+
if (keys.length === 0) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (usr.length > 0) {
|
|
199
|
+
if (this.updatedAt.length > 0) {
|
|
200
|
+
o[this.updatedAt] = now;
|
|
201
|
+
}
|
|
202
|
+
if (this.updatedBy.length > 0) {
|
|
203
|
+
o[this.updatedBy] = usr;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
package/src/LoadController.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface ViewService<T, ID> {
|
|
|
7
7
|
metadata?(): Attributes|undefined;
|
|
8
8
|
load(id: ID, ctx?: any): Promise<T|null>;
|
|
9
9
|
}
|
|
10
|
+
export type Load<T, ID> = ((id: ID, ctx?: any) => Promise<T|null>);
|
|
10
11
|
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T|null>)): (id: ID, ctx?: any) => Promise<T|null> {
|
|
11
12
|
if (typeof viewService === 'function') {
|
|
12
13
|
return viewService;
|
|
@@ -38,8 +39,8 @@ function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: an
|
|
|
38
39
|
}
|
|
39
40
|
export class LoadController<T, ID> {
|
|
40
41
|
protected keys?: Attribute[];
|
|
41
|
-
protected view:
|
|
42
|
-
constructor(protected log: Log, viewService: ViewService<T, ID> |
|
|
42
|
+
protected view: Load<T, ID>;
|
|
43
|
+
constructor(protected log: Log, viewService: ViewService<T, ID> | Load<T, ID>, keys?: Attributes|Attribute[]|string[]) {
|
|
43
44
|
this.load = this.load.bind(this);
|
|
44
45
|
this.view = getViewFunc(viewService);
|
|
45
46
|
this.keys = getKeysFunc(viewService, keys);
|