@commercetools/processors 0.0.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.
package/dist/index.js ADDED
@@ -0,0 +1,476 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ FieldFilteringHandler: () => FieldFilteringHandler,
34
+ defaultFilteringRules: () => defaultFilteringRules,
35
+ defaultJsonRedactionText: () => defaultJsonRedactionText,
36
+ defaultUrlRedactionText: () => defaultUrlRedactionText,
37
+ generateQueryString: () => generateQueryString,
38
+ isFieldFilteringManager: () => isFieldFilteringManager,
39
+ isValidUrl: () => isValidUrl,
40
+ normaliseUrl: () => normaliseUrl,
41
+ transformPropertyName: () => transformPropertyName,
42
+ transformToolOutput: () => transformToolOutput
43
+ });
44
+ module.exports = __toCommonJS(index_exports);
45
+
46
+ // src/field-filtering/defaultFilteringRules.ts
47
+ var defaultJsonRedactionText = "[REDACTED]";
48
+ var defaultUrlRedactionText = "REDACTED";
49
+ var defaultFilteringRules = {
50
+ jsonRedactionText: defaultJsonRedactionText,
51
+ urlRedactionText: defaultUrlRedactionText
52
+ };
53
+
54
+ // src/field-filtering/urlHelpers.ts
55
+ var import_qs = __toESM(require("qs"));
56
+ var normaliseUrl = (url) => {
57
+ const protocolSplit = url.split("//");
58
+ let normalisedUrl = "";
59
+ if (protocolSplit[0] && (protocolSplit[0] === "http:" || protocolSplit[0] === "https:" || protocolSplit[0] === "ws:" || protocolSplit[0] === "wss:")) {
60
+ normalisedUrl = `${protocolSplit[0]}//`;
61
+ protocolSplit.splice(0, 1);
62
+ }
63
+ let query = "";
64
+ const querySplit = protocolSplit.join("//").split("?");
65
+ if (querySplit.length > 1) {
66
+ query = `?${querySplit.slice(1, querySplit.length).join("?")}`;
67
+ }
68
+ const pathSplit = querySplit[0].split("/");
69
+ for (let n = 0; n < pathSplit.length; n++) {
70
+ if (pathSplit[n]) {
71
+ normalisedUrl += `${pathSplit[n]}/`;
72
+ }
73
+ }
74
+ normalisedUrl = normalisedUrl.substring(0, normalisedUrl.length - 1);
75
+ return normalisedUrl + query;
76
+ };
77
+ var toQueryObject = (key, value) => {
78
+ const obj = {};
79
+ obj[key] = value;
80
+ return obj;
81
+ };
82
+ var toQueryString = (obj) => {
83
+ return import_qs.default.stringify(obj, {
84
+ arrayFormat: "indices",
85
+ encodeValuesOnly: true,
86
+ format: "RFC3986"
87
+ });
88
+ };
89
+ var generateQueryString = (query) => {
90
+ let queryString = "?";
91
+ Object.keys(query).forEach((key) => {
92
+ const value = query[key];
93
+ if (value !== void 0 && typeof value !== "function") {
94
+ if (value === null) {
95
+ queryString += `${key}=null&`;
96
+ } else {
97
+ queryString += `${toQueryString(toQueryObject(key, value))}&`;
98
+ }
99
+ }
100
+ });
101
+ queryString = queryString.substring(0, queryString.length - 1);
102
+ return queryString;
103
+ };
104
+ var isValidUrl = (urlLike) => {
105
+ let url;
106
+ try {
107
+ url = new URL(urlLike);
108
+ } catch (_) {
109
+ return false;
110
+ }
111
+ return true;
112
+ };
113
+
114
+ // src/field-filtering/FieldFilteringHandler.ts
115
+ var FieldFilteringHandler = class {
116
+ filterPaths;
117
+ redactPaths;
118
+ filterProperties;
119
+ redactProperties;
120
+ whitelistPaths;
121
+ filterIncludes;
122
+ redactIncludes;
123
+ jsonRedactionText;
124
+ urlRedactionText;
125
+ hasFilteringRules = true;
126
+ constructor(config) {
127
+ this.filterPaths = config.paths?.filter((path) => path.type === "filter") ?? [];
128
+ this.redactPaths = config.paths?.filter((path) => path.type === "redact") ?? [];
129
+ this.filterProperties = config.properties?.filter((path) => path.type === "filter") ?? [];
130
+ this.redactProperties = config.properties?.filter((path) => path.type === "redact") ?? [];
131
+ this.whitelistPaths = config.whitelistPaths ?? [];
132
+ this.filterIncludes = config.includes?.filter((path) => path.type === "filter") ?? [];
133
+ this.redactIncludes = config.includes?.filter((path) => path.type === "redact") ?? [];
134
+ this.jsonRedactionText = config.jsonRedactionText ?? defaultJsonRedactionText;
135
+ this.urlRedactionText = config.urlRedactionText ?? defaultUrlRedactionText;
136
+ if (config.paths?.length === 0 && config.properties?.length === 0 && config.includes?.length === 0) {
137
+ this.hasFilteringRules = false;
138
+ }
139
+ }
140
+ filterFields(data, currentPath) {
141
+ if (this.hasFilteringRules === false) {
142
+ return data;
143
+ }
144
+ if (this.filterConditionMet(currentPath ?? "", "redact")) {
145
+ return this.jsonRedactionText;
146
+ }
147
+ if (typeof data === "string") {
148
+ if (isValidUrl(data)) {
149
+ return this.filterUrlFields(data);
150
+ }
151
+ return data;
152
+ }
153
+ if (typeof data === "object") {
154
+ if (Array.isArray(data)) {
155
+ return data.map(
156
+ (datum) => this.filterFields(datum, currentPath)
157
+ );
158
+ } else if (data) {
159
+ Object.keys(data).forEach((key) => {
160
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
161
+ if (this.filterConditionMet(newPath, "filter")) {
162
+ delete data[key];
163
+ } else {
164
+ data[key] = this.filterFields(
165
+ data[key],
166
+ newPath
167
+ );
168
+ }
169
+ });
170
+ return data;
171
+ }
172
+ }
173
+ return data;
174
+ }
175
+ filterConditionMet(path, type) {
176
+ let ruleIsSatisfied = this.testFilterRules(path, this.whitelistPaths);
177
+ if (ruleIsSatisfied) {
178
+ return false;
179
+ }
180
+ ruleIsSatisfied = this.testFilterRules(path, this[`${type}Paths`]);
181
+ if (ruleIsSatisfied) {
182
+ return true;
183
+ }
184
+ const properties = path.split(".");
185
+ const propertyName = properties[properties.length - 1];
186
+ ruleIsSatisfied = this.testFilterRules(
187
+ propertyName,
188
+ this[`${type}Properties`]
189
+ );
190
+ if (ruleIsSatisfied) {
191
+ return true;
192
+ }
193
+ const includes = this[`${type}Includes`];
194
+ for (let n = 0; n < includes.length; n++) {
195
+ if (propertyName.includes(includes[n].value) || !includes[n].caseSensitive && propertyName.toLowerCase().includes(includes[n].value.toLowerCase())) {
196
+ n = includes.length;
197
+ ruleIsSatisfied = true;
198
+ }
199
+ }
200
+ return ruleIsSatisfied;
201
+ }
202
+ // checks regardless of "filter" | "redact"
203
+ testFilterRules(path, rules) {
204
+ let ruleIsSatisfied = false;
205
+ for (let n = 0; n < rules.length; n++) {
206
+ if (path === rules[n].value || !rules[n].caseSensitive && path.toLowerCase() === rules[n].value.toLowerCase()) {
207
+ n = rules.length;
208
+ ruleIsSatisfied = true;
209
+ }
210
+ }
211
+ return ruleIsSatisfied;
212
+ }
213
+ filterUrlFields(inputUrl) {
214
+ if (this.hasFilteringRules === false) {
215
+ return inputUrl;
216
+ }
217
+ const url = new URL(inputUrl);
218
+ const urlParams = new URLSearchParams(url.search);
219
+ const keysToDelete = [];
220
+ for (const [key, _] of urlParams.entries()) {
221
+ if (this.filterConditionMet(this.urlQueryKeyToObjectPath(key), "filter")) {
222
+ keysToDelete.push(key);
223
+ } else if (this.filterConditionMet(this.urlQueryKeyToObjectPath(key), "redact")) {
224
+ urlParams.set(key, this.urlRedactionText);
225
+ }
226
+ }
227
+ keysToDelete.forEach((key) => urlParams.delete(key));
228
+ url.search = urlParams.toString();
229
+ return url.toString();
230
+ }
231
+ // assumes keys of string only
232
+ urlQueryKeyToObjectPath(urlQueryKey) {
233
+ const urlQueryKeyOpenBracketSplit = urlQueryKey.split("[");
234
+ if (urlQueryKeyOpenBracketSplit.length === 1) {
235
+ return urlQueryKey;
236
+ } else {
237
+ let objectPath = urlQueryKeyOpenBracketSplit.splice(0, 1)[0];
238
+ urlQueryKeyOpenBracketSplit.forEach((openBracketSplitValue, n) => {
239
+ const closeBracketSplit = openBracketSplitValue.split("]");
240
+ if (closeBracketSplit.length !== 2) {
241
+ console.warn(
242
+ `Object in URL params in incorrect format: [${urlQueryKeyOpenBracketSplit[n]}`
243
+ );
244
+ }
245
+ if (!closeBracketSplit[0].match(/^-?\d+$/)) {
246
+ objectPath += `.${closeBracketSplit[0]}`;
247
+ }
248
+ });
249
+ return objectPath;
250
+ }
251
+ }
252
+ };
253
+
254
+ // src/field-filtering/index.ts
255
+ var isFieldFilteringManager = (config) => {
256
+ return Boolean(
257
+ config?.filterFields && config?.filterUrlFields
258
+ );
259
+ };
260
+
261
+ // src/transform/transformPropertyName.ts
262
+ var isUpperCase = (str) => str !== str.toLowerCase();
263
+ var transformPropertyName = (propertyName) => {
264
+ if (!propertyName) {
265
+ return "";
266
+ }
267
+ propertyName = propertyName.replaceAll(/[_-]+/g, " ").trim();
268
+ let newPropertyName = propertyName.charAt(0).toLocaleUpperCase();
269
+ for (let n = 1; n < propertyName.length; n++) {
270
+ const char = propertyName.charAt(n);
271
+ if (!isUpperCase(char)) {
272
+ if (propertyName.charAt(n - 1) === " ") {
273
+ if (char !== " ") {
274
+ newPropertyName += char.toUpperCase();
275
+ }
276
+ } else {
277
+ newPropertyName += char;
278
+ }
279
+ } else {
280
+ if (propertyName.charAt(n - 1) !== " ") {
281
+ newPropertyName += " ";
282
+ }
283
+ newPropertyName += char;
284
+ const i = 1;
285
+ let nextChar = propertyName.charAt(n + i);
286
+ if (isUpperCase(nextChar)) {
287
+ do {
288
+ nextChar = propertyName.charAt(n + i);
289
+ if (isUpperCase(nextChar)) {
290
+ newPropertyName += nextChar;
291
+ n += 1;
292
+ }
293
+ } while (nextChar !== nextChar.toLowerCase());
294
+ }
295
+ }
296
+ }
297
+ return newPropertyName;
298
+ };
299
+
300
+ // src/transform/transformToolOutput.ts
301
+ var emptyObjectTransformValue = "no properties";
302
+ var emptyArrayTransformValue = "none";
303
+ var generateTabs = (tabCount) => {
304
+ let tabs = "";
305
+ for (let n = 0; n < tabCount; n++) {
306
+ tabs += " ";
307
+ }
308
+ return tabs;
309
+ };
310
+ var transformToolOutput = (args) => {
311
+ const { data, title, format } = args;
312
+ if (isPropertyTypeToBeIgnored(data)) {
313
+ return title ? `${transformTitle(title)}
314
+ ${emptyObjectTransformValue}` : emptyObjectTransformValue;
315
+ }
316
+ if (format === "json") {
317
+ if (title) {
318
+ return JSON.stringify({
319
+ [transformTitle(title)]: data
320
+ });
321
+ }
322
+ return JSON.stringify(data);
323
+ }
324
+ let transformedDataAggregate = "";
325
+ if (title) {
326
+ transformedDataAggregate = `${transformTitle(title)}
327
+ `;
328
+ }
329
+ const transformedData = transformData({ data, tabCount: -1 });
330
+ return transformedDataAggregate += transformedData ?? emptyObjectTransformValue;
331
+ };
332
+ var transformTitle = (title) => `${transformPropertyName(title).toUpperCase()}`;
333
+ var transformData = (args) => {
334
+ const { data, tabCount } = args;
335
+ if (isPropertyTypeToBeIgnored(data)) {
336
+ return null;
337
+ }
338
+ switch (typeof data) {
339
+ case "string": {
340
+ if (data === "") {
341
+ return '""';
342
+ }
343
+ return data;
344
+ }
345
+ case "number":
346
+ case "bigint": {
347
+ return data.toString();
348
+ }
349
+ case "boolean": {
350
+ return data ? "Yes" : "No";
351
+ }
352
+ case "object": {
353
+ if (data === null) {
354
+ return "null";
355
+ }
356
+ if (Array.isArray(data)) {
357
+ return handleArrayTransformation({ array: data, tabCount });
358
+ }
359
+ return handleObjectTransformation({
360
+ data,
361
+ tabCount
362
+ });
363
+ }
364
+ default:
365
+ return null;
366
+ }
367
+ };
368
+ var handleArrayTransformation = (args) => {
369
+ const { array, tabCount } = args;
370
+ if (array.length === 0) {
371
+ return emptyArrayTransformValue;
372
+ }
373
+ if (isArrayWithoutObjectsOrArrays(array)) {
374
+ const simpleTransformedArray = array.reduce(
375
+ (previousValue, currentValue) => {
376
+ const transformedCurrentValue = transformData({
377
+ data: currentValue,
378
+ tabCount
379
+ });
380
+ if (transformedCurrentValue === null) {
381
+ return previousValue;
382
+ }
383
+ if (previousValue) {
384
+ return `${previousValue}, ${transformedCurrentValue}`;
385
+ } else {
386
+ return transformedCurrentValue;
387
+ }
388
+ },
389
+ ""
390
+ );
391
+ return simpleTransformedArray === "" ? emptyArrayTransformValue : simpleTransformedArray;
392
+ }
393
+ let transformedData = "";
394
+ let unignoredArrayPropertyIndex = 0;
395
+ array.forEach((value) => {
396
+ if (!isPropertyTypeToBeIgnored(value)) {
397
+ transformedData += transformObjectOrArrayContent({
398
+ key: unignoredArrayPropertyIndex.toString(),
399
+ value,
400
+ tabCount
401
+ });
402
+ transformedData += "\n";
403
+ unignoredArrayPropertyIndex += 1;
404
+ }
405
+ });
406
+ if (transformedData === "") {
407
+ return emptyArrayTransformValue;
408
+ }
409
+ return transformedData.substring(0, transformedData.length - 1);
410
+ };
411
+ var handleObjectTransformation = (args) => {
412
+ const { data, tabCount } = args;
413
+ let transformedData = "";
414
+ Object.keys(data).forEach((key) => {
415
+ if (!isPropertyTypeToBeIgnored(data[key])) {
416
+ transformedData += transformObjectOrArrayContent({
417
+ key,
418
+ value: data[key],
419
+ tabCount: tabCount + 1
420
+ });
421
+ transformedData += "\n";
422
+ }
423
+ });
424
+ if (transformedData === "") {
425
+ return emptyObjectTransformValue;
426
+ }
427
+ return transformedData.substring(0, transformedData.length - 1);
428
+ };
429
+ var transformObjectOrArrayContent = (args) => {
430
+ const { key, value, tabCount } = args;
431
+ const transformedValue = transformData({
432
+ data: value,
433
+ // increase base indentation in arrays so child indices render under the parent label.
434
+ // Objects handle their own +1 indentation internally in handleObjectTransformation.
435
+ tabCount: Array.isArray(value) ? tabCount + 1 : tabCount
436
+ });
437
+ if (transformedValue === null) {
438
+ return "";
439
+ }
440
+ let transformedDataAggregate = `${generateTabs(tabCount)}${transformPropertyName(key)}:`;
441
+ if (typeof value !== "object" || transformedValue === "null" || transformedValue === emptyArrayTransformValue || transformedValue === emptyObjectTransformValue || Array.isArray(value) && isArrayWithoutObjectsOrArrays(value)) {
442
+ transformedDataAggregate += " ";
443
+ } else {
444
+ transformedDataAggregate += `
445
+ `;
446
+ }
447
+ return transformedDataAggregate += transformedValue;
448
+ };
449
+ var isArrayWithoutObjectsOrArrays = (array) => {
450
+ if (array.length === 0) {
451
+ return true;
452
+ }
453
+ let hasObjectOrArrayBeenFound = false;
454
+ for (let n = 0; n < array.length; n++) {
455
+ if (isObject(array[n]) || Array.isArray(array[n])) {
456
+ hasObjectOrArrayBeenFound = true;
457
+ n = array.length;
458
+ }
459
+ }
460
+ return !hasObjectOrArrayBeenFound;
461
+ };
462
+ var isPropertyTypeToBeIgnored = (data) => typeof data === "undefined" || typeof data === "function";
463
+ var isObject = (data) => typeof data === "object" && data !== null && !Array.isArray(data);
464
+ // Annotate the CommonJS export names for ESM import in node:
465
+ 0 && (module.exports = {
466
+ FieldFilteringHandler,
467
+ defaultFilteringRules,
468
+ defaultJsonRedactionText,
469
+ defaultUrlRedactionText,
470
+ generateQueryString,
471
+ isFieldFilteringManager,
472
+ isValidUrl,
473
+ normaliseUrl,
474
+ transformPropertyName,
475
+ transformToolOutput
476
+ });