From 8282fbb99babf1ab79010ed44a3313620f9144aa Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 18 Jul 2023 09:58:43 +0200 Subject: [PATCH] Type responses --- .../src/api/controllers/view/viewsV2.ts | 27 +++++++++++----- .../src/api/routes/tests/viewV2.spec.ts | 31 ++++++++++--------- packages/server/src/sdk/app/views/index.ts | 13 ++++---- packages/types/src/api/web/app/index.ts | 1 + packages/types/src/api/web/app/view.ts | 9 ++++++ packages/types/src/documents/app/view.ts | 4 ++- 6 files changed, 55 insertions(+), 30 deletions(-) create mode 100644 packages/types/src/api/web/app/view.ts diff --git a/packages/server/src/api/controllers/view/viewsV2.ts b/packages/server/src/api/controllers/view/viewsV2.ts index 47f9d645a0..65340682d8 100644 --- a/packages/server/src/api/controllers/view/viewsV2.ts +++ b/packages/server/src/api/controllers/view/viewsV2.ts @@ -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) { @@ -31,10 +37,15 @@ export async function save(ctx: Ctx) { } } -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 } diff --git a/packages/server/src/api/routes/tests/viewV2.spec.ts b/packages/server/src/api/routes/tests/viewV2.spec.ts index db94f2972b..4c45692b3a 100644 --- a/packages/server/src/api/routes/tests/viewV2.spec.ts +++ b/packages/server/src/api/routes/tests/viewV2.spec.ts @@ -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) }) }) }) diff --git a/packages/server/src/sdk/app/views/index.ts b/packages/server/src/sdk/app/views/index.ts index c8f0abca7d..a1cffeb2b5 100644 --- a/packages/server/src/sdk/app/views/index.ts +++ b/packages/server/src/sdk/app/views/index.ts @@ -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 { 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 { 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 { const db = context.getAppDB() - const result = await db.get(viewId) + const result = await db.get(viewId) return result } -export async function save(view: ViewV2) { +export async function save(view: ViewV2): Promise { 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 { const db = context.getAppDB() await db.remove(viewId, rev) } diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts index 9be15ecfe3..bb9eb09abc 100644 --- a/packages/types/src/api/web/app/index.ts +++ b/packages/types/src/api/web/app/index.ts @@ -1,2 +1,3 @@ export * from "./backup" export * from "./datasource" +export * from "./view" diff --git a/packages/types/src/api/web/app/view.ts b/packages/types/src/api/web/app/view.ts new file mode 100644 index 0000000000..524783d090 --- /dev/null +++ b/packages/types/src/api/web/app/view.ts @@ -0,0 +1,9 @@ +import { ViewV2 } from "../../../documents" + +export interface FetchViewResponse { + views: ViewV2[] +} + +export interface ViewResponse { + data: ViewV2 +} diff --git a/packages/types/src/documents/app/view.ts b/packages/types/src/documents/app/view.ts index d36271ba43..f3aad235cc 100644 --- a/packages/types/src/documents/app/view.ts +++ b/packages/types/src/documents/app/view.ts @@ -1,3 +1,5 @@ +import { Document } from "../document" + export interface View { name: string tableId: string @@ -10,7 +12,7 @@ export interface View { meta?: Record } -export interface ViewV2 { +export interface ViewV2 extends Document { name: string tableId: string }