From 0e8d2611d40ab5af6d16b61018142865f4886201 Mon Sep 17 00:00:00 2001 From: Kyle Baylor Date: Fri, 4 Dec 2020 17:04:05 -0800 Subject: [PATCH 1/2] Enhance client-related typings The MailService has a method to set a client, but it isn't exposed in typings. Adding this so there is an easier way to customize underlying agents used (for keep-alive and other connection-pooling settings) without having compilation problems (or subverting the type system by just using 'any') --- packages/client/src/client.d.ts | 5 ++++- packages/helpers/classes/request.d.ts | 3 +++ packages/mail/src/mail.d.ts | 6 ++++++ test/typescript/client.ts | 4 +++- test/typescript/mail.ts | 4 ++++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/client/src/client.d.ts b/packages/client/src/client.d.ts index d8d7b532e..fb84f7cb1 100644 --- a/packages/client/src/client.d.ts +++ b/packages/client/src/client.d.ts @@ -46,5 +46,8 @@ declare class Client { request(data: ClientRequest, cb?: (err: ResponseError, response: [ClientResponse, any]) => void): Promise<[ClientResponse, any]>; } -declare const client: Client & { Client: typeof Client }; +declare const client: Client; +// @ts-ignore export = client + +export {Client}; diff --git a/packages/helpers/classes/request.d.ts b/packages/helpers/classes/request.d.ts index 7b13383b9..f2145cfb7 100644 --- a/packages/helpers/classes/request.d.ts +++ b/packages/helpers/classes/request.d.ts @@ -1,3 +1,5 @@ +import * as https from 'https'; + type HttpMethod = 'get'|'GET'|'post'|'POST'|'put'|'PUT'|'patch'|'PATCH'|'delete'|'DELETE'; export default interface RequestOptions { @@ -7,4 +9,5 @@ export default interface RequestOptions { qs?: TParams; body?: TData; headers?: object; + httpsAgent?: https.Agent; } diff --git a/packages/mail/src/mail.d.ts b/packages/mail/src/mail.d.ts index 496ea7fd7..31f6ba172 100644 --- a/packages/mail/src/mail.d.ts +++ b/packages/mail/src/mail.d.ts @@ -1,3 +1,4 @@ +import { Client } from "@sendgrid/client"; import {ClientResponse} from "@sendgrid/client/src/response"; import {ResponseError} from "@sendgrid/helpers/classes"; import {MailDataRequired} from "@sendgrid/helpers/classes/mail"; @@ -8,6 +9,11 @@ declare class MailService { */ setApiKey(apiKey: string): void; + /** + * Client to use for invoking underlying API + */ + setClient(client: Client): void; + /** * Twilio Email Auth passthrough for convenience. */ diff --git a/test/typescript/client.ts b/test/typescript/client.ts index f0ccf8689..c0fb0d781 100644 --- a/test/typescript/client.ts +++ b/test/typescript/client.ts @@ -1,4 +1,5 @@ import Client = require("@sendgrid/client"); +import * as https from 'https'; // Test setApiKey() method Client.setApiKey("MY_SENDGRID_API_KEY"); @@ -11,7 +12,8 @@ Client.setDefaultHeader({ // Test setDefaultRequest() method Client.setDefaultRequest({ url: "/test", -}).setDefaultRequest("method", "POST"); +}).setDefaultRequest("method", "POST") + .setDefaultRequest('httpsAgent', new https.Agent()) // Test createHeaders() method Client.createHeaders({ diff --git a/test/typescript/mail.ts b/test/typescript/mail.ts index 126663585..681b29e9a 100644 --- a/test/typescript/mail.ts +++ b/test/typescript/mail.ts @@ -1,8 +1,12 @@ +import { Client } from "@sendgrid/client"; import sgMail = require("@sendgrid/mail"); // Test setApiKey() method sgMail.setApiKey("MY_SENDGRID_API_KEY"); +// Test setClient() method +sgMail.setClient(new Client()); + // Test setSubstitutionWrappers() method sgMail.setSubstitutionWrappers("{{", "}}") From 1427e5e15f642bd1bddc7c187e6e73d5c6d20ce4 Mon Sep 17 00:00:00 2001 From: Kyle Baylor Date: Thu, 17 Dec 2020 10:42:19 -0800 Subject: [PATCH 2/2] Style fix --- packages/mail/src/mail.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/mail/src/mail.d.ts b/packages/mail/src/mail.d.ts index 31f6ba172..8a61c2b76 100644 --- a/packages/mail/src/mail.d.ts +++ b/packages/mail/src/mail.d.ts @@ -1,4 +1,4 @@ -import { Client } from "@sendgrid/client"; +import {Client} from "@sendgrid/client"; import {ClientResponse} from "@sendgrid/client/src/response"; import {ResponseError} from "@sendgrid/helpers/classes"; import {MailDataRequired} from "@sendgrid/helpers/classes/mail";