Skip to content

Commit

Permalink
chore(internal): improve type signatures (#609)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jan 5, 2024
1 parent 4ea159f commit e1ccc82
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/_shims/index-deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const _Blob = Blob;
type _Blob = Blob;
export { _Blob as Blob };

export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
export async function getMultipartRequestOptions<T = Record<string, unknown>>(
form: FormData,
opts: RequestOptions<T>,
): Promise<RequestOptions<T>> {
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export type ReadableStream = SelectType<manual.ReadableStream, auto.ReadableStre
// @ts-ignore
export const ReadableStream: SelectType<typeof manual.ReadableStream, typeof auto.ReadableStream>;

export function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
export function getMultipartRequestOptions<T = Record<string, unknown>>(
form: FormData,
opts: RequestOptions<T>,
): Promise<RequestOptions<T>>;
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/node-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async function fileFromPath(path: string, ...args: any[]): Promise<File> {
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });

async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
async function getMultipartRequestOptions<T = Record<string, unknown>>(
form: fd.FormData,
opts: RequestOptions<T>,
): Promise<RequestOptions<T>> {
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface Shims {
Blob: any;
File: any;
ReadableStream: any;
getMultipartRequestOptions: <T extends {} = Record<string, unknown>>(
getMultipartRequestOptions: <T = Record<string, unknown>>(
form: Shims['FormData'],
opts: RequestOptions<T>,
) => Promise<RequestOptions<T>>;
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/web-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export function getRuntime({ manuallyImported }: { manuallyImported?: boolean }
}
}
),
getMultipartRequestOptions: async <T extends {} = Record<string, unknown>>(
getMultipartRequestOptions: async <T = Record<string, unknown>>(
// @ts-ignore
form: FormData,
opts: RequestOptions<T>,
Expand Down
32 changes: 15 additions & 17 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,27 +217,27 @@ export abstract class APIClient {
return `stainless-node-retry-${uuid4()}`;
}

get<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
get<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
return this.methodRequest('get', path, opts);
}

post<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
post<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
return this.methodRequest('post', path, opts);
}

patch<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
patch<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
return this.methodRequest('patch', path, opts);
}

put<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
put<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
return this.methodRequest('put', path, opts);
}

delete<Req extends {}, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
delete<Req, Rsp>(path: string, opts?: PromiseOrValue<RequestOptions<Req>>): APIPromise<Rsp> {
return this.methodRequest('delete', path, opts);
}

private methodRequest<Req extends {}, Rsp>(
private methodRequest<Req, Rsp>(
method: HTTPMethod,
path: string,
opts?: PromiseOrValue<RequestOptions<Req>>,
Expand Down Expand Up @@ -269,9 +269,7 @@ export abstract class APIClient {
return null;
}

buildRequest<Req extends {}>(
options: FinalRequestOptions<Req>,
): { req: RequestInit; url: string; timeout: number } {
buildRequest<Req>(options: FinalRequestOptions<Req>): { req: RequestInit; url: string; timeout: number } {
const { method, path, query, headers: headers = {} } = options;

const body =
Expand Down Expand Up @@ -373,15 +371,15 @@ export abstract class APIClient {
return APIError.generate(status, error, message, headers);
}

request<Req extends {}, Rsp>(
request<Req, Rsp>(
options: PromiseOrValue<FinalRequestOptions<Req>>,
remainingRetries: number | null = null,
): APIPromise<Rsp> {
return new APIPromise(this.makeRequest(options, remainingRetries));
}

private async makeRequest(
optionsInput: PromiseOrValue<FinalRequestOptions>,
private async makeRequest<Req>(
optionsInput: PromiseOrValue<FinalRequestOptions<Req>>,
retriesRemaining: number | null,
): Promise<APIResponseProps> {
const options = await optionsInput;
Expand Down Expand Up @@ -443,7 +441,7 @@ export abstract class APIClient {
return new PagePromise<PageClass, Item>(this, request, Page);
}

buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
buildURL<Req>(path: string, query: Req | null | undefined): string {
const url =
isAbsoluteURL(path) ?
new URL(path)
Expand Down Expand Up @@ -617,7 +615,7 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
);
}
const nextOptions = { ...this.options };
if ('params' in nextInfo) {
if ('params' in nextInfo && typeof nextOptions.query === 'object') {
nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
} else if ('url' in nextInfo) {
const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
Expand Down Expand Up @@ -715,7 +713,7 @@ export type Headers = Record<string, string | null | undefined>;
export type DefaultQuery = Record<string, string | undefined>;
export type KeysEnum<T> = { [P in keyof Required<T>]: true };

export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> = {
method?: HTTPMethod;
path?: string;
query?: Req | undefined;
Expand Down Expand Up @@ -752,7 +750,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
__binaryResponse: true,
};

export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<string, unknown> | Readable> => {
export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
return (
typeof obj === 'object' &&
obj !== null &&
Expand All @@ -761,7 +759,7 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<str
);
};

export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Readable> = RequestOptions<Req> & {
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable> = RequestOptions<Req> & {
method: HTTPMethod;
path: string;
};
Expand Down
4 changes: 2 additions & 2 deletions src/uploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export const isMultipartBody = (body: any): body is MultipartBody =>
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
* Otherwise returns the request as is.
*/
export const maybeMultipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
export const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(
opts: RequestOptions<T>,
): Promise<RequestOptions<T | MultipartBody>> => {
if (!hasUploadableValue(opts.body)) return opts;
Expand All @@ -193,7 +193,7 @@ export const maybeMultipartFormRequestOptions = async <T extends {} = Record<str
return getMultipartRequestOptions(form, opts);
};

export const multipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
export const multipartFormRequestOptions = async <T = Record<string, unknown>>(
opts: RequestOptions<T>,
): Promise<RequestOptions<T | MultipartBody>> => {
const form = await createForm(opts.body);
Expand Down

0 comments on commit e1ccc82

Please sign in to comment.