mynth-logger 2.0.6 → 2.0.7

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.
@@ -1,22 +1,20 @@
1
1
  import { stringify } from "@ungap/structured-clone/json";
2
+ import { type } from "arktype";
3
+ const ErrorType = type({
4
+ message: "string",
5
+ "stack?": "string",
6
+ });
2
7
  const formatItem = (item) => {
3
- if (typeof item === "undefined") {
8
+ if (typeof item === "undefined")
4
9
  return "undefined";
5
- }
6
10
  // Remove colors from strings
7
- if (typeof item === "string") {
11
+ if (typeof item === "string")
8
12
  // eslint-disable-next-line no-control-regex
9
13
  return item.replace(/\x1b\[[0-9;]*m/g, "");
10
- }
11
14
  // Check if this is an Error
12
- if (item &&
13
- typeof item === "object" &&
14
- "message" in item &&
15
- typeof item.message === "string")
16
- return item.message;
17
- // Check if this is a string
18
- if (typeof item === "string")
19
- return item;
15
+ const error = ErrorType(item);
16
+ if (!(error instanceof type.errors))
17
+ return error.stack || error.message;
20
18
  const stringified = (() => {
21
19
  try {
22
20
  return stringify(item);
@@ -5,6 +5,9 @@ const setupLogging = () => {
5
5
  const consola = createConsola({ fancy: true, level: 5 });
6
6
  if (process.env.NODE_ENV === "production")
7
7
  consola.setReporters([DatadogReporter]);
8
+ // Set Discord reporter as first so it can remove
9
+ // Discord-related config before other reporters process the
10
+ // log
8
11
  consola.setReporters([DiscordReporter, ...consola.options.reporters]);
9
12
  consola.wrapConsole();
10
13
  return consola;
@@ -1,7 +1,6 @@
1
1
  import { format } from "../format.js";
2
2
  import { type } from "arktype";
3
- import axios from "axios";
4
- import axiosRetry, { exponentialDelay } from "axios-retry";
3
+ import got from "got";
5
4
  const Discord = type({
6
5
  discord: type("boolean").narrow((v) => v),
7
6
  color: "string",
@@ -30,13 +29,8 @@ const getDiscord = (args) => {
30
29
  return [NullDiscord, args];
31
30
  };
32
31
  const sendToDiscord = async (description, options) => {
33
- const axiosInstance = axios.create();
34
- axiosRetry(axiosInstance, {
35
- retries: 3,
36
- retryDelay: exponentialDelay,
37
- });
38
- try {
39
- await axiosInstance.post(options.webhookUrl, {
32
+ const data = {
33
+ json: {
40
34
  embeds: [
41
35
  {
42
36
  title: options.title,
@@ -44,7 +38,11 @@ const sendToDiscord = async (description, options) => {
44
38
  color: options.color,
45
39
  },
46
40
  ],
47
- });
41
+ },
42
+ retry: { limit: 5 },
43
+ };
44
+ try {
45
+ await got.post(options.webhookUrl, data);
48
46
  }
49
47
  catch (error) {
50
48
  console.error("Unable to send message to Discord", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mynth-logger",
3
- "version": "2.0.6",
3
+ "version": "2.0.7",
4
4
  "description": "Package to format logs for mynth microservices.",
5
5
  "main": "dist/src/index.js",
6
6
  "typings": "dist/src/index.d.ts",
@@ -28,9 +28,8 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@ungap/structured-clone": "^1.2.0",
31
- "axios": "^1.7.2",
32
- "axios-retry": "^4.3.0",
33
- "consola": "^3.2.3"
31
+ "consola": "^3.2.3",
32
+ "got": "^13.0.0"
34
33
  },
35
34
  "devDependencies": {
36
35
  "@types/node": "^20.12.12",
package/src/format.ts CHANGED
@@ -1,27 +1,22 @@
1
1
  import { stringify } from "@ungap/structured-clone/json";
2
+ import { type } from "arktype";
3
+
4
+ const ErrorType = type({
5
+ message: "string",
6
+ "stack?": "string",
7
+ });
2
8
 
3
9
  const formatItem = (item: unknown): string => {
4
- if (typeof item === "undefined") {
5
- return "undefined";
6
- }
10
+ if (typeof item === "undefined") return "undefined";
7
11
 
8
12
  // Remove colors from strings
9
- if (typeof item === "string") {
13
+ if (typeof item === "string")
10
14
  // eslint-disable-next-line no-control-regex
11
15
  return item.replace(/\x1b\[[0-9;]*m/g, "");
12
- }
13
16
 
14
17
  // Check if this is an Error
15
- if (
16
- item &&
17
- typeof item === "object" &&
18
- "message" in item &&
19
- typeof item.message === "string"
20
- )
21
- return item.message;
22
-
23
- // Check if this is a string
24
- if (typeof item === "string") return item;
18
+ const error = ErrorType(item);
19
+ if (!(error instanceof type.errors)) return error.stack || error.message;
25
20
 
26
21
  const stringified = (() => {
27
22
  try {
package/src/logging.ts CHANGED
@@ -1,16 +1,22 @@
1
- import { createConsola } from "consola";
1
+ import { createConsola, ConsolaOptions } from "consola";
2
2
  import DatadogReporter from "./reporters/datadog.js";
3
3
  import DiscordReporter from "./reporters/discord.js";
4
4
 
5
5
  const setupLogging = () => {
6
- const consola = createConsola({ fancy: true, level: 5 });
6
+ const consola = createConsola({ fancy: true, level: 5 } as Options);
7
7
 
8
8
  if (process.env.NODE_ENV === "production")
9
9
  consola.setReporters([DatadogReporter]);
10
+
11
+ // Set Discord reporter as first so it can remove
12
+ // Discord-related config before other reporters process the
13
+ // log
10
14
  consola.setReporters([DiscordReporter, ...consola.options.reporters]);
11
15
 
12
16
  consola.wrapConsole();
13
17
  return consola;
14
18
  };
15
19
 
20
+ type Options = ConsolaOptions & { fancy?: boolean };
21
+
16
22
  export { setupLogging };
@@ -1,8 +1,7 @@
1
1
  import { LogObject } from "consola";
2
2
  import { format } from "../format.js";
3
3
  import { type } from "arktype";
4
- import axios from "axios";
5
- import axiosRetry, { exponentialDelay } from "axios-retry";
4
+ import got from "got";
6
5
 
7
6
  const Discord = type({
8
7
  discord: type("boolean").narrow((v) => v),
@@ -42,14 +41,8 @@ const getDiscord = (args: unknown[]): [Discord | NullDiscord, unknown[]] => {
42
41
  };
43
42
 
44
43
  const sendToDiscord = async (description: string, options: Discord) => {
45
- const axiosInstance = axios.create();
46
- axiosRetry(axiosInstance, {
47
- retries: 3,
48
- retryDelay: exponentialDelay,
49
- });
50
-
51
- try {
52
- await axiosInstance.post(options.webhookUrl, {
44
+ const data = {
45
+ json: {
53
46
  embeds: [
54
47
  {
55
48
  title: options.title,
@@ -57,7 +50,12 @@ const sendToDiscord = async (description: string, options: Discord) => {
57
50
  color: options.color,
58
51
  },
59
52
  ],
60
- });
53
+ },
54
+ retry: { limit: 5 },
55
+ };
56
+
57
+ try {
58
+ await got.post(options.webhookUrl, data);
61
59
  } catch (error) {
62
60
  console.error("Unable to send message to Discord", error);
63
61
  }
package/tsconfig.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "baseUrl": "./src",
4
4
  "target": "es2022",
5
5
  "module": "es2022",
6
- "moduleResolution": "Node",
6
+ "moduleResolution": "bundler",
7
7
  "strict": true,
8
8
  "esModuleInterop": true,
9
9
  "skipLibCheck": true,