1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const lodash_1 = require("lodash");
|
4 | const getArguments = require("function-arguments");
|
5 | const APIConfig_1 = require("./APIConfig");
|
6 | const crypto = require("crypto");
|
7 | const slugify_1 = require("slugify");
|
8 | slugify_1.default.extend({ "/": ":" });
|
9 | class APIUtils {
|
10 | static getRawTypeName(obj) {
|
11 | return Object.prototype.toString.call(obj).slice(8, -1);
|
12 | }
|
13 | static convertToType(value, convertToType) {
|
14 | if (lodash_1.isNil(convertToType)) {
|
15 | return value;
|
16 | }
|
17 | let convertedValue;
|
18 | let rawValueType = APIUtils.getRawTypeName(value);
|
19 |
|
20 | if (rawValueType === convertToType) {
|
21 | return value;
|
22 | }
|
23 |
|
24 | if (rawValueType === "Object" || rawValueType === "Array") {
|
25 | if (convertToType === "String") {
|
26 | try {
|
27 | return JSON.stringify(value);
|
28 | }
|
29 | catch (e) {
|
30 | }
|
31 | }
|
32 | return undefined;
|
33 | }
|
34 | switch (convertToType) {
|
35 | case "String": {
|
36 | convertedValue = lodash_1.toString(value);
|
37 | break;
|
38 | }
|
39 | case "Boolean": {
|
40 | convertedValue = APIUtils.toBoolean(value);
|
41 | break;
|
42 | }
|
43 | case "Number": {
|
44 | convertedValue = lodash_1.toNumber(value);
|
45 | break;
|
46 | }
|
47 | case "Array": {
|
48 | convertedValue = lodash_1.castArray(value);
|
49 | break;
|
50 | }
|
51 | case "Object": {
|
52 | try {
|
53 | convertedValue = JSON.parse(value);
|
54 | }
|
55 | catch (e) {
|
56 | convertedValue = value;
|
57 | }
|
58 | }
|
59 | }
|
60 | return convertedValue;
|
61 | }
|
62 | static toBoolean(input) {
|
63 | if (lodash_1.isBoolean(input)) {
|
64 | return input;
|
65 | }
|
66 | return (/(1|true|yes)/i).test(input);
|
67 | }
|
68 | static coalesce(...inputArgs) {
|
69 | for (let inputArg of inputArgs) {
|
70 |
|
71 | if (lodash_1.isNil(inputArg) || (lodash_1.isString(inputArg) && inputArg === "")) {
|
72 | continue;
|
73 | }
|
74 | return inputArg;
|
75 | }
|
76 | return null;
|
77 | }
|
78 | static getFunctionParamNames(fn) {
|
79 | return getArguments(fn);
|
80 | }
|
81 | static encrypt(text, password = APIConfig_1.APIConfig.ENCRYPTION_SECRET) {
|
82 | let iv = crypto.randomBytes(this._IV_LENGTH);
|
83 | let cipher = crypto.createCipheriv(this._CRYPTO_ALG, Buffer.from(password), iv);
|
84 | let encrypted = cipher.update(text);
|
85 | encrypted = Buffer.concat([encrypted, cipher.final()]);
|
86 | return iv.toString('base64') + ':' + encrypted.toString('base64');
|
87 | }
|
88 | static decrypt(text, password = APIConfig_1.APIConfig.ENCRYPTION_SECRET) {
|
89 | let textParts = text.split(':');
|
90 | let iv = Buffer.from(textParts.shift(), 'base64');
|
91 | let encryptedText = Buffer.from(textParts.join(':'), 'base64');
|
92 | let decipher = crypto.createDecipheriv(this._CRYPTO_ALG, Buffer.from(password), iv);
|
93 | let decrypted = decipher.update(encryptedText);
|
94 | decrypted = Buffer.concat([decrypted, decipher.final()]);
|
95 | return decrypted.toString();
|
96 | }
|
97 | static slugify(text) {
|
98 | return slugify_1.default(text);
|
99 | }
|
100 | static hashString(text) {
|
101 | let shasum = crypto.createHash(this._HASH_ALG);
|
102 | shasum.update(text);
|
103 | return shasum.digest('base64');
|
104 | }
|
105 | static hashMD5(text) {
|
106 | let md5 = crypto.createHash('md5');
|
107 | md5.update(text);
|
108 | return md5.digest('base64');
|
109 | }
|
110 | |
111 |
|
112 |
|
113 |
|
114 | static createExpirationInSeconds(expirationInSeconds) {
|
115 | return Math.floor(Date.now() / 1000) + expirationInSeconds;
|
116 | }
|
117 | }
|
118 | APIUtils._IV_LENGTH = 16;
|
119 | APIUtils._CRYPTO_ALG = 'aes-256-cbc';
|
120 | APIUtils._HASH_ALG = 'sha256';
|
121 | exports.APIUtils = APIUtils;
|
122 |
|
\ | No newline at end of file |