Skip to content
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
23 changes: 19 additions & 4 deletions packages/client/src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,23 @@ declare class Client {
request(data: ClientRequest, cb?: (err: ResponseError, response: [ClientResponse, any]) => void): Promise<[ClientResponse, any]>;
}

declare const client: Client;
// @ts-ignore
export = client
/**
* The module exports the singleton, with the class attached as
* `module.exports.Client` (see index.js), so both meanings of `Client` —
* the value and the instance type — stay importable by name.
*/
declare namespace client {
export {Client};

export {Client};
export function setApiKey(apiKey: string): void;
export function setTwilioEmailAuth(username: string, password: string): void;
export function setImpersonateSubuser(subuser: string): void;
export function setDefaultHeader(key: string | { [s: string]: string }, value ?: string): Client;
export function setDefaultRequest<K extends keyof ClientRequest>(key: K | ClientRequest, value ?: ClientRequest[K]): Client;
export function setDataResidency(region: string): Client;
export function createHeaders(data: { [key: string]: string }): { [key: string]: string };
export function createRequest(data: ClientRequest): ClientRequest;
export function request(data: ClientRequest, cb?: (err: ResponseError, response: [ClientResponse, any]) => void): Promise<[ClientResponse, any]>;
}

export = client;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to create an MR for the exact same reason. From my testing, this is the only change needed. No other changes are required.

export default client;

Of course, I'm not completely sure if this won't break anything else

26 changes: 19 additions & 7 deletions packages/mail/src/mail.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,23 @@ declare class MailService {
sendMultiple(data: MailDataRequired, cb?: (error: Error | ResponseError, result: [ClientResponse, {}]) => void): Promise<[ClientResponse, {}]>;
}

declare const mail: MailService;
// @ts-ignore
export = mail;
/**
* The module exports the singleton, with the class attached as
* `module.exports.MailService` (see index.js).
*/
declare namespace mail {
export {MailService};
export {MailDataRequired};
export {ClientResponse};
export {ResponseError};

export function setApiKey(apiKey: string): void;
export function setClient(client: Client): void;
export function setTwilioEmailAuth(username: string, password: string): void;
export function setTimeout(timeout: number): void;
export function setSubstitutionWrappers(left: string, right: string): void;
export function send(data: MailDataRequired | MailDataRequired[], isMultiple?: boolean, cb?: (err: Error | ResponseError, result: [ClientResponse, {}]) => void): Promise<[ClientResponse, {}]>;
export function sendMultiple(data: MailDataRequired, cb?: (error: Error | ResponseError, result: [ClientResponse, {}]) => void): Promise<[ClientResponse, {}]>;
}

export {MailService};
export {MailDataRequired};
export {ClientResponse};
export {ResponseError};
export = mail;
4 changes: 4 additions & 0 deletions test/typescript/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ Client.request({
}).then(res => {
res[0].statusCode;
});

// Test Client class attached to the module
const instance: Client.Client = new Client.Client();
instance.setApiKey("MY_SENDGRID_API_KEY");
10 changes: 10 additions & 0 deletions test/typescript/mail.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import { Client } from "@sendgrid/client";
import { MailDataRequired, ClientResponse, ResponseError } from "@sendgrid/mail";
import sgMail = require("@sendgrid/mail");

// Test setClient() method
sgMail.setClient(new Client());

// Test Client used as a type
const client: Client = new Client();
sgMail.setClient(client);

// Test types re-exported from the package root
declare const data: MailDataRequired;
declare const onSent: (err: Error | ResponseError, result: [ClientResponse, {}]) => void;
sgMail.send(data, false, onSent);

// Test setApiKey() method
sgMail.setApiKey("MY_SENDGRID_API_KEY");

Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"noEmit": true,
"module": "commonjs",
"target": "es6",
"baseUrl": ".",
"types": ["node"],
"paths": {
"@sendgrid/*": ["packages/*"]
"@sendgrid/*": ["./packages/*"]
}
},
"include": [
Expand Down