@brilab-mailer/provider-resend 0.0.5-5 → 0.1.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.
@@ -1,12 +1,15 @@
1
- import { ConfigService } from '@nestjs/config';
2
- import { CreateEmailOptions, Resend } from 'resend';
3
- import { type MailerMessagesResolved, MailerProvider } from '@brilab-mailer/contracts';
4
- export declare class ResendMailProvider implements MailerProvider<any, Resend, CreateEmailOptions> {
5
- private readonly config;
6
- private _client;
7
- constructor(config: ConfigService);
1
+ import { Resend } from 'resend';
2
+ import type { CreateEmailOptions } from 'resend';
3
+ import { type MailerMessagesResolved, MailerProvider, type MailerSendResult, type MailerSendOptions } from '@brilab-mailer/contracts';
4
+ export type ResendProviderOptions = {
5
+ apiKey: string;
6
+ };
7
+ export declare class ResendMailProvider implements MailerProvider<ResendProviderOptions, Resend, CreateEmailOptions> {
8
+ private readonly options;
9
+ private readonly _client;
10
+ constructor(options: ResendProviderOptions);
8
11
  getClient(): Resend;
9
12
  createPayload(resolved: MailerMessagesResolved): CreateEmailOptions;
10
- send(payload: CreateEmailOptions): Promise<void>;
13
+ send(payload: CreateEmailOptions, options?: MailerSendOptions): Promise<MailerSendResult>;
11
14
  }
12
15
  //# sourceMappingURL=resend-mail.provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"resend-mail.provider.d.ts","sourceRoot":"","sources":["../../src/lib/resend-mail.provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,EAAE,kBAAkB,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EACL,KAAK,sBAAsB,EAC3B,cAAc,EAEf,MAAM,0BAA0B,CAAC;AAElC,qBACa,kBAAmB,YAAW,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,kBAAkB,CAAC;IAG5E,OAAO,CAAC,QAAQ,CAAC,MAAM;IAFnC,OAAO,CAAC,OAAO,CAAS;gBAEK,MAAM,EAAE,aAAa;IAS3C,SAAS,IAAI,MAAM;IAInB,aAAa,CAAC,QAAQ,EAAE,sBAAsB,GAAG,kBAAkB;IAepE,IAAI,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC;CAGvD"}
1
+ {"version":3,"file":"resend-mail.provider.d.ts","sourceRoot":"","sources":["../../src/lib/resend-mail.provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AACjD,OAAO,EAGN,KAAK,sBAAsB,EAC3B,cAAc,EACd,KAAK,gBAAgB,EACrB,KAAK,iBAAiB,EAItB,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,qBAAqB,GAAG;IACnC,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,qBACa,kBAAmB,YAAW,cAAc,CAAC,qBAAqB,EAAE,MAAM,EAAE,kBAAkB,CAAC;IAK1G,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;gBAIf,OAAO,EAAE,qBAAqB;IAOzC,SAAS,IAAI,MAAM;IAInB,aAAa,CAAC,QAAQ,EAAE,sBAAsB,GAAG,kBAAkB;IAqBpE,IAAI,CAAC,OAAO,EAAE,kBAAkB,EAAE,OAAO,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CA2B/F"}
@@ -1,36 +1,69 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable } from '@nestjs/common';
3
- import { ConfigService } from '@nestjs/config';
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Inject, Injectable } from '@nestjs/common';
4
3
  import { Resend } from 'resend';
5
- import { segmentedDataContentPayload, } from '@brilab-mailer/contracts';
4
+ import { MAILER_PROVIDER_OPTIONS, formatAddress, MailerSendError, toMailerSendError, resolveBodies, } from '@brilab-mailer/contracts';
6
5
  let ResendMailProvider = class ResendMailProvider {
7
- config;
6
+ options;
8
7
  _client;
9
- constructor(config) {
10
- this.config = config;
11
- const apiKey = this.config.get('RESEND_API_KEY');
12
- if (!apiKey) {
13
- throw new Error('RESEND_API_KEY is not set');
14
- }
8
+ constructor(options) {
9
+ this.options = options;
10
+ const apiKey = this.options?.apiKey;
11
+ if (!apiKey)
12
+ throw new Error('[ResendMailProvider] apiKey is not set');
15
13
  this._client = new Resend(apiKey);
16
14
  }
17
15
  getClient() {
18
16
  return this._client;
19
17
  }
20
18
  createPayload(resolved) {
21
- const base = {
22
- from: resolved.from.email,
23
- to: resolved.to.map((t) => t.email),
19
+ const { text, html } = resolveBodies(resolved);
20
+ return {
21
+ from: formatAddress(resolved.from),
22
+ to: resolved.to.map(formatAddress),
23
+ cc: resolved.cc?.map(formatAddress),
24
+ bcc: resolved.bcc?.map(formatAddress),
25
+ replyTo: resolved.replyTo ? formatAddress(resolved.replyTo) : undefined,
24
26
  subject: resolved.subject,
27
+ headers: resolved.headers,
28
+ tags: resolved.tags
29
+ ? Object.entries(resolved.tags).map(([name, value]) => ({ name, value }))
30
+ : undefined,
31
+ // Send both when available (multipart) so the plaintext alternative
32
+ // is preserved; fall back to whichever single body exists.
33
+ ...(html ? { html } : {}),
34
+ ...(text ? { text } : {}),
35
+ ...(!html && !text ? { text: '' } : {}),
25
36
  };
26
- return segmentedDataContentPayload(resolved, null, null, base);
27
37
  }
28
- async send(payload) {
29
- await this._client.emails.send(payload);
38
+ async send(payload, options) {
39
+ let data, error;
40
+ try {
41
+ ({ data, error } = await this._client.emails.send(payload, options?.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : undefined));
42
+ }
43
+ catch (err) {
44
+ // Transport/network failure (not an API-level error object).
45
+ throw toMailerSendError('ResendMailProvider', err);
46
+ }
47
+ if (error) {
48
+ // API-level error: validation errors are permanent, rate limits transient.
49
+ const name = error.name;
50
+ const retryable = name === 'rate_limit_exceeded' || name === 'application_error';
51
+ throw new MailerSendError({
52
+ provider: 'ResendMailProvider',
53
+ message: error.message,
54
+ code: name,
55
+ retryable,
56
+ });
57
+ }
58
+ return {
59
+ messageId: data?.id,
60
+ raw: data,
61
+ };
30
62
  }
31
63
  };
32
64
  ResendMailProvider = __decorate([
33
65
  Injectable(),
34
- __metadata("design:paramtypes", [ConfigService])
66
+ __param(0, Inject(MAILER_PROVIDER_OPTIONS)),
67
+ __metadata("design:paramtypes", [Object])
35
68
  ], ResendMailProvider);
36
69
  export { ResendMailProvider };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brilab-mailer/provider-resend",
3
- "version": "0.0.5-5",
3
+ "version": "0.1.0",
4
4
  "author": "Bohdan Radchenko <radchenkobs@gmail.com>",
5
5
  "type": "module",
6
6
  "main": "./index.js",
@@ -25,9 +25,8 @@
25
25
  },
26
26
  "peerDependencies": {
27
27
  "@nestjs/common": "^10.0.0",
28
- "@nestjs/config": "^3.0.0",
29
- "@brilab-mailer/contracts": "^0.0.5-5",
30
- "@brilab-mailer/core": "^0.0.5-5"
28
+ "@brilab-mailer/contracts": "^0.1.0",
29
+ "@brilab-mailer/core": "^0.1.0"
31
30
  },
32
31
  "peerDependenciesMeta": {
33
32
  "@brilab-mailer/contracts": {
@@ -38,11 +37,6 @@
38
37
  }
39
38
  },
40
39
  "dependencies": {
41
- "resend": "^3.0.0"
42
- },
43
- "nx": {
44
- "tags": [
45
- "disabled"
46
- ]
40
+ "resend": "^6.12.4"
47
41
  }
48
42
  }