From 7c7cb853705fe07e23b68a813d8b6fba0c2cc92e Mon Sep 17 00:00:00 2001 From: Paul Sembereka Date: Tue, 4 Apr 2023 14:13:27 +0200 Subject: [PATCH 1/2] fix(mail.ts): correct return types for setApiKey, setClient, setSubstitutionWrappers --- packages/mail/src/mail.d.ts | 6 +++--- packages/mail/src/mail.spec.js | 11 +++++++++++ test/typescript/mail.ts | 13 ++++++++++++- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packages/mail/src/mail.d.ts b/packages/mail/src/mail.d.ts index fa827498d..735e05898 100644 --- a/packages/mail/src/mail.d.ts +++ b/packages/mail/src/mail.d.ts @@ -7,12 +7,12 @@ declare class MailService { /** * SendGrid API key passthrough for convenience. */ - setApiKey(apiKey: string): void; + setApiKey(apiKey: string): this; /** * Client to use for invoking underlying API */ - setClient(client: Client): void; + setClient(client: Client): this; /** * Twilio Email Auth passthrough for convenience. @@ -27,7 +27,7 @@ declare class MailService { /** * Set substitution wrappers */ - setSubstitutionWrappers(left: string, right: string): void; + setSubstitutionWrappers(left: string, right: string): this; /** * Send email diff --git a/packages/mail/src/mail.spec.js b/packages/mail/src/mail.spec.js index 09698baa2..66953e8fe 100644 --- a/packages/mail/src/mail.spec.js +++ b/packages/mail/src/mail.spec.js @@ -132,5 +132,16 @@ describe('sgMail.send()', () => { sgMail.send(data, false, {}); }).to.throw(Error); }); + + it("can be chained", () => { + const sgMailChain = require('./mail'); + sgMailChain.client.setDefaultHeader('X-Mock', 202) + sgMailChain + .setApiKey('SendGrid API Key') + .send(data) + .then(([response, body]) => { + expect(response.statusCode).to.equal(202); + }); + }) }); diff --git a/test/typescript/mail.ts b/test/typescript/mail.ts index fb8199860..e60aa2c5a 100644 --- a/test/typescript/mail.ts +++ b/test/typescript/mail.ts @@ -1,5 +1,5 @@ import { Client } from "@sendgrid/client"; -import sgMail = require("@sendgrid/mail"); +import sgMail from "@sendgrid/mail"; // Test setClient() method sgMail.setClient(new Client()); @@ -277,3 +277,14 @@ sgMail.send({ mailSettings: {}, trackingSettings: {}, }); + +//supports chaining with ts +sgMail.setClient(new Client()) + .setApiKey("MY_SENDGRID_API_KEY") + .send({ + to: 'recipient@example.org', + from: 'sender@example.org', + subject: 'Hello email with categories', + html: '

Some email content

', + category: 'transactional', + }); \ No newline at end of file From f3a082e49ae633a9351f95e3d725e5c879144826 Mon Sep 17 00:00:00 2001 From: Paul Sembereka Date: Tue, 4 Apr 2023 14:14:55 +0200 Subject: [PATCH 2/2] chore(ts): fix node types for ts v3.8 --- package.json | 1 + tsconfig.json | 1 + 2 files changed, 2 insertions(+) diff --git a/package.json b/package.json index 628a4aced..8c92edb2a 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "@babel/cli": "^7.7.7", "@babel/core": "^7.7.7", "@babel/node": "^7.7.7", + "@types/node": "17.0.41", "chai": "4.2.0", "chai-as-promised": "^7.1.1", "dirty-chai": "^2.0.1", diff --git a/tsconfig.json b/tsconfig.json index b8d1ada33..721048c7d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "noEmit": true, + "esModuleInterop": true, "module": "commonjs", "target": "es6", "baseUrl": ".",