Skip to content

Commit 9a0ce5d

Browse files
committed
Refactor paginated api calls
1 parent 4271fd1 commit 9a0ce5d

File tree

1 file changed

+15
-56
lines changed

1 file changed

+15
-56
lines changed

site/src/api/api.ts

Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const getApiKey = async (): Promise<TypesGen.GenerateAPIKeyResponse> => {
134134
export const getUsers = async (
135135
options: TypesGen.UsersRequest,
136136
): Promise<TypesGen.User[]> => {
137-
const url = buildURL("/api/v2/users", options)
137+
const url = getURLWithSearchParams("/api/v2/users", options)
138138
const response = await axios.get<TypesGen.User[]>(url.toString())
139139
return response.data
140140
}
@@ -266,28 +266,19 @@ export const watchWorkspace = (workspaceId: string): EventSource => {
266266

267267
interface SearchParamOptions extends TypesGen.Pagination {
268268
q?: string
269-
filter?: string
270-
}
271-
272-
const buildURL = (basePath: string, options: SearchParamOptions) => {
273-
const url = new URL(basePath)
274-
const keys = Object.keys(options) as (keyof SearchParamOptions)[]
275-
keys.forEach((key) => {
276-
const value = options[key] ?? ""
277-
url.searchParams.append(key, value.toString())
278-
})
279-
return url
280269
}
281270

282271
export const getURLWithSearchParams = (
283272
basePath: string,
284-
filter?: TypesGen.WorkspaceFilter | TypesGen.UsersRequest,
273+
options: SearchParamOptions
285274
): string => {
286275
const searchParams = new URLSearchParams()
287276

288-
if (filter?.q && filter.q !== "") {
289-
searchParams.append("q", filter.q)
290-
}
277+
const keys = Object.keys(options) as (keyof SearchParamOptions)[]
278+
keys.forEach((key) => {
279+
const value = options[key] ?? ""
280+
searchParams.append(key, value.toString())
281+
})
291282

292283
const searchString = searchParams.toString()
293284

@@ -297,33 +288,16 @@ export const getURLWithSearchParams = (
297288
export const getWorkspaces = async (
298289
options: TypesGen.WorkspacesRequest,
299290
): Promise<TypesGen.Workspace[]> => {
300-
const searchParams = new URLSearchParams()
301-
if (options.limit) {
302-
searchParams.set("limit", options.limit.toString())
303-
}
304-
if (options.offset) {
305-
searchParams.set("offset", options.offset.toString())
306-
}
307-
if (options.q) {
308-
searchParams.set("q", options.q)
309-
}
310-
311-
const response = await axios.get<TypesGen.Workspace[]>(
312-
`/api/v2/workspaces?${searchParams.toString()}`,
313-
)
291+
const url = getURLWithSearchParams("/api/v2/workspaces", options)
292+
const response = await axios.get<TypesGen.Workspace[]>(url)
314293
return response.data
315294
}
316295

317296
export const getWorkspacesCount = async (
318297
options: TypesGen.WorkspaceCountRequest,
319298
): Promise<TypesGen.WorkspaceCountResponse> => {
320-
const searchParams = new URLSearchParams()
321-
if (options.q) {
322-
searchParams.set("q", options.q)
323-
}
324-
const response = await axios.get(
325-
`/api/v2/workspaces/count?${searchParams.toString()}`,
326-
)
299+
const url = getURLWithSearchParams("/api/v2/workspaces/count", options)
300+
const response = await axios.get(url)
327301
return response.data
328302
}
329303

@@ -570,31 +544,16 @@ export const getEntitlements = async (): Promise<TypesGen.Entitlements> => {
570544
export const getAuditLogs = async (
571545
options: TypesGen.AuditLogsRequest,
572546
): Promise<TypesGen.AuditLogResponse> => {
573-
const searchParams = new URLSearchParams()
574-
if (options.limit) {
575-
searchParams.set("limit", options.limit.toString())
576-
}
577-
if (options.offset) {
578-
searchParams.set("offset", options.offset.toString())
579-
}
580-
if (options.q) {
581-
searchParams.set("q", options.q)
582-
}
583-
584-
const response = await axios.get(`/api/v2/audit?${searchParams.toString()}`)
547+
const url = getURLWithSearchParams("/api/v2/audit", options)
548+
const response = await axios.get(url)
585549
return response.data
586550
}
587551

588552
export const getAuditLogsCount = async (
589553
options: TypesGen.AuditLogCountRequest = {},
590554
): Promise<TypesGen.AuditLogCountResponse> => {
591-
const searchParams = new URLSearchParams()
592-
if (options.q) {
593-
searchParams.set("q", options.q)
594-
}
595-
const response = await axios.get(
596-
`/api/v2/audit/count?${searchParams.toString()}`,
597-
)
555+
const url = getURLWithSearchParams("/api/v2/audit/count", options)
556+
const response = await axios.get(url)
598557
return response.data
599558
}
600559

0 commit comments

Comments
 (0)