Skip to content

Commit

Permalink
chore: enhance client-related typings (#1227)
Browse files Browse the repository at this point in the history
* 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')
  • Loading branch information
kyle221b authored Jan 12, 2021
1 parent cc100a6 commit 3c60f78
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/client/src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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};
3 changes: 3 additions & 0 deletions packages/helpers/classes/request.d.ts
Original file line number Diff line number Diff line change
@@ -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<TData = any, TParams = object> {
Expand All @@ -7,4 +9,5 @@ export default interface RequestOptions<TData = any, TParams = object> {
qs?: TParams;
body?: TData;
headers?: object;
httpsAgent?: https.Agent;
}
6 changes: 6 additions & 0 deletions packages/mail/src/mail.d.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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.
*/
Expand Down
4 changes: 3 additions & 1 deletion test/typescript/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Client = require("@sendgrid/client");
import * as https from 'https';

// Test setApiKey() method
Client.setApiKey("MY_SENDGRID_API_KEY");
Expand All @@ -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({
Expand Down
4 changes: 4 additions & 0 deletions test/typescript/mail.ts
Original file line number Diff line number Diff line change
@@ -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("{{", "}}")

Expand Down

0 comments on commit 3c60f78

Please sign in to comment.