Skip to content

Commit

Permalink
chore(internal): add helper method (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot authored and rattrayalex committed Aug 26, 2023
1 parent ff33a89 commit 4c6950a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
24 changes: 18 additions & 6 deletions src/core.ts
Expand Up @@ -9,6 +9,7 @@ import {
type RequestInfo,
type RequestInit,
type Response,
type HeadersInit,
} from '@anthropic-ai/sdk/_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
14 changes: 7 additions & 7 deletions src/index.ts
Expand Up @@ -120,9 +120,9 @@ export class Anthropic 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),
'anthropic-version': '2023-06-01',
...this._options.defaultHeaders,
};
Expand All @@ -148,26 +148,26 @@ export class Anthropic extends Core.APIClient {
);
}

protected override authHeaders(): Core.Headers {
const apiKeyHeader = this.apiKeyHeader();
protected override authHeaders(opts: Core.FinalRequestOptions): Core.Headers {
const apiKeyHeader = this.apiKeyHeader(opts);
if (apiKeyHeader != null && !Core.isEmptyObj(apiKeyHeader)) {
return apiKeyHeader;
}
const authTokenBearer = this.authTokenBearer();
const authTokenBearer = this.authTokenBearer(opts);
if (authTokenBearer != null && !Core.isEmptyObj(authTokenBearer)) {
return authTokenBearer;
}
return {};
}

protected apiKeyHeader(): Core.Headers {
protected apiKeyHeader(opts: Core.FinalRequestOptions): Core.Headers {
if (this.apiKey == null) {
return {};
}
return { 'X-Api-Key': this.apiKey };
}

protected authTokenBearer(): Core.Headers {
protected authTokenBearer(opts: Core.FinalRequestOptions): Core.Headers {
if (this.authToken == null) {
return {};
}
Expand Down

0 comments on commit 4c6950a

Please sign in to comment.