Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e1ccc82

Browse files
authoredJan 5, 2024
chore(internal): improve type signatures (#609)
1 parent 4ea159f commit e1ccc82

File tree

7 files changed

+22
-24
lines changed

7 files changed

+22
-24
lines changed
 

‎src/_shims/index-deno.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const _Blob = Blob;
6464
type _Blob = Blob;
6565
export { _Blob as Blob };
6666

67-
export async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
67+
export async function getMultipartRequestOptions<T = Record<string, unknown>>(
6868
form: FormData,
6969
opts: RequestOptions<T>,
7070
): Promise<RequestOptions<T>> {

‎src/_shims/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export type ReadableStream = SelectType<manual.ReadableStream, auto.ReadableStre
6565
// @ts-ignore
6666
export const ReadableStream: SelectType<typeof manual.ReadableStream, typeof auto.ReadableStream>;
6767

68-
export function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
68+
export function getMultipartRequestOptions<T = Record<string, unknown>>(
6969
form: FormData,
7070
opts: RequestOptions<T>,
7171
): Promise<RequestOptions<T>>;

‎src/_shims/node-runtime.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function fileFromPath(path: string, ...args: any[]): Promise<File> {
4343
const defaultHttpAgent: Agent = new KeepAliveAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
4444
const defaultHttpsAgent: Agent = new KeepAliveAgent.HttpsAgent({ keepAlive: true, timeout: 5 * 60 * 1000 });
4545

46-
async function getMultipartRequestOptions<T extends {} = Record<string, unknown>>(
46+
async function getMultipartRequestOptions<T = Record<string, unknown>>(
4747
form: fd.FormData,
4848
opts: RequestOptions<T>,
4949
): Promise<RequestOptions<T>> {

‎src/_shims/registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface Shims {
1313
Blob: any;
1414
File: any;
1515
ReadableStream: any;
16-
getMultipartRequestOptions: <T extends {} = Record<string, unknown>>(
16+
getMultipartRequestOptions: <T = Record<string, unknown>>(
1717
form: Shims['FormData'],
1818
opts: RequestOptions<T>,
1919
) => Promise<RequestOptions<T>>;

‎src/_shims/web-runtime.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export function getRuntime({ manuallyImported }: { manuallyImported?: boolean }
8484
}
8585
}
8686
),
87-
getMultipartRequestOptions: async <T extends {} = Record<string, unknown>>(
87+
getMultipartRequestOptions: async <T = Record<string, unknown>>(
8888
// @ts-ignore
8989
form: FormData,
9090
opts: RequestOptions<T>,

‎src/core.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -217,27 +217,27 @@ export abstract class APIClient {
217217
return `stainless-node-retry-${uuid4()}`;
218218
}
219219

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

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

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

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

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

240-
private methodRequest<Req extends {}, Rsp>(
240+
private methodRequest<Req, Rsp>(
241241
method: HTTPMethod,
242242
path: string,
243243
opts?: PromiseOrValue<RequestOptions<Req>>,
@@ -269,9 +269,7 @@ export abstract class APIClient {
269269
return null;
270270
}
271271

272-
buildRequest<Req extends {}>(
273-
options: FinalRequestOptions<Req>,
274-
): { req: RequestInit; url: string; timeout: number } {
272+
buildRequest<Req>(options: FinalRequestOptions<Req>): { req: RequestInit; url: string; timeout: number } {
275273
const { method, path, query, headers: headers = {} } = options;
276274

277275
const body =
@@ -373,15 +371,15 @@ export abstract class APIClient {
373371
return APIError.generate(status, error, message, headers);
374372
}
375373

376-
request<Req extends {}, Rsp>(
374+
request<Req, Rsp>(
377375
options: PromiseOrValue<FinalRequestOptions<Req>>,
378376
remainingRetries: number | null = null,
379377
): APIPromise<Rsp> {
380378
return new APIPromise(this.makeRequest(options, remainingRetries));
381379
}
382380

383-
private async makeRequest(
384-
optionsInput: PromiseOrValue<FinalRequestOptions>,
381+
private async makeRequest<Req>(
382+
optionsInput: PromiseOrValue<FinalRequestOptions<Req>>,
385383
retriesRemaining: number | null,
386384
): Promise<APIResponseProps> {
387385
const options = await optionsInput;
@@ -443,7 +441,7 @@ export abstract class APIClient {
443441
return new PagePromise<PageClass, Item>(this, request, Page);
444442
}
445443

446-
buildURL<Req extends Record<string, unknown>>(path: string, query: Req | null | undefined): string {
444+
buildURL<Req>(path: string, query: Req | null | undefined): string {
447445
const url =
448446
isAbsoluteURL(path) ?
449447
new URL(path)
@@ -617,7 +615,7 @@ export abstract class AbstractPage<Item> implements AsyncIterable<Item> {
617615
);
618616
}
619617
const nextOptions = { ...this.options };
620-
if ('params' in nextInfo) {
618+
if ('params' in nextInfo && typeof nextOptions.query === 'object') {
621619
nextOptions.query = { ...nextOptions.query, ...nextInfo.params };
622620
} else if ('url' in nextInfo) {
623621
const params = [...Object.entries(nextOptions.query || {}), ...nextInfo.url.searchParams.entries()];
@@ -715,7 +713,7 @@ export type Headers = Record<string, string | null | undefined>;
715713
export type DefaultQuery = Record<string, string | undefined>;
716714
export type KeysEnum<T> = { [P in keyof Required<T>]: true };
717715

718-
export type RequestOptions<Req extends {} = Record<string, unknown> | Readable> = {
716+
export type RequestOptions<Req = unknown | Record<string, unknown> | Readable> = {
719717
method?: HTTPMethod;
720718
path?: string;
721719
query?: Req | undefined;
@@ -752,7 +750,7 @@ const requestOptionsKeys: KeysEnum<RequestOptions> = {
752750
__binaryResponse: true,
753751
};
754752

755-
export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<string, unknown> | Readable> => {
753+
export const isRequestOptions = (obj: unknown): obj is RequestOptions => {
756754
return (
757755
typeof obj === 'object' &&
758756
obj !== null &&
@@ -761,7 +759,7 @@ export const isRequestOptions = (obj: unknown): obj is RequestOptions<Record<str
761759
);
762760
};
763761

764-
export type FinalRequestOptions<Req extends {} = Record<string, unknown> | Readable> = RequestOptions<Req> & {
762+
export type FinalRequestOptions<Req = unknown | Record<string, unknown> | Readable> = RequestOptions<Req> & {
765763
method: HTTPMethod;
766764
path: string;
767765
};

‎src/uploads.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ export const isMultipartBody = (body: any): body is MultipartBody =>
184184
* Returns a multipart/form-data request if any part of the given request body contains a File / Blob value.
185185
* Otherwise returns the request as is.
186186
*/
187-
export const maybeMultipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
187+
export const maybeMultipartFormRequestOptions = async <T = Record<string, unknown>>(
188188
opts: RequestOptions<T>,
189189
): Promise<RequestOptions<T | MultipartBody>> => {
190190
if (!hasUploadableValue(opts.body)) return opts;
@@ -193,7 +193,7 @@ export const maybeMultipartFormRequestOptions = async <T extends {} = Record<str
193193
return getMultipartRequestOptions(form, opts);
194194
};
195195

196-
export const multipartFormRequestOptions = async <T extends {} = Record<string, unknown>>(
196+
export const multipartFormRequestOptions = async <T = Record<string, unknown>>(
197197
opts: RequestOptions<T>,
198198
): Promise<RequestOptions<T | MultipartBody>> => {
199199
const form = await createForm(opts.body);

0 commit comments

Comments
 (0)
Please sign in to comment.