@wandzai/utils 1.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/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "@wandzai/utils",
3
+ "version": "1.0.0",
4
+ "description": "Common used utilities library",
5
+ "main": "src/index.ts",
6
+ "scripts": {
7
+ "build": "tsc && cp package.json ../../dist/libs/utils"
8
+ },
9
+ "author": "wandz-ai-team",
10
+ "license": "ISC"
11
+ }
package/src/gzip.ts ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Gzip single file or every file in given folder
3
+ * @file gzip.js
4
+ */
5
+
6
+ import {createGzip} from 'zlib';
7
+ import {createReadStream, createWriteStream} from 'fs';
8
+
9
+ /**
10
+ * @method gzipFile
11
+ * @param {String} fileIn
12
+ * @param {String} fileOut
13
+ */
14
+ export function gzipFile(fileIn: string, fileOut: string) {
15
+ return new Promise<void>((resolve, reject) => {
16
+ const inp = createReadStream(fileIn);
17
+ const out = createWriteStream(fileOut);
18
+ const gzip = createGzip();
19
+
20
+ inp.pipe(gzip).pipe(out).on('finish', function () {
21
+ resolve();
22
+ }).on('error', function (err) {
23
+ reject(err);
24
+ });
25
+ });
26
+ }
package/src/http.ts ADDED
@@ -0,0 +1,17 @@
1
+ import {Request} from 'express';
2
+
3
+ /**
4
+ * Extracts ip address from http request
5
+ * @param {Request} req - http request
6
+ */
7
+ export function getIpFromHttpRequest(req: Request): string {
8
+ return req.headers['x-forwarded-for']
9
+ ? String(req.headers['x-forwarded-for']).split(',')[0].trim()
10
+ : req.socket.remoteAddress;
11
+ };
12
+
13
+ export function buildUriBodyString(json: object): string {
14
+ return Object.entries(json)
15
+ .map(([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`)
16
+ .join('&');
17
+ };
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './http';
2
+ export * from './logger';
3
+ export * from './gzip';
4
+ export * from './snowflake';
package/src/logger.ts ADDED
@@ -0,0 +1,59 @@
1
+ import {v4 as uuidv4} from 'uuid';
2
+ import * as winston from 'winston';
3
+ import {Logger} from 'winston';
4
+
5
+ const {createLogger, format, transports} = winston;
6
+
7
+ export interface IMeta {
8
+ clientTag?: string;
9
+ domain?: string;
10
+ campaignId?: string;
11
+ userId?: string;
12
+ trackingGuid?: string;
13
+ }
14
+
15
+ export interface IEnrichedLogger extends Logger {
16
+ createChildLogger(meta?: IMeta): Logger;
17
+ }
18
+
19
+ const createEnrichedLogger = (serviceName: string) => {
20
+ const logger = createLogger({
21
+ level: 'debug',
22
+ defaultMeta: {service: serviceName},
23
+ transports: [
24
+ process.env.NX_LOG_READABLE === 'true'
25
+ ? new transports.Console({
26
+ format: format.simple(),
27
+ })
28
+ : new transports.Console({
29
+ format: format.json(),
30
+ }),
31
+ ],
32
+ });
33
+
34
+ return {
35
+ getLogger: () => {
36
+ return logger;
37
+ },
38
+ createChildLogger: (meta?: IMeta) => {
39
+ let combinedMeta = {trackingGuid: uuidv4()};
40
+ if (meta) combinedMeta = {...combinedMeta, ...meta};
41
+ return logger.child(combinedMeta);
42
+ },
43
+ };
44
+ };
45
+
46
+ export function customLogger(serviceName: string): IEnrichedLogger {
47
+ const enrichedLogger = createEnrichedLogger(serviceName);
48
+ const handler = {
49
+ get: function (target, prop, receiver) {
50
+ if (prop === 'createChildLogger') {
51
+ return target.createChildLogger;
52
+ }
53
+
54
+ return target.getLogger()[prop];
55
+ },
56
+ };
57
+
58
+ return new Proxy(enrichedLogger, handler);
59
+ }
@@ -0,0 +1,9 @@
1
+ /** This method will build this structure of procedures:
2
+ * KEY => VALUE
3
+ * This is really better than calling procedure with input of arguments array.. */
4
+ export const buildProcedureJsonString = (
5
+ procedure: string,
6
+ params: object
7
+ ): string => {
8
+ return `CALL ${procedure}(${Object.entries(params).map(([k, v]) => `${k} => \'${v}\'`).join(',')});`;
9
+ };
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "outDir": "../../dist/libs/utils"
6
+ },
7
+ "include": ["src/**/*", "package.json"],
8
+ "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
9
+ }