1
0
Fork 0
mirror of synced 2024-07-08 15:56:23 +12:00

More types.

This commit is contained in:
Sam Rose 2024-02-28 12:13:13 +00:00
parent bfb0064289
commit a9392b2176
No known key found for this signature in database
13 changed files with 72 additions and 35 deletions

View file

@ -25,6 +25,7 @@ import { getActionDefinitions as actionDefs } from "../../automations/actions"
import sdk from "../../sdk" import sdk from "../../sdk"
import { builderSocket } from "../../websockets" import { builderSocket } from "../../websockets"
import env from "../../environment" import env from "../../environment"
import { DocumentDestroyResponse } from "@budibase/nano"
async function getActionDefinitions() { async function getActionDefinitions() {
return removeDeprecated(await actionDefs()) return removeDeprecated(await actionDefs())
@ -209,7 +210,7 @@ export async function find(ctx: UserCtx) {
ctx.body = await db.get(ctx.params.id) ctx.body = await db.get(ctx.params.id)
} }
export async function destroy(ctx: UserCtx) { export async function destroy(ctx: UserCtx<void, DocumentDestroyResponse>) {
const db = context.getAppDB() const db = context.getAppDB()
const automationId = ctx.params.id const automationId = ctx.params.id
const oldAutomation = await db.get<Automation>(automationId) const oldAutomation = await db.get<Automation>(automationId)

View file

@ -1,9 +1,9 @@
import { EMPTY_LAYOUT } from "../../constants/layouts" import { EMPTY_LAYOUT } from "../../constants/layouts"
import { generateLayoutID, getScreenParams } from "../../db/utils" import { generateLayoutID, getScreenParams } from "../../db/utils"
import { events, context } from "@budibase/backend-core" import { events, context } from "@budibase/backend-core"
import { BBContext, Layout } from "@budibase/types" import { BBContext, Layout, UserCtx } from "@budibase/types"
export async function save(ctx: BBContext) { export async function save(ctx: UserCtx<Layout, Layout>) {
const db = context.getAppDB() const db = context.getAppDB()
let layout = ctx.request.body let layout = ctx.request.body

View file

@ -73,7 +73,7 @@ const _import = async (ctx: UserCtx) => {
} }
export { _import as import } export { _import as import }
export async function save(ctx: UserCtx) { export async function save(ctx: UserCtx<Query, Query>) {
const db = context.getAppDB() const db = context.getAppDB()
const query: Query = ctx.request.body const query: Query = ctx.request.body

View file

@ -7,7 +7,13 @@ import {
roles, roles,
} from "@budibase/backend-core" } from "@budibase/backend-core"
import { updateAppPackage } from "./application" import { updateAppPackage } from "./application"
import { Plugin, ScreenProps, BBContext, Screen } from "@budibase/types" import {
Plugin,
ScreenProps,
BBContext,
Screen,
UserCtx,
} from "@budibase/types"
import { builderSocket } from "../../websockets" import { builderSocket } from "../../websockets"
export async function fetch(ctx: BBContext) { export async function fetch(ctx: BBContext) {
@ -31,7 +37,7 @@ export async function fetch(ctx: BBContext) {
) )
} }
export async function save(ctx: BBContext) { export async function save(ctx: UserCtx<Screen, Screen>) {
const db = context.getAppDB() const db = context.getAppDB()
let screen = ctx.request.body let screen = ctx.request.body

View file

@ -86,7 +86,7 @@ describe("/datasources", () => {
}) })
// check variables in cache // check variables in cache
let contents = await checkCacheForDynamicVariable( let contents = await checkCacheForDynamicVariable(
query._id, query._id!,
"variable3" "variable3"
) )
expect(contents.rows.length).toEqual(1) expect(contents.rows.length).toEqual(1)
@ -102,7 +102,7 @@ describe("/datasources", () => {
expect(res.body.errors).toBeUndefined() expect(res.body.errors).toBeUndefined()
// check variables no longer in cache // check variables no longer in cache
contents = await checkCacheForDynamicVariable(query._id, "variable3") contents = await checkCacheForDynamicVariable(query._id!, "variable3")
expect(contents).toBe(null) expect(contents).toBe(null)
}) })
}) })

View file

@ -467,7 +467,10 @@ describe("/queries", () => {
queryString: "test={{ variable3 }}", queryString: "test={{ variable3 }}",
}) })
// check its in cache // check its in cache
const contents = await checkCacheForDynamicVariable(base._id, "variable3") const contents = await checkCacheForDynamicVariable(
base._id!,
"variable3"
)
expect(contents.rows.length).toEqual(1) expect(contents.rows.length).toEqual(1)
const responseBody = await preview(datasource, { const responseBody = await preview(datasource, {
path: "www.failonce.com", path: "www.failonce.com",
@ -490,7 +493,7 @@ describe("/queries", () => {
queryString: "test={{ variable3 }}", queryString: "test={{ variable3 }}",
}) })
// check its in cache // check its in cache
let contents = await checkCacheForDynamicVariable(base._id, "variable3") let contents = await checkCacheForDynamicVariable(base._id!, "variable3")
expect(contents.rows.length).toEqual(1) expect(contents.rows.length).toEqual(1)
// delete the query // delete the query
@ -500,7 +503,7 @@ describe("/queries", () => {
.expect(200) .expect(200)
// check variables no longer in cache // check variables no longer in cache
contents = await checkCacheForDynamicVariable(base._id, "variable3") contents = await checkCacheForDynamicVariable(base._id!, "variable3")
expect(contents).toBe(null) expect(contents).toBe(null)
}) })
}) })

View file

@ -4,6 +4,7 @@ import { AppStatus } from "../../../../db/utils"
import { roles, tenancy, context, db } from "@budibase/backend-core" import { roles, tenancy, context, db } from "@budibase/backend-core"
import env from "../../../../environment" import env from "../../../../environment"
import Nano from "@budibase/nano" import Nano from "@budibase/nano"
import TestConfiguration from "src/tests/utilities/TestConfiguration"
class Request { class Request {
appId: any appId: any
@ -52,10 +53,10 @@ export const clearAllApps = async (
}) })
} }
export const clearAllAutomations = async (config: any) => { export const clearAllAutomations = async (config: TestConfiguration) => {
const automations = await config.getAllAutomations() const automations = await config.getAllAutomations()
for (let auto of automations) { for (let auto of automations) {
await context.doInAppContext(config.appId, async () => { await context.doInAppContext(config.getAppId(), async () => {
await config.deleteAutomation(auto) await config.deleteAutomation(auto)
}) })
} }
@ -101,7 +102,12 @@ export const checkBuilderEndpoint = async ({
method, method,
url, url,
body, body,
}: any) => { }: {
config: TestConfiguration
method: string
url: string
body: any
}) => {
const headers = await config.login({ const headers = await config.login({
userId: "us_fail", userId: "us_fail",
builder: false, builder: false,

View file

@ -1,9 +1,11 @@
import { Layout } from "@budibase/types"
export const BASE_LAYOUT_PROP_IDS = { export const BASE_LAYOUT_PROP_IDS = {
PRIVATE: "layout_private_master", PRIVATE: "layout_private_master",
PUBLIC: "layout_public_master", PUBLIC: "layout_public_master",
} }
export const EMPTY_LAYOUT = { export const EMPTY_LAYOUT: Layout = {
componentLibraries: ["@budibase/standard-components"], componentLibraries: ["@budibase/standard-components"],
title: "{{ name }}", title: "{{ name }}",
favicon: "./_shared/favicon.png", favicon: "./_shared/favicon.png",

View file

@ -1,5 +1,6 @@
import { roles } from "@budibase/backend-core" import { roles } from "@budibase/backend-core"
import { BASE_LAYOUT_PROP_IDS } from "./layouts" import { BASE_LAYOUT_PROP_IDS } from "./layouts"
import { Screen } from "@budibase/types"
export function createHomeScreen( export function createHomeScreen(
config: { config: {
@ -9,10 +10,8 @@ export function createHomeScreen(
roleId: roles.BUILTIN_ROLE_IDS.BASIC, roleId: roles.BUILTIN_ROLE_IDS.BASIC,
route: "/", route: "/",
} }
) { ): Screen {
return { return {
description: "",
url: "",
layoutId: BASE_LAYOUT_PROP_IDS.PRIVATE, layoutId: BASE_LAYOUT_PROP_IDS.PRIVATE,
props: { props: {
_id: "d834fea2-1b3e-4320-ab34-f9009f5ecc59", _id: "d834fea2-1b3e-4320-ab34-f9009f5ecc59",

View file

@ -53,9 +53,12 @@ import {
Datasource, Datasource,
FieldType, FieldType,
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
Layout,
Query,
RelationshipFieldMetadata, RelationshipFieldMetadata,
RelationshipType, RelationshipType,
Row, Row,
Screen,
SearchParams, SearchParams,
SourceName, SourceName,
Table, Table,
@ -63,6 +66,7 @@ import {
User, User,
UserCtx, UserCtx,
View, View,
Webhook,
WithRequired, WithRequired,
} from "@budibase/types" } from "@budibase/types"
@ -182,6 +186,15 @@ export default class TestConfiguration {
return this.automation return this.automation
} }
getDatasource() {
if (!this.datasource) {
throw new Error(
"datasource has not been initialised, call config.init() first"
)
}
return this.datasource
}
async doInContext<T>( async doInContext<T>(
appId: string | undefined, appId: string | undefined,
task: () => Promise<T> task: () => Promise<T>
@ -288,10 +301,10 @@ export default class TestConfiguration {
// UTILS // UTILS
_req<Req extends Record<string, any>, Res>( _req<Req extends Record<string, any> | void, Res>(
handler: (ctx: UserCtx<Req, Res>) => Promise<void>, handler: (ctx: UserCtx<Req, Res>) => Promise<void>,
body?: Req, body?: Req,
params?: Record<string, string> params?: Record<string, string | undefined>
): Promise<Res> { ): Promise<Res> {
// create a fake request ctx // create a fake request ctx
const request: any = {} const request: any = {}
@ -399,7 +412,7 @@ export default class TestConfiguration {
builder, builder,
prodApp, prodApp,
}: { }: {
roleId: string roleId?: string
userId: string userId: string
builder: boolean builder: boolean
prodApp: boolean prodApp: boolean
@ -415,7 +428,7 @@ export default class TestConfiguration {
await this.globalUser({ await this.globalUser({
_id: userId, _id: userId,
builder: { global: builder }, builder: { global: builder },
roles: { [appId]: roleId }, roles: { [appId]: roleId || roles.BUILTIN_ROLE_IDS.BASIC },
}) })
} }
await sessions.createASession(userId, { await sessions.createASession(userId, {
@ -772,7 +785,7 @@ export default class TestConfiguration {
return this._req(automationController.fetch) return this._req(automationController.fetch)
} }
async deleteAutomation(automation?: any) { async deleteAutomation(automation?: Automation) {
automation = automation || this.automation automation = automation || this.automation
if (!automation) { if (!automation) {
return return
@ -783,7 +796,7 @@ export default class TestConfiguration {
}) })
} }
async createWebhook(config?: any) { async createWebhook(config?: Webhook) {
if (!this.automation) { if (!this.automation) {
throw "Must create an automation before creating webhook." throw "Must create an automation before creating webhook."
} }
@ -811,7 +824,7 @@ export default class TestConfiguration {
return { ...this.datasource, _id: this.datasource!._id! } return { ...this.datasource, _id: this.datasource!._id! }
} }
async restDatasource(cfg?: any) { async restDatasource(cfg?: Record<string, any>) {
return this.createDatasource({ return this.createDatasource({
datasource: { datasource: {
...basicDatasource().datasource, ...basicDatasource().datasource,
@ -868,24 +881,23 @@ export default class TestConfiguration {
// QUERY // QUERY
async createQuery(config?: any) { async createQuery(config?: Query) {
if (!this.datasource && !config) { return this._req(
throw "No datasource created for query." queryController.save,
} config || basicQuery(this.getDatasource()._id!)
config = config || basicQuery(this.datasource!._id!) )
return this._req(queryController.save, config)
} }
// SCREEN // SCREEN
async createScreen(config?: any) { async createScreen(config?: Screen) {
config = config || basicScreen() config = config || basicScreen()
return this._req(screenController.save, config) return this._req(screenController.save, config)
} }
// LAYOUT // LAYOUT
async createLayout(config?: any) { async createLayout(config?: Layout) {
config = config || basicLayout() config = config || basicLayout()
return await this._req(layoutController.save, config) return await this._req(layoutController.save, config)
} }

View file

@ -22,6 +22,8 @@ import {
INTERNAL_TABLE_SOURCE_ID, INTERNAL_TABLE_SOURCE_ID,
TableSourceType, TableSourceType,
Query, Query,
Webhook,
WebhookActionType,
} from "@budibase/types" } from "@budibase/types"
import { LoopInput, LoopStepType } from "../../definitions/automations" import { LoopInput, LoopStepType } from "../../definitions/automations"
@ -407,12 +409,12 @@ export function basicLayout() {
return cloneDeep(EMPTY_LAYOUT) return cloneDeep(EMPTY_LAYOUT)
} }
export function basicWebhook(automationId: string) { export function basicWebhook(automationId: string): Webhook {
return { return {
live: true, live: true,
name: "webhook", name: "webhook",
action: { action: {
type: "automation", type: WebhookActionType.AUTOMATION,
target: automationId, target: automationId,
}, },
} }

View file

@ -1,6 +1,11 @@
import { Document } from "../document" import { Document } from "../document"
export interface Layout extends Document { export interface Layout extends Document {
componentLibraries: string[]
title: string
favicon: string
stylesheets: string[]
props: any props: any
layoutId?: string layoutId?: string
name?: string
} }

View file

@ -22,4 +22,5 @@ export interface Screen extends Document {
routing: ScreenRouting routing: ScreenRouting
props: ScreenProps props: ScreenProps
name?: string name?: string
pluginAdded?: boolean
} }