Skip to content

Commit

Permalink
chore(internal): add helper method (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored Aug 25, 2023
1 parent 5f89c5e commit 6d8cff0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type RequestInfo,
type RequestInit,
type Response,
type HeadersInit,
} from 'openai/_shims/fetch';
export { type Response };
import { isMultipartBody } from './uploads';
Expand Down Expand Up @@ -153,7 +154,7 @@ export abstract class APIClient {
this.fetch = overridenFetch ?? fetch;
}

protected authHeaders(): Headers {
protected authHeaders(opts: FinalRequestOptions): Headers {
return {};
}

Expand All @@ -165,13 +166,13 @@ export abstract class APIClient {
* Authorization: 'Bearer 123',
* }
*/
protected defaultHeaders(): Headers {
protected defaultHeaders(opts: FinalRequestOptions): Headers {
return {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': this.getUserAgent(),
...getPlatformHeaders(),
...this.authHeaders(),
...this.authHeaders(opts),
};
}

Expand Down Expand Up @@ -272,7 +273,7 @@ export abstract class APIClient {

const reqHeaders: Record<string, string> = {
...(contentLength && { 'Content-Length': contentLength }),
...this.defaultHeaders(),
...this.defaultHeaders(options),
...headers,
};
// let builtin fetch set the Content-Type for multipart bodies
Expand Down Expand Up @@ -304,7 +305,18 @@ export abstract class APIClient {
* This is useful for cases where you want to add certain headers based off of
* the request properties, e.g. `method` or `url`.
*/
protected async prepareRequest(request: RequestInit, { url }: { url: string }): Promise<void> {}
protected async prepareRequest(
request: RequestInit,
{ url, options }: { url: string; options: FinalRequestOptions },
): Promise<void> {}

protected parseHeaders(headers: HeadersInit | null | undefined): Record<string, string> {
return (
!headers ? {}
: Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header]))
: { ...headers }
);
}

protected makeStatusError(
status: number | undefined,
Expand Down Expand Up @@ -333,7 +345,7 @@ export abstract class APIClient {

const { req, url, timeout } = this.buildRequest(options);

await this.prepareRequest(req, { url });
await this.prepareRequest(req, { url, options });

debug('request', url, options, req.headers);

Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,15 @@ export class OpenAI extends Core.APIClient {
return this._options.defaultQuery;
}

protected override defaultHeaders(): Core.Headers {
protected override defaultHeaders(opts: Core.FinalRequestOptions): Core.Headers {
return {
...super.defaultHeaders(),
...super.defaultHeaders(opts),
'OpenAI-Organization': this.organization,
...this._options.defaultHeaders,
};
}

protected override authHeaders(): Core.Headers {
protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
return { Authorization: `Bearer ${this.apiKey}` };
}

Expand Down

0 comments on commit 6d8cff0

Please sign in to comment.