refactor: improve code formatting and readability across multiple files

This commit is contained in:
jeetul.samaiya 2025-09-01 12:15:37 +05:30
commit cb8c45456b
2 changed files with 79 additions and 42 deletions

View file

@ -779,7 +779,10 @@ describe("useConnection", () => {
expect(headers).toHaveProperty("Authorization", "Bearer token123");
expect(headers).toHaveProperty("X-Tenant-ID", "acme-inc");
expect(headers).toHaveProperty("X-Environment", "staging");
expect(headers).toHaveProperty("x-custom-auth-headers", JSON.stringify(["X-Tenant-ID", "X-Environment"]));
expect(headers).toHaveProperty(
"x-custom-auth-headers",
JSON.stringify(["X-Tenant-ID", "X-Environment"]),
);
});
test("ignores disabled custom headers", async () => {
@ -815,9 +818,7 @@ describe("useConnection", () => {
headerName: "X-Custom-Auth",
};
const { result } = renderHook(() =>
useConnection(propsWithLegacyAuth),
);
const { result } = renderHook(() => useConnection(propsWithLegacyAuth));
await act(async () => {
await result.current.connect();
@ -825,7 +826,10 @@ describe("useConnection", () => {
const headers = mockSSETransport.options?.requestInit?.headers;
expect(headers).toHaveProperty("X-Custom-Auth", "legacy-token");
expect(headers).toHaveProperty("x-custom-auth-headers", JSON.stringify(["X-Custom-Auth"]));
expect(headers).toHaveProperty(
"x-custom-auth-headers",
JSON.stringify(["X-Custom-Auth"]),
);
});
test("uses OAuth token when no custom headers or legacy auth provided", async () => {
@ -833,9 +837,7 @@ describe("useConnection", () => {
...defaultProps,
};
const { result } = renderHook(() =>
useConnection(propsWithoutAuth),
);
const { result } = renderHook(() => useConnection(propsWithoutAuth));
await act(async () => {
await result.current.connect();
@ -857,9 +859,7 @@ describe("useConnection", () => {
headerName: "Authorization",
};
const { result } = renderHook(() =>
useConnection(propsWithBothAuth),
);
const { result } = renderHook(() => useConnection(propsWithBothAuth));
await act(async () => {
await result.current.connect();

View file

@ -17,28 +17,35 @@ export const createHeaderFromBearerToken = (
headerName?: string,
): CustomHeader => ({
name: headerName || "Authorization",
value: headerName?.toLowerCase() === "authorization" || !headerName
? `Bearer ${bearerToken}`
: bearerToken,
value:
headerName?.toLowerCase() === "authorization" || !headerName
? `Bearer ${bearerToken}`
: bearerToken,
enabled: true,
});
export const getEnabledHeaders = (headers: CustomHeaders): CustomHeaders => {
return headers.filter(header => header.enabled && header.name.trim() && header.value.trim());
return headers.filter(
(header) => header.enabled && header.name.trim() && header.value.trim(),
);
};
export const headersToRecord = (headers: CustomHeaders): Record<string, string> => {
export const headersToRecord = (
headers: CustomHeaders,
): Record<string, string> => {
const enabledHeaders = getEnabledHeaders(headers);
const record: Record<string, string> = {};
enabledHeaders.forEach(header => {
enabledHeaders.forEach((header) => {
record[header.name.trim()] = header.value.trim();
});
return record;
};
export const recordToHeaders = (record: Record<string, string>): CustomHeaders => {
export const recordToHeaders = (
record: Record<string, string>,
): CustomHeaders => {
return Object.entries(record).map(([name, value]) => ({
name,
value,
@ -54,6 +61,6 @@ export const migrateFromLegacyAuth = (
if (!bearerToken) {
return [];
}
return [createHeaderFromBearerToken(bearerToken, headerName)];
};