@autofleet/sheilta 1.3.11 → 1.4.0-beta.2
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.
- package/lib/formatter/index.d.ts +19 -0
- package/lib/formatter/index.js +72 -5
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +6 -1
- package/package.json +1 -1
package/lib/formatter/index.d.ts
CHANGED
|
@@ -7,6 +7,25 @@ export declare type FormatPayloadOptions = {
|
|
|
7
7
|
DBFormatter?: any;
|
|
8
8
|
skipSearchTermFormat?: boolean;
|
|
9
9
|
};
|
|
10
|
+
declare type ConditionWithOperator = {
|
|
11
|
+
operator: string;
|
|
12
|
+
value: string;
|
|
13
|
+
};
|
|
14
|
+
export declare type ConditionValue = ConditionWithOperator | ConditionWithOperator[] | string | string[];
|
|
15
|
+
/**
|
|
16
|
+
* Generates replacements for the given conditions.
|
|
17
|
+
*
|
|
18
|
+
* @param conditions - The conditions to generate replacements for.
|
|
19
|
+
* @returns The replacements object.
|
|
20
|
+
*/
|
|
21
|
+
export declare const generateFilterReplacements: (conditions: Record<string, ConditionValue>) => Record<string, string>;
|
|
22
|
+
/**
|
|
23
|
+
* Generates replacements for the given order array.
|
|
24
|
+
*
|
|
25
|
+
* @param order - The order array to generate replacements for.
|
|
26
|
+
* @returns The replacements object.
|
|
27
|
+
*/
|
|
28
|
+
export declare const generateOrderReplacements: (order: string[]) => Record<string, string>;
|
|
10
29
|
declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
|
|
11
30
|
order?: any[];
|
|
12
31
|
page?: number;
|
package/lib/formatter/index.js
CHANGED
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.generateOrderReplacements = exports.generateFilterReplacements = void 0;
|
|
6
7
|
const lodash_1 = __importDefault(require("lodash"));
|
|
7
8
|
const common_types_1 = require("@autofleet/common-types");
|
|
8
9
|
const utils_1 = require("../utils");
|
|
@@ -37,7 +38,60 @@ const getAttributeFromOrder = (order, options = {}) => {
|
|
|
37
38
|
}, [[], []]);
|
|
38
39
|
return [formattedOrder, attributes];
|
|
39
40
|
};
|
|
40
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Generates replacements for the given conditions.
|
|
43
|
+
*
|
|
44
|
+
* @param conditions - The conditions to generate replacements for.
|
|
45
|
+
* @returns The replacements object.
|
|
46
|
+
*/
|
|
47
|
+
exports.generateFilterReplacements = (conditions) => {
|
|
48
|
+
const replacements = {};
|
|
49
|
+
Object.entries(conditions).forEach(([key, condition]) => {
|
|
50
|
+
const replacementKey = utils_1.generateRandomString();
|
|
51
|
+
replacements[replacementKey] = key.split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
|
|
52
|
+
if (Array.isArray(condition)) {
|
|
53
|
+
condition.forEach((value) => {
|
|
54
|
+
const valueKey = utils_1.generateRandomString();
|
|
55
|
+
replacements[valueKey] = typeof value === 'string' ? value : value.value;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
else if (typeof condition === 'string') {
|
|
59
|
+
const conditionKey = utils_1.generateRandomString();
|
|
60
|
+
replacements[conditionKey] = condition;
|
|
61
|
+
}
|
|
62
|
+
else if (condition === null || condition === void 0 ? void 0 : condition.operator) {
|
|
63
|
+
const operatorKey = utils_1.generateRandomString();
|
|
64
|
+
replacements[operatorKey] = condition.value;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
return replacements;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Generates replacements for the given order array.
|
|
71
|
+
*
|
|
72
|
+
* @param order - The order array to generate replacements for.
|
|
73
|
+
* @returns The replacements object.
|
|
74
|
+
*/
|
|
75
|
+
exports.generateOrderReplacements = (order) => {
|
|
76
|
+
const replacementMap = {};
|
|
77
|
+
order.forEach((item) => {
|
|
78
|
+
const itemKey = item.startsWith(CUSTOM_FIELDS_QUERY_PREFIX)
|
|
79
|
+
? item.split(CUSTOM_FIELDS_QUERY_PREFIX)[1]
|
|
80
|
+
: item.substring(1).split(CUSTOM_FIELDS_QUERY_PREFIX)[1];
|
|
81
|
+
const randomValue = utils_1.generateRandomString();
|
|
82
|
+
replacementMap[itemKey] = randomValue;
|
|
83
|
+
});
|
|
84
|
+
return replacementMap;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Creates a combined replacement map from order and query.
|
|
88
|
+
*
|
|
89
|
+
* @param order - The order array.
|
|
90
|
+
* @param query - The query object.
|
|
91
|
+
* @returns The combined replacements object.
|
|
92
|
+
*/
|
|
93
|
+
const createReplacementMap = (order, query) => (Object.assign(Object.assign({}, exports.generateOrderReplacements(order)), exports.generateFilterReplacements(query)));
|
|
94
|
+
const formatOrder = ({ order, associationModels = [], replacementsMap = {}, }) => {
|
|
41
95
|
const formattedOrders = [];
|
|
42
96
|
const orderScopesMap = new Map();
|
|
43
97
|
order.forEach((o) => {
|
|
@@ -66,11 +120,17 @@ const formatOrder = ({ order, associationModels = [], }) => {
|
|
|
66
120
|
});
|
|
67
121
|
return {
|
|
68
122
|
formattedOrders,
|
|
123
|
+
replacementsMap,
|
|
69
124
|
orderScopes: Array.from(orderScopesMap.entries()).map(([scopeName, scopeValue]) => {
|
|
70
125
|
if (!scopeValue) {
|
|
71
126
|
return scopeName;
|
|
72
127
|
}
|
|
73
|
-
return {
|
|
128
|
+
return {
|
|
129
|
+
method: [scopeName, {
|
|
130
|
+
replacementsMap,
|
|
131
|
+
scopeValue,
|
|
132
|
+
}],
|
|
133
|
+
};
|
|
74
134
|
}),
|
|
75
135
|
};
|
|
76
136
|
};
|
|
@@ -87,7 +147,7 @@ const formatInclude = (include, associationsMap = {}) => {
|
|
|
87
147
|
formattedInclude = formattedInclude.map(i => lodash_1.default.omit(i, ['model']));
|
|
88
148
|
return formattedInclude;
|
|
89
149
|
};
|
|
90
|
-
const formatQuery = (query, associationModels) => {
|
|
150
|
+
const formatQuery = (query, associationModels, replacementsMap) => {
|
|
91
151
|
const formattedQuery = {};
|
|
92
152
|
const formattedScopeMap = new Map();
|
|
93
153
|
Object.entries(query).forEach(([queryItemKey, queryItemValue]) => {
|
|
@@ -109,7 +169,12 @@ const formatQuery = (query, associationModels) => {
|
|
|
109
169
|
if (!scopeValue) {
|
|
110
170
|
return scopeName;
|
|
111
171
|
}
|
|
112
|
-
return {
|
|
172
|
+
return {
|
|
173
|
+
method: [scopeName, {
|
|
174
|
+
replacementsMap,
|
|
175
|
+
scopeValue,
|
|
176
|
+
}],
|
|
177
|
+
};
|
|
113
178
|
});
|
|
114
179
|
return {
|
|
115
180
|
formattedQuery,
|
|
@@ -126,10 +191,12 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
|
|
|
126
191
|
})),
|
|
127
192
|
});
|
|
128
193
|
const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
|
|
194
|
+
const replacementsMap = createReplacementMap(order, query);
|
|
129
195
|
const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
|
|
130
196
|
const { formattedOrders, orderScopes } = formatOrder({
|
|
131
197
|
order: [...order, DEFAULT_ORDER],
|
|
132
198
|
associationModels,
|
|
199
|
+
replacementsMap,
|
|
133
200
|
});
|
|
134
201
|
const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
|
|
135
202
|
const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
|
|
@@ -137,7 +204,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
|
|
|
137
204
|
const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
|
|
138
205
|
const formattedPage = formatPage(page);
|
|
139
206
|
const formattedPerPage = formatPerPage(perPage);
|
|
140
|
-
const result = formatQuery(query, associationModels);
|
|
207
|
+
const result = formatQuery(query, associationModels, replacementsMap);
|
|
141
208
|
const queryScopes = result.formattedScopes;
|
|
142
209
|
let { formattedQuery } = result;
|
|
143
210
|
if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
|
package/lib/utils.d.ts
CHANGED
|
@@ -11,3 +11,4 @@ export declare const extractAttributeNameFromOrder: (order: any, associationMode
|
|
|
11
11
|
export declare const isOrderDesc: (order: any) => boolean;
|
|
12
12
|
export declare const throwBadRequestError: (message: string) => never;
|
|
13
13
|
export declare const extractAssociatedAttributeNameFromOrder: (order: any) => string;
|
|
14
|
+
export declare const generateRandomString: (length?: number) => string;
|
package/lib/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.extractAssociatedAttributeNameFromOrder = exports.throwBadRequestError = exports.isOrderDesc = exports.extractAttributeNameFromOrder = exports.isAttributeByAssociation = exports.wrapAttributeWithOperator = exports.PAGE_MIN = exports.PER_PAGE_MIN_LIMIT = exports.PER_PAGE_MAX_LIMIT = exports.PAGE_DEFAULT = exports.PER_PAGE_DEFAULT = exports.ASSOCIATION_PREFIX = exports.ORDER_PREFIX = void 0;
|
|
3
|
+
exports.generateRandomString = exports.extractAssociatedAttributeNameFromOrder = exports.throwBadRequestError = exports.isOrderDesc = exports.extractAttributeNameFromOrder = exports.isAttributeByAssociation = exports.wrapAttributeWithOperator = exports.PAGE_MIN = exports.PER_PAGE_MIN_LIMIT = exports.PER_PAGE_MAX_LIMIT = exports.PAGE_DEFAULT = exports.PER_PAGE_DEFAULT = exports.ASSOCIATION_PREFIX = exports.ORDER_PREFIX = void 0;
|
|
4
|
+
const node_crypto_1 = require("node:crypto");
|
|
4
5
|
const errors_1 = require("@autofleet/errors");
|
|
5
6
|
const operators_1 = require("./operators");
|
|
6
7
|
exports.ORDER_PREFIX = '-';
|
|
@@ -29,3 +30,7 @@ exports.throwBadRequestError = (message) => {
|
|
|
29
30
|
throw new errors_1.BadRequest([{ message }], null);
|
|
30
31
|
};
|
|
31
32
|
exports.extractAssociatedAttributeNameFromOrder = (order) => order.split(exports.ASSOCIATION_PREFIX)[1];
|
|
33
|
+
exports.generateRandomString = (length = 5) => {
|
|
34
|
+
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
|
35
|
+
return Array.from({ length }, () => characters.charAt(node_crypto_1.randomInt(characters.length))).join('');
|
|
36
|
+
};
|