@autofleet/sheilta 1.3.9 → 1.3.10-beta-34f81272.0

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.
@@ -7,7 +7,7 @@ export declare type FormatPayloadOptions = {
7
7
  DBFormatter?: any;
8
8
  skipSearchTermFormat?: boolean;
9
9
  };
10
- declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
10
+ declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, allowedSearchableAttributes, }: {
11
11
  order?: any[];
12
12
  page?: number;
13
13
  perPage?: number;
@@ -15,6 +15,7 @@ declare const formatPayload: ({ order, page, perPage, include, query, attributes
15
15
  query?: {};
16
16
  attributes?: any;
17
17
  searchTerm?: any;
18
+ allowedSearchableAttributes?: any;
18
19
  }, model?: any, options?: any) => {
19
20
  attributes: any[] | {
20
21
  include: any[];
@@ -118,14 +118,14 @@ const formatQuery = (query, associationModels) => {
118
118
  };
119
119
  const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
120
120
  $and: searchTerm.split(' ').map(term => ({
121
- $or: attributesToSend.filter(attrKey => rawAttributes[attrKey].type.key === 'STRING').map(attr => ({
121
+ $or: attributesToSend.filter(attrKey => { var _a; return ((_a = rawAttributes[attrKey]) === null || _a === void 0 ? void 0 : _a.type.key) === 'STRING'; }).map(attr => ({
122
122
  [attr]: {
123
123
  $iLike: `%${term}%`,
124
124
  },
125
125
  })),
126
126
  })),
127
127
  });
128
- const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
128
+ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, allowedSearchableAttributes = null, }, model, options) => {
129
129
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
130
130
  const { formattedOrders, orderScopes } = formatOrder({
131
131
  order: [...order, DEFAULT_ORDER],
@@ -141,7 +141,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
141
141
  const queryScopes = result.formattedScopes;
142
142
  let { formattedQuery } = result;
143
143
  if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
144
- const attributesToSend = (attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes);
144
+ const attributesToSend = allowedSearchableAttributes || ((attributes === null || attributes === void 0 ? void 0 : attributes.length) ? attributes : Object.keys(model.rawAttributes));
145
145
  const queryWithSearchTerm = formatSearchTerm(searchTerm, attributesToSend, model.rawAttributes);
146
146
  formattedQuery = lodash_1.default.isEmpty(formattedQuery) ? queryWithSearchTerm : {
147
147
  $and: [
@@ -25,6 +25,7 @@ const querySchema = joi_1.object({
25
25
  perPage: joi_1.number(),
26
26
  include: joi_1.array().items(joi_1.any()),
27
27
  searchTerm: joi_1.string(),
28
+ allowedSearchableAttributes: joi_1.array().items(joi_1.string()),
28
29
  });
29
30
  exports.newQueryValidationMiddleware = (inner = 'body') => (model, options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
30
31
  const { query, attributes, order, page, perPage, include, } = req[inner];
@@ -56,7 +57,7 @@ exports.newQueryValidationMiddleware = (inner = 'body') => (model, options = {})
56
57
  }
57
58
  });
58
59
  exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
59
- const { order, page, perPage, include, query, attributes, searchTerm, } = req[inner];
60
+ const { order, page, perPage, include, query, attributes, searchTerm, allowedSearchableAttributes, } = req[inner];
60
61
  const { query: formattedQuery, order: formattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: formattedScopes, attributes: formattedAttribute, } = formatter_1.default({
61
62
  query,
62
63
  order,
@@ -65,6 +66,7 @@ exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) =>
65
66
  include,
66
67
  attributes,
67
68
  searchTerm,
69
+ allowedSearchableAttributes,
68
70
  }, model, options);
69
71
  req[inner].query = formattedQuery;
70
72
  req[inner].order = formattedOrder;
@@ -73,6 +75,7 @@ exports.newQueryFormatMiddleware = (inner = 'body') => (model, options = {}) =>
73
75
  req[inner].perPage = formattedPerPage;
74
76
  req[inner].include = formattedInclude;
75
77
  req[inner].scopes = formattedScopes;
78
+ req[inner].allowedSearchableAttributes = new Set(allowedSearchableAttributes);
76
79
  if (options.includeRawPayload) {
77
80
  req[inner].rawPayload = {
78
81
  order,
@@ -7,10 +7,23 @@ exports.validatePayload = void 0;
7
7
  const lodash_1 = __importDefault(require("lodash"));
8
8
  const utils_1 = require("../utils");
9
9
  const operators_1 = require("../operators");
10
+ const cleanAttributeFromPrefix = (attr, prefix) => {
11
+ if (attr.startsWith(prefix)) {
12
+ return attr.slice(1);
13
+ }
14
+ return attr.endsWith(prefix) ? attr.slice(0, -1) : attr;
15
+ };
10
16
  const validateOperator = (operator) => operators_1.OPERATORS.includes(operator.split(operators_1.OPERATOR_PREFIX)[1]);
11
- const validateQueryAttribute = (attribute, modelAttributes = [], associationModels = []) => [...modelAttributes, ...associationModels]
12
- .includes(attribute.includes(utils_1.ASSOCIATION_PREFIX)
13
- ? attribute.split(utils_1.ASSOCIATION_PREFIX)[0] : attribute);
17
+ const validateQueryAttribute = (attribute, modelAttributes = [], associationModels = []) => {
18
+ const attributes = [...modelAttributes, ...associationModels];
19
+ if (attribute.includes(utils_1.ASSOCIATION_PREFIX) && attribute.includes(operators_1.OPERATOR_PREFIX)) {
20
+ const parts = attribute.split(utils_1.ASSOCIATION_PREFIX);
21
+ return parts.every(part => attributes.includes(cleanAttributeFromPrefix(part, operators_1.OPERATOR_PREFIX)));
22
+ }
23
+ return attributes
24
+ .includes(attribute.includes(utils_1.ASSOCIATION_PREFIX)
25
+ ? attribute.split(utils_1.ASSOCIATION_PREFIX)[0] : attribute);
26
+ };
14
27
  const validateSingleOrder = (currentOrder, rawAttributes, associationModels, options = {}) => {
15
28
  var _a, _b;
16
29
  const isOrderDescOrder = utils_1.isOrderDesc(currentOrder);
@@ -42,9 +55,8 @@ const validateAttributes = (attributes, rawAttributes) => {
42
55
  };
43
56
  const validateQueryPayload = (query, rawAttributes, associationModels = []) => {
44
57
  // eslint-disable-next-line array-callback-return
45
- Object.keys(query).map((key) => {
46
- const value = query[key];
47
- if (lodash_1.default.isArray(value)) {
58
+ Object.entries(query).map(([key, value]) => {
59
+ if (Array.isArray(value)) {
48
60
  if (lodash_1.default.isObject(value[0])) {
49
61
  value.map(v => validateQueryPayload(v, rawAttributes, associationModels));
50
62
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.3.9",
3
+ "version": "1.3.10-beta-34f81272.0",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -46,8 +46,10 @@
46
46
  "dependencies": {
47
47
  "@autofleet/common-types": "^1.7.42",
48
48
  "@autofleet/errors": "^1.0.10",
49
+ "@autofleet/sadot": "^0.6.6",
49
50
  "joi": "^17.9.2",
50
- "lodash": "^4.17.21"
51
+ "lodash": "^4.17.21",
52
+ "npm-watch": "^0.13.0"
51
53
  },
52
54
  "devDependencies": {
53
55
  "@types/jest": "^24.9.0",