express-ext 0.1.23 → 0.1.26

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,11 +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, build) {
23
+ function GenericController(log, service, status, build, validate) {
24
24
  var _this = _super.call(this, log, service) || this;
25
25
  _this.service = service;
26
- _this.validate = validate;
27
26
  _this.build = build;
27
+ _this.validate = validate;
28
28
  _this.status = edit_1.initializeStatus(status);
29
29
  if (service.metadata) {
30
30
  var m = service.metadata();
@@ -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, build, validate, dates, numbers) {
23
+ var _this = _super.call(this, log, service, config, build, validate) || this;
24
24
  _this.find = find;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -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;
@@ -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, build, validate, dates, numbers) {
23
+ var _this = _super.call(this, log, lowCodeService, config, build, validate) || 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.23",
3
+ "version": "0.1.26",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -20,7 +20,7 @@ export interface GenericService<T, ID, R> {
20
20
  export class GenericController<T, ID> extends LoadController<T, ID> {
21
21
  status: StatusConfig;
22
22
  metadata?: Attributes;
23
- constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?: Validate<T>, public build?: Build<T>) {
23
+ constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public build?: Build<T>, public validate?: Validate<T>) {
24
24
  super(log, service);
25
25
  this.status = initializeStatus(status);
26
26
  if (service.metadata) {
@@ -157,12 +157,13 @@ export function useBuild<T>(c: ModelConfig, generate?: (() => string)): Build<T>
157
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 : '');
158
158
  return b.build;
159
159
  }
160
+ // tslint:disable-next-line:max-classes-per-file
160
161
  export class Builder<T> {
161
162
  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) {
162
163
  this.build = this.build.bind(this);
163
164
  }
164
165
  build(res: Response, obj: T, isCreate?: boolean, isPatch?: boolean): void {
165
- let o: any = obj;
166
+ const o: any = obj;
166
167
  let usr = '';
167
168
  if (this.user.length > 0) {
168
169
  if (this.payload.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, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
+ super(log, service, config, build, validate);
21
21
  this.search = this.search.bind(this);
22
22
  this.config = initializeConfig(config);
23
23
  if (this.config) {
@@ -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
+ }
@@ -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, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
23
+ super(log, lowCodeService, config, build, validate);
24
24
  this.search = this.search.bind(this);
25
25
  this.config = initializeConfig(config);
26
26
  if (this.config) {