Skip to content

Commit d457f93

Browse files
greyscaledkylecarbs
authored andcommitted
feat: getWorkspaces filter site api (#1564)
1 parent fba6b1f commit d457f93

File tree

5 files changed

+41
-10
lines changed

5 files changed

+41
-10
lines changed

coderd/workspaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (api *api) workspaces(rw http.ResponseWriter, r *http.Request) {
151151

152152
// Empty strings mean no filter
153153
orgFilter := r.URL.Query().Get("organization_id")
154-
ownerFilter := r.URL.Query().Get("owner_id")
154+
ownerFilter := r.URL.Query().Get("owner")
155155

156156
filter := database.GetWorkspacesWithFilterParams{Deleted: false}
157157
if orgFilter != "" {

codersdk/workspaces.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (f WorkspaceFilter) asRequestOption() requestOption {
194194
q.Set("organization_id", f.OrganizationID.String())
195195
}
196196
if f.Owner != "" {
197-
q.Set("owner_id", f.Owner)
197+
q.Set("owner", f.Owner)
198198
}
199199
r.URL.RawQuery = q.Encode()
200200
}

site/src/api/api.test.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import axios from "axios"
2-
import { getApiKey, login, logout } from "./api"
2+
import { getApiKey, getWorkspacesURL, login, logout } from "./api"
33
import * as TypesGen from "./typesGenerated"
44

55
describe("api.ts", () => {
@@ -113,4 +113,17 @@ describe("api.ts", () => {
113113
}
114114
})
115115
})
116+
117+
describe("getWorkspacesURL", () => {
118+
it.each<[TypesGen.WorkspaceFilter | undefined, string]>([
119+
[undefined, "/api/v2/workspaces"],
120+
121+
[{ OrganizationID: "1", Owner: "" }, "/api/v2/workspaces?organization_id=1"],
122+
[{ OrganizationID: "", Owner: "1" }, "/api/v2/workspaces?owner=1"],
123+
124+
[{ OrganizationID: "1", Owner: "me" }, "/api/v2/workspaces?organization_id=1&owner=me"],
125+
])(`getWorkspacesURL(%p) returns %p`, (filter, expected) => {
126+
expect(getWorkspacesURL(filter)).toBe(expected)
127+
})
128+
})
116129
})

site/src/api/api.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,25 @@ export const getWorkspace = async (workspaceId: string): Promise<TypesGen.Worksp
120120
return response.data
121121
}
122122

123-
// TODO: @emyrk add query params as arguments. Supports 'organization_id' and 'owner'
124-
// 'owner' can be a username, user_id, or 'me'
125-
export const getWorkspaces = async (): Promise<TypesGen.Workspace[]> => {
126-
const response = await axios.get<TypesGen.Workspace[]>(`/api/v2/workspaces`)
123+
export const getWorkspacesURL = (filter?: TypesGen.WorkspaceFilter): string => {
124+
const basePath = "/api/v2/workspaces"
125+
const searchParams = new URLSearchParams()
126+
127+
if (filter?.OrganizationID) {
128+
searchParams.append("organization_id", filter.OrganizationID)
129+
}
130+
if (filter?.Owner) {
131+
searchParams.append("owner", filter.Owner)
132+
}
133+
134+
const searchString = searchParams.toString()
135+
136+
return searchString ? `${basePath}?${searchString}` : basePath
137+
}
138+
139+
export const getWorkspaces = async (filter?: TypesGen.WorkspaceFilter): Promise<TypesGen.Workspace[]> => {
140+
const url = getWorkspacesURL(filter)
141+
const response = await axios.get<TypesGen.Workspace[]>(url)
127142
return response.data
128143
}
129144

site/src/testHelpers/handlers.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ export const handlers = [
3636
rest.post("/api/v2/users/me/workspaces", async (req, res, ctx) => {
3737
return res(ctx.status(200), ctx.json(M.MockWorkspace))
3838
}),
39-
rest.get("/api/v2/workspaces", async (req, res, ctx) => {
40-
return res(ctx.status(200), ctx.json([M.MockWorkspace]))
41-
}),
4239
rest.get("/api/v2/users/me/organizations", (req, res, ctx) => {
4340
return res(ctx.status(200), ctx.json([M.MockOrganization]))
4441
}),
@@ -79,6 +76,12 @@ export const handlers = [
7976
}),
8077

8178
// workspaces
79+
80+
// REMARK: This endpoint works with query parameters, but they won't be
81+
// reflected in the return.
82+
rest.get("/api/v2/workspaces", async (req, res, ctx) => {
83+
return res(ctx.status(200), ctx.json([M.MockWorkspace]))
84+
}),
8285
rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => {
8386
if (req.params.workspaceName !== M.MockWorkspace.name) {
8487
return res(

0 commit comments

Comments
 (0)