@brilab-mailer/core 0.0.1-beta.47

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/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # @brilab-mailer/core
2
+
3
+ ## Overview
4
+ `@brilab-mailer/core` provides the foundational NestJS module responsible for:
5
+ - registering email providers
6
+ - registering template engines
7
+ - orchestrating rendering + sending
8
+ - exposing `MailerService` as a unified high-level interface
9
+
10
+ This is the heart of the Brilab Mailer architecture.
11
+
12
+ ---
13
+
14
+ ## High-Level Architecture
15
+
16
+ ```
17
+ +---------------------+
18
+ | NestJS Application |
19
+ +---------------------+
20
+ |
21
+ v
22
+ +---------------------+
23
+ | MailerModule |
24
+ +---------------------+
25
+ | |
26
+ v v
27
+ +-------------------+ +-------------------+
28
+ | MailerProvider | | TemplateEngine |
29
+ +-------------------+ +-------------------+
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Core Responsibilities
35
+
36
+ ### 1. Provider Registration
37
+ You pass `providerClass` in `MailerModule.register()`:
38
+ ```ts
39
+ MailerModule.register({
40
+ providerClass: MailtrapApiProvider,
41
+ });
42
+ ```
43
+
44
+ Core will:
45
+ - instantiate the provider
46
+ - validate that it implements `MailerProvider`
47
+ - expose it through `MAILER_PROVIDER`
48
+
49
+ ---
50
+
51
+ ### 2. Template Engine Registration
52
+ ```ts
53
+ MailerModule.register({
54
+ templateEngineClass: HandlebarsTemplateEngine,
55
+ });
56
+ ```
57
+
58
+ ---
59
+
60
+ ### 3. Unified MailerService
61
+ ```ts
62
+ await this.mailer.send({
63
+ to: email,
64
+ subject: 'Welcome!',
65
+ html: await this.template.render('welcome', { name }),
66
+ });
67
+ ```
68
+
69
+ ---
70
+
71
+ ## API Documentation
72
+
73
+ ### `MailerModule.register(options)`
74
+ ```ts
75
+ interface MailerModuleOptions {
76
+ providerClass: Type<MailerProvider>;
77
+ templateEngineClass?: Type<MailerTemplateEngine>;
78
+ }
79
+ ```
80
+
81
+ ---
82
+
83
+ ### `MailerService`
84
+
85
+ ```ts
86
+ send(options: MailerSendOptions): Promise<void>
87
+ render(template: string, context: any): Promise<string>
88
+ ```
89
+
90
+ ---
91
+
92
+ ## Error Handling Strategy
93
+ - missing provider → `MailerMissingProviderException`
94
+ - provider.send() error → wrapped into `MailerProviderException`
95
+ - missing template engine → bypass rendering (raw content)
96
+
97
+ ---
98
+
99
+ ## Lifecycle
100
+ - provider instantiated once per module
101
+ - template engine instantiated once
102
+ - service stateless
103
+
104
+ ---
105
+
106
+ ## Best Practices
107
+ - Always use dynamic module configuration.
108
+ - Use ConfigModule for provider secrets.
109
+ - Keep providers stateless.
110
+
package/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './lib/mailer.service.js';
2
+ export * from './lib/mailer.module.js';
3
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC"}
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './lib/mailer.service.js';
2
+ export * from './lib/mailer.module.js';
@@ -0,0 +1,10 @@
1
+ import { MailerProvider, MailerMessagesBuilder } from '@brilab-mailer/contracts';
2
+ import { MailerService } from "./mailer.service.js";
3
+ type MailerProviderType<MessagePayload> = MailerProvider<any, any, MessagePayload>;
4
+ export declare class MailerSender<MessagePayload = object, Provider extends MailerProviderType<MessagePayload> = MailerProviderType<MessagePayload>> extends MailerMessagesBuilder {
5
+ private readonly _send;
6
+ constructor(_send: MailerService<Provider>['send']);
7
+ send(): Promise<void>;
8
+ }
9
+ export {};
10
+ //# sourceMappingURL=mailer-sender.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer-sender.d.ts","sourceRoot":"","sources":["../../src/lib/mailer-sender.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,cAAc,EACd,qBAAqB,EACrB,MAAM,0BAA0B,CAAA;AACjC,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,KAAK,kBAAkB,CAAC,cAAc,IAAI,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,cAAc,CAAC,CAAA;AAElF,qBAAa,YAAY,CACxB,cAAc,GAAG,MAAM,EACvB,QAAQ,SAAS,kBAAkB,CAAC,cAAc,CAAC,GAAG,kBAAkB,CAAC,cAAc,CAAC,CACvF,SAAQ,qBAAqB;IAE7B,OAAO,CAAC,QAAQ,CAAC,KAAK;gBAAL,KAAK,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC;IAK3C,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;CAIlC"}
@@ -0,0 +1,12 @@
1
+ import { MailerMessagesBuilder, } from '@brilab-mailer/contracts';
2
+ export class MailerSender extends MailerMessagesBuilder {
3
+ _send;
4
+ constructor(_send) {
5
+ super();
6
+ this._send = _send;
7
+ }
8
+ async send() {
9
+ const resolved = this.prepare();
10
+ return this._send(resolved);
11
+ }
12
+ }
@@ -0,0 +1,3 @@
1
+ export declare const MAILER_RETRY_ATTEMPTS_DEFAULT = 3;
2
+ export declare const MAILER_RETRY_BACKOFF_MS_DEFAULT: number[];
3
+ //# sourceMappingURL=mailer.constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer.constants.d.ts","sourceRoot":"","sources":["../../src/lib/mailer.constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,6BAA6B,IAAI,CAAC;AAC/C,eAAO,MAAM,+BAA+B,UAAoB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export const MAILER_RETRY_ATTEMPTS_DEFAULT = 3;
2
+ export const MAILER_RETRY_BACKOFF_MS_DEFAULT = [500, 1000, 2000];
@@ -0,0 +1,6 @@
1
+ import { DynamicModule } from '@nestjs/common';
2
+ import type { MailerModuleParams } from './types.js';
3
+ export declare class MailerModule {
4
+ static register(params: MailerModuleParams): DynamicModule;
5
+ }
6
+ //# sourceMappingURL=mailer.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer.module.d.ts","sourceRoot":"","sources":["../../src/lib/mailer.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAU,MAAM,gBAAgB,CAAC;AAavD,OAAO,KAAK,EAAuB,kBAAkB,EAAE,MAAM,YAAY,CAAA;AAEzE,qBACa,YAAY;IACxB,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,kBAAkB,GAAG,aAAa;CAoD1D"}
@@ -0,0 +1,60 @@
1
+ var MailerModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Module } from '@nestjs/common';
4
+ import { ConfigModule, ConfigService } from '@nestjs/config';
5
+ import { MAILER_OPTIONS, MAILER_PROVIDER, MAILER_PROVIDER_NATIVE_CLIENT, MAILER_PROVIDER_OPTIONS, MAILER_RETRY_ATTEMPTS, MAILER_RETRY_BACKOFF_MS, } from '@brilab-mailer/contracts';
6
+ import { MailerService } from './mailer.service.js';
7
+ import { MAILER_RETRY_ATTEMPTS_DEFAULT, MAILER_RETRY_BACKOFF_MS_DEFAULT } from "./mailer.constants.js";
8
+ let MailerModule = MailerModule_1 = class MailerModule {
9
+ static register(params) {
10
+ if (!params?.providerClass) {
11
+ throw new Error('[MailerModule] providerClass is required. ' +
12
+ 'Example: MailerModule.register({ providerClass: MailtrapApiProvider, templateEngineClass: HandlebarsTemplateEngine })');
13
+ }
14
+ return {
15
+ module: MailerModule_1,
16
+ imports: [ConfigModule],
17
+ providers: [
18
+ {
19
+ provide: MAILER_OPTIONS,
20
+ useFactory: (config) => {
21
+ const maxAttempts = config.get(MAILER_RETRY_ATTEMPTS) ?? MAILER_RETRY_ATTEMPTS_DEFAULT;
22
+ const backoffConfig = config.get(MAILER_RETRY_BACKOFF_MS);
23
+ const backoffMs = backoffConfig
24
+ ? backoffConfig.split(',').map((v) => Number(v.trim()) || 0)
25
+ : MAILER_RETRY_BACKOFF_MS_DEFAULT;
26
+ return {
27
+ maxAttempts: params.options?.maxAttempts ?? maxAttempts,
28
+ backoffMs: (params.options?.backoffMs ?? backoffMs),
29
+ };
30
+ },
31
+ inject: [ConfigService],
32
+ },
33
+ {
34
+ provide: MAILER_PROVIDER_OPTIONS,
35
+ useValue: params.providerOptions || {},
36
+ },
37
+ {
38
+ provide: MAILER_PROVIDER,
39
+ useClass: params.providerClass,
40
+ },
41
+ {
42
+ provide: MAILER_PROVIDER_NATIVE_CLIENT,
43
+ useFactory: (provider) => provider?.getClient?.(),
44
+ inject: [MAILER_PROVIDER],
45
+ },
46
+ MailerService,
47
+ ],
48
+ exports: [
49
+ MailerService,
50
+ MAILER_OPTIONS,
51
+ MAILER_PROVIDER_NATIVE_CLIENT,
52
+ MAILER_PROVIDER
53
+ ]
54
+ };
55
+ }
56
+ };
57
+ MailerModule = MailerModule_1 = __decorate([
58
+ Module({})
59
+ ], MailerModule);
60
+ export { MailerModule };
@@ -0,0 +1,17 @@
1
+ import { ConfigService } from '@nestjs/config';
2
+ import { MailerMessagesResolved } from '@brilab-mailer/contracts';
3
+ import type { MailerProvider, MailerNativeClientType } from '@brilab-mailer/contracts';
4
+ import type { MailerModuleOptions } from "./types.js";
5
+ import { MailerSender } from "./mailer-sender.js";
6
+ export declare class MailerService<Provider extends MailerProvider<any, any, any> = MailerProvider> {
7
+ private readonly config;
8
+ private readonly options;
9
+ private readonly provider;
10
+ private readonly native_client?;
11
+ constructor(config: ConfigService, options: MailerModuleOptions, provider: Provider, native_client?: MailerNativeClientType<Provider> | undefined);
12
+ get nativeClient(): MailerNativeClientType<Provider>;
13
+ sender(): MailerSender<object, Provider>;
14
+ send(resolved: MailerMessagesResolved): Promise<void>;
15
+ private withRetry;
16
+ }
17
+ //# sourceMappingURL=mailer.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer.service.d.ts","sourceRoot":"","sources":["../../src/lib/mailer.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAGyB,sBAAsB,EACrD,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EACX,cAAc,EACd,sBAAsB,EACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,qBACa,aAAa,CAAC,QAAQ,SAAS,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,cAAc;IAExF,OAAO,CAAC,QAAQ,CAAC,MAAM;IAEvB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAExB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAGzB,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC;gBAPd,MAAM,EAAE,aAAa,EAErB,OAAO,EAAE,mBAAmB,EAE5B,QAAQ,EAAE,QAAQ,EAGlB,aAAa,CAAC,EAAE,sBAAsB,CAAC,QAAQ,CAAC,YAAA;IAKlE,IAAW,YAAY,IAAI,sBAAsB,CAAC,QAAQ,CAAC,CAQ1D;IAEM,MAAM,IAAI,YAAY,CAAC,MAAM,EAAE,QAAQ,CAAC;IAMlC,IAAI,CAAC,QAAQ,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;YAUpD,SAAS;CAsBvB"}
@@ -0,0 +1,66 @@
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Inject, Injectable, Optional } from '@nestjs/common';
3
+ import { ConfigService } from '@nestjs/config';
4
+ import { MAILER_OPTIONS, MAILER_PROVIDER, MAILER_PROVIDER_NATIVE_CLIENT, } from '@brilab-mailer/contracts';
5
+ import { MailerSender } from "./mailer-sender.js";
6
+ let MailerService = class MailerService {
7
+ config;
8
+ options;
9
+ provider;
10
+ native_client;
11
+ constructor(config, options, provider, native_client) {
12
+ this.config = config;
13
+ this.options = options;
14
+ this.provider = provider;
15
+ this.native_client = native_client;
16
+ console.log('this.config', this.config);
17
+ }
18
+ get nativeClient() {
19
+ const injected = this.native_client;
20
+ if (injected)
21
+ return injected;
22
+ const fromProvider = this.provider.getClient?.();
23
+ if (fromProvider)
24
+ return fromProvider;
25
+ throw new Error('[MailerService] Native client is not available');
26
+ }
27
+ sender() {
28
+ return new MailerSender((...rest) => this.send.call(this, ...rest));
29
+ }
30
+ async send(resolved) {
31
+ const payload = this.provider.createPayload(resolved);
32
+ const run = async () => {
33
+ await this.provider.send(payload);
34
+ };
35
+ return this.withRetry(run);
36
+ }
37
+ async withRetry(fn) {
38
+ let attempt = 0;
39
+ let lastError;
40
+ const { maxAttempts, backoffMs } = this.options;
41
+ while (attempt < maxAttempts) {
42
+ try {
43
+ await fn();
44
+ return;
45
+ }
46
+ catch (err) {
47
+ lastError = err;
48
+ attempt++;
49
+ if (attempt >= maxAttempts)
50
+ break;
51
+ const delay = backoffMs[Math.min(attempt - 1, backoffMs.length - 1)];
52
+ await new Promise(res => setTimeout(res, delay));
53
+ }
54
+ }
55
+ throw lastError;
56
+ }
57
+ };
58
+ MailerService = __decorate([
59
+ Injectable(),
60
+ __param(1, Inject(MAILER_OPTIONS)),
61
+ __param(2, Inject(MAILER_PROVIDER)),
62
+ __param(3, Optional()),
63
+ __param(3, Inject(MAILER_PROVIDER_NATIVE_CLIENT)),
64
+ __metadata("design:paramtypes", [ConfigService, Object, Object, Object])
65
+ ], MailerService);
66
+ export { MailerService };
@@ -0,0 +1,10 @@
1
+ import { DynamicModule, Type } from '@nestjs/common';
2
+ import { MailerProvider, MailerTemplateEngine } from '@brilab-mailer/contracts';
3
+ export interface MailerModuleOptions {
4
+ providerClass: Type<MailerProvider>;
5
+ templateEngineClass: Type<MailerTemplateEngine>;
6
+ }
7
+ export declare class MailerModule {
8
+ static register(options: MailerModuleOptions): DynamicModule;
9
+ }
10
+ //# sourceMappingURL=mailer_templater.module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer_templater.module.d.ts","sourceRoot":"","sources":["../../src/lib/mailer_templater.module.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAU,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAE7D,OAAO,EAGN,cAAc,EACd,oBAAoB,EACpB,MAAM,0BAA0B,CAAC;AAGlC,MAAM,WAAW,mBAAmB;IACnC,aAAa,EAAE,IAAI,CAAC,cAAc,CAAC,CAAC;IACpC,mBAAmB,EAAE,IAAI,CAAC,oBAAoB,CAAC,CAAC;CAChD;AAED,qBACa,YAAY;IACxB,MAAM,CAAC,QAAQ,CAAC,OAAO,EAAE,mBAAmB,GAAG,aAAa;CAgC5D"}
@@ -0,0 +1,38 @@
1
+ var MailerModule_1;
2
+ import { __decorate } from "tslib";
3
+ import { Module } from '@nestjs/common';
4
+ import { ConfigModule } from '@nestjs/config';
5
+ import { MAILER_PROVIDER, MAILER_TEMPLATE_ENGINE, } from '@brilab-mailer/contracts';
6
+ import { MailerService } from './mailer.service.js';
7
+ let MailerModule = MailerModule_1 = class MailerModule {
8
+ static register(options) {
9
+ if (!options?.providerClass) {
10
+ throw new Error('[MailerModule] providerClass is required. ' +
11
+ 'Example: MailerModule.register({ providerClass: MailtrapApiProvider, templateEngineClass: HandlebarsTemplateEngine })');
12
+ }
13
+ if (!options?.templateEngineClass) {
14
+ throw new Error('[MailerModule] templateEngineClass is required. ' +
15
+ 'Example: MailerModule.register({ providerClass: MailtrapApiProvider, templateEngineClass: HandlebarsTemplateEngine })');
16
+ }
17
+ return {
18
+ module: MailerModule_1,
19
+ imports: [ConfigModule],
20
+ providers: [
21
+ {
22
+ provide: MAILER_PROVIDER,
23
+ useClass: options.providerClass,
24
+ },
25
+ {
26
+ provide: MAILER_TEMPLATE_ENGINE,
27
+ useClass: options.templateEngineClass,
28
+ },
29
+ MailerService,
30
+ ],
31
+ exports: [MailerService],
32
+ };
33
+ }
34
+ };
35
+ MailerModule = MailerModule_1 = __decorate([
36
+ Module({})
37
+ ], MailerModule);
38
+ export { MailerModule };
package/lib/types.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { Type } from "@nestjs/common";
2
+ import { MailerProvider, type MailerProviderOptionsType } from "@brilab-mailer/contracts";
3
+ export interface MailerModuleOptions {
4
+ maxAttempts: number;
5
+ backoffMs: [number, number, number];
6
+ }
7
+ export interface MailerModuleParams {
8
+ providerClass: Type<MailerProvider<any, any>>;
9
+ providerOptions?: MailerProviderOptionsType;
10
+ options?: Partial<MailerModuleOptions>;
11
+ }
12
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACtC,OAAO,EACN,cAAc,EACd,KAAK,yBAAyB,EAC9B,MAAM,0BAA0B,CAAC;AAElC,MAAM,WAAW,mBAAmB;IACnC,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC;AAED,MAAM,WAAW,kBAAkB;IAClC,aAAa,EAAE,IAAI,CAAC,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;IAC9C,eAAe,CAAC,EAAE,yBAAyB,CAAC;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAC,mBAAmB,CAAC,CAAC;CACvC"}
package/lib/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@brilab-mailer/core",
3
+ "version": "0.0.1-beta.47",
4
+ "author": "Bohdan Radchenko <radchenkobs@gmail.com>",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "module": "./index.js",
8
+ "types": "./index.d.ts",
9
+ "exports": {
10
+ "./package.json": "./package.json",
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./index.js",
14
+ "default": "./index.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "**/*",
19
+ "!**/*.tsbuildinfo"
20
+ ],
21
+ "license": "MIT",
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "directory": "dist"
25
+ },
26
+ "peerDependencies": {
27
+ "@nestjs/common": "^10.0.0",
28
+ "@nestjs/config": "^3.0.0"
29
+ },
30
+ "dependencies": {
31
+ "@brilab-mailer/contracts": "^0.0.1"
32
+ }
33
+ }