@serhii.mazur/directus-gu-logs 1.0.10 → 1.0.11

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.
@@ -19,5 +19,6 @@ export declare class Logs {
19
19
  printLogs(functionName: string, error: string, notifySlack?: boolean): Promise<void>;
20
20
  createActivity(action: string, collection: string, id: PrimaryKey): Promise<void>;
21
21
  createNotification(message: string, customSubject?: string | null, recipientOverride?: string | null, collection?: string | null, item?: string | null, notifySlack?: boolean): Promise<void>;
22
+ notifyEmail(message: string, customSubject?: string | null, recipientOverride?: string | null, collection?: string | null, item?: string | null): Promise<void>;
22
23
  notifySlack(message: string, customSubject?: string | null): Promise<void>;
23
24
  }
@@ -94,6 +94,19 @@ export class Logs {
94
94
  }
95
95
  }
96
96
  async createNotification(message, customSubject = null, recipientOverride = null, collection = null, item = null, notifySlack = true) {
97
+ const slackEnabled = await this.slack.isEnabled();
98
+ if (slackEnabled && notifySlack) {
99
+ try {
100
+ await this.notifySlack(`*Error:* ${message}`, customSubject);
101
+ return;
102
+ }
103
+ catch (err) {
104
+ this.context.logger.error({ msg: "❌ Slack notification failed", error: err });
105
+ }
106
+ }
107
+ await this.notifyEmail(message, customSubject, recipientOverride, collection, item);
108
+ }
109
+ async notifyEmail(message, customSubject = null, recipientOverride = null, collection = null, item = null) {
97
110
  const meta = await this.getProjectMeta();
98
111
  await this.directus.notify(message, meta, {
99
112
  subject: customSubject,
@@ -101,15 +114,6 @@ export class Logs {
101
114
  collection,
102
115
  item,
103
116
  });
104
- try {
105
- if (notifySlack) {
106
- const meta = await this.getProjectMeta();
107
- await this.slack.notify(`*Error:* ${message}`, meta, customSubject);
108
- }
109
- }
110
- catch (err) {
111
- this.context.logger.error({ msg: "Slack failed", error: err });
112
- }
113
117
  }
114
118
  async notifySlack(message, customSubject = null) {
115
119
  const meta = await this.getProjectMeta();
@@ -10,6 +10,7 @@ export declare class SlackNotifier {
10
10
  private buildBlocks;
11
11
  send(payload: SlackPayload): Promise<void>;
12
12
  notify(message: string, meta: ProjectMeta, subject?: string | null): Promise<void>;
13
+ isEnabled(): Promise<boolean>;
13
14
  private getConfig;
14
15
  clearConfigCache(): void;
15
16
  }
@@ -20,6 +20,7 @@ export class SlackNotifier {
20
20
  }
21
21
  buildBlocks(title, envIcon, message, meta) {
22
22
  return [
23
+ { type: "divider" },
23
24
  {
24
25
  type: "header",
25
26
  text: { type: "plain_text", text: `${envIcon} ${title}: ${this.extension}` },
@@ -33,8 +34,8 @@ export class SlackNotifier {
33
34
  { type: "mrkdwn", text: `*Backend*\n${meta.backendUrl}` },
34
35
  ],
35
36
  },
36
- { type: "divider" },
37
37
  { type: "section", text: { type: "mrkdwn", text: message } },
38
+ { type: "divider" },
38
39
  ];
39
40
  }
40
41
  async send(payload) {
@@ -71,6 +72,10 @@ export class SlackNotifier {
71
72
  blocks: this.buildBlocks(title, envIcon, message, meta),
72
73
  });
73
74
  }
75
+ async isEnabled() {
76
+ const config = await this.getConfig();
77
+ return Boolean(config?.slack_notifications && config?.slack_webhook_url);
78
+ }
74
79
  async getConfig() {
75
80
  const now = Date.now();
76
81
  if (this.configCache && now - this.configCachedAt < this.CONFIG_CACHE_TTL) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serhii.mazur/directus-gu-logs",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Reusable logging utility for Directus extensions — persists error entries, tracks activity, and sends notifications via Slack and Directus inbox.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -118,6 +118,27 @@ export class Logs {
118
118
  collection: string | null = null,
119
119
  item: string | null = null,
120
120
  notifySlack = true,
121
+ ): Promise<void> {
122
+ const slackEnabled = await this.slack.isEnabled();
123
+
124
+ if (slackEnabled && notifySlack) {
125
+ try {
126
+ await this.notifySlack(`*Error:* ${message}`, customSubject);
127
+ return;
128
+ } catch (err) {
129
+ this.context.logger.error({ msg: "❌ Slack notification failed", error: err });
130
+ }
131
+ }
132
+
133
+ await this.notifyEmail(message, customSubject, recipientOverride, collection, item);
134
+ }
135
+
136
+ async notifyEmail(
137
+ message: string,
138
+ customSubject: string | null = null,
139
+ recipientOverride: string | null = null,
140
+ collection: string | null = null,
141
+ item: string | null = null,
121
142
  ): Promise<void> {
122
143
  const meta = await this.getProjectMeta();
123
144
 
@@ -127,15 +148,6 @@ export class Logs {
127
148
  collection,
128
149
  item,
129
150
  });
130
-
131
- try {
132
- if (notifySlack) {
133
- const meta = await this.getProjectMeta();
134
- await this.slack.notify(`*Error:* ${message}`, meta, customSubject);
135
- }
136
- } catch (err) {
137
- this.context.logger.error({ msg: "Slack failed", error: err });
138
- }
139
151
  }
140
152
 
141
153
  async notifySlack(message: string, customSubject: string | null = null): Promise<void> {
@@ -26,6 +26,7 @@ export class SlackNotifier {
26
26
 
27
27
  private buildBlocks(title: string, envIcon: string, message: string, meta: ProjectMeta) {
28
28
  return [
29
+ { type: "divider" },
29
30
  {
30
31
  type: "header",
31
32
  text: { type: "plain_text", text: `${envIcon} ${title}: ${this.extension}` },
@@ -39,8 +40,8 @@ export class SlackNotifier {
39
40
  { type: "mrkdwn", text: `*Backend*\n${meta.backendUrl}` },
40
41
  ],
41
42
  },
42
- { type: "divider" },
43
43
  { type: "section", text: { type: "mrkdwn", text: message } },
44
+ { type: "divider" },
44
45
  ];
45
46
  }
46
47
 
@@ -85,6 +86,11 @@ export class SlackNotifier {
85
86
  });
86
87
  }
87
88
 
89
+ async isEnabled(): Promise<boolean> {
90
+ const config = await this.getConfig();
91
+ return Boolean(config?.slack_notifications && config?.slack_webhook_url);
92
+ }
93
+
88
94
  private async getConfig(): Promise<{ slack_webhook_url: string | null; slack_notifications: boolean } | undefined> {
89
95
  const now = Date.now();
90
96