@digione/node-custom-api 0.1.9 → 0.2.0-alpha10

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.
@@ -1,9 +1,9 @@
1
1
  import { Request, Response } from 'express';
2
2
  export declare const getAuthParam: (req: Request, res: Response, next: any) => void;
3
3
  export declare const getStremParam: (req: Request, res: Response, next: any) => void;
4
- export declare const getQueryParam: (req: Request, res: Response, next: any) => void;
4
+ export declare const getQueryParam: (req: Request, res: Response, next: any) => Promise<void | Response<any, Record<string, any>>>;
5
5
  export declare const getQueryParamCUD: (req: Request, res: Response, next: any) => void;
6
- export declare const getQueryParamFile: (req: Request, res: Response, next: any) => void;
6
+ export declare const getQueryParamFile: (req: Request, res: Response, next: any) => Promise<void | Response<any, Record<string, any>>>;
7
7
  export declare const changeQueryParam: (value?: {}, { skipSystem }?: {
8
8
  skipSystem?: boolean;
9
9
  }) => (req: Request, res: Response, next: any) => any;
@@ -14,3 +14,6 @@ export declare const changeBodyParam: (value?: {}, { skipSystem, omit }?: {
14
14
  skipSystem?: boolean;
15
15
  omit?: any[];
16
16
  }) => (req: Request, res: Response, next: any) => any;
17
+ export declare const changeConfigParam: (value?: {}, { skipSystem }?: {
18
+ skipSystem?: boolean;
19
+ }) => (req: Request, res: Response, next: any) => any;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.changeBodyParam = exports.changeOptionParam = exports.changeQueryParam = exports.getQueryParamFile = exports.getQueryParamCUD = exports.getQueryParam = exports.getStremParam = exports.getAuthParam = void 0;
3
+ exports.changeConfigParam = exports.changeBodyParam = exports.changeOptionParam = exports.changeQueryParam = exports.getQueryParamFile = exports.getQueryParamCUD = exports.getQueryParam = exports.getStremParam = exports.getAuthParam = void 0;
4
+ const tslib_1 = require("tslib");
4
5
  const helper_1 = require("../utils/helper");
6
+ const errors_1 = require("../errors");
5
7
  const _ = require("lodash");
6
8
  const getAuthParam = (req, res, next) => {
7
9
  res.locals.option['hostname'] = ((0, helper_1.getENV)('NODE_ENV') == 'development' ? 'http' : 'https') + '://' + req.get('host');
@@ -77,7 +79,8 @@ const getStremParam = (req, res, next) => {
77
79
  });
78
80
  };
79
81
  exports.getStremParam = getStremParam;
80
- const getQueryParam = (req, res, next) => {
82
+ const getQueryParam = (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
83
+ let config = res.locals.config || {};
81
84
  res.locals.option = res.locals.option || {};
82
85
  res.locals.option['lang_code'] = 'en';
83
86
  res.locals.option['currency'] = 'thb';
@@ -93,8 +96,20 @@ const getQueryParam = (req, res, next) => {
93
96
  else {
94
97
  res.locals.include = {};
95
98
  }
99
+ if (config['maxQueryLimit']) {
100
+ req.query.limit = req.query.limit || config['maxQueryLimit'];
101
+ }
96
102
  if (req.query.limit) {
97
103
  res.locals.option['limit'] = Number(req.query.limit);
104
+ if (config['maxQueryLimit'] && res.locals.option['limit'] > config['maxQueryLimit']) {
105
+ try {
106
+ yield (0, errors_1.paramError)(`"limit" must be less than or equal to ${config['maxQueryLimit']}`, { field: "limit" });
107
+ }
108
+ catch (err) {
109
+ let error = new errors_1.CustomError(err);
110
+ return res.status(error.code).send(error);
111
+ }
112
+ }
98
113
  }
99
114
  if (req.query.offset) {
100
115
  res.locals.option['offset'] = Number(req.query.offset);
@@ -124,9 +139,10 @@ const getQueryParam = (req, res, next) => {
124
139
  }
125
140
  return next();
126
141
  });
127
- };
142
+ });
128
143
  exports.getQueryParam = getQueryParam;
129
144
  const getQueryParamCUD = (req, res, next) => {
145
+ let config = res.locals.config || {};
130
146
  res.locals.option = res.locals.option || {};
131
147
  if (req.query.include) {
132
148
  try {
@@ -144,7 +160,7 @@ const getQueryParamCUD = (req, res, next) => {
144
160
  res.locals.option['records'] = String(req.query.records).split(',');
145
161
  }
146
162
  if (req.query.ids) {
147
- res.locals.option['ids'] = String(req.query.ids).split(',').slice(0, 25);
163
+ res.locals.option['ids'] = String(req.query.ids).split(',').slice(0, (config['maxQueryIds'] || 25));
148
164
  }
149
165
  return (0, exports.getAuthParam)(req, res, () => {
150
166
  if (req.query.lang_code) {
@@ -218,3 +234,13 @@ const changeBodyParam = (value = {}, { skipSystem = false, omit = [] } = {}) =>
218
234
  return next();
219
235
  };
220
236
  exports.changeBodyParam = changeBodyParam;
237
+ const changeConfigParam = (value = {}, { skipSystem = false } = {}) => (req, res, next) => {
238
+ if (skipSystem && req.params.web_system_token) {
239
+ let bol = (typeof req.params.web != "undefined") || (req.params.web_system == req.params.web);
240
+ if (bol)
241
+ return next();
242
+ }
243
+ res.locals.config = Object.assign(Object.assign({}, res.locals.config || {}), value);
244
+ return next();
245
+ };
246
+ exports.changeConfigParam = changeConfigParam;
@@ -90,6 +90,7 @@ export declare const FileAttr: {
90
90
  upload: sequelize.TinyIntegerDataType;
91
91
  custom: sequelize.AbstractDataTypeConstructor;
92
92
  tags: sequelize.AbstractDataTypeConstructor;
93
+ group: sequelize.StringDataType;
93
94
  file_link: sequelize.EnumDataType<"no" | "yes">;
94
95
  file_id: sequelize.StringDataType;
95
96
  uid: sequelize.StringDataType;
@@ -95,6 +95,7 @@ exports.FileAttr = {
95
95
  upload: sequelize.TINYINT({ length: 1 }),
96
96
  custom: sequelize.JSON,
97
97
  tags: sequelize.JSON,
98
+ group: sequelize.STRING({ length: 20 }),
98
99
  file_link: sequelize.ENUM('no', 'yes'),
99
100
  file_id: sequelize.STRING({ length: 15 }),
100
101
  uid: sequelize.STRING({ length: 100 })
@@ -50,9 +50,6 @@ export declare const UserAttr: {
50
50
  organ_id: {
51
51
  type: sequelize.IntegerDataType;
52
52
  };
53
- member_id: {
54
- type: sequelize.StringDataType;
55
- };
56
53
  provider: sequelize.StringDataType;
57
54
  mobile: sequelize.StringDataType;
58
55
  mobile_code: sequelize.StringDataType;
@@ -58,10 +58,6 @@ exports.UserAttr = {
58
58
  organ_id: {
59
59
  type: sequelize.INTEGER({ length: 11 })
60
60
  },
61
- // points:sequelize.INTEGER({length:11}),
62
- member_id: {
63
- type: sequelize.STRING(100)
64
- },
65
61
  provider: sequelize.STRING(40),
66
62
  mobile: sequelize.STRING(20),
67
63
  mobile_code: sequelize.STRING(10)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digione/node-custom-api",
3
- "version": "0.1.9",
3
+ "version": "0.2.0-alpha10",
4
4
  "description": "Typescript node digione-api",
5
5
  "author": "Monchai Jirayupong <monchai.j@seven.co.th>",
6
6
  "license": "MIT",
package/utils/auth.d.ts CHANGED
@@ -3,8 +3,10 @@ export declare const getTokenFromReq: (req: any) => any;
3
3
  export declare class AuthUtil {
4
4
  ref: string;
5
5
  constructor(ref: string);
6
- create(id: any, { include }?: {
6
+ create(id: any, { include, include_token, setting }?: {
7
7
  include?: {};
8
+ include_token?: {};
9
+ setting?: {};
8
10
  }): Promise<{
9
11
  refresh_token: string;
10
12
  access_token: string;
package/utils/auth.js CHANGED
@@ -11,7 +11,7 @@ const authError_1 = require("../errors/authError");
11
11
  const jwt = require("jsonwebtoken");
12
12
  const signToken = (id, expIn, opt = {}) => {
13
13
  const date = (0, helper_1.getDateTime)();
14
- return jwt.sign(Object.assign({ id: id, createAt: date, expAt: date.add(expIn, 'days') }, opt), (0, helper_1.getENV)('JWT_SECRET'));
14
+ return jwt.sign(Object.assign({ id: id, createAt: date, expAt: date.clone().add(expIn, 'days') }, opt), (0, helper_1.getENV)('JWT_SECRET'));
15
15
  };
16
16
  const getIdFromReq = (req) => {
17
17
  try {
@@ -30,14 +30,19 @@ class AuthUtil {
30
30
  constructor(ref) {
31
31
  this.ref = ref;
32
32
  }
33
- create(id, { include = {} } = {}) {
33
+ create(id, { include = {}, include_token = {}, setting = {} } = {}) {
34
34
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
35
35
  let refreshTokenUUID = (0, helper_1.getUUID)();
36
36
  let include_access = {};
37
37
  if (Object.keys(include).length) {
38
38
  include_access['setting'] = JSON.stringify(include);
39
39
  }
40
- let input = Object.assign({ id: refreshTokenUUID, user_id: id, expired_at: (0, helper_1.getDateTime)().add((0, helper_1.getENV)('REFRESH_TOKEN_AGE'), 'days'), token: signToken(id, (0, helper_1.getENV)('REFRESH_TOKEN_AGE'), { refresh_token_id: refreshTokenUUID }), accessToken: Object.assign({ user_id: id, expired_at: (0, helper_1.getDateTime)().add((0, helper_1.getENV)('ACCESS_TOKEN_AGE'), 'days'), token: signToken(id, (0, helper_1.getENV)('ACCESS_TOKEN_AGE'), { refresh_token_id: refreshTokenUUID }) }, include_access) }, include);
40
+ let rf_expire_in = Number(setting['expire_in'] || (0, helper_1.getENV)('REFRESH_TOKEN_AGE', 1));
41
+ let ac_expire_in = Number((0, helper_1.getENV)('ACCESS_TOKEN_AGE', 1));
42
+ if (rf_expire_in < ac_expire_in) {
43
+ ac_expire_in = rf_expire_in;
44
+ }
45
+ let input = Object.assign({ id: refreshTokenUUID, user_id: id, expired_at: (0, helper_1.getDateTime)().add(rf_expire_in, 'days'), token: signToken(id, rf_expire_in, Object.assign({ refresh_token_id: refreshTokenUUID }, include_token)), accessToken: Object.assign({ user_id: id, expired_at: (0, helper_1.getDateTime)().add(ac_expire_in, 'days'), token: signToken(id, ac_expire_in, Object.assign({ refresh_token_id: refreshTokenUUID }, include_token)) }, include_access) }, include);
41
46
  yield refresh_token_1.RefreshTokenModel.schema(this.ref, "_").create(input, {
42
47
  include: [
43
48
  {
package/utils/db.js CHANGED
@@ -32,12 +32,12 @@ const getWhereVal = (smth) => {
32
32
  return [key, value].join(` ${smth.comparator} `);
33
33
  };
34
34
  const genWhere = (where = {}, { as = "", delimiter = ".", prefix = true, bracket = false } = {}) => {
35
- as = as ? as + delimiter : as;
35
+ let keyPrefix = as ? "`" + as + "`" + delimiter : as;
36
36
  let symbolKeys = Object.getOwnPropertySymbols(where);
37
37
  let rawWhere = (Object.keys(where).concat(symbolKeys)).reduce((result, key) => {
38
38
  let value = where[key];
39
39
  if (typeof value !== 'undefined') {
40
- key = typeof (value) == "string" ? `${as}${key}` : key;
40
+ key = typeof (key) == "string" ? keyPrefix + "`" + (key.split('.').join("`.`")) + "`" : key;
41
41
  if (typeof (value) == "string") {
42
42
  value = SqlString.escape(value);
43
43
  }
@@ -48,7 +48,7 @@ const genWhere = (where = {}, { as = "", delimiter = ".", prefix = true, bracket
48
48
  result = `${result}( ${op}) AND `;
49
49
  }
50
50
  else {
51
- result = `${result + key} IN (${value.map(i => "'" + i + "'").join(',')}) AND `;
51
+ result = result + key + ` IN (${value.map(i => "'" + i + "'").join(',')}) AND `;
52
52
  }
53
53
  return result;
54
54
  }
@@ -62,7 +62,7 @@ const genWhere = (where = {}, { as = "", delimiter = ".", prefix = true, bracket
62
62
  return result;
63
63
  }
64
64
  else if (typeof value == "object" && value['val']) {
65
- result = `${result}${key}=${value['val']} AND `;
65
+ result = result + key + `=${value['val']} AND `;
66
66
  return result;
67
67
  }
68
68
  result = result + key + "=" + value + " AND ";
package/utils/file.d.ts CHANGED
@@ -62,16 +62,23 @@ export declare class FileUtil {
62
62
  width?: number;
63
63
  height?: number;
64
64
  }): Promise<any>;
65
- beforeFindFile(where?: {}, { tags, group }?: {
65
+ beforeFindFile(where?: {}, { attributes, tags, group, verify }?: {
66
+ attributes?: any;
66
67
  tags?: TagFile;
67
68
  group?: any;
68
- }): {};
69
- findAllFile({ where, folder, full, file_link, thumb, width, height, parent_folder, attributes, limit, offset, tag, tags, group }?: {
69
+ verify?: boolean;
70
+ }): {
71
+ where: {};
72
+ attributes: any;
73
+ order: any[];
74
+ };
75
+ findAllFile({ where, folder, full, file_link, thumb, verify, width, height, parent_folder, attributes, limit, offset, tag, tags, group }?: {
70
76
  where?: {};
71
77
  folder?: boolean;
72
78
  full?: boolean;
73
79
  file_link?: boolean;
74
80
  thumb?: boolean;
81
+ verify?: boolean;
75
82
  width?: number;
76
83
  height?: number;
77
84
  parent_folder?: string;
@@ -82,6 +89,12 @@ export declare class FileUtil {
82
89
  tags?: TagFile;
83
90
  group?: any;
84
91
  }): Promise<any[]>;
92
+ countFileByFolder(slug: string, { where, parent_folder, tags, group }?: {
93
+ where?: {};
94
+ parent_folder?: string;
95
+ tags?: any[];
96
+ group?: any;
97
+ }): Promise<number>;
85
98
  findAndCountAllFileByFolder(slug: string, { where, file_link, parent_folder, limit, offset, tag, tags, group }?: {
86
99
  where?: {};
87
100
  file_link?: boolean;
@@ -124,11 +137,12 @@ export declare class FileUtil {
124
137
  tags?: TagFile;
125
138
  group?: any;
126
139
  }): Promise<any>;
127
- uploadByReq(req: Request, slug: string, user_id: any, { parent_id, parent_slug, name, root, thumb, width, height, input, is_hidden, tags, group }?: {
140
+ uploadByReq(req: Request, slug: string, user_id: any, { parent_id, parent_slug, name, root, format, thumb, width, height, input, is_hidden, tags, group }?: {
128
141
  parent_id?: number;
129
142
  parent_slug?: string;
130
143
  name?: any;
131
144
  root?: boolean;
145
+ format?: boolean;
132
146
  thumb?: boolean;
133
147
  width?: number;
134
148
  height?: number;
@@ -146,7 +160,7 @@ export declare class FileUtil {
146
160
  hidden?: number;
147
161
  input?: {};
148
162
  }): Promise<any>;
149
- createFileByPath(files: Array<string>, slug: string, user_id: any, { parent_slug, type, name, root, upload, tags, group }?: {
163
+ createFileByPath(files: Array<any>, slug: string, user_id: any, { parent_slug, type, name, root, upload, tags, group }?: {
150
164
  parent_slug?: string;
151
165
  type?: string;
152
166
  name?: any;
@@ -156,7 +170,8 @@ export declare class FileUtil {
156
170
  group?: any;
157
171
  }): Promise<any>;
158
172
  allowedFileType(extension: any): Promise<any>;
159
- formatFile(folder_id: any, user_id: any, reqs: Array<any>, files: Array<any>, { use_id, field, formatDateTime, input, is_hidden, tags, group }?: {
173
+ formatFile(folder_id: any, user_id: any, reqs: Array<any>, files: Array<any>, { format, use_id, field, formatDateTime, input, is_hidden, tags, group }?: {
174
+ format?: boolean;
160
175
  use_id?: boolean;
161
176
  field?: any[];
162
177
  formatDateTime?: string;
@@ -169,7 +184,7 @@ export declare class FileUtil {
169
184
  user_id: any;
170
185
  organ_id: any;
171
186
  organ_by: any;
172
- sort: string;
187
+ sort: any;
173
188
  date_added: string;
174
189
  name: any;
175
190
  filename: any;
@@ -179,15 +194,18 @@ export declare class FileUtil {
179
194
  extension: string;
180
195
  type: any;
181
196
  is_hidden: any;
182
- tags: TagFile;
197
+ tags: any;
198
+ custom: any;
183
199
  group: any;
184
200
  }[]>;
185
- insertFileUnique(results: Array<any>, { thumb, width, height }?: {
201
+ insertFileUnique(results: Array<any>, { format, thumb, width, height }?: {
202
+ format?: boolean;
186
203
  thumb?: boolean;
187
204
  width?: number;
188
205
  height?: number;
189
206
  }): any;
190
- uploadRawFormat(files: Array<any>, { field }?: {
207
+ uploadRawFormat(files: Array<any>, { format, field }?: {
208
+ format?: boolean;
191
209
  field?: string[];
192
210
  }): Promise<any>;
193
211
  updateFile(id: string, input?: any, { paths }?: {