From 7229189d663df0ab724c3b40d68fb1b41bce1724 Mon Sep 17 00:00:00 2001 From: Stainless Bot Date: Fri, 25 Aug 2023 20:14:47 +0000 Subject: [PATCH] chore(internal): add helper method --- src/core.ts | 24 ++++++++++++++++++------ src/index.ts | 6 +++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/core.ts b/src/core.ts index 814370617..db7cb7269 100644 --- a/src/core.ts +++ b/src/core.ts @@ -9,6 +9,7 @@ import { type RequestInfo, type RequestInit, type Response, + type HeadersInit, } from 'openai/_shims/fetch'; export { type Response }; import { isMultipartBody } from './uploads'; @@ -153,7 +154,7 @@ export abstract class APIClient { this.fetch = overridenFetch ?? fetch; } - protected authHeaders(): Headers { + protected authHeaders(opts: FinalRequestOptions): Headers { return {}; } @@ -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), }; } @@ -272,7 +273,7 @@ export abstract class APIClient { const reqHeaders: Record = { ...(contentLength && { 'Content-Length': contentLength }), - ...this.defaultHeaders(), + ...this.defaultHeaders(options), ...headers, }; // let builtin fetch set the Content-Type for multipart bodies @@ -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 {} + protected async prepareRequest( + request: RequestInit, + { url, options }: { url: string; options: FinalRequestOptions }, + ): Promise {} + + protected parseHeaders(headers: HeadersInit | null | undefined): Record { + return ( + !headers ? {} + : Symbol.iterator in headers ? Object.fromEntries(Array.from(headers).map((header) => [...header])) + : { ...headers } + ); + } protected makeStatusError( status: number | undefined, @@ -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); diff --git a/src/index.ts b/src/index.ts index 4e83483ae..e8013a515 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}` }; }