express-ext 0.2.0 → 0.2.2

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.
@@ -20,12 +20,12 @@ 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, build, validate) {
23
+ function GenericController(log, service, build, validate, returnNumber) {
24
24
  var _this = _super.call(this, log, service) || this;
25
25
  _this.service = service;
26
26
  _this.build = build;
27
27
  _this.validate = validate;
28
- _this.status = edit_1.initializeStatus(status);
28
+ _this.returnNumber = returnNumber;
29
29
  if (service.metadata) {
30
30
  var m = service.metadata();
31
31
  if (m) {
@@ -47,18 +47,18 @@ var GenericController = (function (_super) {
47
47
  return this.insert(req, res);
48
48
  };
49
49
  GenericController.prototype.insert = function (req, res) {
50
- validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
50
+ validateAndCreate(req, res, this.service.insert, this.log, this.validate, this.build);
51
51
  };
52
52
  GenericController.prototype.update = function (req, res) {
53
53
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.update);
54
54
  if (id) {
55
- validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
55
+ validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, this.build);
56
56
  }
57
57
  };
58
58
  GenericController.prototype.patch = function (req, res) {
59
59
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.patch);
60
60
  if (id && this.service.patch) {
61
- validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
61
+ validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, this.build);
62
62
  }
63
63
  };
64
64
  GenericController.prototype.delete = function (req, res) {
@@ -78,7 +78,7 @@ var GenericController = (function (_super) {
78
78
  return GenericController;
79
79
  }(LoadController_1.LoadController));
80
80
  exports.GenericController = GenericController;
81
- function validateAndCreate(req, res, status, save, log, validate, build) {
81
+ function validateAndCreate(req, res, save, log, validate, build, returnNumber) {
82
82
  var obj = req.body;
83
83
  if (!obj || obj === '') {
84
84
  res.status(400).end('The request body cannot be empty.');
@@ -87,40 +87,38 @@ function validateAndCreate(req, res, status, save, log, validate, build) {
87
87
  if (validate) {
88
88
  validate(obj).then(function (errors) {
89
89
  if (errors && errors.length > 0) {
90
- var r = { status: status.validation_error, errors: errors };
91
- res.status(getStatusCode(errors)).json(r).end();
90
+ res.status(getStatusCode(errors)).json(errors).end();
92
91
  }
93
92
  else {
94
93
  if (build) {
95
94
  build(res, obj, true);
96
95
  }
97
- edit_1.create(res, status, obj, save, log);
96
+ edit_1.create(res, obj, save, log, returnNumber);
98
97
  }
99
98
  }).catch(function (err) { return http_1.handleError(err, res, log); });
100
99
  }
101
100
  else {
102
- edit_1.create(res, status, obj, save, log);
101
+ edit_1.create(res, obj, save, log, returnNumber);
103
102
  }
104
103
  }
105
104
  }
106
105
  exports.validateAndCreate = validateAndCreate;
107
- function validateAndUpdate(res, status, obj, isPatch, save, log, validate, build) {
106
+ function validateAndUpdate(res, obj, isPatch, save, log, validate, build, returnNumber) {
108
107
  if (validate) {
109
108
  validate(obj, isPatch).then(function (errors) {
110
109
  if (errors && errors.length > 0) {
111
- var r = { status: status.validation_error, errors: errors };
112
- res.status(getStatusCode(errors)).json(r).end();
110
+ res.status(getStatusCode(errors)).json(errors).end();
113
111
  }
114
112
  else {
115
113
  if (build) {
116
114
  build(res, obj, false, isPatch);
117
115
  }
118
- edit_1.update(res, status, obj, save, log);
116
+ edit_1.update(res, obj, save, log, returnNumber);
119
117
  }
120
118
  }).catch(function (err) { return http_1.handleError(err, res, log); });
121
119
  }
122
120
  else {
123
- edit_1.update(res, status, obj, save, log);
121
+ edit_1.update(res, obj, save, log, returnNumber);
124
122
  }
125
123
  }
126
124
  exports.validateAndUpdate = validateAndUpdate;
@@ -20,7 +20,7 @@ var search_func_1 = require("./search_func");
20
20
  var GenericSearchController = (function (_super) {
21
21
  __extends(GenericSearchController, _super);
22
22
  function GenericSearchController(log, find, service, config, build, validate, dates, numbers) {
23
- var _this = _super.call(this, log, service, config, build, validate) || this;
23
+ var _this = _super.call(this, log, service, build, validate) || this;
24
24
  _this.find = find;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -20,7 +20,7 @@ var search_func_1 = require("./search_func");
20
20
  var LowcodeController = (function (_super) {
21
21
  __extends(LowcodeController, _super);
22
22
  function LowcodeController(log, lowCodeService, config, build, validate, dates, numbers) {
23
- var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
23
+ var _this = _super.call(this, log, lowCodeService, build, validate) || this;
24
24
  _this.lowCodeService = lowCodeService;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -55,7 +55,7 @@ exports.LowcodeHandler = LowcodeController;
55
55
  var Controller = (function (_super) {
56
56
  __extends(Controller, _super);
57
57
  function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
58
- var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
58
+ var _this = _super.call(this, log, lowCodeService, build, validate) || this;
59
59
  _this.lowCodeService = lowCodeService;
60
60
  _this.search = _this.search.bind(_this);
61
61
  _this.config = search_1.initializeConfig(config);
package/lib/edit.js CHANGED
@@ -1,21 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var http_1 = require("./http");
4
- function initializeStatus(s) {
5
- if (s) {
6
- return s;
7
- }
8
- var s1 = {
9
- duplicate_key: 0,
10
- not_found: 0,
11
- success: 1,
12
- version_error: 2,
13
- validation_error: 4,
14
- error: 4
15
- };
16
- return s1;
17
- }
18
- exports.initializeStatus = initializeStatus;
19
4
  function checkId(obj, id, keys) {
20
5
  var n = (keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id');
21
6
  var o = obj;
@@ -48,43 +33,41 @@ function checkId(obj, id, keys) {
48
33
  return true;
49
34
  }
50
35
  exports.checkId = checkId;
51
- function create(res, status, obj, insert, log) {
36
+ function create(res, obj, insert, log, returnNumber) {
52
37
  insert(obj).then(function (result) {
53
38
  if (typeof result === 'number') {
54
39
  if (result >= 1) {
55
- var r = { status: status.success, value: obj };
56
- res.status(201).json(r).end();
57
- }
58
- else if (result === 0) {
59
- var r = { status: status.duplicate_key };
60
- res.status(201).json(r).end();
40
+ res.status(201).json(returnNumber ? result : obj).end();
61
41
  }
62
42
  else {
63
- res.status(500).end('Internal Server Error');
43
+ res.status(409).json(result).end();
64
44
  }
65
45
  }
46
+ else if (Array.isArray(result)) {
47
+ res.status(422).json(result).end();
48
+ }
66
49
  else {
67
- res.status(200).json(result).end();
50
+ res.status(201).json(result).end();
68
51
  }
69
52
  }).catch(function (err) { return http_1.handleError(err, res, log); });
70
53
  }
71
54
  exports.create = create;
72
- function update(res, status, obj, save, log) {
55
+ function update(res, obj, save, log, returnNumber) {
73
56
  save(obj).then(function (result) {
74
57
  if (typeof result === 'number') {
75
58
  if (result >= 1) {
76
- var r = { status: status.success, value: obj };
77
- res.status(201).json(r).end();
59
+ res.status(200).json(returnNumber ? result : obj).end();
78
60
  }
79
61
  else if (result === 0) {
80
- var r = { status: status.not_found };
81
- res.status(404).json(r).end();
62
+ res.status(404).json(result).end();
82
63
  }
83
64
  else {
84
- var r = { status: status.not_found };
85
- res.status(409).json(r).end();
65
+ res.status(409).json(result).end();
86
66
  }
87
67
  }
68
+ else if (Array.isArray(result)) {
69
+ res.status(422).json(result).end();
70
+ }
88
71
  else {
89
72
  res.status(200).json(result).end();
90
73
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,5 +1,5 @@
1
1
  import {Request, Response} from 'express';
2
- import {checkId, create, initializeStatus, isTypeError, ResultInfo, StatusConfig, update} from './edit';
2
+ import {checkId, create, isTypeError, update} from './edit';
3
3
  import {handleError, Log} from './http';
4
4
  import {LoadController} from './LoadController';
5
5
  import {Attribute, Attributes, ErrorMessage} from './metadata';
@@ -8,7 +8,7 @@ import {buildAndCheckId, buildId} from './view';
8
8
 
9
9
  export type Build<T> = (res: Response, obj: T, isCreate?: boolean, isPatch?: boolean) => void;
10
10
  export type Validate<T> = (obj: T, patch?: boolean) => Promise<ErrorMessage[]>;
11
- export type Save<T> = (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>;
11
+ export type Save<T> = (obj: T, ctx?: any) => Promise<number|T|ErrorMessage[]>;
12
12
  export interface GenericService<T, ID, R> {
13
13
  metadata?(): Attributes|undefined;
14
14
  load(id: ID, ctx?: any): Promise<T|null>;
@@ -18,11 +18,11 @@ export interface GenericService<T, ID, R> {
18
18
  delete?(id: ID, ctx?: any): Promise<number>;
19
19
  }
20
20
  export class GenericController<T, ID> extends LoadController<T, ID> {
21
- status: StatusConfig;
22
21
  metadata?: Attributes;
23
- constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public build?: Build<T>, public validate?: Validate<T>) {
22
+ returnNumber?: boolean;
23
+ constructor(log: Log, public service: GenericService<T, ID, number|T|ErrorMessage[]>, public build?: Build<T>, public validate?: Validate<T>, returnNumber?: boolean) {
24
24
  super(log, service);
25
- this.status = initializeStatus(status);
25
+ this.returnNumber = returnNumber;
26
26
  if (service.metadata) {
27
27
  const m = service.metadata();
28
28
  if (m) {
@@ -43,18 +43,18 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
43
43
  return this.insert(req, res);
44
44
  }
45
45
  insert(req: Request, res: Response): void {
46
- validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
46
+ validateAndCreate(req, res, this.service.insert, this.log, this.validate, this.build);
47
47
  }
48
48
  update(req: Request, res: Response): void {
49
49
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update);
50
50
  if (id) {
51
- validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
51
+ validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, this.build);
52
52
  }
53
53
  }
54
54
  patch(req: Request, res: Response): void {
55
55
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch);
56
56
  if (id && this.service.patch) {
57
- validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
57
+ validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, this.build);
58
58
  }
59
59
  }
60
60
  delete(req: Request, res: Response): void {
@@ -70,7 +70,7 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
70
70
  }
71
71
  }
72
72
  }
73
- export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>): void {
73
+ export function validateAndCreate<T>(req: Request, res: Response, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>, returnNumber?: boolean): void {
74
74
  const obj = req.body;
75
75
  if (!obj || obj === '') {
76
76
  res.status(400).end('The request body cannot be empty.');
@@ -78,35 +78,33 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
78
78
  if (validate) {
79
79
  validate(obj).then(errors => {
80
80
  if (errors && errors.length > 0) {
81
- const r: ResultInfo<T> = {status: status.validation_error, errors};
82
- res.status(getStatusCode(errors)).json(r).end();
81
+ res.status(getStatusCode(errors)).json(errors).end();
83
82
  } else {
84
83
  if (build) {
85
84
  build(res, obj, true);
86
85
  }
87
- create(res, status, obj, save, log);
86
+ create(res, obj, save, log, returnNumber);
88
87
  }
89
88
  }).catch(err => handleError(err, res, log));
90
89
  } else {
91
- create(res, status, obj, save, log);
90
+ create(res, obj, save, log, returnNumber);
92
91
  }
93
92
  }
94
93
  }
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 {
94
+ export function validateAndUpdate<T>(res: Response, obj: T, isPatch: boolean, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>, returnNumber?: boolean): void {
96
95
  if (validate) {
97
96
  validate(obj, isPatch).then(errors => {
98
97
  if (errors && errors.length > 0) {
99
- const r: ResultInfo<T> = {status: status.validation_error, errors};
100
- res.status(getStatusCode(errors)).json(r).end();
98
+ res.status(getStatusCode(errors)).json(errors).end();
101
99
  } else {
102
100
  if (build) {
103
101
  build(res, obj, false, isPatch);
104
102
  }
105
- update(res, status, obj, save, log);
103
+ update(res, obj, save, log, returnNumber);
106
104
  }
107
105
  }).catch(err => handleError(err, res, log));
108
106
  } else {
109
- update(res, status, obj, save, log);
107
+ update(res, obj, save, log, returnNumber);
110
108
  }
111
109
  }
112
110
  export function buildAndCheckIdWithBody<T, ID, R>(req: Request, res: Response, keys?: Attribute[], patch?: (obj: T, ctx?: any) => Promise<R>): ID | undefined {
@@ -1,13 +1,10 @@
1
1
  import {Request, Response} from 'express';
2
- import {ResultInfo, StatusConfig} from './edit';
3
2
  import {Build, GenericController, GenericService} from './GenericController';
4
3
  import {handleError, Log} from './http';
5
4
  import {ErrorMessage} from './metadata';
6
5
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
7
6
  import {getMetadataFunc} from './search_func';
8
7
 
9
- export interface Config extends StatusConfig, SearchConfig {
10
- }
11
8
  export class GenericSearchController<T, ID, S extends Filter> extends GenericController<T, ID> {
12
9
  config?: SearchConfig;
13
10
  csv?: boolean;
@@ -16,8 +13,8 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
16
13
  fields?: string;
17
14
  excluding?: string;
18
15
  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, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
- super(log, service, config, build, validate);
16
+ constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ErrorMessage[]>, config?: SearchConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
17
+ super(log, service, build, validate);
21
18
  this.search = this.search.bind(this);
22
19
  this.config = initializeConfig(config);
23
20
  if (this.config) {
@@ -1,13 +1,10 @@
1
1
  import {Request, Response} from 'express';
2
- import {ResultInfo, StatusConfig} from './edit';
3
2
  import {Build, GenericController, GenericService} from './GenericController';
4
3
  import {handleError, Log} from './http';
5
4
  import {ErrorMessage} from './metadata';
6
5
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
7
6
  import {getMetadataFunc} from './search_func';
8
7
 
9
- export interface LowCodeConfig extends StatusConfig, SearchConfig {
10
- }
11
8
  export interface Service<T, ID, R, S extends Filter> extends GenericService<T, ID, R> {
12
9
  search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
13
10
  }
@@ -19,8 +16,8 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
19
16
  fields?: string;
20
17
  excluding?: string;
21
18
  array?: string[];
22
- constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
23
- super(log, lowCodeService, config, build, validate);
19
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ErrorMessage[], S>, config?: SearchConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
+ super(log, lowCodeService, build, validate);
24
21
  this.search = this.search.bind(this);
25
22
  this.config = initializeConfig(config);
26
23
  if (this.config) {
@@ -55,8 +52,8 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
55
52
  fields?: string;
56
53
  excluding?: string;
57
54
  array?: string[];
58
- constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: LowCodeConfig, dates?: string[], numbers?: string[]) {
59
- super(log, lowCodeService, config, build, validate);
55
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|T|ErrorMessage[], S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: SearchConfig, dates?: string[], numbers?: string[]) {
56
+ super(log, lowCodeService, build, validate);
60
57
  this.search = this.search.bind(this);
61
58
  this.config = initializeConfig(config);
62
59
  if (this.config) {
package/src/edit.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {Response} from 'express';
2
2
  import {handleError} from './http';
3
3
  import {Attribute, ErrorMessage} from './metadata';
4
-
4
+ /*
5
5
  export interface StatusConfig {
6
6
  duplicate_key: number|string;
7
7
  not_found: number|string;
@@ -30,6 +30,7 @@ export function initializeStatus(s?: StatusConfig): StatusConfig {
30
30
  };
31
31
  return s1;
32
32
  }
33
+ */
33
34
  export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
34
35
  const n: string = (keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id');
35
36
  const o: any = obj;
@@ -61,36 +62,33 @@ export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
61
62
  }
62
63
  return true;
63
64
  }
64
- export function create<T>(res: Response, status: StatusConfig, obj: T, insert: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string, ctx?: any) => void): void {
65
+ export function create<T>(res: Response, obj: T, insert: (obj: T, ctx?: any) => Promise<number|T|ErrorMessage[]>, log: (msg: string, ctx?: any) => void, returnNumber?: boolean): void {
65
66
  insert(obj).then(result => {
66
67
  if (typeof result === 'number') {
67
68
  if (result >= 1) {
68
- const r: ResultInfo<T> = {status: status.success, value: obj};
69
- res.status(201).json(r).end();
70
- } else if (result === 0) {
71
- const r: ResultInfo<T> = {status: status.duplicate_key};
72
- res.status(201).json(r).end();
69
+ res.status(201).json(returnNumber ? result : obj).end();
73
70
  } else {
74
- res.status(500).end('Internal Server Error');
71
+ res.status(409).json(result).end();
75
72
  }
73
+ } else if (Array.isArray(result)) {
74
+ res.status(422).json(result).end();
76
75
  } else {
77
- res.status(200).json(result).end();
76
+ res.status(201).json(result).end();
78
77
  }
79
78
  }).catch(err => handleError(err, res, log));
80
79
  }
81
- export function update<T>(res: Response, status: StatusConfig, obj: T, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string, ctx?: any) => void): void {
80
+ export function update<T>(res: Response, obj: T, save: (obj: T, ctx?: any) => Promise<number|T|ErrorMessage[]>, log: (msg: string, ctx?: any) => void, returnNumber?: boolean): void {
82
81
  save(obj).then(result => {
83
82
  if (typeof result === 'number') {
84
83
  if (result >= 1) {
85
- const r: ResultInfo<T> = {status: status.success, value: obj};
86
- res.status(201).json(r).end();
84
+ res.status(200).json(returnNumber ? result : obj).end();
87
85
  } else if (result === 0) {
88
- const r: ResultInfo<T> = {status: status.not_found};
89
- res.status(404).json(r).end();
86
+ res.status(404).json(result).end();
90
87
  } else {
91
- const r: ResultInfo<T> = {status: status.not_found};
92
- res.status(409).json(r).end();
88
+ res.status(409).json(result).end();
93
89
  }
90
+ } else if (Array.isArray(result)) {
91
+ res.status(422).json(result).end();
94
92
  } else {
95
93
  res.status(200).json(result).end();
96
94
  }