diff --git a/coderd/workspaces.go b/coderd/workspaces.go index b1758ae758004..c41de082f1c1e 100644 --- a/coderd/workspaces.go +++ b/coderd/workspaces.go @@ -151,7 +151,7 @@ func (api *api) workspaces(rw http.ResponseWriter, r *http.Request) { // Empty strings mean no filter orgFilter := r.URL.Query().Get("organization_id") - ownerFilter := r.URL.Query().Get("owner_id") + ownerFilter := r.URL.Query().Get("owner") filter := database.GetWorkspacesWithFilterParams{Deleted: false} if orgFilter != "" { diff --git a/codersdk/workspaces.go b/codersdk/workspaces.go index 42f6af672093c..5c2855d3424da 100644 --- a/codersdk/workspaces.go +++ b/codersdk/workspaces.go @@ -194,7 +194,7 @@ func (f WorkspaceFilter) asRequestOption() requestOption { q.Set("organization_id", f.OrganizationID.String()) } if f.Owner != "" { - q.Set("owner_id", f.Owner) + q.Set("owner", f.Owner) } r.URL.RawQuery = q.Encode() } diff --git a/site/src/api/api.test.ts b/site/src/api/api.test.ts index 6f477646437c9..5714c080d384e 100644 --- a/site/src/api/api.test.ts +++ b/site/src/api/api.test.ts @@ -1,5 +1,5 @@ import axios from "axios" -import { getApiKey, login, logout } from "./api" +import { getApiKey, getWorkspacesURL, login, logout } from "./api" import * as TypesGen from "./typesGenerated" describe("api.ts", () => { @@ -113,4 +113,17 @@ describe("api.ts", () => { } }) }) + + describe("getWorkspacesURL", () => { + it.each<[TypesGen.WorkspaceFilter | undefined, string]>([ + [undefined, "/api/v2/workspaces"], + + [{ OrganizationID: "1", Owner: "" }, "/api/v2/workspaces?organization_id=1"], + [{ OrganizationID: "", Owner: "1" }, "/api/v2/workspaces?owner=1"], + + [{ OrganizationID: "1", Owner: "me" }, "/api/v2/workspaces?organization_id=1&owner=me"], + ])(`getWorkspacesURL(%p) returns %p`, (filter, expected) => { + expect(getWorkspacesURL(filter)).toBe(expected) + }) + }) }) diff --git a/site/src/api/api.ts b/site/src/api/api.ts index 500768976697f..b2e47b628f1ad 100644 --- a/site/src/api/api.ts +++ b/site/src/api/api.ts @@ -120,10 +120,25 @@ export const getWorkspace = async (workspaceId: string): Promise => { - const response = await axios.get(`/api/v2/workspaces`) +export const getWorkspacesURL = (filter?: TypesGen.WorkspaceFilter): string => { + const basePath = "/api/v2/workspaces" + const searchParams = new URLSearchParams() + + if (filter?.OrganizationID) { + searchParams.append("organization_id", filter.OrganizationID) + } + if (filter?.Owner) { + searchParams.append("owner", filter.Owner) + } + + const searchString = searchParams.toString() + + return searchString ? `${basePath}?${searchString}` : basePath +} + +export const getWorkspaces = async (filter?: TypesGen.WorkspaceFilter): Promise => { + const url = getWorkspacesURL(filter) + const response = await axios.get(url) return response.data } diff --git a/site/src/testHelpers/handlers.ts b/site/src/testHelpers/handlers.ts index 529995fa68245..be6f201a3ceeb 100644 --- a/site/src/testHelpers/handlers.ts +++ b/site/src/testHelpers/handlers.ts @@ -36,9 +36,6 @@ export const handlers = [ rest.post("/api/v2/users/me/workspaces", async (req, res, ctx) => { return res(ctx.status(200), ctx.json(M.MockWorkspace)) }), - rest.get("/api/v2/workspaces", async (req, res, ctx) => { - return res(ctx.status(200), ctx.json([M.MockWorkspace])) - }), rest.get("/api/v2/users/me/organizations", (req, res, ctx) => { return res(ctx.status(200), ctx.json([M.MockOrganization])) }), @@ -79,6 +76,12 @@ export const handlers = [ }), // workspaces + + // REMARK: This endpoint works with query parameters, but they won't be + // reflected in the return. + rest.get("/api/v2/workspaces", async (req, res, ctx) => { + return res(ctx.status(200), ctx.json([M.MockWorkspace])) + }), rest.get("/api/v2/organizations/:organizationId/workspaces/:userName/:workspaceName", (req, res, ctx) => { if (req.params.workspaceName !== M.MockWorkspace.name) { return res(