vtlab-generic-functions 1.0.23 → 1.0.24
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/lambdaClass/index.js +2 -2
- package/package.json +1 -1
- package/utils/index.js +83 -0
package/lambdaClass/index.js
CHANGED
|
@@ -57,9 +57,9 @@ module.exports = class Lambda {
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
s3 = {
|
|
60
|
-
upload: async (data, path) => {
|
|
60
|
+
upload: async (data, path, options = {}) => {
|
|
61
61
|
return await s3bucket.uploadToS3(path, data, {
|
|
62
|
-
ContentEncoding: "base64", ContentType: "image/png",
|
|
62
|
+
ContentEncoding: options.contentEncoding || "base64", ContentType: options.ContentType || "image/png",
|
|
63
63
|
});
|
|
64
64
|
}, download: async (path) => {
|
|
65
65
|
return await s3bucket.downloadFileFromS3(path);
|
package/package.json
CHANGED
package/utils/index.js
CHANGED
|
@@ -303,7 +303,87 @@ const getValueOrNullControl = (
|
|
|
303
303
|
throw new Error(`Failed trying to get value with path ${path}`);
|
|
304
304
|
}
|
|
305
305
|
};
|
|
306
|
+
/**
|
|
307
|
+
* Retrieves a value from a nested object structure based on a given path,
|
|
308
|
+
* with an option to fall back to a default value if the path does not yield a valid result.
|
|
309
|
+
*
|
|
310
|
+
* @param {Object} [value={}] - The object from which to retrieve the value. Defaults to an empty object if not provided.
|
|
311
|
+
* @param {Array<string>} [paths=[]] - An array of string paths, each representing a path to a nested value in the object.
|
|
312
|
+
* The function tries each path in sequence until a valid result is obtained.
|
|
313
|
+
* @param {*} [defaultFallback=null] - The default value to return if no valid value is found at any of the specified paths.
|
|
314
|
+
* Defaults to null if not provided.
|
|
315
|
+
* @returns {*} - Returns the value found at the first valid path, the default fallback value if none of the paths are valid,
|
|
316
|
+
* or throws an error if no paths are provided or if no fallback is set when a required path yields null.
|
|
317
|
+
* @throws {Error} - Throws an error if the first path is null (indicating no paths were provided), if a required value
|
|
318
|
+
* at a path is null and no more paths are left to check (without a valid defaultFallback), or if any
|
|
319
|
+
* other unexpected error occurs during execution.
|
|
320
|
+
*/
|
|
321
|
+
const getValueOrNullControlWithFallback = (
|
|
322
|
+
value = {},
|
|
323
|
+
paths = [],
|
|
324
|
+
defaultFallback = null
|
|
325
|
+
) => {
|
|
326
|
+
try {
|
|
327
|
+
const path = paths[0];
|
|
328
|
+
if(path == null) throw new Error(`Path is required`);
|
|
329
|
+
const result = getValueOrNull(value, path);
|
|
330
|
+
if(result == null && paths.length === 1 && defaultFallback != null) return defaultFallback;
|
|
331
|
+
if(result == null && paths.length === 1) throw new Error(`Value path ${path} is required`);
|
|
332
|
+
if (result) return result;
|
|
333
|
+
if(paths.length === 1) return defaultFallback;
|
|
334
|
+
return getValueOrNullControlWithFallback(value, paths.slice(1), defaultFallback);
|
|
335
|
+
} catch (e) {
|
|
336
|
+
throw e || new Error(`Failed trying to get value with paths ${paths}`);
|
|
337
|
+
}
|
|
338
|
+
};
|
|
306
339
|
|
|
340
|
+
/**
|
|
341
|
+
* Determines the MIME type of a file encoded in base64 format based on the file's signature.
|
|
342
|
+
*
|
|
343
|
+
* @param {string} base64 - The base64 encoded string of the file.
|
|
344
|
+
* @returns {string} - The MIME type of the file. Returns 'application/octet-stream' if the type cannot be determined.
|
|
345
|
+
*/
|
|
346
|
+
const getMimeTypeFromBase64 = (base64,defaultType = 'application/pdf') => {
|
|
347
|
+
const signatures = {
|
|
348
|
+
JVBERi0: 'application/pdf',
|
|
349
|
+
R0lGOD: 'image/gif',
|
|
350
|
+
iVBOR: 'image/png',
|
|
351
|
+
'/9j/': 'image/jpg',
|
|
352
|
+
};
|
|
353
|
+
|
|
354
|
+
for (const sign in signatures) {
|
|
355
|
+
if (base64.includes(sign)) return signatures[sign];
|
|
356
|
+
}
|
|
357
|
+
return defaultType;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Determines the type of freight charges based on the given INCOTERM.
|
|
363
|
+
*
|
|
364
|
+
* @param {string} incoterm - The INCOTERM code based on which the freight charge type is determined.
|
|
365
|
+
* @returns {string} - Returns "D" for direct charges or "P" for prepaid charges.
|
|
366
|
+
*/
|
|
367
|
+
const getTipoPortes = incoterm => {
|
|
368
|
+
switch (String(incoterm).toUpperCase()) {
|
|
369
|
+
case "EXW":
|
|
370
|
+
case "FAS":
|
|
371
|
+
case "FOB":
|
|
372
|
+
return "D";
|
|
373
|
+
case "FCA":
|
|
374
|
+
case "CPT":
|
|
375
|
+
case "CIP":
|
|
376
|
+
case "DPU":
|
|
377
|
+
case "DAP":
|
|
378
|
+
case "DDP":
|
|
379
|
+
case "CFR":
|
|
380
|
+
case "CIF":
|
|
381
|
+
return "P";
|
|
382
|
+
default:
|
|
383
|
+
return "P";
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
307
387
|
// use it to create safe fields for csv files writing
|
|
308
388
|
const generateCSVField = (
|
|
309
389
|
obj,
|
|
@@ -576,4 +656,7 @@ module.exports = {
|
|
|
576
656
|
generateUuid,
|
|
577
657
|
mergePDF,
|
|
578
658
|
getMessageAndCodeFromError,
|
|
659
|
+
getValueOrNullControlWithFallback,
|
|
660
|
+
getMimeTypeFromBase64,
|
|
661
|
+
getTipoPortes,
|
|
579
662
|
};
|