@digione/node-custom-api 0.2.0-beta5 → 0.2.0-beta8

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;
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
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,7 +139,7 @@ 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) => {
130
145
  let config = res.locals.config || {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@digione/node-custom-api",
3
- "version": "0.2.0-beta5",
3
+ "version": "0.2.0-beta8",
4
4
  "description": "Typescript node digione-api",
5
5
  "author": "Monchai Jirayupong <monchai.j@seven.co.th>",
6
6
  "license": "MIT",
@@ -29,6 +29,7 @@
29
29
  "moment": "2.30.1",
30
30
  "multer": "1.4.5-lts.1",
31
31
  "mysql2": "3.9.1",
32
+ "nanoid": "5.1.6",
32
33
  "numeral": "2.0.6",
33
34
  "php-serialize": "4.1.1",
34
35
  "raven": "2.6.4",
package/utils/file.d.ts CHANGED
@@ -89,6 +89,12 @@ export declare class FileUtil {
89
89
  tags?: TagFile;
90
90
  group?: any;
91
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>;
92
98
  findAndCountAllFileByFolder(slug: string, { where, file_link, parent_folder, limit, offset, tag, tags, group }?: {
93
99
  where?: {};
94
100
  file_link?: boolean;
package/utils/file.js CHANGED
@@ -14,7 +14,6 @@ const aws = require("aws-sdk");
14
14
  const jimp = require("jimp/dist");
15
15
  const mime = require("mime-types");
16
16
  const _ = require("lodash");
17
- const uuid_1 = require("uuid");
18
17
  const path = require("path");
19
18
  const fs = require("fs");
20
19
  const getBuffer = (image, { mimetype = "" } = {}) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
@@ -373,6 +372,33 @@ class FileUtil {
373
372
  }
374
373
  });
375
374
  }
375
+ countFileByFolder(slug, { where = {}, parent_folder = "", tags = [], group = null } = {}) {
376
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
377
+ let include_folder = [];
378
+ if (parent_folder) {
379
+ where['$`f->p`.slug$'] = parent_folder;
380
+ include_folder.push({
381
+ required: true, attributes: [],
382
+ model: folder_1.FolderModel.schema(this.ref, "_"),
383
+ association: new sequelize_1.BelongsTo(folder_1.FolderModel, folder_1.FolderModel, { as: "p", foreignKey: "parent_id", constraints: false })
384
+ });
385
+ }
386
+ where['$f.slug$'] = slug;
387
+ where['is_hidden'] = where['is_hidden'] || "no";
388
+ let option = this.beforeFindFile(where, { tags, group, verify: true });
389
+ return yield file_1.FileModel.schema(this.ref, "_").count({
390
+ where: option.where,
391
+ include: [
392
+ {
393
+ required: true, attributes: [],
394
+ model: folder_1.FolderModel.schema(this.ref, "_"),
395
+ association: new sequelize_1.BelongsTo(file_1.FileModel, folder_1.FolderModel, { as: "f", foreignKey: "folder_id", constraints: false }),
396
+ include: include_folder
397
+ }
398
+ ]
399
+ });
400
+ });
401
+ }
376
402
  findAndCountAllFileByFolder(slug, { where = {}, file_link = false, parent_folder = "", limit = undefined, offset = undefined, tag = {}, tags = [], group = null } = {}) {
377
403
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
378
404
  let where_folder = {};
@@ -583,7 +609,7 @@ class FileUtil {
583
609
  try {
584
610
  let result = yield Promise.all(files.map((file, key) => tslib_1.__awaiter(this, void 0, void 0, function* () {
585
611
  folder_id = reqs[key].folder ? reqs[key].folder : folder_id;
586
- let id = (0, uuid_1.v4)().replace(/-/gi, '').substring(0, 15);
612
+ let id = (0, helper_1.getFileId)();
587
613
  let organ_id = this.option.organ_id;
588
614
  let organ_by = this.option.parent_organ_id;
589
615
  let sort = (0, helper_1.getDateTime)().format(formatDateTime);
@@ -649,7 +675,7 @@ class FileUtil {
649
675
  }
650
676
  catch (err) {
651
677
  if (err.errors && err.errors[0].validatorKey == "not_unique") {
652
- results = results.map(item => (Object.assign(Object.assign({}, item), { id: (0, uuid_1.v4)().replace(/-/gi, '').substring(0, 15) })));
678
+ results = results.map(item => (Object.assign(Object.assign({}, item), { id: (0, helper_1.getFileId)() })));
653
679
  return yield this.insertFileUnique(results, { format, thumb, width, height });
654
680
  }
655
681
  return Promise.reject(errors_1.generalError.BAD_REQUEST);
package/utils/helper.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare const getENVIndex: (index: string, { prefix, force }?: {
17
17
  force?: boolean;
18
18
  }) => string;
19
19
  export declare const getSlug: (length?: number) => string;
20
+ export declare const getFileId: () => string;
20
21
  export declare const getDateMysql: (date?: any) => any;
21
22
  export declare const getTimestampMysql: (date?: any) => number;
22
23
  export declare const getDateTime: (time?: any, { utc }?: {
package/utils/helper.js CHANGED
@@ -1,12 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertCurrency = exports.xorMerge = exports.xorDecode = exports.xorEncode = exports.getInstanceNumber = exports.getDomain = exports.getParent = exports.replaceColon = exports.stripEmoji = exports.stripHtml = exports.slugifyField = exports.slugify = exports.convertToBigEndian = exports.expandTree = exports.buildTree = exports.treeToArray = exports.randomRef = exports.multiExecArray = exports.isBase64 = exports.isNumber = exports.removeAt = exports.removeSpace = exports.formatMobileNumber = exports.getMobileNumber = exports.removeHyphen = exports.signToken = exports.getTokenDataFromTokenBySecret = exports.getUUID32 = exports.getUUID = exports.sleep = exports.getValueByLang = exports.getValueFunctionLang = exports.getValueFunctionArray = exports.getValueFunctionFile = exports.getDateTime = exports.getTimestampMysql = exports.getDateMysql = exports.getSlug = exports.getENVIndex = exports.getENV = exports.Joi_date = exports.getDaysBetweenDates = exports.getDateFromString = exports.getDateRangeFormat = void 0;
3
+ exports.convertCurrency = exports.xorMerge = exports.xorDecode = exports.xorEncode = exports.getInstanceNumber = exports.getDomain = exports.getParent = exports.replaceColon = exports.stripEmoji = exports.stripHtml = exports.slugifyField = exports.slugify = exports.convertToBigEndian = exports.expandTree = exports.buildTree = exports.treeToArray = exports.randomRef = exports.multiExecArray = exports.isBase64 = exports.isNumber = exports.removeAt = exports.removeSpace = exports.formatMobileNumber = exports.getMobileNumber = exports.removeHyphen = exports.signToken = exports.getTokenDataFromTokenBySecret = exports.getUUID32 = exports.getUUID = exports.sleep = exports.getValueByLang = exports.getValueFunctionLang = exports.getValueFunctionArray = exports.getValueFunctionFile = exports.getDateTime = exports.getTimestampMysql = exports.getDateMysql = exports.getFileId = exports.getSlug = exports.getENVIndex = exports.getENV = exports.Joi_date = exports.getDaysBetweenDates = exports.getDateFromString = exports.getDateRangeFormat = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const crypto = require("crypto");
6
6
  const dotenv = require("dotenv");
7
7
  const moment = require("moment");
8
8
  const uuid_1 = require("uuid");
9
9
  const short_unique_id_1 = require("short-unique-id");
10
+ const nanoid_1 = require("nanoid");
10
11
  const jwt = require("jsonwebtoken");
11
12
  const Joi = require("joi");
12
13
  const date_1 = require("@joi/date");
@@ -68,6 +69,12 @@ const getSlug = (length = 8) => {
68
69
  return new short_unique_id_1.default().randomUUID(length);
69
70
  };
70
71
  exports.getSlug = getSlug;
72
+ const getFileId = () => {
73
+ const alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
74
+ const nanoid = (0, nanoid_1.customAlphabet)(alphabet, 15);
75
+ return nanoid();
76
+ };
77
+ exports.getFileId = getFileId;
71
78
  const getDateMysql = (date = undefined) => {
72
79
  try {
73
80
  return (0, exports.getDateTime)(date).format("YYYY-MM-DD HH:mm:ss");
package/utils/stream.d.ts CHANGED
@@ -76,13 +76,14 @@ export declare class StreamUtil {
76
76
  include?: any;
77
77
  include_only?: boolean;
78
78
  }): any;
79
- buildStream(slug: string, namespace: string, { query, builder, fetch, label, attribute, attributes, field_exclude, include, include_all }?: {
79
+ buildStream(slug: string, namespace: string, { query, builder, fetch, label, attribute, attributes, field_option, field_exclude, include, include_all }?: {
80
80
  query?: boolean;
81
81
  builder?: boolean;
82
82
  fetch?: boolean;
83
83
  label?: boolean;
84
84
  attribute?: boolean;
85
85
  attributes?: any;
86
+ field_option?: {};
86
87
  field_exclude?: any[];
87
88
  include?: {};
88
89
  include_all?: boolean;
@@ -92,10 +93,11 @@ export declare class StreamUtil {
92
93
  fetchFn: any;
93
94
  attributes: any;
94
95
  }>;
95
- getBuilder(slug: string, namespace: string, { query, index, builder, field_exclude }?: {
96
+ getBuilder(slug: string, namespace: string, { query, index, builder, field_option, field_exclude }?: {
96
97
  query?: boolean;
97
98
  index?: string;
98
99
  builder?: boolean;
100
+ field_option?: {};
99
101
  field_exclude?: any[];
100
102
  }): Promise<any>;
101
103
  getValue(slug: string, namespace: string, value: any, { query, transform, spread, spread_all, raw, lang_code }?: {
@@ -305,7 +307,7 @@ export declare class StreamUtil {
305
307
  stripUnknown?: boolean;
306
308
  builder?: boolean;
307
309
  }): Promise<Joi.ObjectSchema<any>>;
308
- validate(slug: string, namespace: string, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage, builder, field_exclude }?: {
310
+ validate(slug: string, namespace: string, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage, builder, field_option, field_exclude }?: {
309
311
  req?: Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>;
310
312
  isNew?: boolean;
311
313
  default_value?: {};
@@ -315,6 +317,7 @@ export declare class StreamUtil {
315
317
  inputLangSeparate?: boolean;
316
318
  prefixMessage?: string;
317
319
  builder?: boolean;
320
+ field_option?: {};
318
321
  field_exclude?: any[];
319
322
  }): Promise<any>;
320
323
  validateByField(streams: Array<any>, input: any, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage }?: {
package/utils/stream.js CHANGED
@@ -235,13 +235,13 @@ class StreamUtil {
235
235
  }
236
236
  return data;
237
237
  }
238
- buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_exclude = [], include = {}, include_all = false } = {}) {
238
+ buildStream(slug, namespace, { query = true, builder = true, fetch = false, label = false, attribute = false, attributes = null, field_option = {}, field_exclude = [], include = {}, include_all = false } = {}) {
239
239
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
240
240
  let build = this.builder[slug + "_" + namespace] || { stream: null, fields: null, fetchFn: [], attributes: null };
241
241
  if (!build['fields']) {
242
242
  let data = {};
243
243
  if (query)
244
- data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_exclude });
244
+ data = yield this.findField(slug, namespace, { field_only: false, convert_option: true, field_option, field_exclude });
245
245
  build['fields'] = data['fields'] || [];
246
246
  build['stream'] = _.omit(data, ['fields']);
247
247
  }
@@ -312,9 +312,9 @@ class StreamUtil {
312
312
  return build;
313
313
  });
314
314
  }
315
- getBuilder(slug, namespace, { query = true, index = "", builder = true, field_exclude = [] } = {}) {
315
+ getBuilder(slug, namespace, { query = true, index = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
316
316
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
317
- let result = yield this.buildStream(slug, namespace, { query, builder, field_exclude });
317
+ let result = yield this.buildStream(slug, namespace, { query, builder, field_option, field_exclude });
318
318
  return index ? result[index] : result;
319
319
  });
320
320
  }
@@ -2056,9 +2056,9 @@ class StreamUtil {
2056
2056
  return yield this.getValidator(streams, { req, isNew, required, lang, stripUnknown });
2057
2057
  });
2058
2058
  }
2059
- validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_exclude = [] } = {}) {
2059
+ validate(slug, namespace, input, { req = {}, isNew = false, default_value = {}, lang = [], stripUnknown = false, inputLang = false, inputLangSeparate = true, prefixMessage = "", builder = true, field_option = {}, field_exclude = [] } = {}) {
2060
2060
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
2061
- let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_exclude });
2061
+ let streams = yield this.getBuilder(slug, namespace, { index: "fields", builder, field_option, field_exclude });
2062
2062
  return this.validateByField(streams, input, { req, isNew, default_value, lang, stripUnknown, inputLang, inputLangSeparate, prefixMessage });
2063
2063
  });
2064
2064
  }