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

chore(internal): add helper method #255

Merged
merged 1 commit into from
Aug 25, 2023
Merged
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
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