Better typing responses
This commit is contained in:
parent
160d949423
commit
7140df6ed3
|
@ -1,7 +1,13 @@
|
||||||
import sdk from "../../../sdk"
|
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
|
const { tableId } = ctx.query
|
||||||
|
|
||||||
if (tableId && typeof tableId !== "string") {
|
if (tableId && typeof tableId !== "string") {
|
||||||
|
@ -15,7 +21,7 @@ export async function fetch(ctx: Ctx<{}, FetchViewResponse>) {
|
||||||
ctx.body = { views }
|
ctx.body = { views }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: Ctx<{}, ViewResponse>) {
|
export async function find(ctx: Ctx<void, ViewResponse>) {
|
||||||
const { viewId } = ctx.params
|
const { viewId } = ctx.params
|
||||||
|
|
||||||
const view = await sdk.views.get(viewId)
|
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 view = ctx.request.body
|
||||||
|
|
||||||
const result = await sdk.views.save(view)
|
const result = await sdk.views.save(view)
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
data: {
|
||||||
...view,
|
...view,
|
||||||
...result,
|
...result,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function remove(ctx: Ctx<{}, {}>) {
|
export async function remove(ctx: Ctx) {
|
||||||
const { viewId } = ctx.params
|
const { viewId } = ctx.params
|
||||||
const doc = await sdk.views.get(viewId)
|
const doc = await sdk.views.get(viewId)
|
||||||
if (!doc) {
|
if (!doc) {
|
||||||
|
|
|
@ -136,12 +136,14 @@ describe("/v2/views", () => {
|
||||||
.expect("Content-Type", /json/)
|
.expect("Content-Type", /json/)
|
||||||
.expect(200)
|
.expect(200)
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
expect(res.body._id).toBeDefined()
|
expect(res.body.data._id).toBeDefined()
|
||||||
|
|
||||||
expect(res.body).toEqual({
|
expect(res.body).toEqual({
|
||||||
|
data: {
|
||||||
...newView,
|
...newView,
|
||||||
_id: expect.any(String),
|
_id: expect.any(String),
|
||||||
_rev: expect.any(String),
|
_rev: expect.any(String),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -646,7 +646,8 @@ class TestConfiguration {
|
||||||
name: generator.guid(),
|
name: generator.guid(),
|
||||||
...config,
|
...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 => {
|
get: (viewId: string): supertest.Test => {
|
||||||
return this.request!.get(`/api/v2/views/${viewId}`)
|
return this.request!.get(`/api/v2/views/${viewId}`)
|
||||||
|
|
|
@ -7,3 +7,8 @@ export interface FetchViewResponse {
|
||||||
export interface ViewResponse {
|
export interface ViewResponse {
|
||||||
data: ViewV2
|
data: ViewV2
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type CreateViewRequest = Omit<
|
||||||
|
ViewV2,
|
||||||
|
"_id" | "_rev" | "createdAt" | "updatedAt"
|
||||||
|
>
|
||||||
|
|
Loading…
Reference in New Issue