Skip to content

Commit cb8c454

Browse files
refactor: improve code formatting and readability across multiple files
1 parent c5acc63 commit cb8c454

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

client/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ import ElicitationTab, {
7474
PendingElicitationRequest,
7575
ElicitationResponse,
7676
} from "./components/ElicitationTab";
77-
import { CustomHeaders, migrateFromLegacyAuth } from "./lib/types/customHeaders";
77+
import {
78+
CustomHeaders,
79+
migrateFromLegacyAuth,
80+
} from "./lib/types/customHeaders";
7881

7982
const CONFIG_LOCAL_STORAGE_KEY = "inspectorConfig_v1";
8083

client/src/components/CustomHeaders.tsx

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@ import { Input } from "@/components/ui/input";
44
import { Textarea } from "@/components/ui/textarea";
55
import { Switch } from "@/components/ui/switch";
66
import { Plus, Trash2, Eye, EyeOff } from "lucide-react";
7-
import { CustomHeaders as CustomHeadersType, CustomHeader, createEmptyHeader } from "@/lib/types/customHeaders";
7+
import {
8+
CustomHeaders as CustomHeadersType,
9+
CustomHeader,
10+
createEmptyHeader,
11+
} from "@/lib/types/customHeaders";
812

913
interface CustomHeadersProps {
1014
headers: CustomHeadersType;
1115
onChange: (headers: CustomHeadersType) => void;
1216
className?: string;
1317
}
1418

15-
const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) => {
19+
const CustomHeaders = ({
20+
headers,
21+
onChange,
22+
className,
23+
}: CustomHeadersProps) => {
1624
const [isJsonMode, setIsJsonMode] = useState(false);
1725
const [jsonValue, setJsonValue] = useState("");
1826
const [jsonError, setJsonError] = useState<string | null>(null);
1927
const [visibleValues, setVisibleValues] = useState<Set<number>>(new Set());
2028

21-
const updateHeader = (index: number, field: keyof CustomHeader, value: string | boolean) => {
29+
const updateHeader = (
30+
index: number,
31+
field: keyof CustomHeader,
32+
value: string | boolean,
33+
) => {
2234
const newHeaders = [...headers];
2335
newHeaders[index] = { ...newHeaders[index], [field]: value };
2436
onChange(newHeaders);
@@ -58,16 +70,22 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
5870
const switchToFormMode = () => {
5971
try {
6072
const parsed = JSON.parse(jsonValue);
61-
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
73+
if (
74+
typeof parsed !== "object" ||
75+
parsed === null ||
76+
Array.isArray(parsed)
77+
) {
6278
setJsonError("JSON must be an object with string key-value pairs");
6379
return;
6480
}
6581

66-
const newHeaders: CustomHeadersType = Object.entries(parsed).map(([name, value]) => ({
67-
name,
68-
value: String(value),
69-
enabled: true,
70-
}));
82+
const newHeaders: CustomHeadersType = Object.entries(parsed).map(
83+
([name, value]) => ({
84+
name,
85+
value: String(value),
86+
enabled: true,
87+
}),
88+
);
7189

7290
onChange(newHeaders);
7391
setJsonError(null);
@@ -86,7 +104,9 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
86104
return (
87105
<div className={`space-y-3 ${className}`}>
88106
<div className="flex justify-between items-center gap-2">
89-
<h4 className="text-sm font-semibold flex-shrink-0">Custom Headers (JSON)</h4>
107+
<h4 className="text-sm font-semibold flex-shrink-0">
108+
Custom Headers (JSON)
109+
</h4>
90110
<Button
91111
type="button"
92112
variant="outline"
@@ -104,9 +124,7 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
104124
placeholder='{\n "Authorization": "Bearer token123",\n "X-Tenant-ID": "acme-inc",\n "X-Environment": "staging"\n}'
105125
className="font-mono text-sm min-h-[100px] resize-none"
106126
/>
107-
{jsonError && (
108-
<p className="text-sm text-red-600">{jsonError}</p>
109-
)}
127+
{jsonError && <p className="text-sm text-red-600">{jsonError}</p>}
110128
<p className="text-xs text-muted-foreground">
111129
Enter headers as a JSON object with string key-value pairs.
112130
</p>
@@ -156,7 +174,9 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
156174
>
157175
<Switch
158176
checked={header.enabled}
159-
onCheckedChange={(enabled) => updateHeader(index, "enabled", enabled)}
177+
onCheckedChange={(enabled) =>
178+
updateHeader(index, "enabled", enabled)
179+
}
160180
className="shrink-0 mt-2"
161181
/>
162182
<div className="flex-1 min-w-0 space-y-2">
@@ -170,7 +190,9 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
170190
<Input
171191
placeholder="Header Value"
172192
value={header.value}
173-
onChange={(e) => updateHeader(index, "value", e.target.value)}
193+
onChange={(e) =>
194+
updateHeader(index, "value", e.target.value)
195+
}
174196
type={visibleValues.has(index) ? "text" : "password"}
175197
className="font-mono text-xs pr-8"
176198
/>
@@ -205,7 +227,8 @@ const CustomHeaders = ({ headers, onChange, className }: CustomHeadersProps) =>
205227

206228
{headers.length > 0 && (
207229
<p className="text-xs text-muted-foreground">
208-
Use the toggle to enable/disable headers. Only enabled headers with both name and value will be sent.
230+
Use the toggle to enable/disable headers. Only enabled headers with
231+
both name and value will be sent.
209232
</p>
210233
)}
211234
</div>

client/src/components/ui/switch.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const Switch = React.forwardRef<
1010
<SwitchPrimitives.Root
1111
className={cn(
1212
"peer inline-flex h-5 w-9 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=unchecked]:bg-input",
13-
className
13+
className,
1414
)}
1515
{...props}
1616
ref={ref}
1717
>
1818
<SwitchPrimitives.Thumb
1919
className={cn(
20-
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0"
20+
"pointer-events-none block h-4 w-4 rounded-full bg-background shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-4 data-[state=unchecked]:translate-x-0",
2121
)}
2222
/>
2323
</SwitchPrimitives.Root>

client/src/lib/hooks/__tests__/useConnection.test.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,10 @@ describe("useConnection", () => {
779779
expect(headers).toHaveProperty("Authorization", "Bearer token123");
780780
expect(headers).toHaveProperty("X-Tenant-ID", "acme-inc");
781781
expect(headers).toHaveProperty("X-Environment", "staging");
782-
expect(headers).toHaveProperty("x-custom-auth-headers", JSON.stringify(["X-Tenant-ID", "X-Environment"]));
782+
expect(headers).toHaveProperty(
783+
"x-custom-auth-headers",
784+
JSON.stringify(["X-Tenant-ID", "X-Environment"]),
785+
);
783786
});
784787

785788
test("ignores disabled custom headers", async () => {
@@ -815,27 +818,26 @@ describe("useConnection", () => {
815818
headerName: "X-Custom-Auth",
816819
};
817820

818-
const { result } = renderHook(() =>
819-
useConnection(propsWithLegacyAuth),
820-
);
821+
const { result } = renderHook(() => useConnection(propsWithLegacyAuth));
821822

822823
await act(async () => {
823824
await result.current.connect();
824825
});
825826

826827
const headers = mockSSETransport.options?.requestInit?.headers;
827828
expect(headers).toHaveProperty("X-Custom-Auth", "legacy-token");
828-
expect(headers).toHaveProperty("x-custom-auth-headers", JSON.stringify(["X-Custom-Auth"]));
829+
expect(headers).toHaveProperty(
830+
"x-custom-auth-headers",
831+
JSON.stringify(["X-Custom-Auth"]),
832+
);
829833
});
830834

831835
test("uses OAuth token when no custom headers or legacy auth provided", async () => {
832836
const propsWithoutAuth = {
833837
...defaultProps,
834838
};
835839

836-
const { result } = renderHook(() =>
837-
useConnection(propsWithoutAuth),
838-
);
840+
const { result } = renderHook(() => useConnection(propsWithoutAuth));
839841

840842
await act(async () => {
841843
await result.current.connect();
@@ -857,9 +859,7 @@ describe("useConnection", () => {
857859
headerName: "Authorization",
858860
};
859861

860-
const { result } = renderHook(() =>
861-
useConnection(propsWithBothAuth),
862-
);
862+
const { result } = renderHook(() => useConnection(propsWithBothAuth));
863863

864864
await act(async () => {
865865
await result.current.connect();

client/src/lib/types/customHeaders.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,35 @@ export const createHeaderFromBearerToken = (
1717
headerName?: string,
1818
): CustomHeader => ({
1919
name: headerName || "Authorization",
20-
value: headerName?.toLowerCase() === "authorization" || !headerName
21-
? `Bearer ${bearerToken}`
22-
: bearerToken,
20+
value:
21+
headerName?.toLowerCase() === "authorization" || !headerName
22+
? `Bearer ${bearerToken}`
23+
: bearerToken,
2324
enabled: true,
2425
});
2526

2627
export const getEnabledHeaders = (headers: CustomHeaders): CustomHeaders => {
27-
return headers.filter(header => header.enabled && header.name.trim() && header.value.trim());
28+
return headers.filter(
29+
(header) => header.enabled && header.name.trim() && header.value.trim(),
30+
);
2831
};
2932

30-
export const headersToRecord = (headers: CustomHeaders): Record<string, string> => {
33+
export const headersToRecord = (
34+
headers: CustomHeaders,
35+
): Record<string, string> => {
3136
const enabledHeaders = getEnabledHeaders(headers);
3237
const record: Record<string, string> = {};
33-
34-
enabledHeaders.forEach(header => {
38+
39+
enabledHeaders.forEach((header) => {
3540
record[header.name.trim()] = header.value.trim();
3641
});
37-
42+
3843
return record;
3944
};
4045

41-
export const recordToHeaders = (record: Record<string, string>): CustomHeaders => {
46+
export const recordToHeaders = (
47+
record: Record<string, string>,
48+
): CustomHeaders => {
4249
return Object.entries(record).map(([name, value]) => ({
4350
name,
4451
value,
@@ -54,6 +61,6 @@ export const migrateFromLegacyAuth = (
5461
if (!bearerToken) {
5562
return [];
5663
}
57-
64+
5865
return [createHeaderFromBearerToken(bearerToken, headerName)];
5966
};

server/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ const getHttpHeaders = (req: express.Request): Record<string, string> => {
9393
// Handle multiple custom headers (new approach)
9494
if (req.headers["x-custom-auth-headers"] !== undefined) {
9595
try {
96-
const customHeaderNames = JSON.parse(req.headers["x-custom-auth-headers"] as string) as string[];
96+
const customHeaderNames = JSON.parse(
97+
req.headers["x-custom-auth-headers"] as string,
98+
) as string[];
9799
if (Array.isArray(customHeaderNames)) {
98100
customHeaderNames.forEach((headerName) => {
99101
const lowerCaseHeaderName = headerName.toLowerCase();
100102
if (req.headers[lowerCaseHeaderName] !== undefined) {
101103
const value = req.headers[lowerCaseHeaderName];
102-
headers[headerName] = Array.isArray(value) ? value[value.length - 1] : value;
104+
headers[headerName] = Array.isArray(value)
105+
? value[value.length - 1]
106+
: value;
103107
}
104108
});
105109
}

0 commit comments

Comments
 (0)