From 895e1f8b47137fc3e870b4964931a6a7bccd8909 Mon Sep 17 00:00:00 2001 From: Takeshi Yoneda Date: Wed, 23 Oct 2024 10:11:32 -0700 Subject: [PATCH] Correctly set Accept header to text/event-stream for streaming Signed-off-by: Takeshi Yoneda --- src/core.ts | 2 +- tests/index.test.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/core.ts b/src/core.ts index 9d90178ab..86dc31cb9 100644 --- a/src/core.ts +++ b/src/core.ts @@ -228,7 +228,7 @@ export abstract class APIClient { */ protected defaultHeaders(opts: FinalRequestOptions): Headers { return { - Accept: 'application/json', + Accept: opts.stream ? 'text/event-stream' : 'application/json', 'Content-Type': 'application/json', 'User-Agent': this.getUserAgent(), ...getPlatformHeaders(), diff --git a/tests/index.test.ts b/tests/index.test.ts index f39571121..f8521622f 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -31,6 +31,11 @@ describe('instantiate client', () => { expect((req.headers as Headers)['x-my-default-header']).toEqual('2'); }); + test('streaming request must have Accept header set to text/event-stream', () => { + const { req } = client.buildRequest({ path: '/', method: 'get', stream: true }); + expect((req.headers as Headers)['accept']).toEqual('text/event-stream'); + }); + test('can ignore `undefined` and leave the default', () => { const { req } = client.buildRequest({ path: '/foo',