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

feat(client): Add generic typing to @sendgrid/client request function #1399

Open
wants to merge 1 commit 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
25 changes: 17 additions & 8 deletions packages/client/src/client.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ResponseError} from "@sendgrid/helpers/classes";
import {ClientRequest} from "@sendgrid/client/src/request";
import {ClientResponse} from "@sendgrid/client/src/response";
import { ResponseError } from "@sendgrid/helpers/classes";
import { ClientRequest } from "@sendgrid/client/src/request";
import { ClientResponse } from "@sendgrid/client/src/response";

declare class Client {
constructor();
Expand All @@ -23,12 +23,15 @@ declare class Client {
/**
* Set default header
*/
setDefaultHeader(key: string | { [s: string]: string }, value ?: string): this;
setDefaultHeader(key: string | { [s: string]: string }, value?: string): this;

/**
* Set default request
*/
setDefaultRequest<K extends keyof ClientRequest>(key: K | ClientRequest, value ?: ClientRequest[K]): this;
setDefaultRequest<K extends keyof ClientRequest>(
key: K | ClientRequest,
value?: ClientRequest[K]
): this;

/**
* Sets the data residency as per region provided
Expand All @@ -48,11 +51,17 @@ declare class Client {
/**
* Do a request
*/
request(data: ClientRequest, cb?: (err: ResponseError, response: [ClientResponse, any]) => void): Promise<[ClientResponse, any]>;
request<TResponse = object>(
data: ClientRequest,
cb?: (
err: ResponseError,
response: [ClientResponse<TResponse>, any]
) => void
): Promise<[ClientResponse<TResponse>, any]>;
}

declare const client: Client;
// @ts-ignore
export = client
export = client;

export {Client};
export { Client };
5 changes: 4 additions & 1 deletion packages/client/src/request.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import RequestOptions from "@sendgrid/helpers/classes/request";

export type ClientRequest = RequestOptions;
export type ClientRequest<TData = any, TParams = any> = RequestOptions<
TData,
TParams
>;
2 changes: 1 addition & 1 deletion packages/client/src/response.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import Response from "@sendgrid/helpers/classes/response";

export type ClientResponse = Response;
export type ClientResponse<T = object> = Response<T>;
Loading