Skip to content

Commit f58c69f

Browse files
committed
Extend hosts API client with search param
CMK-35352 Change-Id: I428861ec1d0f018ec70c32d59dea5563a22d7dde
1 parent 5e336df commit f58c69f

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

packages/cmk-frontend-vue/src/monitoring/all-hosts/api/hosts.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ import type { HostsRequest, HostsResponse } from '../../shared/api/types'
1212
export interface HostQueryParams {
1313
limit?: number
1414
sort?: SortingState
15+
searchQuery?: string
1516
}
1617

1718
export class HostApi {
1819
public async fetchHosts(params: HostQueryParams = {}): Promise<HostsResponse> {
1920
const sort = (params.sort ?? []).map((s) => `${s.id}:${s.desc ? 'desc' : 'asc'}`)
21+
// Drop an empty or whitespace-only search query so an empty search matches a request without
22+
// the `q` param.
23+
const searchQuery = params.searchQuery?.trim()
2024
// The schema types limit as string and sort as string (not string[]); openapi-fetch's
2125
// defaultQuerySerializer handles string[] as repeated params at runtime, so the cast
2226
// is only needed to bridge the incorrectly-generated schema types.
@@ -25,7 +29,8 @@ export class HostApi {
2529
params: {
2630
query: {
2731
...(params.limit !== undefined && { limit: String(params.limit) }),
28-
...(sort.length > 0 && { sort })
32+
...(sort.length > 0 && { sort }),
33+
...(searchQuery !== undefined && searchQuery !== '' && { q: searchQuery })
2934
} as unknown as NonNullable<HostsRequest>
3035
}
3136
})

packages/cmk-frontend-vue/tests/monitoring/all-hosts/api/hosts.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,42 @@ describe('HostApi.fetchHosts', () => {
130130
await expect(new HostApi().fetchHosts()).rejects.toThrow()
131131
})
132132

133+
it('forwards a non-empty search query as the q param', async () => {
134+
mockSuccess(makeHostsResponse([]))
135+
136+
await new HostApi().fetchHosts({ searchQuery: 'web-server' })
137+
138+
expect(getSpy).toHaveBeenCalledWith('/monitor/hosts', {
139+
params: { query: { q: 'web-server' } }
140+
})
141+
})
142+
143+
it('omits the q param when the search query is empty', async () => {
144+
mockSuccess(makeHostsResponse([]))
145+
146+
await new HostApi().fetchHosts({ searchQuery: '' })
147+
148+
expect(getSpy).toHaveBeenCalledWith('/monitor/hosts', { params: { query: {} } })
149+
})
150+
151+
it('omits the q param when the search query is only whitespace', async () => {
152+
mockSuccess(makeHostsResponse([]))
153+
154+
await new HostApi().fetchHosts({ searchQuery: ' ' })
155+
156+
expect(getSpy).toHaveBeenCalledWith('/monitor/hosts', { params: { query: {} } })
157+
})
158+
159+
it('keeps other params when omitting an empty q', async () => {
160+
mockSuccess(makeHostsResponse([]))
161+
162+
await new HostApi().fetchHosts({ limit: 50, searchQuery: '' })
163+
164+
expect(getSpy).toHaveBeenCalledWith('/monitor/hosts', {
165+
params: { query: { limit: '50' } }
166+
})
167+
})
168+
133169
it('returns the response data from the API', async () => {
134170
const hosts = [makeHost({ name: 'db-1', state: 'DOWN' }), makeHost({ name: 'web-1' })]
135171
const response = makeHostsResponse(hosts)

0 commit comments

Comments
 (0)