@autofleet/sheilta 1.4.0 → 1.4.1-beta-test

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,6 +7,7 @@ export declare type LiteralAttribute = {
7
7
  };
8
8
  export declare type MiddlewareValidationOption = {
9
9
  literalAttributes?: LiteralAttribute[];
10
+ enrichmentAttributes?: string[];
10
11
  };
11
12
  export declare const newQueryValidationMiddleware: (inner?: string) => (model: any, options?: MiddlewareValidationOption) => (req: any, res: any, next: any) => Promise<void>;
12
13
  export declare const newQueryFormatMiddleware: (inner?: string) => (model: any, options?: FormatPayloadOptions) => (req: any, res: any, next: any) => Promise<void>;
@@ -25,9 +25,10 @@ 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
+ enrichments: 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
- const { query, attributes, order, page, perPage, include, } = req[inner];
31
+ const { query, attributes, order, page, perPage, include, enrichments, } = req[inner];
31
32
  try {
32
33
  const result = querySchema.validate(req[inner]);
33
34
  if (result.error) {
@@ -40,6 +41,7 @@ exports.newQueryValidationMiddleware = (inner = 'body') => (model, options = {})
40
41
  page,
41
42
  perPage,
42
43
  include,
44
+ enrichments,
43
45
  }, model, options);
44
46
  return next();
45
47
  }
@@ -1,9 +1,10 @@
1
1
  import type { MiddlewareValidationOption } from '../middleware';
2
- export declare const validatePayload: ({ query, order, attributes, include, page, perPage, }: {
2
+ export declare const validatePayload: ({ query, order, attributes, include, page, perPage, enrichments, }: {
3
3
  query?: {};
4
4
  order?: any[];
5
5
  attributes?: any[];
6
6
  include?: any[];
7
7
  page?: number;
8
8
  perPage?: number;
9
+ enrichments?: any[];
9
10
  }, model?: any, options?: MiddlewareValidationOption) => boolean;
@@ -40,6 +40,16 @@ const validateOrderAttributes = (order, rawAttributes, associationModels = [], o
40
40
  const validateAttributes = (attributes, rawAttributes) => {
41
41
  attributes.map(a => validateSingleAttribute(a, rawAttributes));
42
42
  };
43
+ const validateEnrichments = (enrichments, options) => {
44
+ if (!(enrichments === null || enrichments === void 0 ? void 0 : enrichments.length)) {
45
+ return;
46
+ }
47
+ const invalidEnrichment = enrichments
48
+ .find(enrichment => { var _a; return !((_a = options === null || options === void 0 ? void 0 : options.enrichmentAttributes) === null || _a === void 0 ? void 0 : _a.includes(enrichment)); });
49
+ if (invalidEnrichment) {
50
+ utils_1.throwBadRequestError(`enrichment attribute ${invalidEnrichment} is invalid`);
51
+ }
52
+ };
43
53
  const validateQueryPayload = (query, rawAttributes, associationModels = []) => {
44
54
  // eslint-disable-next-line array-callback-return
45
55
  Object.keys(query).map((key) => {
@@ -93,7 +103,7 @@ const validateIncludePayload = (include, associations) => {
93
103
  }
94
104
  });
95
105
  };
96
- exports.validatePayload = ({ query = {}, order = [], attributes = [], include = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, }, model, options = {}) => {
106
+ exports.validatePayload = ({ query = {}, order = [], attributes = [], include = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, enrichments = [], }, model, options = {}) => {
97
107
  const rawAttributes = Object.keys(model.rawAttributes);
98
108
  const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
99
109
  if (!attributes || attributes.length === 0) {
@@ -105,6 +115,7 @@ exports.validatePayload = ({ query = {}, order = [], attributes = [], include =
105
115
  }
106
116
  validateOrderAttributes(order, rawAttributes, associationModels, options);
107
117
  validateQueryPayload(query, rawAttributes, associationModels);
118
+ validateEnrichments(enrichments, options);
108
119
  if (include.length && typeof include === 'object') {
109
120
  validateIncludePayload(include, model === null || model === void 0 ? void 0 : model.associations);
110
121
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sheilta",
3
- "version": "1.4.0",
3
+ "version": "1.4.1-beta-test",
4
4
  "description": "manage cache",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",