Skip to content

add custom headers on initial _startOrAuth call #318

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions src/client/sse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ describe("SSEClientTransport", () => {
expect(mockAuthProvider.tokens).toHaveBeenCalled();
});

it("attaches custom header from provider on initial SSE connection", async () => {
mockAuthProvider.tokens.mockResolvedValue({
access_token: "test-token",
token_type: "Bearer"
});
const customHeaders = {
"X-Custom-Header": "custom-value",
};

transport = new SSEClientTransport(baseUrl, {
authProvider: mockAuthProvider,
requestInit: {
headers: customHeaders,
},
});

await transport.start();

expect(lastServerRequest.headers.authorization).toBe("Bearer test-token");
expect(lastServerRequest.headers["x-custom-header"]).toBe("custom-value");
expect(mockAuthProvider.tokens).toHaveBeenCalled();
});

it("attaches auth header from provider on POST requests", async () => {
mockAuthProvider.tokens.mockResolvedValue({
access_token: "test-token",
Expand Down
18 changes: 11 additions & 7 deletions src/client/sse.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should update the commonHeaders method & align it w/ the streamableHttp version. Something like:

  private async _commonHeaders(): Promise<Headers> {
    const headers: HeadersInit = {};
    if (this._authProvider) {
      const tokens = await this._authProvider.tokens();
      if (tokens) {
        headers["Authorization"] = `Bearer ${tokens.access_token}`;
      }
    }

    return new Headers(
      { ...headers, ...this._requestInit?.headers }
    );
  }

Could you please also update the header usage in the send method?

      const headers = await this._commonHeaders();
      headers.set("content-type", "application/json");

Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,17 @@ export class SSEClientTransport implements Transport {
this._eventSource = new EventSource(
this._url.href,
this._eventSourceInit ?? {
fetch: (url, init) => this._commonHeaders().then((headers) => fetch(url, {
...init,
headers: {
...headers,
Accept: "text/event-stream"
}
})),
fetch: async (url, init) => {
const commonHeaders = await this._commonHeaders();
const allHeaders = { ...commonHeaders, ...this._requestInit?.headers};
return fetch(url, {
...init,
headers: {
...allHeaders,
Accept: "text/event-stream"
}
Comment on lines +117 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const commonHeaders = await this._commonHeaders();
const allHeaders = { ...commonHeaders, ...this._requestInit?.headers};
return fetch(url, {
...init,
headers: {
...allHeaders,
Accept: "text/event-stream"
}
const headers = await this._commonHeaders();
headers.set("Accept", "text/event-stream");
return fetch(url, {
...init,
headers,
})

})
}
},
);
this._abortController = new AbortController();
Expand Down