@brilab-mailer/provider-sendgrid 0.0.5-4 → 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,11 +1,15 @@
1
- import { ConfigService } from '@nestjs/config';
2
- import * as sgMail from '@sendgrid/mail';
3
- import { type MailerMessagesResolved, MailerProvider } from '@brilab-mailer/contracts';
4
- export declare class SendgridMailProvider implements MailerProvider<any, any> {
5
- private readonly config;
6
- constructor(config: ConfigService);
7
- getClient(): void;
8
- createPayload(resolved: MailerMessagesResolved): sgMail.MailDataRequired;
9
- send(payload: sgMail.MailDataRequired): Promise<void>;
1
+ import { MailService } from '@sendgrid/mail';
2
+ import type { MailDataRequired } from '@sendgrid/mail';
3
+ import { type MailerMessagesResolved, MailerProvider, type MailerSendResult } from '@brilab-mailer/contracts';
4
+ export type SendgridProviderOptions = {
5
+ apiKey: string;
6
+ };
7
+ export declare class SendgridMailProvider implements MailerProvider<SendgridProviderOptions, MailService, MailDataRequired> {
8
+ private readonly options;
9
+ private readonly _client;
10
+ constructor(options: SendgridProviderOptions);
11
+ getClient(): MailService;
12
+ createPayload(resolved: MailerMessagesResolved): MailDataRequired;
13
+ send(payload: MailDataRequired): Promise<MailerSendResult>;
10
14
  }
11
15
  //# sourceMappingURL=sendgrid-mail.provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sendgrid-mail.provider.d.ts","sourceRoot":"","sources":["../../src/lib/sendgrid-mail.provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAC/C,OAAO,KAAK,MAAM,MAAM,gBAAgB,CAAC;AACzC,OAAO,EACL,KAAK,sBAAsB,EAC3B,cAAc,EACf,MAAM,0BAA0B,CAAC;AAElC,qBACa,oBAAqB,YAAW,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC;IACvD,OAAO,CAAC,QAAQ,CAAC,MAAM;gBAAN,MAAM,EAAE,aAAa;IAU3C,SAAS;IAIT,aAAa,CAAC,QAAQ,EAAE,sBAAsB,GAAG,MAAM,CAAC,gBAAgB;IAezE,IAAI,CAAC,OAAO,EAAE,MAAM,CAAC,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC;CAG5D"}
1
+ {"version":3,"file":"sendgrid-mail.provider.d.ts","sourceRoot":"","sources":["../../src/lib/sendgrid-mail.provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAC7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAGN,KAAK,sBAAsB,EAC3B,cAAc,EACd,KAAK,gBAAgB,EAGrB,MAAM,0BAA0B,CAAC;AAElC,MAAM,MAAM,uBAAuB,GAAG;IACrC,MAAM,EAAE,MAAM,CAAC;CACf,CAAC;AAIF,qBACa,oBAAqB,YAAW,cAAc,CAAC,uBAAuB,EAAE,WAAW,EAAE,gBAAgB,CAAC;IAKjH,OAAO,CAAC,QAAQ,CAAC,OAAO;IAJzB,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAc;gBAIpB,OAAO,EAAE,uBAAuB;IAU3C,SAAS,IAAI,WAAW;IAIxB,aAAa,CAAC,QAAQ,EAAE,sBAAsB,GAAG,gBAAgB;IAkBlE,IAAI,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,gBAAgB,CAAC;CAWhE"}
@@ -1,35 +1,56 @@
1
- import { __decorate, __metadata } from "tslib";
2
- import { Injectable } from '@nestjs/common';
3
- import { ConfigService } from '@nestjs/config';
4
- import * as sgMail from '@sendgrid/mail';
5
- import { segmentedDataContentPayload, } from '@brilab-mailer/contracts';
1
+ import { __decorate, __metadata, __param } from "tslib";
2
+ import { Inject, Injectable } from '@nestjs/common';
3
+ import { MailService } from '@sendgrid/mail';
4
+ import { MAILER_PROVIDER_OPTIONS, toMailerSendError, resolveBodies, } from '@brilab-mailer/contracts';
5
+ const toEmail = (a) => ({ email: a.email, name: a.name });
6
6
  let SendgridMailProvider = class SendgridMailProvider {
7
- config;
8
- constructor(config) {
9
- this.config = config;
10
- const apiKey = this.config.get('SENDGRID_API_KEY');
11
- if (!apiKey) {
12
- throw new Error('SENDGRID_API_KEY is not set');
13
- }
14
- sgMail.default.setApiKey(apiKey);
7
+ options;
8
+ _client;
9
+ constructor(options) {
10
+ this.options = options;
11
+ const apiKey = this.options?.apiKey;
12
+ if (!apiKey)
13
+ throw new Error('[SendgridMailProvider] apiKey is not set');
14
+ // Isolated instance (not the shared module singleton) so DI keeps its own key.
15
+ this._client = new MailService();
16
+ this._client.setApiKey(apiKey);
15
17
  }
16
18
  getClient() {
17
- throw new Error('Method not implemented.');
19
+ return this._client;
18
20
  }
19
21
  createPayload(resolved) {
20
- const base = {
21
- from: resolved.from,
22
- to: resolved.to,
22
+ const { text, html } = resolveBodies(resolved);
23
+ return {
24
+ from: toEmail(resolved.from),
25
+ to: resolved.to.map(toEmail),
26
+ cc: resolved.cc?.map(toEmail),
27
+ bcc: resolved.bcc?.map(toEmail),
28
+ replyTo: resolved.replyTo ? toEmail(resolved.replyTo) : undefined,
23
29
  subject: resolved.subject,
30
+ headers: resolved.headers,
31
+ categories: resolved.tags ? Object.values(resolved.tags) : undefined,
32
+ content: [
33
+ ...(text !== undefined ? [{ type: 'text/plain', value: text }] : []),
34
+ ...(html !== undefined ? [{ type: 'text/html', value: html }] : []),
35
+ ],
24
36
  };
25
- return segmentedDataContentPayload(resolved, null, null, base);
26
37
  }
27
38
  async send(payload) {
28
- await sgMail.default.send(payload);
39
+ try {
40
+ const [res] = await this._client.send(payload);
41
+ return {
42
+ messageId: res?.headers?.['x-message-id'],
43
+ raw: res,
44
+ };
45
+ }
46
+ catch (err) {
47
+ throw toMailerSendError('SendgridMailProvider', err);
48
+ }
29
49
  }
30
50
  };
31
51
  SendgridMailProvider = __decorate([
32
52
  Injectable(),
33
- __metadata("design:paramtypes", [ConfigService])
53
+ __param(0, Inject(MAILER_PROVIDER_OPTIONS)),
54
+ __metadata("design:paramtypes", [Object])
34
55
  ], SendgridMailProvider);
35
56
  export { SendgridMailProvider };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brilab-mailer/provider-sendgrid",
3
- "version": "0.0.5-4",
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-4",
30
- "@brilab-mailer/core": "^0.0.5-4"
28
+ "@brilab-mailer/contracts": "^0.1.0",
29
+ "@brilab-mailer/core": "^0.1.0"
31
30
  },
32
31
  "peerDependenciesMeta": {
33
32
  "@brilab-mailer/contracts": {
@@ -39,10 +38,5 @@
39
38
  },
40
39
  "dependencies": {
41
40
  "@sendgrid/mail": "^8.0.0"
42
- },
43
- "nx": {
44
- "tags": [
45
- "disabled"
46
- ]
47
41
  }
48
42
  }