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