fix: fix review comments, again😉
- Remove unnecessary auth header name check - Extract operations on headers into a function
This commit is contained in:
parent
13f736033a
commit
2870441fea
2 changed files with 39 additions and 44 deletions
|
|
@ -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
|
// Add custom header name as a special request header to let the server know which header to pass through
|
||||||
if (authHeaderName.toLowerCase() !== "authorization") {
|
if (authHeaderName.toLowerCase() !== "authorization") {
|
||||||
headers[authHeaderName] = token;
|
headers[authHeaderName] = token;
|
||||||
if (headerName) {
|
|
||||||
headers["x-custom-auth-header"] = headerName;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
headers[authHeaderName] = `Bearer ${token}`;
|
headers[authHeaderName] = `Bearer ${token}`;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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();
|
const app = express();
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
|
|
@ -79,27 +116,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
|
||||||
return transport;
|
return transport;
|
||||||
} else if (transportType === "sse") {
|
} else if (transportType === "sse") {
|
||||||
const url = query.url as string;
|
const url = query.url as string;
|
||||||
const headers: HeadersInit = {
|
|
||||||
Accept: "text/event-stream",
|
|
||||||
};
|
|
||||||
|
|
||||||
for (const key of SSE_HEADERS_PASSTHROUGH) {
|
const headers = getHttpHeaders(req, transportType);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
|
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");
|
console.log("Connected to SSE transport");
|
||||||
return transport;
|
return transport;
|
||||||
} else if (transportType === "streamable-http") {
|
} else if (transportType === "streamable-http") {
|
||||||
const headers: HeadersInit = {
|
const headers = getHttpHeaders(req, transportType);
|
||||||
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 transport = new StreamableHTTPClientTransport(
|
const transport = new StreamableHTTPClientTransport(
|
||||||
new URL(query.url as string),
|
new URL(query.url as string),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue