@gapi/sendgrid 1.8.122

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/.eslintrc.js ADDED
@@ -0,0 +1,27 @@
1
+ module.exports = {
2
+ // Specifies the ESLint parser
3
+ parser: "@typescript-eslint/parser",
4
+ extends: [
5
+ // Uses the recommended rules from the @typescript-eslint/eslint-plugin
6
+ "plugin:@typescript-eslint/recommended",
7
+ // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
8
+ "prettier/@typescript-eslint",
9
+ // Enables eslint-plugin-prettier and eslint-config-prettier. This will display prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array.
10
+ "plugin:prettier/recommended"
11
+ ],
12
+ parserOptions: {
13
+ // Allows for the parsing of modern ECMAScript features
14
+ ecmaVersion: 2018,
15
+ // Allows for the use of imports
16
+ sourceType: "module"
17
+ },
18
+ rules: {
19
+ "@typescript-eslint/explicit-function-return-type": 0,
20
+ "simple-import-sort/sort": "error",
21
+ "sort-imports": "off",
22
+ "import/order": "off",
23
+ "@typescript-eslint/camelcase": 0,
24
+ "@typescript-eslint/interface-name-prefix": 0
25
+ },
26
+ plugins: ["simple-import-sort"]
27
+ };
@@ -0,0 +1,5 @@
1
+ semi: true
2
+ trailingComma: all
3
+ singleQuote: true
4
+ printWidth: 80
5
+ tabWidth: 2
package/README.md ADDED
@@ -0,0 +1,36 @@
1
+ ```ts
2
+ import { SendgridModule } from '@gapi/sendgrid';
3
+ import { Module } from '@rxdi/core';
4
+
5
+ import { ProfilingRequestTemplate } from './email-templates/profile-request.template';
6
+ import { SubscriptionTemplate } from './email-templates/subscription.template';
7
+
8
+ @Module({
9
+ imports: [
10
+ SendgridModule.forRoot({
11
+ templates: [
12
+ {
13
+ type: 'subscribe',
14
+ subject: 'Thank you for subscribing to Graphql Server feed!',
15
+ // tslint:disable-next-line:max-line-length
16
+ text: `From now on, you'll get regular updates. Cheers, Graphql Server`,
17
+ asyncHtml: SubscriptionTemplate,
18
+ from: null,
19
+ to: null,
20
+ },
21
+ {
22
+ type: 'profiling',
23
+ subject: 'Thank you for your profiling request!',
24
+ text: `You have successfully requested free profiling.`,
25
+ asyncHtml: ProfilingRequestTemplate,
26
+ from: null,
27
+ to: null,
28
+ },
29
+ ],
30
+ apiKey: 'SENDGRID_API_KEY',
31
+ defaultEmail: 'kristiqn.tachev@gmail.com',
32
+ }),
33
+ ],
34
+ })
35
+ export class CoreModule {}
36
+ ```
@@ -0,0 +1,3 @@
1
+ export * from './send-grid.module';
2
+ export * from './send-grid.service';
3
+ export * from './send-grid.helpers';
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./send-grid.module"), exports);
14
+ __exportStar(require("./send-grid.service"), exports);
15
+ __exportStar(require("./send-grid.helpers"), exports);
16
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,qDAAmC;AACnC,sDAAoC;AACpC,sDAAoC"}
@@ -0,0 +1,11 @@
1
+ import { MailData } from '@sendgrid/helpers/classes/mail';
2
+ import { Mailer, Templates, TemplatesModel } from './tokens';
3
+ export declare class SendGridHelperService {
4
+ private templates;
5
+ private mailer;
6
+ constructor(templates: Templates, mailer: Mailer);
7
+ getTemplate<T extends string>(type: T): TemplatesModel;
8
+ send(from: string, to: string, template: TemplatesModel, options?: Partial<MailData>): Promise<unknown>;
9
+ sendWithParams<T>(from: string, to: string, options?: Partial<MailData>): (template: TemplatesModel, params: T) => Promise<unknown>;
10
+ raw(subject: string, text: string, html: string, from: string, to: string, options?: Partial<MailData>): Promise<[import("request").Response, {}]>;
11
+ }
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.SendGridHelperService = void 0;
25
+ const core_1 = require("@rxdi/core");
26
+ const tokens_1 = require("./tokens");
27
+ let SendGridHelperService = class SendGridHelperService {
28
+ constructor(templates, mailer) {
29
+ this.templates = templates;
30
+ this.mailer = mailer;
31
+ }
32
+ getTemplate(type) {
33
+ return this.templates.filter((t) => t.type === type)[0];
34
+ }
35
+ send(from, to, template, options) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ const html = yield template.html();
38
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
39
+ return resolve(yield this.mailer.send(Object.assign({ subject: template.subject, text: template.text, html,
40
+ from,
41
+ to }, options), null, (e) => (e ? reject(e) : null)));
42
+ }));
43
+ });
44
+ }
45
+ sendWithParams(from, to, options) {
46
+ return (template, params) => __awaiter(this, void 0, void 0, function* () {
47
+ const html = yield template.html(params);
48
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
49
+ return resolve(yield this.mailer.send(Object.assign({ subject: template.subject, text: template.text, html,
50
+ from,
51
+ to }, options), null, (e) => (e ? reject(e) : null)));
52
+ }));
53
+ });
54
+ }
55
+ raw(subject, text, html, from, to, options) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ return yield this.mailer.send(Object.assign({ subject,
58
+ text,
59
+ html,
60
+ from,
61
+ to }, options));
62
+ });
63
+ }
64
+ };
65
+ SendGridHelperService = __decorate([
66
+ core_1.Service(),
67
+ __param(0, core_1.Inject(tokens_1.Templates)),
68
+ __param(1, core_1.Inject(tokens_1.Mailer)),
69
+ __metadata("design:paramtypes", [Array, Object])
70
+ ], SendGridHelperService);
71
+ exports.SendGridHelperService = SendGridHelperService;
72
+ //# sourceMappingURL=send-grid.helpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send-grid.helpers.js","sourceRoot":"","sources":["../src/send-grid.helpers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAA6C;AAG7C,qCAA6D;AAG7D,IAAa,qBAAqB,GAAlC,MAAa,qBAAqB;IAChC,YAC6B,SAAoB,EACvB,MAAc;QADX,cAAS,GAAT,SAAS,CAAW;QACvB,WAAM,GAAN,MAAM,CAAQ;IACrC,CAAC;IAEJ,WAAW,CAAmB,IAAO;QACnC,OAAO,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1D,CAAC;IAEK,IAAI,CACR,IAAY,EACZ,EAAU,EACV,QAAwB,EACxB,OAA2B;;YAE3B,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACnC,OAAO,IAAI,OAAO,CAAC,CAAO,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC3C,OAAA,OAAO,CACL,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,iBAElB,OAAO,EAAE,QAAQ,CAAC,OAAO,EACzB,IAAI,EAAE,QAAQ,CAAC,IAAI,EACnB,IAAI;oBACJ,IAAI;oBACJ,EAAE,IACC,OAAO,GAEZ,IAAI,EACJ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAC9B,CACF,CAAA;cAAA,CACF,CAAC;QACJ,CAAC;KAAA;IAED,cAAc,CAAI,IAAY,EAAE,EAAU,EAAE,OAA2B;QACrE,OAAO,CAAO,QAAwB,EAAE,MAAS,EAAE,EAAE;YACnD,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;YACzC,OAAO,IAAI,OAAO,CAAC,CAAO,OAAO,EAAE,MAAM,EAAE,EAAE;gBAC3C,OAAA,OAAO,CACL,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,iBAElB,OAAO,EAAE,QAAQ,CAAC,OAAO,EACzB,IAAI,EAAE,QAAQ,CAAC,IAAI,EACnB,IAAI;oBACJ,IAAI;oBACJ,EAAE,IACC,OAAO,GAEZ,IAAI,EACJ,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAC9B,CACF,CAAA;cAAA,CACF,CAAC;QACJ,CAAC,CAAA,CAAC;IACJ,CAAC;IAEK,GAAG,CACP,OAAe,EACf,IAAY,EACZ,IAAY,EACZ,IAAY,EACZ,EAAU,EACV,OAA2B;;YAE3B,OAAO,MAAM,IAAI,CAAC,MAAM,CAAC,IAAI,iBAC3B,OAAO;gBACP,IAAI;gBACJ,IAAI;gBACJ,IAAI;gBACJ,EAAE,IACC,OAAO,EACV,CAAC;QACL,CAAC;KAAA;CACF,CAAA;AA1EY,qBAAqB;IADjC,cAAO,EAAE;IAGL,WAAA,aAAM,CAAC,kBAAS,CAAC,CAAA;IACjB,WAAA,aAAM,CAAC,eAAM,CAAC,CAAA;;GAHN,qBAAqB,CA0EjC;AA1EY,sDAAqB"}
@@ -0,0 +1,9 @@
1
+ import { ModuleWithProviders } from '@gapi/core';
2
+ import { TemplatesModel } from './tokens';
3
+ export declare class SendgridModule {
4
+ static forRoot(options?: {
5
+ apiKey?: string;
6
+ templates?: TemplatesModel[];
7
+ defaultEmail?: string;
8
+ }): ModuleWithProviders;
9
+ }
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var SendgridModule_1;
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.SendgridModule = void 0;
11
+ const core_1 = require("@gapi/core");
12
+ const sgMail = require("@sendgrid/mail");
13
+ const tokens_1 = require("./tokens");
14
+ let SendgridModule = SendgridModule_1 = class SendgridModule {
15
+ static forRoot(options = {
16
+ apiKey: '',
17
+ templates: [],
18
+ defaultEmail: '',
19
+ }) {
20
+ return {
21
+ module: SendgridModule_1,
22
+ providers: [
23
+ {
24
+ provide: tokens_1.Mailer,
25
+ useFactory: () => {
26
+ const mailer = sgMail['default'] || sgMail;
27
+ mailer.setApiKey(options.apiKey);
28
+ return mailer;
29
+ },
30
+ },
31
+ {
32
+ provide: tokens_1.Templates,
33
+ useValue: options.templates,
34
+ },
35
+ {
36
+ provide: tokens_1.DefaultEmail,
37
+ useValue: options.defaultEmail,
38
+ },
39
+ ],
40
+ };
41
+ }
42
+ };
43
+ SendgridModule = SendgridModule_1 = __decorate([
44
+ core_1.Module()
45
+ ], SendgridModule);
46
+ exports.SendgridModule = SendgridModule;
47
+ //# sourceMappingURL=send-grid.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send-grid.module.js","sourceRoot":"","sources":["../src/send-grid.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,qCAAyD;AACzD,yCAAyC;AAEzC,qCAA2E;AAG3E,IAAa,cAAc,sBAA3B,MAAa,cAAc;IAClB,MAAM,CAAC,OAAO,CACnB,UAII;QACF,MAAM,EAAE,EAAE;QACV,SAAS,EAAE,EAAE;QACb,YAAY,EAAE,EAAE;KACjB;QAED,OAAO;YACL,MAAM,EAAE,gBAAc;YACtB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,eAAM;oBACf,UAAU,EAAE,GAAG,EAAE;wBACf,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,IAAI,MAAM,CAAC;wBAC3C,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;wBACjC,OAAO,MAAM,CAAC;oBAChB,CAAC;iBACF;gBACD;oBACE,OAAO,EAAE,kBAAS;oBAClB,QAAQ,EAAE,OAAO,CAAC,SAAS;iBAC5B;gBACD;oBACE,OAAO,EAAE,qBAAY;oBACrB,QAAQ,EAAE,OAAO,CAAC,YAAY;iBAC/B;aACF;SACF,CAAC;IACJ,CAAC;CACF,CAAA;AAlCY,cAAc;IAD1B,aAAM,EAAE;GACI,cAAc,CAkC1B;AAlCY,wCAAc"}
@@ -0,0 +1,22 @@
1
+ import { MailData } from '@sendgrid/helpers/classes/mail';
2
+ import { SendGridHelperService } from './send-grid.helpers';
3
+ export declare class SendGridService {
4
+ private sendGridHelper;
5
+ private defaultEmail;
6
+ constructor(sendGridHelper: SendGridHelperService, defaultEmail: string);
7
+ sendEmail<T extends string>(to: string, type: T, options?: Partial<MailData>): Promise<{
8
+ status: string;
9
+ }>;
10
+ sendEmailWithParams<T extends string, P>(to: string, type: T, options?: Partial<MailData>): (params: P) => Promise<{
11
+ status: string;
12
+ }>;
13
+ sendTemplateString({ from, html, subject, text, to, options, }: {
14
+ subject: string;
15
+ text: string;
16
+ html: string;
17
+ from: string;
18
+ to: string;
19
+ options?: Partial<MailData>;
20
+ }): Promise<[import("request").Response, {}]>;
21
+ addDefaultEmail(to: string): string;
22
+ }
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
12
+ return function (target, key) { decorator(target, key, paramIndex); }
13
+ };
14
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
15
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
16
+ return new (P || (P = Promise))(function (resolve, reject) {
17
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
20
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
21
+ });
22
+ };
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.SendGridService = void 0;
25
+ const core_1 = require("@rxdi/core");
26
+ const send_grid_helpers_1 = require("./send-grid.helpers");
27
+ const tokens_1 = require("./tokens");
28
+ let SendGridService = class SendGridService {
29
+ constructor(sendGridHelper, defaultEmail) {
30
+ this.sendGridHelper = sendGridHelper;
31
+ this.defaultEmail = defaultEmail;
32
+ }
33
+ sendEmail(to, type, options) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const template = this.sendGridHelper.getTemplate(type);
36
+ yield this.sendGridHelper.send((options === null || options === void 0 ? void 0 : options.from) || this.defaultEmail, to, template, options);
37
+ return { status: 'ok' };
38
+ });
39
+ }
40
+ sendEmailWithParams(to, type, options) {
41
+ return (params) => __awaiter(this, void 0, void 0, function* () {
42
+ const template = this.sendGridHelper.getTemplate(type);
43
+ yield this.sendGridHelper.sendWithParams((options === null || options === void 0 ? void 0 : options.from) || this.defaultEmail, to, options)(template, params);
44
+ return { status: 'ok' };
45
+ });
46
+ }
47
+ sendTemplateString({ from, html, subject, text, to, options, }) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return yield this.sendGridHelper.raw(subject, text, html, from, this.addDefaultEmail(to), options);
50
+ });
51
+ }
52
+ addDefaultEmail(to) {
53
+ return to + '; ' + this.defaultEmail;
54
+ }
55
+ };
56
+ SendGridService = __decorate([
57
+ core_1.Service(),
58
+ __param(1, core_1.Inject(tokens_1.DefaultEmail)),
59
+ __metadata("design:paramtypes", [send_grid_helpers_1.SendGridHelperService, String])
60
+ ], SendGridService);
61
+ exports.SendGridService = SendGridService;
62
+ //# sourceMappingURL=send-grid.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send-grid.service.js","sourceRoot":"","sources":["../src/send-grid.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,qCAA6C;AAG7C,2DAA4D;AAC5D,qCAAwC;AAGxC,IAAa,eAAe,GAA5B,MAAa,eAAe;IAC1B,YACU,cAAqC,EACf,YAAoB;QAD1C,mBAAc,GAAd,cAAc,CAAuB;QACf,iBAAY,GAAZ,YAAY,CAAQ;IACjD,CAAC;IAEE,SAAS,CACb,EAAU,EACV,IAAO,EACP,OAA2B;;YAE3B,MAAM,QAAQ,GAAG,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;YACvD,MAAM,IAAI,CAAC,cAAc,CAAC,IAAI,CAC5B,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAe,KAAI,IAAI,CAAC,YAAY,EAC9C,EAAE,EACF,QAAQ,EACR,OAAgB,CACjB,CAAC;YACF,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1B,CAAC;KAAA;IAED,mBAAmB,CACjB,EAAU,EACV,IAAO,EACP,OAA2B;QAE3B,OAAO,CAAO,MAAS,EAAE,EAAE;YACzB,MAAM,QAAQ,GAAG,IAAI,CAAC,cAAc,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;YACvD,MAAM,IAAI,CAAC,cAAc,CAAC,cAAc,CACtC,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,IAAe,KAAI,IAAI,CAAC,YAAY,EAC9C,EAAE,EACF,OAAgB,CACjB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;YACpB,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;QAC1B,CAAC,CAAA,CAAC;IACJ,CAAC;IACK,kBAAkB,CAAC,EACvB,IAAI,EACJ,IAAI,EACJ,OAAO,EACP,IAAI,EACJ,EAAE,EACF,OAAO,GAQR;;YACC,OAAO,MAAM,IAAI,CAAC,cAAc,CAAC,GAAG,CAClC,OAAO,EACP,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,EACxB,OAAgB,CACjB,CAAC;QACJ,CAAC;KAAA;IAED,eAAe,CAAC,EAAU;QACxB,OAAO,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC,YAAY,CAAC;IACvC,CAAC;CACF,CAAA;AAhEY,eAAe;IAD3B,cAAO,EAAE;IAIL,WAAA,aAAM,CAAC,qBAAY,CAAC,CAAA;qCADG,yCAAqB;GAFpC,eAAe,CAgE3B;AAhEY,0CAAe"}
@@ -0,0 +1,17 @@
1
+ import { InjectionToken } from '@rxdi/core';
2
+ import { MailData } from '@sendgrid/helpers/classes/mail';
3
+ import { MailService } from '@sendgrid/mail';
4
+ export declare const Mailer: InjectionToken<unknown>;
5
+ export declare const Templates: InjectionToken<TemplatesModel[]>;
6
+ export declare const DefaultEmail: InjectionToken<string>;
7
+ export declare type Mailer = typeof MailService;
8
+ export declare type Templates = TemplatesModel[];
9
+ export declare type Without<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
10
+ export declare class TemplatesModel implements Without<MailData, 'html'> {
11
+ type?: string;
12
+ subject: string;
13
+ text: string;
14
+ html?: (arg?: any) => Promise<string>;
15
+ to: string;
16
+ from: string;
17
+ }
package/dist/tokens.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TemplatesModel = exports.DefaultEmail = exports.Templates = exports.Mailer = void 0;
4
+ const core_1 = require("@rxdi/core");
5
+ exports.Mailer = new core_1.InjectionToken('sendgrid-mailer');
6
+ exports.Templates = new core_1.InjectionToken('sendgrid-mailer-templates');
7
+ exports.DefaultEmail = new core_1.InjectionToken('sendgrid-mailer-default-email');
8
+ class TemplatesModel {
9
+ }
10
+ exports.TemplatesModel = TemplatesModel;
11
+ //# sourceMappingURL=tokens.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tokens.js","sourceRoot":"","sources":["../src/tokens.ts"],"names":[],"mappings":";;;AACA,qCAA4C;AAI/B,QAAA,MAAM,GAAG,IAAI,qBAAc,CAAC,iBAAiB,CAAC,CAAC;AAC/C,QAAA,SAAS,GAAG,IAAI,qBAAc,CACzC,2BAA2B,CAC5B,CAAC;AACW,QAAA,YAAY,GAAG,IAAI,qBAAc,CAC5C,+BAA+B,CAChC,CAAC;AAOF,MAAa,cAAc;CAO1B;AAPD,wCAOC"}
package/jest.config.js ADDED
@@ -0,0 +1,16 @@
1
+ module.exports = {
2
+ testEnvironment: 'node',
3
+ testPathIgnorePatterns: ['/node_modules/'],
4
+ coverageReporters: ['lcov', 'html'],
5
+ rootDir: './',
6
+ moduleFileExtensions: ['ts', 'tsx', 'js', 'json', 'node'],
7
+ globals: {
8
+ __DEV__: true,
9
+ },
10
+ transform: {
11
+ '\\.(ts|tsx)$': 'ts-jest',
12
+ },
13
+ testRegex: '/test/.*\\.spec.(ts|tsx|js)$',
14
+ verbose: true,
15
+ collectCoverage: true,
16
+ };
package/package.json ADDED
@@ -0,0 +1,52 @@
1
+ {
2
+ "name": "@gapi/sendgrid",
3
+ "version": "1.8.122",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/Stradivario/gapi.git"
7
+ },
8
+ "scripts": {
9
+ "patch": "npm run build && npm version patch && npm publish --update-readme --access public && npm run delete-dist",
10
+ "delete-dist": "rm -rf dist",
11
+ "clean": "git clean -dxf",
12
+ "lint": "npx eslint . --ext .ts",
13
+ "test": "echo TESTS",
14
+ "lint-fix": "npx eslint . --fix --ext .ts",
15
+ "build": "rm -rf dist && tsc || true"
16
+ },
17
+ "author": {
18
+ "name": "Kristian Tachev(Stradivario)",
19
+ "email": "kristiqn.tachev@gmail.com"
20
+ },
21
+ "keywords": [
22
+ "graphql",
23
+ "gapi",
24
+ "node",
25
+ "access-control"
26
+ ],
27
+ "license": "MIT",
28
+ "bugs": {
29
+ "url": "https://github.com/Stradivario/gapi/issues"
30
+ },
31
+ "dependencies": {
32
+ "@sendgrid/mail": "6.5.2"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^13.11.1",
36
+ "@types/jest": "^25.2.1",
37
+ "@typescript-eslint/eslint-plugin": "^2.10.0",
38
+ "@typescript-eslint/parser": "^2.10.0",
39
+ "eslint": "^6.7.2",
40
+ "prettier": "^2.0.4",
41
+ "typescript": "^3.8.3",
42
+ "eslint-config-prettier": "^6.7.0",
43
+ "eslint-plugin-prettier": "^3.1.1",
44
+ "eslint-plugin-simple-import-sort": "^5.0.0",
45
+ "jest": "^25.5.4",
46
+ "ts-jest": "^25.4.0"
47
+ },
48
+ "main": "./dist/index.js",
49
+ "types": "./dist/index.d.ts",
50
+ "module": "./dist/index.js",
51
+ "typings": "./dist/index.d.ts"
52
+ }
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from './send-grid.module';
2
+ export * from './send-grid.service';
3
+ export * from './send-grid.helpers';
@@ -0,0 +1,81 @@
1
+ import { Inject, Service } from '@rxdi/core';
2
+ import { MailData } from '@sendgrid/helpers/classes/mail';
3
+
4
+ import { Mailer, Templates, TemplatesModel } from './tokens';
5
+
6
+ @Service()
7
+ export class SendGridHelperService {
8
+ constructor(
9
+ @Inject(Templates) private templates: Templates,
10
+ @Inject(Mailer) private mailer: Mailer,
11
+ ) {}
12
+
13
+ getTemplate<T extends string>(type: T) {
14
+ return this.templates.filter((t) => t.type === type)[0];
15
+ }
16
+
17
+ async send(
18
+ from: string,
19
+ to: string,
20
+ template: TemplatesModel,
21
+ options?: Partial<MailData>,
22
+ ) {
23
+ const html = await template.html();
24
+ return new Promise(async (resolve, reject) =>
25
+ resolve(
26
+ await this.mailer.send(
27
+ {
28
+ subject: template.subject,
29
+ text: template.text,
30
+ html,
31
+ from,
32
+ to,
33
+ ...options,
34
+ },
35
+ null,
36
+ (e) => (e ? reject(e) : null),
37
+ ),
38
+ ),
39
+ );
40
+ }
41
+
42
+ sendWithParams<T>(from: string, to: string, options?: Partial<MailData>) {
43
+ return async (template: TemplatesModel, params: T) => {
44
+ const html = await template.html(params);
45
+ return new Promise(async (resolve, reject) =>
46
+ resolve(
47
+ await this.mailer.send(
48
+ {
49
+ subject: template.subject,
50
+ text: template.text,
51
+ html,
52
+ from,
53
+ to,
54
+ ...options,
55
+ },
56
+ null,
57
+ (e) => (e ? reject(e) : null),
58
+ ),
59
+ ),
60
+ );
61
+ };
62
+ }
63
+
64
+ async raw(
65
+ subject: string,
66
+ text: string,
67
+ html: string,
68
+ from: string,
69
+ to: string,
70
+ options?: Partial<MailData>,
71
+ ) {
72
+ return await this.mailer.send({
73
+ subject,
74
+ text,
75
+ html,
76
+ from,
77
+ to,
78
+ ...options,
79
+ });
80
+ }
81
+ }
@@ -0,0 +1,41 @@
1
+ import { Module, ModuleWithProviders } from '@gapi/core';
2
+ import * as sgMail from '@sendgrid/mail';
3
+
4
+ import { DefaultEmail, Mailer, Templates, TemplatesModel } from './tokens';
5
+
6
+ @Module()
7
+ export class SendgridModule {
8
+ public static forRoot(
9
+ options: {
10
+ apiKey?: string;
11
+ templates?: TemplatesModel[];
12
+ defaultEmail?: string;
13
+ } = {
14
+ apiKey: '',
15
+ templates: [],
16
+ defaultEmail: '',
17
+ },
18
+ ): ModuleWithProviders {
19
+ return {
20
+ module: SendgridModule,
21
+ providers: [
22
+ {
23
+ provide: Mailer,
24
+ useFactory: () => {
25
+ const mailer = sgMail['default'] || sgMail;
26
+ mailer.setApiKey(options.apiKey);
27
+ return mailer;
28
+ },
29
+ },
30
+ {
31
+ provide: Templates,
32
+ useValue: options.templates,
33
+ },
34
+ {
35
+ provide: DefaultEmail,
36
+ useValue: options.defaultEmail,
37
+ },
38
+ ],
39
+ };
40
+ }
41
+ }
@@ -0,0 +1,72 @@
1
+ import { Inject, Service } from '@rxdi/core';
2
+ import { MailData } from '@sendgrid/helpers/classes/mail';
3
+
4
+ import { SendGridHelperService } from './send-grid.helpers';
5
+ import { DefaultEmail } from './tokens';
6
+
7
+ @Service()
8
+ export class SendGridService {
9
+ constructor(
10
+ private sendGridHelper: SendGridHelperService,
11
+ @Inject(DefaultEmail) private defaultEmail: string,
12
+ ) {}
13
+
14
+ async sendEmail<T extends string>(
15
+ to: string,
16
+ type: T,
17
+ options?: Partial<MailData>,
18
+ ) {
19
+ const template = this.sendGridHelper.getTemplate(type);
20
+ await this.sendGridHelper.send(
21
+ (options?.from as string) || this.defaultEmail,
22
+ to,
23
+ template,
24
+ options as never,
25
+ );
26
+ return { status: 'ok' };
27
+ }
28
+
29
+ sendEmailWithParams<T extends string, P>(
30
+ to: string,
31
+ type: T,
32
+ options?: Partial<MailData>,
33
+ ) {
34
+ return async (params: P) => {
35
+ const template = this.sendGridHelper.getTemplate(type);
36
+ await this.sendGridHelper.sendWithParams(
37
+ (options?.from as string) || this.defaultEmail,
38
+ to,
39
+ options as never,
40
+ )(template, params);
41
+ return { status: 'ok' };
42
+ };
43
+ }
44
+ async sendTemplateString({
45
+ from,
46
+ html,
47
+ subject,
48
+ text,
49
+ to,
50
+ options,
51
+ }: {
52
+ subject: string;
53
+ text: string;
54
+ html: string;
55
+ from: string;
56
+ to: string;
57
+ options?: Partial<MailData>;
58
+ }) {
59
+ return await this.sendGridHelper.raw(
60
+ subject,
61
+ text,
62
+ html,
63
+ from,
64
+ this.addDefaultEmail(to),
65
+ options as never,
66
+ );
67
+ }
68
+
69
+ addDefaultEmail(to: string): string {
70
+ return to + '; ' + this.defaultEmail;
71
+ }
72
+ }
package/src/tokens.ts ADDED
@@ -0,0 +1,35 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { InjectionToken } from '@rxdi/core';
3
+ import { MailData } from '@sendgrid/helpers/classes/mail';
4
+ import { MailService } from '@sendgrid/mail';
5
+
6
+ export const Mailer = new InjectionToken('sendgrid-mailer');
7
+ export const Templates = new InjectionToken<TemplatesModel[]>(
8
+ 'sendgrid-mailer-templates',
9
+ );
10
+ export const DefaultEmail = new InjectionToken<string>(
11
+ 'sendgrid-mailer-default-email',
12
+ );
13
+
14
+ export type Mailer = typeof MailService;
15
+ export type Templates = TemplatesModel[];
16
+
17
+ export type Without<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
18
+
19
+ export class TemplatesModel implements Without<MailData, 'html'> {
20
+ type?: string;
21
+ subject: string;
22
+ text: string;
23
+ html?: (arg?: any) => Promise<string>;
24
+ to: string;
25
+ from: string;
26
+ }
27
+
28
+ /*
29
+ {
30
+ content: attachment,
31
+ filename: "attachment.pdf",
32
+ type: "application/pdf",
33
+ disposition: "attachment"
34
+ }
35
+ */
package/tsconfig.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "declaration": true,
4
+ "module": "commonjs",
5
+ "target": "es6",
6
+ "baseUrl": ".",
7
+ "stripInternal": true,
8
+ "emitDecoratorMetadata": true,
9
+ "experimentalDecorators": true,
10
+ "moduleResolution": "node",
11
+ "outDir": "dist",
12
+ "removeComments": true,
13
+ "allowSyntheticDefaultImports": true,
14
+ "preserveConstEnums": true,
15
+ "sourceMap": true,
16
+ "strictNullChecks": false,
17
+ "forceConsistentCasingInFileNames": true,
18
+ "noFallthroughCasesInSwitch": true,
19
+ "noImplicitAny": false,
20
+ "noImplicitReturns": true,
21
+ "noImplicitThis": false,
22
+ "noUnusedLocals": true,
23
+ "noUnusedParameters": false,
24
+ "rootDir": "src",
25
+ "lib": ["es2017", "es2016", "es2015", "es6", "dom", "esnext.asynciterable"],
26
+ "skipLibCheck": true,
27
+ "typeRoots": ["node_modules/@types"]
28
+ },
29
+ "files": ["src/index.ts"]
30
+ }