Skip to content

Commit 3d1810d

Browse files
committed
Normalize API responses for Bouncers and Hub
Make response handling more defensive across two pages: Bouncers.tsx now reads response.data.data into a local `raw` and adapts to either an array or a wrapped object (e.g. { bouncers: [...] }). If the backend returns an object, it scans values for the first array and returns it; otherwise it falls back to an empty array. HubBrowser.tsx now returns `response.data.data ?? null` to explicitly yield null when data is absent. These changes make the UI resilient to varying backend response shapes.
1 parent b47b2eb commit 3d1810d

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

web/src/pages/Bouncers.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ export default function Bouncers() {
5252
queryKey: ['bouncers'],
5353
queryFn: async () => {
5454
const response = await api.crowdsec.getBouncers()
55-
return response.data.data
56-
},
57-
select: (data) => {
58-
if (Array.isArray(data)) return data as Bouncer[]
59-
if (data && Array.isArray((data as { bouncers: Bouncer[] }).bouncers)) {
60-
return (data as { bouncers: Bouncer[] }).bouncers
55+
const raw = response.data.data
56+
// Adapt to whatever shape the backend returns
57+
if (Array.isArray(raw)) return raw as Bouncer[]
58+
if (raw && typeof raw === 'object') {
59+
// Backend wraps as { bouncers: [...], count: N }
60+
const obj = raw as Record<string, unknown>
61+
for (const val of Object.values(obj)) {
62+
if (Array.isArray(val)) return val as Bouncer[]
63+
}
6164
}
6265
return [] as Bouncer[]
6366
},

web/src/pages/HubBrowser.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default function HubBrowser() {
103103
queryKey: ['hub-items-browser'],
104104
queryFn: async () => {
105105
const response = await hubAPI.list()
106-
return response.data.data
106+
return response.data.data ?? null
107107
},
108108
})
109109

0 commit comments

Comments
 (0)