mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: getWorkspaces filter site api (#1564)
This commit is contained in:
@@ -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 != "" {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
+19
-4
@@ -120,10 +120,25 @@ export const getWorkspace = async (workspaceId: string): Promise<TypesGen.Worksp
|
||||
return response.data
|
||||
}
|
||||
|
||||
// TODO: @emyrk add query params as arguments. Supports 'organization_id' and 'owner'
|
||||
// 'owner' can be a username, user_id, or 'me'
|
||||
export const getWorkspaces = async (): Promise<TypesGen.Workspace[]> => {
|
||||
const response = await axios.get<TypesGen.Workspace[]>(`/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<TypesGen.Workspace[]> => {
|
||||
const url = getWorkspacesURL(filter)
|
||||
const response = await axios.get<TypesGen.Workspace[]>(url)
|
||||
return response.data
|
||||
}
|
||||
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user