Better typing responses

This commit is contained in:
Adria Navarro 2023-07-18 12:06:35 +02:00
parent 160d949423
commit 7140df6ed3
4 changed files with 29 additions and 12 deletions

View File

@ -1,7 +1,13 @@
import sdk from "../../../sdk"
import { Ctx, FetchViewResponse, ViewResponse, ViewV2 } from "@budibase/types"
import {
CreateViewRequest,
Ctx,
FetchViewResponse,
ViewResponse,
ViewV2,
} from "@budibase/types"
export async function fetch(ctx: Ctx<{}, FetchViewResponse>) {
export async function fetch(ctx: Ctx<void, FetchViewResponse>) {
const { tableId } = ctx.query
if (tableId && typeof tableId !== "string") {
@ -15,7 +21,7 @@ export async function fetch(ctx: Ctx<{}, FetchViewResponse>) {
ctx.body = { views }
}
export async function find(ctx: Ctx<{}, ViewResponse>) {
export async function find(ctx: Ctx<void, ViewResponse>) {
const { viewId } = ctx.params
const view = await sdk.views.get(viewId)
@ -28,16 +34,19 @@ export async function find(ctx: Ctx<{}, ViewResponse>) {
}
}
export async function save(ctx: Ctx<ViewV2>) {
export async function save(ctx: Ctx<CreateViewRequest, ViewResponse>) {
const view = ctx.request.body
const result = await sdk.views.save(view)
ctx.body = {
...view,
...result,
data: {
...view,
...result,
},
}
}
export async function remove(ctx: Ctx<{}, {}>) {
export async function remove(ctx: Ctx) {
const { viewId } = ctx.params
const doc = await sdk.views.get(viewId)
if (!doc) {

View File

@ -136,12 +136,14 @@ describe("/v2/views", () => {
.expect("Content-Type", /json/)
.expect(200)
expect(res.status).toBe(200)
expect(res.body._id).toBeDefined()
expect(res.body.data._id).toBeDefined()
expect(res.body).toEqual({
...newView,
_id: expect.any(String),
_rev: expect.any(String),
data: {
...newView,
_id: expect.any(String),
_rev: expect.any(String),
},
})
})
})

View File

@ -646,7 +646,8 @@ class TestConfiguration {
name: generator.guid(),
...config,
}
return this._req(view, null, controllers.view.v2.save)
const result = await this._req(view, null, controllers.view.v2.save)
return result.data
},
get: (viewId: string): supertest.Test => {
return this.request!.get(`/api/v2/views/${viewId}`)

View File

@ -7,3 +7,8 @@ export interface FetchViewResponse {
export interface ViewResponse {
data: ViewV2
}
export type CreateViewRequest = Omit<
ViewV2,
"_id" | "_rev" | "createdAt" | "updatedAt"
>