Type responses

This commit is contained in:
Adria Navarro 2023-07-18 09:58:43 +02:00
parent 03f84170b8
commit 8282fbb99b
6 changed files with 55 additions and 30 deletions

View File

@ -1,7 +1,7 @@
import sdk from "../../../sdk"
import { Ctx, ViewV2 } from "@budibase/types"
import { Ctx, FetchViewResponse, ViewResponse, ViewV2 } from "@budibase/types"
export async function fetch(ctx: Ctx) {
export async function fetch(ctx: Ctx<{}, FetchViewResponse>) {
const { tableId } = ctx.query
if (tableId && typeof tableId !== "string") {
@ -15,11 +15,17 @@ export async function fetch(ctx: Ctx) {
ctx.body = { views }
}
export async function find(ctx: Ctx) {
export async function find(ctx: Ctx<{}, ViewResponse>) {
const { viewId } = ctx.params
const result = await sdk.views.get(viewId)
ctx.body = result
const view = await sdk.views.get(viewId)
if (!view) {
ctx.throw(404)
}
ctx.body = {
data: view,
}
}
export async function save(ctx: Ctx<ViewV2>) {
@ -31,10 +37,15 @@ export async function save(ctx: Ctx<ViewV2>) {
}
}
export async function remove(ctx: Ctx) {
export async function remove(ctx: Ctx<{}, {}>) {
const { viewId } = ctx.params
const { _rev } = await sdk.views.get(viewId)
const doc = await sdk.views.get(viewId)
if (!doc) {
ctx.throw(404)
}
await sdk.views.remove(viewId, _rev)
const { _rev } = doc
await sdk.views.remove(viewId, _rev!)
ctx.status = 204
}

View File

@ -59,7 +59,7 @@ describe("/v2/views", () => {
}
describe("fetch", () => {
const views: any[] = []
const views: ViewV2[] = []
beforeAll(async () => {
table = await config.createTable(priceTable())
@ -115,22 +115,23 @@ describe("/v2/views", () => {
})
describe("getView", () => {
let view: any
let view: ViewV2
beforeAll(async () => {
view = (await saveView(createView(table._id!))).body
})
it("can fetch the expected view", async () => {
const res = await getView(view._id).expect(200)
const res = await getView(view._id!).expect(200)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
expect(res.body).toEqual({
...view,
_id: expect.any(String),
_rev: expect.any(String),
createdAt: expect.any(String),
updatedAt: expect.any(String),
data: {
...view,
_id: view._id,
_rev: view._rev,
createdAt: expect.any(String),
updatedAt: expect.any(String),
},
})
})
@ -141,13 +142,13 @@ describe("/v2/views", () => {
describe("create", () => {
it("persist the view when the view is successfully created", async () => {
const view = createView(table._id!)
const res = await saveView(view)
const newView = createView(table._id!)
const res = await saveView(newView)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
expect(res.body).toEqual({
...view,
...newView,
_id: expect.any(String),
_rev: expect.any(String),
})
@ -155,7 +156,7 @@ describe("/v2/views", () => {
})
describe("delete", () => {
let view: any
let view: ViewV2
beforeAll(async () => {
table = await config.createTable(priceTable())
@ -163,14 +164,14 @@ describe("/v2/views", () => {
})
it("can delete an existing view", async () => {
await getView(view._id).expect(200)
await getView(view._id!).expect(200)
await request
.delete(`/api/v2/views/${view._id}`)
.set(config.defaultHeaders())
.expect(204)
await getView(view._id).expect(404)
await getView(view._id!).expect(404)
})
})
})

View File

@ -7,7 +7,7 @@ import {
import { ViewV2 } from "@budibase/types"
import * as utils from "../../../db/utils"
export async function fetch() {
export async function fetch(): Promise<ViewV2[]> {
const db = context.getAppDB()
const startKey = `${DocumentType.VIEW}${SEPARATOR}`
@ -20,7 +20,7 @@ export async function fetch() {
return response.rows.map(r => r.doc)
}
export async function findByTable(tableId: string) {
export async function findByTable(tableId: string): Promise<ViewV2[]> {
const db = context.getAppDB()
const startKey = utils.viewIDPrefix(tableId)
@ -33,13 +33,13 @@ export async function findByTable(tableId: string) {
return response.rows.map(r => r.doc)
}
export async function get(viewId: string) {
export async function get(viewId: string): Promise<ViewV2 | undefined> {
const db = context.getAppDB()
const result = await db.get(viewId)
const result = await db.get<ViewV2>(viewId)
return result
}
export async function save(view: ViewV2) {
export async function save(view: ViewV2): Promise<ViewV2> {
const db = context.getAppDB()
const response = await db.put(
@ -50,12 +50,13 @@ export async function save(view: ViewV2) {
{}
)
return {
...view,
_id: response.id,
_rev: response.rev,
}
}
export async function remove(viewId: string, rev: string) {
export async function remove(viewId: string, rev: string): Promise<void> {
const db = context.getAppDB()
await db.remove(viewId, rev)
}

View File

@ -1,2 +1,3 @@
export * from "./backup"
export * from "./datasource"
export * from "./view"

View File

@ -0,0 +1,9 @@
import { ViewV2 } from "../../../documents"
export interface FetchViewResponse {
views: ViewV2[]
}
export interface ViewResponse {
data: ViewV2
}

View File

@ -1,3 +1,5 @@
import { Document } from "../document"
export interface View {
name: string
tableId: string
@ -10,7 +12,7 @@ export interface View {
meta?: Record<string, any>
}
export interface ViewV2 {
export interface ViewV2 extends Document {
name: string
tableId: string
}