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

fix(headers): always send lowercase headers and strip undefined (BREAKING in rare cases) #245

Merged
merged 1 commit into from
Jan 4, 2024
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
63 changes: 50 additions & 13 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,7 @@ export abstract class APIClient {
headers[this.idempotencyHeader] = options.idempotencyKey;
}

const reqHeaders: Record<string, string> = {
...(contentLength && { 'Content-Length': contentLength }),
...this.defaultHeaders(options),
...headers,
};
// let builtin fetch set the Content-Type for multipart bodies
if (isMultipartBody(options.body) && shimsKind !== 'node') {
delete reqHeaders['Content-Type'];
}

// Strip any headers being explicitly omitted with null
Object.keys(reqHeaders).forEach((key) => reqHeaders[key] === null && delete reqHeaders[key]);
const reqHeaders = this.buildHeaders({ options, headers, contentLength });

const req: RequestInit = {
method,
Expand All @@ -324,9 +313,35 @@ export abstract class APIClient {
signal: options.signal ?? null,
};

return { req, url, timeout };
}

private buildHeaders({
options,
headers,
contentLength,
}: {
options: FinalRequestOptions;
headers: Record<string, string | null | undefined>;
contentLength: string | null | undefined;
}): Record<string, string> {
const reqHeaders: Record<string, string> = {};
if (contentLength) {
reqHeaders['content-length'] = contentLength;
}

const defaultHeaders = this.defaultHeaders(options);
applyHeadersMut(reqHeaders, defaultHeaders);
applyHeadersMut(reqHeaders, headers);

// let builtin fetch set the Content-Type for multipart bodies
if (isMultipartBody(options.body) && shimsKind !== 'node') {
delete reqHeaders['content-type'];
}

this.validateHeaders(reqHeaders, headers);

return { req, url, timeout };
return reqHeaders;
}

/**
Expand Down Expand Up @@ -1013,6 +1028,28 @@ export function hasOwn(obj: Object, key: string): boolean {
return Object.prototype.hasOwnProperty.call(obj, key);
}

/**
* Copies headers from "newHeaders" onto "targetHeaders",
* using lower-case for all properties,
* ignoring any keys with undefined values,
* and deleting any keys with null values.
*/
function applyHeadersMut(targetHeaders: Headers, newHeaders: Headers): void {
for (const k in newHeaders) {
if (!hasOwn(newHeaders, k)) continue;
const lowerKey = k.toLowerCase();
if (!lowerKey) continue;

const val = newHeaders[k];

if (val === null) {
delete targetHeaders[lowerKey];
} else if (val !== undefined) {
targetHeaders[lowerKey] = val;
}
}
}

export function debug(action: string, ...args: any[]) {
if (typeof process !== 'undefined' && process.env['DEBUG'] === 'true') {
console.log(`Anthropic:DEBUG:${action}`, ...args);
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,17 @@ export class Anthropic extends Core.APIClient {
}

protected override validateHeaders(headers: Core.Headers, customHeaders: Core.Headers) {
if (this.apiKey && headers['X-Api-Key']) {
if (this.apiKey && headers['x-api-key']) {
return;
}
if (customHeaders['X-Api-Key'] === null) {
if (customHeaders['x-api-key'] === null) {
return;
}

if (this.authToken && headers['Authorization']) {
if (this.authToken && headers['authorization']) {
return;
}
if (customHeaders['Authorization'] === null) {
if (customHeaders['authorization'] === null) {
return;
}

Expand Down
29 changes: 22 additions & 7 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ describe('instantiate client', () => {

test('they are used in the request', () => {
const { req } = client.buildRequest({ path: '/foo', method: 'post' });
expect((req.headers as Headers)['X-My-Default-Header']).toEqual('2');
expect((req.headers as Headers)['x-my-default-header']).toEqual('2');
});

test('can be overriden with `undefined`', () => {
test('can ignore `undefined` and leave the default', () => {
const { req } = client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': undefined },
});
expect((req.headers as Headers)['X-My-Default-Header']).toBeUndefined();
expect((req.headers as Headers)['x-my-default-header']).toEqual('2');
});

test('can be overriden with `null`', () => {
test('can be removed with `null`', () => {
const { req } = client.buildRequest({
path: '/foo',
method: 'post',
headers: { 'X-My-Default-Header': null },
});
expect((req.headers as Headers)['X-My-Default-Header']).toBeUndefined();
expect(req.headers as Headers).not.toHaveProperty('x-my-default-header');
});
});

Expand Down Expand Up @@ -185,12 +185,27 @@ describe('request building', () => {
describe('Content-Length', () => {
test('handles multi-byte characters', () => {
const { req } = client.buildRequest({ path: '/foo', method: 'post', body: { value: '—' } });
expect((req.headers as Record<string, string>)['Content-Length']).toEqual('20');
expect((req.headers as Record<string, string>)['content-length']).toEqual('20');
});

test('handles standard characters', () => {
const { req } = client.buildRequest({ path: '/foo', method: 'post', body: { value: 'hello' } });
expect((req.headers as Record<string, string>)['Content-Length']).toEqual('22');
expect((req.headers as Record<string, string>)['content-length']).toEqual('22');
});
});

describe('custom headers', () => {
test('handles undefined', () => {
const { req } = client.buildRequest({
path: '/foo',
method: 'post',
body: { value: 'hello' },
headers: { 'X-Foo': 'baz', 'x-foo': 'bar', 'x-Foo': undefined, 'x-baz': 'bam', 'X-Baz': null },
});
expect((req.headers as Record<string, string>)['x-foo']).toEqual('bar');
expect((req.headers as Record<string, string>)['x-Foo']).toEqual(undefined);
expect((req.headers as Record<string, string>)['X-Foo']).toEqual(undefined);
expect((req.headers as Record<string, string>)['x-baz']).toEqual(undefined);
});
});
});
Expand Down