fix: fix review comments, again😉

- Remove unnecessary auth header name check
- Extract operations on headers into a function
This commit is contained in:
xiaoli 2025-05-25 11:17:37 +08:00
commit 2870441fea
2 changed files with 39 additions and 44 deletions

View file

@ -301,9 +301,6 @@ export function useConnection({
// Add custom header name as a special request header to let the server know which header to pass through
if (authHeaderName.toLowerCase() !== "authorization") {
headers[authHeaderName] = token;
if (headerName) {
headers["x-custom-auth-header"] = headerName;
}
} else {
headers[authHeaderName] = `Bearer ${token}`;
}

View file

@ -41,6 +41,43 @@ const { values } = parseArgs({
},
});
// Function to get HTTP headers.
// Supports only "sse" and "streamable-http" transport types.
const getHttpHeaders = (
req: express.Request,
transportType: string,
): HeadersInit => {
const headers: HeadersInit = {
Accept:
transportType === "sse"
? "text/event-stream"
: "text/event-stream, application/json",
};
const defaultHeaders =
transportType === "sse"
? SSE_HEADERS_PASSTHROUGH
: STREAMABLE_HTTP_HEADERS_PASSTHROUGH;
for (const key of defaultHeaders) {
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
if (req.headers[customHeaderName] !== undefined) {
const value = req.headers[customHeaderName];
headers[customHeaderName] = value as string;
}
}
return headers;
};
const app = express();
app.use(cors());
app.use((req, res, next) => {
@ -79,27 +116,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
return transport;
} else if (transportType === "sse") {
const url = query.url as string;
const headers: HeadersInit = {
Accept: "text/event-stream",
};
for (const key of SSE_HEADERS_PASSTHROUGH) {
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
if (req.headers[customHeaderName] !== undefined) {
const value = req.headers[customHeaderName];
headers[customHeaderName] = value as string;
}
}
const headers = getHttpHeaders(req, transportType);
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
@ -116,27 +134,7 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
console.log("Connected to SSE transport");
return transport;
} else if (transportType === "streamable-http") {
const headers: HeadersInit = {
Accept: "text/event-stream, application/json",
};
for (const key of STREAMABLE_HTTP_HEADERS_PASSTHROUGH) {
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
if (req.headers[customHeaderName] !== undefined) {
const value = req.headers[customHeaderName];
headers[customHeaderName] = value as string;
}
}
const headers = getHttpHeaders(req, transportType);
const transport = new StreamableHTTPClientTransport(
new URL(query.url as string),