Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix mail.d.ts types #1381

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/mail/src/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,7 +27,7 @@ declare class MailService {
/**
* Set substitution wrappers
*/
setSubstitutionWrappers(left: string, right: string): void;
setSubstitutionWrappers(left: string, right: string): this;

/**
* Send email
Expand Down
11 changes: 11 additions & 0 deletions packages/mail/src/mail.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})
});

13 changes: 12 additions & 1 deletion test/typescript/mail.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand Down Expand Up @@ -277,3 +277,14 @@ sgMail.send({
mailSettings: {},
trackingSettings: {},
});

//supports chaining with ts
sgMail.setClient(new Client())
.setApiKey("MY_SENDGRID_API_KEY")
.send({
to: '[email protected]',
from: '[email protected]',
subject: 'Hello email with categories',
html: '<p>Some email content</p>',
category: 'transactional',
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"noEmit": true,
"esModuleInterop": true,
"module": "commonjs",
"target": "es6",
"baseUrl": ".",
Expand Down