feat: getWorkspaces filter site api (#1564)

This commit is contained in:
G r e y
2022-05-19 12:08:55 -04:00
committed by GitHub
parent 0438430c7c
commit 67333b6186
5 changed files with 41 additions and 10 deletions
+1 -1
View File
@@ -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 != "" {
+1 -1
View File
@@ -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()
}
+14 -1
View File
@@ -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
View File
@@ -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
}
+6 -3
View File
@@ -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(