Simplifying endpoints
This commit is contained in:
parent
4ca25ee065
commit
02bc1d2cdc
3 changed files with 43 additions and 57 deletions
|
@ -2,24 +2,26 @@ import sdk from "../../../sdk"
|
||||||
import { Ctx, ViewV2 } from "@budibase/types"
|
import { Ctx, ViewV2 } from "@budibase/types"
|
||||||
|
|
||||||
export async function fetch(ctx: Ctx) {
|
export async function fetch(ctx: Ctx) {
|
||||||
ctx.body = { views: await sdk.views.fetch() }
|
const { tableId } = ctx.query
|
||||||
|
|
||||||
|
if (tableId && typeof tableId !== "string") {
|
||||||
|
ctx.throw(400, "tableId type is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
const views = tableId
|
||||||
|
? await sdk.views.findByTable(tableId)
|
||||||
|
: await sdk.views.fetch()
|
||||||
|
|
||||||
|
ctx.body = { views }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: Ctx) {
|
export async function find(ctx: Ctx) {
|
||||||
const { tableId, viewId } = ctx.params
|
const { viewId } = ctx.params
|
||||||
|
|
||||||
const result = await sdk.views.get(viewId)
|
const result = await sdk.views.get(viewId)
|
||||||
if (result?.tableId !== tableId) {
|
|
||||||
ctx.throw(404)
|
|
||||||
}
|
|
||||||
ctx.body = result
|
ctx.body = result
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function findByTable(ctx: Ctx) {
|
|
||||||
const { tableId } = ctx.params
|
|
||||||
ctx.body = { views: await sdk.views.findByTable(tableId) }
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function save(ctx: Ctx<ViewV2>) {
|
export async function save(ctx: Ctx<ViewV2>) {
|
||||||
const view = ctx.request.body
|
const view = ctx.request.body
|
||||||
const result = await sdk.views.save(view)
|
const result = await sdk.views.save(view)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import * as setup from "./utilities"
|
import * as setup from "./utilities"
|
||||||
import { FieldType, Table, ViewV2 } from "@budibase/types"
|
import { FieldType, Table, ViewV2 } from "@budibase/types"
|
||||||
import { generator, structures } from "@budibase/backend-core/tests"
|
import { generator, structures } from "@budibase/backend-core/tests"
|
||||||
import sdk from "../../../sdk"
|
|
||||||
|
|
||||||
function priceTable(): Table {
|
function priceTable(): Table {
|
||||||
return {
|
return {
|
||||||
|
@ -45,15 +44,9 @@ describe("/views/v2", () => {
|
||||||
.expect(200)
|
.expect(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
const getView = ({
|
const getView = (viewId: string) => {
|
||||||
tableId,
|
|
||||||
viewId,
|
|
||||||
}: {
|
|
||||||
tableId: string
|
|
||||||
viewId: string
|
|
||||||
}) => {
|
|
||||||
return request
|
return request
|
||||||
.get(`/api/views/v2/${tableId}/${viewId}`)
|
.get(`/api/views/v2/${viewId}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
}
|
}
|
||||||
|
@ -88,28 +81,36 @@ describe("/views/v2", () => {
|
||||||
expect.arrayContaining(views.map(v => expect.objectContaining(v)))
|
expect.arrayContaining(views.map(v => expect.objectContaining(v)))
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe("findByTable", () => {
|
it("can filter by table id", async () => {
|
||||||
const views: any[] = []
|
const newTable = await config.createTable(priceTable())
|
||||||
|
const newViews = []
|
||||||
beforeAll(async () => {
|
|
||||||
table = await config.createTable(priceTable())
|
|
||||||
for (let id = 0; id < 5; id++) {
|
for (let id = 0; id < 5; id++) {
|
||||||
const res = await saveView(createView(table._id!))
|
const res = await saveView(createView(newTable._id!))
|
||||||
views.push(res.body)
|
newViews.push(res.body)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
it("returns all views", async () => {
|
|
||||||
const res = await request
|
const res = await request
|
||||||
.get(`/api/views/v2/${table._id}`)
|
.get(`/api/views/v2?tableId=${newTable._id}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
|
|
||||||
expect(res.body.views.length).toBe(5)
|
expect(res.body.views.length).toBe(5)
|
||||||
expect(res.body.views).toEqual(expect.arrayContaining([]))
|
expect(res.body.views).toEqual(
|
||||||
|
expect.arrayContaining(newViews.map(v => expect.objectContaining(v)))
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("can not filter by multiple table ids", async () => {
|
||||||
|
const res = await request
|
||||||
|
.get(
|
||||||
|
`/api/views/v2?tableId=${structures.generator.guid()}&tableId=${structures.generator.guid()}`
|
||||||
|
)
|
||||||
|
.set(config.defaultHeaders())
|
||||||
|
.expect("Content-Type", /json/)
|
||||||
|
.expect(400)
|
||||||
|
|
||||||
|
expect(res.body.message).toBe("tableId type is not valid")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -120,10 +121,7 @@ describe("/views/v2", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can fetch the expected view", async () => {
|
it("can fetch the expected view", async () => {
|
||||||
const res = await getView({
|
const res = await getView(view._id).expect(200)
|
||||||
tableId: view.tableId,
|
|
||||||
viewId: view._id,
|
|
||||||
}).expect(200)
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body._id).toBeDefined()
|
expect(res.body._id).toBeDefined()
|
||||||
|
|
||||||
|
@ -136,11 +134,8 @@ describe("/views/v2", () => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("will return 404 if the wrong table id is provided", async () => {
|
it("will return 404 if the unnexisting id is provided", async () => {
|
||||||
await getView({
|
await getView(structures.generator.guid()).expect(404)
|
||||||
tableId: structures.generator.guid(),
|
|
||||||
viewId: view._id,
|
|
||||||
}).expect(404)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -168,20 +163,14 @@ describe("/views/v2", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
it("can delete an existing view", async () => {
|
it("can delete an existing view", async () => {
|
||||||
await getView({
|
await getView(view._id).expect(200)
|
||||||
tableId: view.tableId,
|
|
||||||
viewId: view._id,
|
|
||||||
}).expect(200)
|
|
||||||
|
|
||||||
await request
|
await request
|
||||||
.delete(`/api/views/v2/${view.tableId}/${view._id}`)
|
.delete(`/api/views/v2/${view._id}`)
|
||||||
.set(config.defaultHeaders())
|
.set(config.defaultHeaders())
|
||||||
.expect(204)
|
.expect(204)
|
||||||
|
|
||||||
await getView({
|
await getView(view._id).expect(404)
|
||||||
tableId: view.tableId,
|
|
||||||
viewId: view._id,
|
|
||||||
}).expect(404)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -14,12 +14,7 @@ router
|
||||||
viewController.v2.fetch
|
viewController.v2.fetch
|
||||||
)
|
)
|
||||||
.get(
|
.get(
|
||||||
`/api/views/v2/:tableId`,
|
`/api/views/v2/:viewId`,
|
||||||
authorized(permissions.BUILDER),
|
|
||||||
viewController.v2.findByTable
|
|
||||||
)
|
|
||||||
.get(
|
|
||||||
`/api/views/v2/:tableId/:viewId`,
|
|
||||||
authorized(permissions.BUILDER),
|
authorized(permissions.BUILDER),
|
||||||
viewController.v2.find
|
viewController.v2.find
|
||||||
)
|
)
|
||||||
|
@ -29,7 +24,7 @@ router
|
||||||
viewController.v2.save
|
viewController.v2.save
|
||||||
)
|
)
|
||||||
.delete(
|
.delete(
|
||||||
`/api/views/v2/:tableId/:viewId`,
|
`/api/views/v2/:viewId`,
|
||||||
authorized(permissions.BUILDER),
|
authorized(permissions.BUILDER),
|
||||||
viewController.v2.remove
|
viewController.v2.remove
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue