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/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 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": ".",