90dc-core 1.2.16 → 1.3.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 CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "90dc-core",
3
- "version": "1.2.16",
3
+ "version": "1.3.0",
4
4
  "description": "A package that contains utils and interfaces used to create 90dc",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "devDependencies": {
8
8
  "@swc/cli": "^0.1.62",
9
9
  "@swc/core": "^1.3.36",
10
+ "@types/debug": "^4.1.12",
10
11
  "@types/jest": "^29.4.0",
11
12
  "@types/node": "^18.14.1",
12
13
  "@types/validator": "^13.7.12",
@@ -20,6 +21,8 @@
20
21
  },
21
22
  "dependencies": {
22
23
  "@typescript-eslint/parser": "^5.53.0",
24
+ "axios": "^1.6.2",
25
+ "debug": "^4.3.4",
23
26
  "eslint": "^8.34.0",
24
27
  "sequelize-typescript": "^2.1.5",
25
28
  "winston": "^3.8.2"
@@ -1,8 +1,8 @@
1
- import { Column, DataType, Default, Model, Table } from 'sequelize-typescript';
2
- import { UsersFriends } from './UsersFriends';
1
+ import { Column, DataType, Default, Index, Model, Table } from "sequelize-typescript";
3
2
 
4
3
  @Table
5
4
  export class PersistedUser extends Model {
5
+
6
6
  @Default(DataType.UUIDV4)
7
7
  @Column({
8
8
  type: DataType.UUID,
@@ -12,6 +12,7 @@ export class PersistedUser extends Model {
12
12
  })
13
13
  declare userUuid: string;
14
14
 
15
+ @Index
15
16
  @Column({ type: DataType.TEXT, allowNull: false, unique: false })
16
17
  declare email: string;
17
18
 
@@ -93,17 +94,4 @@ export class PersistedUser extends Model {
93
94
  @Column({ type: DataType.JSONB, allowNull: true })
94
95
  declare subscription: object;
95
96
 
96
- async $addFriend(friendUuid: string) {
97
- await UsersFriends.create({
98
- userId: this.userUuid,
99
- friendId: friendUuid,
100
- createDate: new Date()
101
- });
102
-
103
- await UsersFriends.create({
104
- userId: friendUuid,
105
- friendId: this.userUuid,
106
- createDate: new Date()
107
- });
108
- }
109
97
  }
@@ -1,54 +1,78 @@
1
- import winston from 'winston'
1
+ /* eslint-disable @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access */
2
+ import axios from "axios";
3
+ import Debug, { Debugger } from "debug";
4
+ import * as crypto from "crypto";
2
5
 
3
- class Logger {
6
+ export class Log {
7
+ private static instance: Log | null = null;
8
+ private readonly debugLib: Debugger;
4
9
 
5
- public static getLogger(serviceName: string){
6
- const levels = {
7
- error: 0,
8
- warn: 1,
9
- info: 2,
10
- http: 3,
11
- debug: 4,
12
- }
10
+ constructor(debug: Debugger) {
11
+ this.debugLib = debug;
12
+ }
13
13
 
14
- const level = () => {
15
- const env = process.env.NODE_ENV || 'development'
16
- const isDevelopment = env === 'development'
17
- return isDevelopment ? 'debug' : 'warn'
14
+ public static getInstance(): Log {
15
+ if (!Log.instance) {
16
+ const debug: Debugger = Debug(
17
+ "@swish:" + (process.env.npm_package_name || "package-name-not-specified")
18
+ );
19
+ Log.instance = new Log(debug);
18
20
  }
19
21
 
20
- const colors = {
21
- error: 'red',
22
- warn: 'yellow',
23
- info: 'magenta',
24
- http: 'magenta',
25
- debug: 'white',
26
- serviceName: 'magenta'
27
- }
22
+ return Log.instance;
23
+ }
24
+
25
+ public static extendInstance(name: string) {
26
+ return this.getInstance().extend(name);
27
+ }
28
+
29
+ public debug(message: string): void {
30
+ this.debugLib(message);
31
+ }
28
32
 
29
- winston.addColors(colors)
33
+ public info(message: string): void {
34
+ // eslint-disable-next-line no-console
35
+ this.debugLib.log = console.info.bind(console);
36
+ this.debugLib(message);
37
+ }
30
38
 
31
- const format: winston.Logform.Format = winston.format.combine(
32
- winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss:ms' }),
33
- winston.format.colorize({ all: true }),
34
- winston.format.printf(
35
- // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
36
- (info) => `\n @${serviceName.toUpperCase()}: ${info.message}\n`,
37
- ),
38
- )
39
+ public warn(message: string): void {
40
+ const warnDebug: Debugger = this.debugLib.extend("warning");
41
+ // eslint-disable-next-line no-console
42
+ warnDebug.log = console.warn.bind(console);
43
+ warnDebug(message);
44
+ }
39
45
 
40
- const transports = [
41
- new winston.transports.Console(),
42
- ]
43
- return winston.createLogger({
44
- level: level(),
45
- levels,
46
- format,
47
- transports,
48
- })
46
+ public error(message: string | unknown, error?: unknown): void {
47
+ const errorDebug: Debugger = this.debugLib.extend("error");
48
+ // eslint-disable-next-line no-console
49
+ errorDebug.log = console.error.bind(console);
50
+ errorDebug(message, error);
51
+ }
49
52
 
53
+ public catchError(error: unknown): void {
54
+ if (axios.isAxiosError(error)) {
55
+ this.error(error.toJSON());
56
+ }
57
+ if (typeof error === "string") {
58
+ error.toUpperCase(); // works, `e` narrowed to string
59
+ this.error(error);
60
+ } else if (error instanceof Error) {
61
+ error.message; // works, `e` narrowed to Error
62
+ this.error(error.message);
63
+ }
50
64
  }
51
- }
52
65
 
66
+ public catchErrorAndLogUuid(error: unknown): string {
67
+ const uuid: string = crypto.randomUUID();
68
+ this.error(`ERROR UUID: ${uuid}`);
69
+ this.catchError(error);
70
+
71
+ return uuid;
72
+ }
53
73
 
54
- export default Logger
74
+ public extend(extensionName: string): Log {
75
+ const debugExtension: Debugger = this.debugLib.extend(extensionName);
76
+ return new Log(debugExtension);
77
+ }
78
+ }