@autofleet/sheilta 1.3.11 → 1.4.0-beta.1
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 +13 -0
- package/lib/formatter/index.js +79 -5
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +6 -1
- package/package.json +2 -2
package/lib/formatter/index.d.ts
CHANGED
|
@@ -7,6 +7,19 @@ 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
|
+
* @param name - The model type name used to join custom_field_definitions.
|
|
20
|
+
* @returns The replacements object.
|
|
21
|
+
*/
|
|
22
|
+
export declare const generateFilterReplacements: (conditions: Record<string, ConditionValue>) => Record<string, string>;
|
|
10
23
|
declare const formatPayload: ({ order, page, perPage, include, query, attributes, searchTerm, }: {
|
|
11
24
|
order?: any[];
|
|
12
25
|
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.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,61 @@ 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
|
+
* @param name - The model type name used to join custom_field_definitions.
|
|
46
|
+
* @returns The replacements object.
|
|
47
|
+
*/
|
|
48
|
+
exports.generateFilterReplacements = (conditions) => {
|
|
49
|
+
const replacements = {};
|
|
50
|
+
console.log('sheilta - conditions:', conditions);
|
|
51
|
+
Object.entries(conditions).forEach(([key, condition]) => {
|
|
52
|
+
const replacemetKey = utils_1.generateRandomString();
|
|
53
|
+
replacements[replacemetKey] = `${key.split(CUSTOM_FIELDS_QUERY_PREFIX)[1]}`;
|
|
54
|
+
if (Array.isArray(condition)) {
|
|
55
|
+
condition.forEach((value) => {
|
|
56
|
+
if (typeof value === 'string') {
|
|
57
|
+
const valRandom = utils_1.generateRandomString();
|
|
58
|
+
replacements[`${valRandom}`] = `${value}`;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const valRep = utils_1.generateRandomString();
|
|
62
|
+
replacements[valRep] = `${value.value}`;
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
else if (typeof condition === 'string') {
|
|
67
|
+
const conditionRep = utils_1.generateRandomString();
|
|
68
|
+
replacements[conditionRep] = `${condition}`;
|
|
69
|
+
}
|
|
70
|
+
else if (condition === null || condition === void 0 ? void 0 : condition.operator) {
|
|
71
|
+
const valueRep = utils_1.generateRandomString();
|
|
72
|
+
replacements[valueRep] = `${condition.value}`;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return replacements;
|
|
76
|
+
};
|
|
77
|
+
const createReplacementMap = (order, query) => {
|
|
78
|
+
const replacementMap = {};
|
|
79
|
+
order.forEach((o) => {
|
|
80
|
+
if (o.startsWith(CUSTOM_FIELDS_QUERY_PREFIX)) {
|
|
81
|
+
const rand = utils_1.generateRandomString();
|
|
82
|
+
replacementMap[o.split(CUSTOM_FIELDS_QUERY_PREFIX)[1]] = rand;
|
|
83
|
+
}
|
|
84
|
+
else if (o.substring(1).startsWith(CUSTOM_FIELDS_QUERY_PREFIX)) {
|
|
85
|
+
const rand = utils_1.generateRandomString();
|
|
86
|
+
replacementMap[o.substring(1).split(CUSTOM_FIELDS_QUERY_PREFIX)[1]] = rand;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
console.log('🐶');
|
|
90
|
+
console.log('query:', query);
|
|
91
|
+
const queryReplacements = exports.generateFilterReplacements(query);
|
|
92
|
+
console.log('queryReplacements:', queryReplacements);
|
|
93
|
+
return Object.assign(Object.assign({}, replacementMap), queryReplacements);
|
|
94
|
+
};
|
|
95
|
+
const formatOrder = ({ order, associationModels = [], replacementsMap = {}, }) => {
|
|
41
96
|
const formattedOrders = [];
|
|
42
97
|
const orderScopesMap = new Map();
|
|
43
98
|
order.forEach((o) => {
|
|
@@ -66,11 +121,17 @@ const formatOrder = ({ order, associationModels = [], }) => {
|
|
|
66
121
|
});
|
|
67
122
|
return {
|
|
68
123
|
formattedOrders,
|
|
124
|
+
replacementsMap,
|
|
69
125
|
orderScopes: Array.from(orderScopesMap.entries()).map(([scopeName, scopeValue]) => {
|
|
70
126
|
if (!scopeValue) {
|
|
71
127
|
return scopeName;
|
|
72
128
|
}
|
|
73
|
-
return {
|
|
129
|
+
return {
|
|
130
|
+
method: [scopeName, {
|
|
131
|
+
replacementsMap,
|
|
132
|
+
scopeValue,
|
|
133
|
+
}],
|
|
134
|
+
};
|
|
74
135
|
}),
|
|
75
136
|
};
|
|
76
137
|
};
|
|
@@ -87,7 +148,7 @@ const formatInclude = (include, associationsMap = {}) => {
|
|
|
87
148
|
formattedInclude = formattedInclude.map(i => lodash_1.default.omit(i, ['model']));
|
|
88
149
|
return formattedInclude;
|
|
89
150
|
};
|
|
90
|
-
const formatQuery = (query, associationModels) => {
|
|
151
|
+
const formatQuery = (query, associationModels, replacementsMap) => {
|
|
91
152
|
const formattedQuery = {};
|
|
92
153
|
const formattedScopeMap = new Map();
|
|
93
154
|
Object.entries(query).forEach(([queryItemKey, queryItemValue]) => {
|
|
@@ -109,7 +170,12 @@ const formatQuery = (query, associationModels) => {
|
|
|
109
170
|
if (!scopeValue) {
|
|
110
171
|
return scopeName;
|
|
111
172
|
}
|
|
112
|
-
return {
|
|
173
|
+
return {
|
|
174
|
+
method: [scopeName, {
|
|
175
|
+
replacementsMap,
|
|
176
|
+
scopeValue,
|
|
177
|
+
}],
|
|
178
|
+
};
|
|
113
179
|
});
|
|
114
180
|
return {
|
|
115
181
|
formattedQuery,
|
|
@@ -126,10 +192,12 @@ const formatSearchTerm = (searchTerm, attributesToSend, rawAttributes) => ({
|
|
|
126
192
|
})),
|
|
127
193
|
});
|
|
128
194
|
const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = utils_1.PER_PAGE_DEFAULT, include = [], query = {}, attributes = null, searchTerm = null, }, model, options) => {
|
|
195
|
+
const replacementsMap = createReplacementMap(order, query);
|
|
129
196
|
const associationModels = Object.keys((model === null || model === void 0 ? void 0 : model.associations) || {});
|
|
130
197
|
const { formattedOrders, orderScopes } = formatOrder({
|
|
131
198
|
order: [...order, DEFAULT_ORDER],
|
|
132
199
|
associationModels,
|
|
200
|
+
replacementsMap,
|
|
133
201
|
});
|
|
134
202
|
const [filteredFormattedOrder, filteredLiteralAttributes] = getAttributeFromOrder(formattedOrders, options);
|
|
135
203
|
const allAttributes = [...filteredLiteralAttributes, ...(attributes || [])];
|
|
@@ -137,7 +205,7 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
|
|
|
137
205
|
const formattedInclude = formatInclude(include, model === null || model === void 0 ? void 0 : model.associations);
|
|
138
206
|
const formattedPage = formatPage(page);
|
|
139
207
|
const formattedPerPage = formatPerPage(perPage);
|
|
140
|
-
const result = formatQuery(query, associationModels);
|
|
208
|
+
const result = formatQuery(query, associationModels, replacementsMap);
|
|
141
209
|
const queryScopes = result.formattedScopes;
|
|
142
210
|
let { formattedQuery } = result;
|
|
143
211
|
if (searchTerm && !(options === null || options === void 0 ? void 0 : options.skipSearchTermFormat)) {
|
|
@@ -150,6 +218,12 @@ const formatPayload = ({ order = [], page = utils_1.PAGE_DEFAULT, perPage = util
|
|
|
150
218
|
],
|
|
151
219
|
};
|
|
152
220
|
}
|
|
221
|
+
console.log('🐶');
|
|
222
|
+
console.log('scopes:', [...orderScopes, ...queryScopes]);
|
|
223
|
+
[...orderScopes, ...queryScopes].map((scope) => {
|
|
224
|
+
console.log('scope:', scope.method[0]);
|
|
225
|
+
console.log('scope:', scope.method[1]);
|
|
226
|
+
});
|
|
153
227
|
return Object.assign({ query: formattedQuery, order: filteredFormattedOrder, page: formattedPage, perPage: formattedPerPage, include: formattedInclude, scopes: [...queryScopes, ...orderScopes] }, (formattedAttribute && { attributes: formattedAttribute }));
|
|
154
228
|
};
|
|
155
229
|
exports.default = formatPayload;
|
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,8 +1,9 @@
|
|
|
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
4
|
const errors_1 = require("@autofleet/errors");
|
|
5
5
|
const operators_1 = require("./operators");
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
7
|
exports.ORDER_PREFIX = '-';
|
|
7
8
|
exports.ASSOCIATION_PREFIX = '.';
|
|
8
9
|
exports.PER_PAGE_DEFAULT = 20;
|
|
@@ -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
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/sheilta",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-beta.1",
|
|
4
4
|
"description": "manage cache",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"test": "jest --forceExit --runInBand",
|
|
12
12
|
"test-auto": "jest --watch --runInBand",
|
|
13
13
|
"linter": "./node_modules/.bin/eslint src/**/*.ts",
|
|
14
|
-
"build-to-local-repo": "npm run build && cp -r lib/*
|
|
14
|
+
"build-to-local-repo": "npm run build && cp -r lib/* ../task-ms/node_modules/@autofleet/sheilta/lib",
|
|
15
15
|
"watch": "npm-watch build-to-local-repo"
|
|
16
16
|
},
|
|
17
17
|
"watch": {
|