plutin 1.1.3 → 1.2.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/dist/index.cjs +30 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -1
- package/dist/infra/adapters/notifications/notification-factory.cjs +219 -0
- package/dist/infra/adapters/notifications/notification-factory.cjs.map +1 -0
- package/dist/infra/adapters/notifications/notification-factory.d.cts +18 -0
- package/dist/infra/adapters/notifications/notification-factory.d.ts +18 -0
- package/dist/infra/adapters/notifications/notification-factory.js +184 -0
- package/dist/infra/adapters/notifications/notification-factory.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/infra/adapters/notifications/notification-factory.ts","../../../../src/core/decorators/dependency-container.ts","../../../../src/infra/adapters/notifications/discord.ts","../../../../src/infra/adapters/notifications/in-memory.ts","../../../../src/infra/adapters/notifications/sentry.ts"],"sourcesContent":["import type { z } from 'zod'\n\nimport type { baseEnvSchema } from '../../env'\nimport { DiscordNotifier } from './discord'\nimport { NotificationErrorInMemory } from './in-memory'\nimport { SentryNotifier } from './sentry'\n\ntype OptionsNotifications = 'console' | 'discord' | 'sentry'\ntype EnvironmentEnum = z.infer<typeof baseEnvSchema>['ENVIRONMENT']\n\ntype Props = {\n local?: OptionsNotifications\n test?: OptionsNotifications\n development?: OptionsNotifications\n staging?: OptionsNotifications\n production?: OptionsNotifications\n}\n\nexport class NotificationFactory {\n static define(env: EnvironmentEnum, definitions?: Props): any {\n const defaultDefinition = {\n test: this.defineProvider(definitions?.local ?? 'console'),\n development: this.defineProvider(definitions?.local ?? 'console'),\n staging: this.defineProvider(definitions?.local ?? 'discord'),\n production: this.defineProvider(definitions?.local ?? 'sentry'),\n }\n\n return defaultDefinition[env]\n }\n\n private static defineProvider(provider: OptionsNotifications) {\n switch (provider) {\n case 'console':\n return NotificationErrorInMemory\n case 'discord':\n return DiscordNotifier\n case 'sentry':\n return SentryNotifier\n default:\n return NotificationErrorInMemory\n }\n }\n}\n","import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","import { Inject } from 'core/decorators/dependency-container'\nimport { MessageBuilder, Webhook } from 'discord-webhook-node'\n\nimport type { ContextError } from '../../../core/http/base-controller'\nimport type { IErrorNotifier } from '../../../core/http/error-notifier'\n\ntype DiscordOptions = {\n url: string\n env: string\n}\n\nexport class DiscordNotifier implements IErrorNotifier {\n private webhook: Webhook\n\n constructor(\n @Inject('DiscordConfig') private readonly options: DiscordOptions\n ) {\n this.webhook = new Webhook(this.options.url)\n }\n\n async notify(error: Error, context: ContextError): Promise<void> {\n const embed = new MessageBuilder()\n .setTitle('🚨 Error')\n .addField('Message', `\\`\\`\\`${error.message.slice(0, 300)}\\`\\`\\``)\n .addField(\n 'Route:',\n `\\`[${context?.request?.method}] ${context?.request?.url}\\``,\n true\n )\n .addField(\n 'Params:',\n '```json\\n' +\n JSON.stringify(context?.request?.params, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Query:',\n '```json\\n' +\n JSON.stringify(context?.request?.query, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Headers:',\n '```json\\n' +\n JSON.stringify(context?.request?.headers, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Body:',\n '```json\\n' + JSON.stringify(context?.request?.body, null, 2) + '\\n```'\n )\n .addField(\n 'Stack Trace:',\n '```' + (error.stack || 'No stack provided').slice(0, 900) + '```'\n )\n .setFooter(`Env: ${this.options.env || 'development'}`)\n .setTimestamp()\n\n await this.webhook.send(embed)\n }\n}\n","import type { ContextError } from '../../../core/http/base-controller'\nimport type { IErrorNotifier } from '../../../core/http/error-notifier'\n\nexport class NotificationErrorInMemory implements IErrorNotifier {\n public errors: any[] = []\n\n async notify(error: Error, context?: ContextError): Promise<void> {\n console.log('NOTIFICATION ERROR: ', error)\n\n this.errors.push({\n error: error.message,\n context,\n })\n }\n}\n","import * as Sentry from '@sentry/node'\nimport type { ContextError } from 'core/http/base-controller'\n\nimport { Inject } from '../../../core/decorators/dependency-container'\nimport { IErrorNotifier } from '../../../core/http/error-notifier'\n\ntype SentryOptions = {\n dsn: string\n environment: string\n}\n\nexport class SentryNotifier implements IErrorNotifier {\n constructor(@Inject('SentryConfig') private readonly options: SentryOptions) {\n Sentry.init({\n dsn: this.options.dsn,\n environment: this.options.environment,\n attachStacktrace: true,\n tracesSampleRate: this.options.environment === 'production' ? 0.1 : 1.0,\n maxBreadcrumbs: 100,\n debug: this.options.environment !== 'production',\n })\n }\n\n async notify(error: Error, context: ContextError): Promise<void> {\n Sentry.withScope((scope) => {\n scope.setLevel('error')\n\n if (context?.env) scope.setTag('env', context.env)\n\n if (context?.user) {\n scope.setUser({\n id: context.user.id,\n username: context.user.name,\n email: context.user.email,\n })\n }\n\n if (context?.request) {\n const { body, query, params, headers, method, url, requestId } =\n context.request\n\n scope.setContext('http', {\n method,\n requestId,\n url,\n headers,\n query,\n body,\n params,\n })\n }\n\n Sentry.captureException(error)\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA;;;;;;;ACHA,8BAAO;AA4EA,SAASA,OAAOC,OAAa;AAClC,SAAO,CACLC,QACAC,cACAC,mBAAAA;AAEA,UAAMC,cACJ,OAAOH,WAAW,aAAaA,SAASA,OAAOG;AAEjD,UAAMC,yBACJC,QAAQC,eAAe,iBAAiBH,WAAAA,KAAgB,CAAC;AAE3DC,2BAAuBF,cAAAA,IAAkBH;AAEzCM,YAAQE,eAAe,iBAAiBH,wBAAwBD,WAAAA;EAClE;AACF;AAhBgBL;;;AC3EhB,kCAAwC;;;;;;;;;;;;;;;;;;;;;;;AAUjC,IAAMU,kBAAN,MAAMA;SAAAA;;;;EACHC;EAERC,YAC4CC,SAC1C;SAD0CA,UAAAA;AAE1C,SAAKF,UAAU,IAAIG,oCAAQ,KAAKD,QAAQE,GAAG;EAC7C;EAEA,MAAMC,OAAOC,OAAcC,SAAsC;AAC/D,UAAMC,QAAQ,IAAIC,2CAAAA,EACfC,SAAS,iBAAA,EACTC,SAAS,WAAW,SAASL,MAAMM,QAAQC,MAAM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AcC,SAAsC;AAC/DV,IAAOW,iBAAU,CAACC,UAAAA;AAChBA,YAAMC,SAAS,OAAA;AAEf,UAAIH,SAASI;AAAKF,cAAMG,OAAO,OAAOL,QAAQI,GAAG;AAEjD,UAAIJ,SAASM,MAAM;AACjBJ,cAAMK,QAAQ;UACZC,IAAIR,QAAQM,KAAKE;UACjBC,UAAUT,QAAQM,KAAKI;UACvBC,OAAOX,QAAQM,KAAKK;QACtB,CAAA;MACF;AAEA,UAAIX,SAASY,SAAS;AACpB,cAAM,EAAEC,MAAMC,OAAOC,QAAQC,SAASC,QAAQC,KAAKC,UAAS,IAC1DnB,QAAQY;AAEVV,cAAMkB,WAAW,QAAQ;UACvBH;UACAE;UACAD;UACAF;UACAF;UACAD;UACAE;QACF,CAAA;MACF;AAEAzB,MAAO+B,wBAAiBtB,KAAAA;IAC1B,CAAA;EACF;AACF;;;;;;;;;;AJrCO,IAAMuB,sBAAN,MAAMA;EAfb,OAeaA;;;EACX,OAAOC,OAAOC,KAAsBC,aAA0B;AAC5D,UAAMC,oBAAoB;MACxBC,MAAM,KAAKC,eAAeH,aAAaI,SAAS,SAAA;MAChDC,aAAa,KAAKF,eAAeH,aAAaI,SAAS,SAAA;MACvDE,SAAS,KAAKH,eAAeH,aAAaI,SAAS,SAAA;MACnDG,YAAY,KAAKJ,eAAeH,aAAaI,SAAS,QAAA;IACxD;AAEA,WAAOH,kBAAkBF,GAAAA;EAC3B;EAEA,OAAeI,eAAeK,UAAgC;AAC5D,YAAQA,UAAAA;MACN,KAAK;AACH,eAAOC;MACT,KAAK;AACH,eAAOC;MACT,KAAK;AACH,eAAOC;MACT;AACE,eAAOF;IACX;EACF;AACF;","names":["Inject","token","target","_propertyKey","parameterIndex","constructor","existingInjectedParams","Reflect","getOwnMetadata","defineMetadata","DiscordNotifier","webhook","constructor","options","Webhook","url","notify","error","context","embed","MessageBuilder","setTitle","addField","message","slice","request","method","JSON","stringify","params","query","headers","body","stack","setFooter","env","setTimestamp","send","NotificationErrorInMemory","errors","notify","error","context","console","log","push","message","SentryNotifier","constructor","options","Sentry","init","dsn","environment","attachStacktrace","tracesSampleRate","maxBreadcrumbs","debug","notify","error","context","withScope","scope","setLevel","env","setTag","user","setUser","id","username","name","email","request","body","query","params","headers","method","url","requestId","setContext","captureException","NotificationFactory","define","env","definitions","defaultDefinition","test","defineProvider","local","development","staging","production","provider","NotificationErrorInMemory","DiscordNotifier","SentryNotifier"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { baseEnvSchema } from '../../env/index.cjs';
|
|
3
|
+
|
|
4
|
+
type OptionsNotifications = 'console' | 'discord' | 'sentry';
|
|
5
|
+
type EnvironmentEnum = z.infer<typeof baseEnvSchema>['ENVIRONMENT'];
|
|
6
|
+
type Props = {
|
|
7
|
+
local?: OptionsNotifications;
|
|
8
|
+
test?: OptionsNotifications;
|
|
9
|
+
development?: OptionsNotifications;
|
|
10
|
+
staging?: OptionsNotifications;
|
|
11
|
+
production?: OptionsNotifications;
|
|
12
|
+
};
|
|
13
|
+
declare class NotificationFactory {
|
|
14
|
+
static define(env: EnvironmentEnum, definitions?: Props): any;
|
|
15
|
+
private static defineProvider;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { NotificationFactory };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { baseEnvSchema } from '../../env/index.js';
|
|
3
|
+
|
|
4
|
+
type OptionsNotifications = 'console' | 'discord' | 'sentry';
|
|
5
|
+
type EnvironmentEnum = z.infer<typeof baseEnvSchema>['ENVIRONMENT'];
|
|
6
|
+
type Props = {
|
|
7
|
+
local?: OptionsNotifications;
|
|
8
|
+
test?: OptionsNotifications;
|
|
9
|
+
development?: OptionsNotifications;
|
|
10
|
+
staging?: OptionsNotifications;
|
|
11
|
+
production?: OptionsNotifications;
|
|
12
|
+
};
|
|
13
|
+
declare class NotificationFactory {
|
|
14
|
+
static define(env: EnvironmentEnum, definitions?: Props): any;
|
|
15
|
+
private static defineProvider;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { NotificationFactory };
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/core/decorators/dependency-container.ts
|
|
5
|
+
import "reflect-metadata";
|
|
6
|
+
function Inject(token) {
|
|
7
|
+
return (target, _propertyKey, parameterIndex) => {
|
|
8
|
+
const constructor = typeof target === "function" ? target : target.constructor;
|
|
9
|
+
const existingInjectedParams = Reflect.getOwnMetadata("inject:params", constructor) || {};
|
|
10
|
+
existingInjectedParams[parameterIndex] = token;
|
|
11
|
+
Reflect.defineMetadata("inject:params", existingInjectedParams, constructor);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
__name(Inject, "Inject");
|
|
15
|
+
|
|
16
|
+
// src/infra/adapters/notifications/discord.ts
|
|
17
|
+
import { MessageBuilder, Webhook } from "discord-webhook-node";
|
|
18
|
+
function _ts_decorate(decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
21
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
22
|
+
else
|
|
23
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
24
|
+
if (d = decorators[i])
|
|
25
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
26
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
27
|
+
}
|
|
28
|
+
__name(_ts_decorate, "_ts_decorate");
|
|
29
|
+
function _ts_metadata(k, v) {
|
|
30
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
31
|
+
return Reflect.metadata(k, v);
|
|
32
|
+
}
|
|
33
|
+
__name(_ts_metadata, "_ts_metadata");
|
|
34
|
+
function _ts_param(paramIndex, decorator) {
|
|
35
|
+
return function(target, key) {
|
|
36
|
+
decorator(target, key, paramIndex);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
__name(_ts_param, "_ts_param");
|
|
40
|
+
var DiscordNotifier = class {
|
|
41
|
+
static {
|
|
42
|
+
__name(this, "DiscordNotifier");
|
|
43
|
+
}
|
|
44
|
+
options;
|
|
45
|
+
webhook;
|
|
46
|
+
constructor(options) {
|
|
47
|
+
this.options = options;
|
|
48
|
+
this.webhook = new Webhook(this.options.url);
|
|
49
|
+
}
|
|
50
|
+
async notify(error, context) {
|
|
51
|
+
const embed = new MessageBuilder().setTitle("\u{1F6A8} Error").addField("Message", `\`\`\`${error.message.slice(0, 300)}\`\`\``).addField("Route:", `\`[${context?.request?.method}] ${context?.request?.url}\``, true).addField("Params:", "```json\n" + JSON.stringify(context?.request?.params, null, 2) + "\n```", true).addField("Query:", "```json\n" + JSON.stringify(context?.request?.query, null, 2) + "\n```", true).addField("Headers:", "```json\n" + JSON.stringify(context?.request?.headers, null, 2) + "\n```", true).addField("Body:", "```json\n" + JSON.stringify(context?.request?.body, null, 2) + "\n```").addField("Stack Trace:", "```" + (error.stack || "No stack provided").slice(0, 900) + "```").setFooter(`Env: ${this.options.env || "development"}`).setTimestamp();
|
|
52
|
+
await this.webhook.send(embed);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
DiscordNotifier = _ts_decorate([
|
|
56
|
+
_ts_param(0, Inject("DiscordConfig")),
|
|
57
|
+
_ts_metadata("design:type", Function),
|
|
58
|
+
_ts_metadata("design:paramtypes", [
|
|
59
|
+
typeof DiscordOptions === "undefined" ? Object : DiscordOptions
|
|
60
|
+
])
|
|
61
|
+
], DiscordNotifier);
|
|
62
|
+
|
|
63
|
+
// src/infra/adapters/notifications/in-memory.ts
|
|
64
|
+
var NotificationErrorInMemory = class {
|
|
65
|
+
static {
|
|
66
|
+
__name(this, "NotificationErrorInMemory");
|
|
67
|
+
}
|
|
68
|
+
errors = [];
|
|
69
|
+
async notify(error, context) {
|
|
70
|
+
console.log("NOTIFICATION ERROR: ", error);
|
|
71
|
+
this.errors.push({
|
|
72
|
+
error: error.message,
|
|
73
|
+
context
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/infra/adapters/notifications/sentry.ts
|
|
79
|
+
import * as Sentry from "@sentry/node";
|
|
80
|
+
function _ts_decorate2(decorators, target, key, desc) {
|
|
81
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
82
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
|
|
83
|
+
r = Reflect.decorate(decorators, target, key, desc);
|
|
84
|
+
else
|
|
85
|
+
for (var i = decorators.length - 1; i >= 0; i--)
|
|
86
|
+
if (d = decorators[i])
|
|
87
|
+
r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
88
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
89
|
+
}
|
|
90
|
+
__name(_ts_decorate2, "_ts_decorate");
|
|
91
|
+
function _ts_metadata2(k, v) {
|
|
92
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function")
|
|
93
|
+
return Reflect.metadata(k, v);
|
|
94
|
+
}
|
|
95
|
+
__name(_ts_metadata2, "_ts_metadata");
|
|
96
|
+
function _ts_param2(paramIndex, decorator) {
|
|
97
|
+
return function(target, key) {
|
|
98
|
+
decorator(target, key, paramIndex);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
__name(_ts_param2, "_ts_param");
|
|
102
|
+
var SentryNotifier = class {
|
|
103
|
+
static {
|
|
104
|
+
__name(this, "SentryNotifier");
|
|
105
|
+
}
|
|
106
|
+
options;
|
|
107
|
+
constructor(options) {
|
|
108
|
+
this.options = options;
|
|
109
|
+
Sentry.init({
|
|
110
|
+
dsn: this.options.dsn,
|
|
111
|
+
environment: this.options.environment,
|
|
112
|
+
attachStacktrace: true,
|
|
113
|
+
tracesSampleRate: this.options.environment === "production" ? 0.1 : 1,
|
|
114
|
+
maxBreadcrumbs: 100,
|
|
115
|
+
debug: this.options.environment !== "production"
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
async notify(error, context) {
|
|
119
|
+
Sentry.withScope((scope) => {
|
|
120
|
+
scope.setLevel("error");
|
|
121
|
+
if (context?.env)
|
|
122
|
+
scope.setTag("env", context.env);
|
|
123
|
+
if (context?.user) {
|
|
124
|
+
scope.setUser({
|
|
125
|
+
id: context.user.id,
|
|
126
|
+
username: context.user.name,
|
|
127
|
+
email: context.user.email
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (context?.request) {
|
|
131
|
+
const { body, query, params, headers, method, url, requestId } = context.request;
|
|
132
|
+
scope.setContext("http", {
|
|
133
|
+
method,
|
|
134
|
+
requestId,
|
|
135
|
+
url,
|
|
136
|
+
headers,
|
|
137
|
+
query,
|
|
138
|
+
body,
|
|
139
|
+
params
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
Sentry.captureException(error);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
SentryNotifier = _ts_decorate2([
|
|
147
|
+
_ts_param2(0, Inject("SentryConfig")),
|
|
148
|
+
_ts_metadata2("design:type", Function),
|
|
149
|
+
_ts_metadata2("design:paramtypes", [
|
|
150
|
+
typeof SentryOptions === "undefined" ? Object : SentryOptions
|
|
151
|
+
])
|
|
152
|
+
], SentryNotifier);
|
|
153
|
+
|
|
154
|
+
// src/infra/adapters/notifications/notification-factory.ts
|
|
155
|
+
var NotificationFactory = class {
|
|
156
|
+
static {
|
|
157
|
+
__name(this, "NotificationFactory");
|
|
158
|
+
}
|
|
159
|
+
static define(env, definitions) {
|
|
160
|
+
const defaultDefinition = {
|
|
161
|
+
test: this.defineProvider(definitions?.local ?? "console"),
|
|
162
|
+
development: this.defineProvider(definitions?.local ?? "console"),
|
|
163
|
+
staging: this.defineProvider(definitions?.local ?? "discord"),
|
|
164
|
+
production: this.defineProvider(definitions?.local ?? "sentry")
|
|
165
|
+
};
|
|
166
|
+
return defaultDefinition[env];
|
|
167
|
+
}
|
|
168
|
+
static defineProvider(provider) {
|
|
169
|
+
switch (provider) {
|
|
170
|
+
case "console":
|
|
171
|
+
return NotificationErrorInMemory;
|
|
172
|
+
case "discord":
|
|
173
|
+
return DiscordNotifier;
|
|
174
|
+
case "sentry":
|
|
175
|
+
return SentryNotifier;
|
|
176
|
+
default:
|
|
177
|
+
return NotificationErrorInMemory;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
export {
|
|
182
|
+
NotificationFactory
|
|
183
|
+
};
|
|
184
|
+
//# sourceMappingURL=notification-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/core/decorators/dependency-container.ts","../../../../src/infra/adapters/notifications/discord.ts","../../../../src/infra/adapters/notifications/in-memory.ts","../../../../src/infra/adapters/notifications/sentry.ts","../../../../src/infra/adapters/notifications/notification-factory.ts"],"sourcesContent":["import 'reflect-metadata'\n\ntype Class<T = any> = new (...args: any[]) => T\n\ntype Registration =\n | { type: 'class'; myClass: Class; singleton: boolean }\n | { type: 'value'; value: any }\n\nexport class DependencyContainer {\n static registry = new Map<string, Registration>()\n static singletons = new Map<string, any>()\n\n static register<T>(\n token: string,\n myClass: Class<T>,\n options: { singleton: boolean }\n ) {\n this.registry.set(token, {\n type: 'class',\n myClass,\n singleton: options.singleton,\n })\n }\n\n static registerValue<T>(token: string, value: T) {\n this.registry.set(token, { type: 'value', value })\n }\n\n static resolve<T>(target: Class<T>): T {\n const paramTypes = Reflect.getMetadata('design:paramtypes', target) || []\n\n const injectMetadata: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', target) || {}\n\n const params = paramTypes.map((_: any, index: number) => {\n const token = injectMetadata[index]\n\n if (!token) {\n throw new Error(\n `Missing @Inject token for parameter index ${index} in ${target.name}`\n )\n }\n\n return this.resolveToken(token)\n })\n\n return new target(...params)\n }\n\n static resolveToken(token: string): any {\n const registration = this.registry.get(token)\n\n if (!registration) {\n throw new Error(\n `\"${token}\" not registered. Please register it in the container.`\n )\n }\n\n if (registration.type === 'value') {\n return registration.value\n }\n\n const { myClass, singleton } = registration\n\n if (singleton) {\n if (!this.singletons.has(token)) {\n const instance = this.resolve(myClass)\n this.singletons.set(token, instance)\n }\n return this.singletons.get(token)\n }\n\n return this.resolve(myClass)\n }\n}\n\nexport function Inject(token: string): ParameterDecorator {\n return (\n target: object,\n _propertyKey: string | symbol | undefined,\n parameterIndex: number\n ): void => {\n const constructor =\n typeof target === 'function' ? target : target.constructor\n\n const existingInjectedParams: Record<number, string> =\n Reflect.getOwnMetadata('inject:params', constructor) || {}\n\n existingInjectedParams[parameterIndex] = token\n\n Reflect.defineMetadata('inject:params', existingInjectedParams, constructor)\n }\n}\n","import { Inject } from 'core/decorators/dependency-container'\nimport { MessageBuilder, Webhook } from 'discord-webhook-node'\n\nimport type { ContextError } from '../../../core/http/base-controller'\nimport type { IErrorNotifier } from '../../../core/http/error-notifier'\n\ntype DiscordOptions = {\n url: string\n env: string\n}\n\nexport class DiscordNotifier implements IErrorNotifier {\n private webhook: Webhook\n\n constructor(\n @Inject('DiscordConfig') private readonly options: DiscordOptions\n ) {\n this.webhook = new Webhook(this.options.url)\n }\n\n async notify(error: Error, context: ContextError): Promise<void> {\n const embed = new MessageBuilder()\n .setTitle('🚨 Error')\n .addField('Message', `\\`\\`\\`${error.message.slice(0, 300)}\\`\\`\\``)\n .addField(\n 'Route:',\n `\\`[${context?.request?.method}] ${context?.request?.url}\\``,\n true\n )\n .addField(\n 'Params:',\n '```json\\n' +\n JSON.stringify(context?.request?.params, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Query:',\n '```json\\n' +\n JSON.stringify(context?.request?.query, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Headers:',\n '```json\\n' +\n JSON.stringify(context?.request?.headers, null, 2) +\n '\\n```',\n true\n )\n .addField(\n 'Body:',\n '```json\\n' + JSON.stringify(context?.request?.body, null, 2) + '\\n```'\n )\n .addField(\n 'Stack Trace:',\n '```' + (error.stack || 'No stack provided').slice(0, 900) + '```'\n )\n .setFooter(`Env: ${this.options.env || 'development'}`)\n .setTimestamp()\n\n await this.webhook.send(embed)\n }\n}\n","import type { ContextError } from '../../../core/http/base-controller'\nimport type { IErrorNotifier } from '../../../core/http/error-notifier'\n\nexport class NotificationErrorInMemory implements IErrorNotifier {\n public errors: any[] = []\n\n async notify(error: Error, context?: ContextError): Promise<void> {\n console.log('NOTIFICATION ERROR: ', error)\n\n this.errors.push({\n error: error.message,\n context,\n })\n }\n}\n","import * as Sentry from '@sentry/node'\nimport type { ContextError } from 'core/http/base-controller'\n\nimport { Inject } from '../../../core/decorators/dependency-container'\nimport { IErrorNotifier } from '../../../core/http/error-notifier'\n\ntype SentryOptions = {\n dsn: string\n environment: string\n}\n\nexport class SentryNotifier implements IErrorNotifier {\n constructor(@Inject('SentryConfig') private readonly options: SentryOptions) {\n Sentry.init({\n dsn: this.options.dsn,\n environment: this.options.environment,\n attachStacktrace: true,\n tracesSampleRate: this.options.environment === 'production' ? 0.1 : 1.0,\n maxBreadcrumbs: 100,\n debug: this.options.environment !== 'production',\n })\n }\n\n async notify(error: Error, context: ContextError): Promise<void> {\n Sentry.withScope((scope) => {\n scope.setLevel('error')\n\n if (context?.env) scope.setTag('env', context.env)\n\n if (context?.user) {\n scope.setUser({\n id: context.user.id,\n username: context.user.name,\n email: context.user.email,\n })\n }\n\n if (context?.request) {\n const { body, query, params, headers, method, url, requestId } =\n context.request\n\n scope.setContext('http', {\n method,\n requestId,\n url,\n headers,\n query,\n body,\n params,\n })\n }\n\n Sentry.captureException(error)\n })\n }\n}\n","import type { z } from 'zod'\n\nimport type { baseEnvSchema } from '../../env'\nimport { DiscordNotifier } from './discord'\nimport { NotificationErrorInMemory } from './in-memory'\nimport { SentryNotifier } from './sentry'\n\ntype OptionsNotifications = 'console' | 'discord' | 'sentry'\ntype EnvironmentEnum = z.infer<typeof baseEnvSchema>['ENVIRONMENT']\n\ntype Props = {\n local?: OptionsNotifications\n test?: OptionsNotifications\n development?: OptionsNotifications\n staging?: OptionsNotifications\n production?: OptionsNotifications\n}\n\nexport class NotificationFactory {\n static define(env: EnvironmentEnum, definitions?: Props): any {\n const defaultDefinition = {\n test: this.defineProvider(definitions?.local ?? 'console'),\n development: this.defineProvider(definitions?.local ?? 'console'),\n staging: this.defineProvider(definitions?.local ?? 'discord'),\n production: this.defineProvider(definitions?.local ?? 'sentry'),\n }\n\n return defaultDefinition[env]\n }\n\n private static defineProvider(provider: OptionsNotifications) {\n switch (provider) {\n case 'console':\n return NotificationErrorInMemory\n case 'discord':\n return DiscordNotifier\n case 'sentry':\n return SentryNotifier\n default:\n return NotificationErrorInMemory\n }\n }\n}\n"],"mappings":";;;;AAAA,OAAO;AA4EA,SAASA,OAAOC,OAAa;AAClC,SAAO,CACLC,QACAC,cACAC,mBAAAA;AAEA,UAAMC,cACJ,OAAOH,WAAW,aAAaA,SAASA,OAAOG;AAEjD,UAAMC,yBACJC,QAAQC,eAAe,iBAAiBH,WAAAA,KAAgB,CAAC;AAE3DC,2BAAuBF,cAAAA,IAAkBH;AAEzCM,YAAQE,eAAe,iBAAiBH,wBAAwBD,WAAAA;EAClE;AACF;AAhBgBL;;;AC3EhB,SAASU,gBAAgBC,eAAe;;;;;;;;;;;;;;;;;;;;;;;AAUjC,IAAMC,kBAAN,MAAMA;SAAAA;;;;EACHC;EAERC,YAC4CC,SAC1C;SAD0CA,UAAAA;AAE1C,SAAKF,UAAU,IAAIG,QAAQ,KAAKD,QAAQE,GAAG;EAC7C;EAEA,MAAMC,OAAOC,OAAcC,SAAsC;AAC/D,UAAMC,QAAQ,IAAIC,eAAAA,EACfC,SAAS,iB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AAQI,gBAAgB;IACtC,CAAA;EACF;EAEA,MAAMK,OAAOC,OAAcC,SAAsC;AAC/DV,IAAOW,iBAAU,CAACC,UAAAA;AAChBA,YAAMC,SAAS,OAAA;AAEf,UAAIH,SAASI;AAAKF,cAAMG,OAAO,OAAOL,QAAQI,GAAG;AAEjD,UAAIJ,SAASM,MAAM;AACjBJ,cAAMK,QAAQ;UACZC,IAAIR,QAAQM,KAAKE;UACjBC,UAAUT,QAAQM,KAAKI;UACvBC,OAAOX,QAAQM,KAAKK;QACtB,CAAA;MACF;AAEA,UAAIX,SAASY,SAAS;AACpB,cAAM,EAAEC,MAAMC,OAAOC,QAAQC,SAASC,QAAQC,KAAKC,UAAS,IAC1DnB,QAAQY;AAEVV,cAAMkB,WAAW,QAAQ;UACvBH;UACAE;UACAD;UACAF;UACAF;UACAD;UACAE;QACF,CAAA;MACF;AAEAzB,MAAO+B,wBAAiBtB,KAAAA;IAC1B,CAAA;EACF;AACF;;;;;;;;;;ACrCO,IAAMuB,sBAAN,MAAMA;EAfb,OAeaA;;;EACX,OAAOC,OAAOC,KAAsBC,aAA0B;AAC5D,UAAMC,oBAAoB;MACxBC,MAAM,KAAKC,eAAeH,aAAaI,SAAS,SAAA;MAChDC,aAAa,KAAKF,eAAeH,aAAaI,SAAS,SAAA;MACvDE,SAAS,KAAKH,eAAeH,aAAaI,SAAS,SAAA;MACnDG,YAAY,KAAKJ,eAAeH,aAAaI,SAAS,QAAA;IACxD;AAEA,WAAOH,kBAAkBF,GAAAA;EAC3B;EAEA,OAAeI,eAAeK,UAAgC;AAC5D,YAAQA,UAAAA;MACN,KAAK;AACH,eAAOC;MACT,KAAK;AACH,eAAOC;MACT,KAAK;AACH,eAAOC;MACT;AACE,eAAOF;IACX;EACF;AACF;","names":["Inject","token","target","_propertyKey","parameterIndex","constructor","existingInjectedParams","Reflect","getOwnMetadata","defineMetadata","MessageBuilder","Webhook","DiscordNotifier","webhook","constructor","options","Webhook","url","notify","error","context","embed","MessageBuilder","setTitle","addField","message","slice","request","method","JSON","stringify","params","query","headers","body","stack","setFooter","env","setTimestamp","send","NotificationErrorInMemory","errors","notify","error","context","console","log","push","message","Sentry","SentryNotifier","constructor","options","Sentry","init","dsn","environment","attachStacktrace","tracesSampleRate","maxBreadcrumbs","debug","notify","error","context","withScope","scope","setLevel","env","setTag","user","setUser","id","username","name","email","request","body","query","params","headers","method","url","requestId","setContext","captureException","NotificationFactory","define","env","definitions","defaultDefinition","test","defineProvider","local","development","staging","production","provider","NotificationErrorInMemory","DiscordNotifier","SentryNotifier"]}
|