Merge pull request #8197 from Budibase/api-create-app

Api create app
This commit is contained in:
Pedro Silva 2022-10-11 17:28:22 +01:00 committed by GitHub
commit 44a35880d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 217 additions and 4 deletions

View File

@ -103,7 +103,7 @@ exports.revert = async ctx => {
target: appId, target: appId,
}) })
try { try {
if (!env.isCypress()) { if (env.COUCH_DB_URL) {
// in-memory db stalls on rollback // in-memory db stalls on rollback
await replication.rollback() await replication.rollback()
} }

View File

@ -18,6 +18,7 @@ import { Table } from "@budibase/types"
import { quotas } from "@budibase/pro" import { quotas } from "@budibase/pro"
import { isEqual } from "lodash" import { isEqual } from "lodash"
import { cloneDeep } from "lodash/fp" import { cloneDeep } from "lodash/fp"
import env from "../../../environment"
function checkAutoColumns(table: Table, oldTable: Table) { function checkAutoColumns(table: Table, oldTable: Table) {
if (!table.schema) { if (!table.schema) {
@ -167,7 +168,7 @@ export async function destroy(ctx: any) {
await db.remove(tableToDelete) await db.remove(tableToDelete)
// remove table search index // remove table search index
if (!isTest()) { if (!isTest() || env.COUCH_DB_URL) {
const currentIndexes = await db.getIndexes() const currentIndexes = await db.getIndexes()
const existingIndex = currentIndexes.indexes.find( const existingIndex = currentIndexes.indexes.find(
(existing: any) => existing.name === `search:${ctx.params.tableId}` (existing: any) => existing.name === `search:${ctx.params.tableId}`

View File

@ -3,6 +3,11 @@ import { App } from "@budibase/types"
import { Response } from "node-fetch" import { Response } from "node-fetch"
import InternalAPIClient from "./InternalAPIClient" import InternalAPIClient from "./InternalAPIClient"
import FormData from "form-data" import FormData from "form-data"
import { RouteConfig } from "../fixtures/types/routing"
import { AppPackageResponse } from "../fixtures/types/appPackage"
import { DeployConfig } from "../fixtures/types/deploy"
type messageResponse = { message: string }
export default class AppApi { export default class AppApi {
api: InternalAPIClient api: InternalAPIClient
@ -23,13 +28,13 @@ export default class AppApi {
return [response, Object.keys(json.routes).length > 0] return [response, Object.keys(json.routes).length > 0]
} }
async getAppPackage(appId: string): Promise<[Response, any]> { async getAppPackage(appId: string): Promise<[Response, AppPackageResponse]> {
const response = await this.api.get(`/applications/${appId}/appPackage`) const response = await this.api.get(`/applications/${appId}/appPackage`)
const json = await response.json() const json = await response.json()
return [response, json] return [response, json]
} }
async publish(): Promise<[Response, string]> { async publish(): Promise<[Response, DeployConfig]> {
const response = await this.api.post("/deploy") const response = await this.api.post("/deploy")
const json = await response.json() const json = await response.json()
return [response, json] return [response, json]
@ -46,4 +51,47 @@ export default class AppApi {
const json = await response.json() const json = await response.json()
return [response, json.data] return [response, json.data]
} }
async sync(appId: string): Promise<[Response, messageResponse]> {
const response = await this.api.post(`/applications/${appId}/sync`)
const json = await response.json()
return [response, json]
}
async updateClient(appId: string, body: any): Promise<[Response, Application]> {
const response = await this.api.put(`/applications/${appId}/client/update`, { body })
const json = await response.json()
return [response, json]
}
async revert(appId: string): Promise<[Response, messageResponse]> {
const response = await this.api.post(`/dev/${appId}/revert`)
const json = await response.json()
return [response, json]
}
async delete(appId: string): Promise<[Response, any]> {
const response = await this.api.del(`/applications/${appId}`)
const json = await response.json()
return [response, json]
}
async update(appId: string, body: any): Promise<[Response, Application]> {
const response = await this.api.put(`/applications/${appId}`, { body })
const json = await response.json()
return [response, json]
}
async addScreentoApp(body: any): Promise<[Response, Application]> {
const response = await this.api.post(`/screens`, { body })
const json = await response.json()
return [response, json]
}
async getRoutes(): Promise<[Response, RouteConfig]> {
const response = await this.api.get(`/routing`)
const json = await response.json()
return [response, json]
}
} }

View File

@ -0,0 +1,34 @@
import generator from "../../generator"
const randomId = generator.guid()
const generateScreen = (): any => ({
showNavigation: true,
width: "Large",
props: {
_id: randomId,
_component: "@budibase/standard-components/container",
_styles: {
normal: {},
hover: {},
active: {},
selected: {},
},
_children: [],
_instanceName: "New Screen",
direction: "column",
hAlign: "stretch",
vAlign: "top",
size: "grow",
gap: "M",
},
routing: {
route: "/test",
roleId: "BASIC",
homeScreen: false,
},
name: randomId,
template: "createFromScratch",
})
export default generateScreen

View File

@ -0,0 +1,9 @@
import { Application } from "@budibase/server/api/controllers/public/mapping/types"
import { Layout } from "@budibase/types"
import { Screen } from "@budibase/types"
// Create type for getAppPackage response
export interface AppPackageResponse {
application: Partial<Application>,
layout: Layout,
screens: Screen[]
}

View File

@ -0,0 +1,6 @@
export interface DeployConfig {
appUrl: string,
status: string,
"_id": string
}

View File

@ -0,0 +1,17 @@
export interface RouteConfig {
routes: Record<string, Route>
}
export interface Route {
subpaths: Record<string, Subpath>
}
export interface Subpath {
screens: ScreenRouteConfig
}
export interface ScreenRouteConfig {
BASIC?: string
POWER?: string
ADMIN?: string
}

View File

@ -4,6 +4,7 @@ import { db } from "@budibase/backend-core"
import InternalAPIClient from "../../../config/internal-api/TestConfiguration/InternalAPIClient" import InternalAPIClient from "../../../config/internal-api/TestConfiguration/InternalAPIClient"
import generateApp from "../../../config/internal-api/fixtures/applications" import generateApp from "../../../config/internal-api/fixtures/applications"
import generator from "../../../config/generator" import generator from "../../../config/generator"
import generateScreen from "../../../config/internal-api/fixtures/screens"
describe("Internal API - /applications endpoints", () => { describe("Internal API - /applications endpoints", () => {
const api = new InternalAPIClient() const api = new InternalAPIClient()
@ -84,4 +85,101 @@ describe("Internal API - /applications endpoints", () => {
await config.applications.canRender() await config.applications.canRender()
expect(publishedAppRenders).toBe(true) expect(publishedAppRenders).toBe(true)
}) })
it("POST - Sync application before deployment", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
config.applications.api.appId = app.appId
const [syncResponse, sync] = await config.applications.sync(<string>app.appId)
expect(syncResponse).toHaveStatusCode(200)
expect(sync).toEqual({
message: "App sync not required, app not deployed."
})
})
it("POST - Sync application after deployment", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
config.applications.api.appId = app.appId
// publish app
await config.applications.publish()
const [syncResponse, sync] = await config.applications.sync(<string>app.appId)
expect(syncResponse).toHaveStatusCode(200)
expect(sync).toEqual({
message: "App sync completed successfully."
})
})
it("PUT - Update an application", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
config.applications.api.appId = app.appId
const [updateResponse, updatedApp] = await config.applications.update(<string>app.appId, {
name: generator.word(),
})
expect(updateResponse).toHaveStatusCode(200)
expect(updatedApp.name).not.toEqual(app.name)
})
it("POST - Revert Changes without changes", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
config.applications.api.appId = app.appId
const [revertResponse, revert] = await config.applications.revert(<string>app.appId)
expect(revertResponse).toHaveStatusCode(400)
expect(revert).toEqual({
message: "App has not yet been deployed",
status: 400
})
})
it("POST - Revert Changes", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
config.applications.api.appId = app.appId
// publish app
const [publishResponse, publish] = await config.applications.publish()
expect(publishResponse).toHaveStatusCode(200)
expect(publish.status).toEqual("SUCCESS")
// Change/add component to the app
const [screenResponse, screen] = await config.applications.addScreentoApp(generateScreen())
expect(screenResponse).toHaveStatusCode(200)
expect(screen._id).toBeDefined()
// // Revert the app to published state
const [revertResponse, revert] = await config.applications.revert(<string>app.appId)
expect(revertResponse).toHaveStatusCode(200)
expect(revert).toEqual({
message: "Reverted changes successfully."
})
// Check screen is removed
const [routesResponse, routes] = await config.applications.getRoutes()
expect(routesResponse).toHaveStatusCode(200)
expect(routes.routes["/test"]).toBeUndefined()
})
it("DELETE - Delete an application", async () => {
const [response, app] = await config.applications.create(generateApp())
expect(response).toHaveStatusCode(200)
expect(app.appId).toBeDefined()
const [deleteResponse] = await config.applications.delete(<string>app.appId)
expect(deleteResponse).toHaveStatusCode(200)
})
}) })