Skip to content
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
5 changes: 5 additions & 0 deletions packages/client/src/core/apply-request-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ import type { ExecutionContext } from './execution-context';

export function applyRequestMetadata(execution: ExecutionContext): void {
execution.headers['x-request-id'] = execution.headers['x-request-id'] ?? execution.requestId;

if (execution.request.idempotencyKey) {
execution.headers['idempotency-key'] =
execution.headers['idempotency-key'] ?? execution.request.idempotencyKey;
}
}
1 change: 1 addition & 0 deletions packages/client/src/types/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RequestConfig = {
retry?: RetryConfig;
signal?: AbortSignal;
requestId?: string;
idempotencyKey?: string;
validateResponse?: ResponseValidator;
};

Expand Down
67 changes: 67 additions & 0 deletions packages/client/tests/integration/headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,71 @@ describe('request headers', () => {
expect(typeof requestId).toBe('string');
expect(requestId!.length).toBeGreaterThan(0);
});

it('propagates idempotencyKey to idempotency-key header', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: {
'content-type': 'application/json',
},
}),
);

const client = createClient({
baseUrl: 'https://api.test.com',
fetch: fetchMock,
});

await client.post(
'/payments',
{
amount: 100,
},
{
idempotencyKey: 'idem_123',
},
);

expect(fetchMock).toHaveBeenCalledTimes(1);

const init = getFirstFetchInit(fetchMock);
const headers = init.headers as Record<string, string>;

expect(headers['idempotency-key']).toBe('idem_123');
});

it('prefers explicit idempotency-key header over idempotencyKey option', async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: {
'content-type': 'application/json',
},
}),
);

const client = createClient({
baseUrl: 'https://api.test.com',
fetch: fetchMock,
});

await client.post(
'/payments',
{
amount: 100,
},
{
idempotencyKey: 'idem_from_option',
headers: {
'idempotency-key': 'idem_from_header',
},
},
);

const init = getFirstFetchInit(fetchMock);
const headers = init.headers as Record<string, string>;

expect(headers['idempotency-key']).toBe('idem_from_header');
});
});
40 changes: 40 additions & 0 deletions packages/client/tests/unit/apply-request-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,44 @@ describe('applyRequestMetadata', () => {

expect(execution.headers['x-request-id']).toBe('existing-request-id');
});

it('adds idempotency-key header when idempotencyKey is provided', () => {
const execution = createExecutionContext({
request: {
method: 'POST',
path: '/payments',
idempotencyKey: 'idem-123',
},
});

applyRequestMetadata(execution);

expect(execution.headers['idempotency-key']).toBe('idem-123');
});

it('does not overwrite an existing idempotency-key header', () => {
const execution = createExecutionContext({
request: {
method: 'POST',
path: '/payments',
idempotencyKey: 'idem-from-option',
},
headers: {
accept: 'application/json',
'idempotency-key': 'idem-from-header',
},
});

applyRequestMetadata(execution);

expect(execution.headers['idempotency-key']).toBe('idem-from-header');
});

it('does not add idempotency-key header when idempotencyKey is not provided', () => {
const execution = createExecutionContext();

applyRequestMetadata(execution);

expect(execution.headers).not.toHaveProperty('idempotency-key');
});
});
Loading