@brilab-mailer/provider-sendgrid 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,11 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { type MailerMessagesResolved, MailerProvider } from '@brilab-mailer/contracts';
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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,
|
|
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 {
|
|
4
|
-
import
|
|
5
|
-
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
+
return this._client;
|
|
18
20
|
}
|
|
19
21
|
createPayload(resolved) {
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
"@
|
|
29
|
-
"@brilab-mailer/
|
|
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": {
|
|
@@ -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
|
}
|