diff --git a/packages/client/src/client.d.ts b/packages/client/src/client.d.ts index 84075d1f9..6845ea9ec 100644 --- a/packages/client/src/client.d.ts +++ b/packages/client/src/client.d.ts @@ -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(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; diff --git a/packages/mail/src/mail.d.ts b/packages/mail/src/mail.d.ts index fa827498d..f223e037f 100644 --- a/packages/mail/src/mail.d.ts +++ b/packages/mail/src/mail.d.ts @@ -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; diff --git a/test/typescript/client.ts b/test/typescript/client.ts index c0fb0d781..db472daf6 100644 --- a/test/typescript/client.ts +++ b/test/typescript/client.ts @@ -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"); diff --git a/test/typescript/mail.ts b/test/typescript/mail.ts index fb8199860..cca8c448d 100644 --- a/test/typescript/mail.ts +++ b/test/typescript/mail.ts @@ -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"); diff --git a/tsconfig.json b/tsconfig.json index b8d1ada33..f61d45bf8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,9 +3,9 @@ "noEmit": true, "module": "commonjs", "target": "es6", - "baseUrl": ".", + "types": ["node"], "paths": { - "@sendgrid/*": ["packages/*"] + "@sendgrid/*": ["./packages/*"] } }, "include": [