Updating all of the route files to typescript, as well as some controllers.

This commit is contained in:
mike12345567 2022-11-22 18:49:19 +00:00
parent a620791d13
commit ed6a6ab8ce
59 changed files with 748 additions and 639 deletions

View File

@ -2,8 +2,9 @@ import { DocumentType } from "../../db/utils"
import { Plugin } from "@budibase/types"
import { db as dbCore, context, tenancy } from "@budibase/backend-core"
import { getComponentLibraryManifest } from "../../utilities/fileSystem"
import { BBContext } from "@budibase/types"
exports.fetchAppComponentDefinitions = async function (ctx: any) {
export async function fetchAppComponentDefinitions(ctx: BBContext) {
try {
const db = context.getAppDB()
const app = await db.get(DocumentType.APP_METADATA)

View File

@ -1,15 +1,20 @@
const newid = require("../../../db/newid")
const { getAppId } = require("@budibase/backend-core/context")
import newid from "../../../db/newid"
import { context } from "@budibase/backend-core"
/**
* This is used to pass around information about the deployment that is occurring
*/
class Deployment {
export class Deployment {
_id: string
verification: any
status?: string
err?: any
constructor(id = null) {
this._id = id || newid()
}
setVerification(verification) {
setVerification(verification: any) {
if (!verification) {
return
}
@ -20,14 +25,14 @@ class Deployment {
return this.verification
}
setStatus(status, err = null) {
setStatus(status: string, err?: any) {
this.status = status
if (err) {
this.err = err
}
}
fromJSON(json) {
fromJSON(json: any) {
if (json.verification) {
this.setVerification(json.verification)
}
@ -37,9 +42,9 @@ class Deployment {
}
getJSON() {
const obj = {
const obj: any = {
_id: this._id,
appId: getAppId(),
appId: context.getAppId(),
status: this.status,
}
if (this.err) {
@ -51,5 +56,3 @@ class Deployment {
return obj
}
}
module.exports = Deployment

View File

@ -1,6 +1,5 @@
import { npmUpload, urlUpload, githubUpload, fileUpload } from "./uploaders"
import { getGlobalDB } from "@budibase/backend-core/tenancy"
import { validate } from "@budibase/backend-core/plugins"
import { plugins as pluginCore, tenancy } from "@budibase/backend-core"
import { PluginType, FileType, PluginSource } from "@budibase/types"
import env from "../../../environment"
import { ClientAppSocket } from "../../../websocket"
@ -8,7 +7,7 @@ import { db as dbCore } from "@budibase/backend-core"
import { plugins } from "@budibase/pro"
export async function getPlugins(type?: PluginType) {
const db = getGlobalDB()
const db = tenancy.getGlobalDB()
const response = await db.allDocs(
dbCore.getPluginParams(null, {
include_docs: true,
@ -76,7 +75,7 @@ export async function create(ctx: any) {
break
}
validate(metadata?.schema)
pluginCore.validate(metadata?.schema)
// Only allow components in cloud
if (!env.SELF_HOSTED && metadata?.schema?.type !== PluginType.COMPONENT) {
@ -121,7 +120,7 @@ export async function processUploadedPlugin(
source?: PluginSource
) {
const { metadata, directory } = await fileUpload(plugin)
validate(metadata?.schema)
pluginCore.validate(metadata?.schema)
// Only allow components in cloud
if (!env.SELF_HOSTED && metadata?.schema?.type !== PluginType.COMPONENT) {

View File

@ -27,12 +27,8 @@ import { breakExternalTableId, isSQL } from "../../../integrations/utils"
import { processObjectSync } from "@budibase/string-templates"
// @ts-ignore
import { cloneDeep } from "lodash/fp"
import {
processFormulas,
processDates,
} from "../../../utilities/rowProcessor/utils"
// @ts-ignore
import { getAppDB } from "@budibase/backend-core/context"
import { processFormulas, processDates } from "../../../utilities/rowProcessor"
import { context } from "@budibase/backend-core"
interface ManyRelationship {
tableId?: string
@ -444,7 +440,7 @@ module External {
// Process some additional data types
let finalRowArray = Object.values(finalRows)
finalRowArray = processDates(table, finalRowArray)
finalRowArray = processFormulas(table, finalRowArray)
finalRowArray = processFormulas(table, finalRowArray) as Row[]
return finalRowArray.map((row: Row) =>
this.squashRelationshipColumns(table, row, relationships)
@ -673,7 +669,7 @@ module External {
throw "Unable to run without a table name"
}
if (!this.datasource) {
const db = getAppDB()
const db = context.getAppDB()
this.datasource = await db.get(datasourceId)
if (!this.datasource || !this.datasource.entities) {
throw "No tables found, fetch tables before query."

View File

@ -1,15 +1,35 @@
const { SearchIndexes } = require("../../../db/utils")
const { removeKeyNumbering } = require("./utils")
const fetch = require("node-fetch")
const { getCouchInfo } = require("@budibase/backend-core/db")
const { getAppId } = require("@budibase/backend-core/context")
import { SearchIndexes } from "../../../db/utils"
import { removeKeyNumbering } from "./utils"
import fetch from "node-fetch"
import { db as dbCore, context } from "@budibase/backend-core"
import { SearchFilters, Row } from "@budibase/types"
type SearchParams = {
tableId: string
sort?: string
sortOrder?: string
sortType?: string
limit?: number
bookmark?: string
version?: string
rows?: Row[]
}
/**
* Class to build lucene query URLs.
* Optionally takes a base lucene query object.
*/
class QueryBuilder {
constructor(base) {
export class QueryBuilder {
query: SearchFilters
limit: number
sort?: string
bookmark?: string
sortOrder: string
sortType: string
includeDocs: boolean
version?: string
constructor(base?: SearchFilters) {
this.query = {
allOr: false,
string: {},
@ -29,49 +49,52 @@ class QueryBuilder {
this.sortOrder = "ascending"
this.sortType = "string"
this.includeDocs = true
this.version = null
}
setVersion(version) {
setVersion(version?: string) {
if (version != null) {
this.version = version
}
return this
}
setTable(tableId) {
this.query.equal.tableId = tableId
setTable(tableId: string) {
this.query.equal!.tableId = tableId
return this
}
setLimit(limit) {
setLimit(limit?: number) {
if (limit != null) {
this.limit = limit
}
return this
}
setSort(sort) {
setSort(sort?: string) {
if (sort != null) {
this.sort = sort
}
return this
}
setSortOrder(sortOrder) {
setSortOrder(sortOrder?: string) {
if (sortOrder != null) {
this.sortOrder = sortOrder
}
return this
}
setSortType(sortType) {
setSortType(sortType?: string) {
if (sortType != null) {
this.sortType = sortType
}
return this
}
setBookmark(bookmark) {
setBookmark(bookmark?: string) {
if (bookmark != null) {
this.bookmark = bookmark
}
return this
}
@ -80,61 +103,61 @@ class QueryBuilder {
return this
}
addString(key, partial) {
this.query.string[key] = partial
addString(key: string, partial: string) {
this.query.string![key] = partial
return this
}
addFuzzy(key, fuzzy) {
this.query.fuzzy[key] = fuzzy
addFuzzy(key: string, fuzzy: string) {
this.query.fuzzy![key] = fuzzy
return this
}
addRange(key, low, high) {
this.query.range = {
addRange(key: string, low: string | number, high: string | number) {
this.query.range![key] = {
low,
high,
}
return this
}
addEqual(key, value) {
this.query.equal[key] = value
addEqual(key: string, value: any) {
this.query.equal![key] = value
return this
}
addNotEqual(key, value) {
this.query.notEqual[key] = value
addNotEqual(key: string, value: any) {
this.query.notEqual![key] = value
return this
}
addEmpty(key, value) {
this.query.empty[key] = value
addEmpty(key: string, value: any) {
this.query.empty![key] = value
return this
}
addNotEmpty(key, value) {
this.query.notEmpty[key] = value
addNotEmpty(key: string, value: any) {
this.query.notEmpty![key] = value
return this
}
addOneOf(key, value) {
this.query.oneOf[key] = value
addOneOf(key: string, value: any) {
this.query.oneOf![key] = value
return this
}
addContains(key, value) {
this.query.contains[key] = value
addContains(key: string, value: any) {
this.query.contains![key] = value
return this
}
addNotContains(key, value) {
this.query.notContains[key] = value
addNotContains(key: string, value: any) {
this.query.notContains![key] = value
return this
}
addContainsAny(key, value) {
this.query.containsAny[key] = value
addContainsAny(key: string, value: any) {
this.query.containsAny![key] = value
return this
}
@ -145,7 +168,7 @@ class QueryBuilder {
* @param options The preprocess options
* @returns {string|*}
*/
preprocess(value, { escape, lowercase, wrap, type } = {}) {
preprocess(value: any, { escape, lowercase, wrap, type }: any = {}) {
const hasVersion = !!this.version
// Determine if type needs wrapped
const originalType = typeof value
@ -173,12 +196,12 @@ class QueryBuilder {
let query = allOr ? "" : "*:*"
const allPreProcessingOpts = { escape: true, lowercase: true, wrap: true }
let tableId
if (this.query.equal.tableId) {
tableId = this.query.equal.tableId
delete this.query.equal.tableId
if (this.query.equal!.tableId) {
tableId = this.query.equal!.tableId
delete this.query.equal!.tableId
}
const equal = (key, value) => {
const equal = (key: string, value: any) => {
// 0 evaluates to false, which means we would return all rows if we don't check it
if (!value && value !== 0) {
return null
@ -186,7 +209,7 @@ class QueryBuilder {
return `${key}:${builder.preprocess(value, allPreProcessingOpts)}`
}
const contains = (key, value, mode = "AND") => {
const contains = (key: string, value: any, mode = "AND") => {
if (Array.isArray(value) && value.length === 0) {
return null
}
@ -202,16 +225,17 @@ class QueryBuilder {
return `${key}:(${statement})`
}
const notContains = (key, value) => {
const notContains = (key: string, value: any) => {
// @ts-ignore
const allPrefix = allOr === "" ? "*:* AND" : ""
return allPrefix + "NOT " + contains(key, value)
}
const containsAny = (key, value) => {
const containsAny = (key: string, value: any) => {
return contains(key, value, "OR")
}
const oneOf = (key, value) => {
const oneOf = (key: string, value: any) => {
if (!Array.isArray(value)) {
if (typeof value === "string") {
value = value.split(",")
@ -229,7 +253,7 @@ class QueryBuilder {
return `${key}:(${orStatement})`
}
function build(structure, queryFn) {
function build(structure: any, queryFn: any) {
for (let [key, value] of Object.entries(structure)) {
// check for new format - remove numbering if needed
key = removeKeyNumbering(key)
@ -249,7 +273,7 @@ class QueryBuilder {
// Construct the actual lucene search query string from JSON structure
if (this.query.string) {
build(this.query.string, (key, value) => {
build(this.query.string, (key: string, value: any) => {
if (!value) {
return null
}
@ -262,7 +286,7 @@ class QueryBuilder {
})
}
if (this.query.range) {
build(this.query.range, (key, value) => {
build(this.query.range, (key: string, value: any) => {
if (!value) {
return null
}
@ -278,7 +302,7 @@ class QueryBuilder {
})
}
if (this.query.fuzzy) {
build(this.query.fuzzy, (key, value) => {
build(this.query.fuzzy, (key: string, value: any) => {
if (!value) {
return null
}
@ -294,7 +318,7 @@ class QueryBuilder {
build(this.query.equal, equal)
}
if (this.query.notEqual) {
build(this.query.notEqual, (key, value) => {
build(this.query.notEqual, (key: string, value: any) => {
if (!value) {
return null
}
@ -302,10 +326,10 @@ class QueryBuilder {
})
}
if (this.query.empty) {
build(this.query.empty, key => `!${key}:["" TO *]`)
build(this.query.empty, (key: string) => `!${key}:["" TO *]`)
}
if (this.query.notEmpty) {
build(this.query.notEmpty, key => `${key}:["" TO *]`)
build(this.query.notEmpty, (key: string) => `${key}:["" TO *]`)
}
if (this.query.oneOf) {
build(this.query.oneOf, oneOf)
@ -329,7 +353,7 @@ class QueryBuilder {
}
buildSearchBody() {
let body = {
let body: any = {
q: this.buildSearchQuery(),
limit: Math.min(this.limit, 200),
include_docs: this.includeDocs,
@ -346,17 +370,14 @@ class QueryBuilder {
}
async run() {
const appId = getAppId()
const { url, cookie } = getCouchInfo()
const appId = context.getAppId()
const { url, cookie } = dbCore.getCouchInfo()
const fullPath = `${url}/${appId}/_design/database/_search/${SearchIndexes.ROWS}`
const body = this.buildSearchBody()
return await runQuery(fullPath, body, cookie)
}
}
// exported for unit testing
exports.QueryBuilder = QueryBuilder
/**
* Executes a lucene search query.
* @param url The query URL
@ -364,7 +385,7 @@ exports.QueryBuilder = QueryBuilder
* @param cookie The auth cookie for CouchDB
* @returns {Promise<{rows: []}>}
*/
const runQuery = async (url, body, cookie) => {
const runQuery = async (url: string, body: any, cookie: string) => {
const response = await fetch(url, {
body: JSON.stringify(body),
method: "POST",
@ -374,11 +395,11 @@ const runQuery = async (url, body, cookie) => {
})
const json = await response.json()
let output = {
let output: any = {
rows: [],
}
if (json.rows != null && json.rows.length > 0) {
output.rows = json.rows.map(row => row.doc)
output.rows = json.rows.map((row: any) => row.doc)
}
if (json.bookmark) {
output.bookmark = json.bookmark
@ -402,7 +423,7 @@ const runQuery = async (url, body, cookie) => {
* rows {array|null} Current results in the recursive search
* @returns {Promise<*[]|*>}
*/
const recursiveSearch = async (query, params) => {
async function recursiveSearch(query: any, params: any): Promise<any> {
const bookmark = params.bookmark
const rows = params.rows || []
if (rows.length >= params.limit) {
@ -450,7 +471,10 @@ const recursiveSearch = async (query, params) => {
* bookmark {string} The bookmark to resume from
* @returns {Promise<{hasNextPage: boolean, rows: *[]}>}
*/
exports.paginatedSearch = async (query, params) => {
export async function paginatedSearch(
query: SearchFilters,
params: SearchParams
) {
let limit = params.limit
if (limit == null || isNaN(limit) || limit < 0) {
limit = 50
@ -496,7 +520,7 @@ exports.paginatedSearch = async (query, params) => {
* limit {number} The desired number of results
* @returns {Promise<{rows: *}>}
*/
exports.fullSearch = async (query, params) => {
export async function fullSearch(query: SearchFilters, params: SearchParams) {
let limit = params.limit
if (limit == null || isNaN(limit) || limit < 0) {
limit = 1000

View File

@ -1,13 +1,14 @@
const { getRowParams } = require("../../../db/utils")
const {
import { getRowParams } from "../../../db/utils"
import {
outputProcessing,
processAutoColumn,
processFormulas,
} = require("../../../utilities/rowProcessor")
const { FieldTypes, FormulaTypes } = require("../../../constants")
} from "../../../utilities/rowProcessor"
import { FieldTypes, FormulaTypes } from "../../../constants"
import { context } from "@budibase/backend-core"
import { Table, Row } from "@budibase/types"
const { isEqual } = require("lodash")
const { cloneDeep } = require("lodash/fp")
const { getAppDB } = require("@budibase/backend-core/context")
/**
* This function runs through a list of enriched rows, looks at the rows which
@ -15,22 +16,22 @@ const { getAppDB } = require("@budibase/backend-core/context")
* updated.
* NOTE: this will only for affect static formulas.
*/
exports.updateRelatedFormula = async (table, enrichedRows) => {
const db = getAppDB()
exports.updateRelatedFormula = async (table: Table, enrichedRows: Row[]) => {
const db = context.getAppDB()
// no formula to update, we're done
if (!table.relatedFormula) {
return
}
let promises = []
let promises: Promise<any>[] = []
for (let enrichedRow of Array.isArray(enrichedRows)
? enrichedRows
: [enrichedRows]) {
// the related rows by tableId
let relatedRows = {}
let relatedRows: Record<string, Row[]> = {}
for (let [key, field] of Object.entries(enrichedRow)) {
const columnDefinition = table.schema[key]
if (columnDefinition && columnDefinition.type === FieldTypes.LINK) {
const relatedTableId = columnDefinition.tableId
const relatedTableId = columnDefinition.tableId!
if (!relatedRows[relatedTableId]) {
relatedRows[relatedTableId] = []
}
@ -38,7 +39,7 @@ exports.updateRelatedFormula = async (table, enrichedRows) => {
}
}
for (let tableId of table.relatedFormula) {
let relatedTable
let relatedTable: Table
try {
// no rows to update, skip
if (!relatedRows[tableId] || relatedRows[tableId].length === 0) {
@ -48,7 +49,7 @@ exports.updateRelatedFormula = async (table, enrichedRows) => {
} catch (err) {
// no error scenario, table doesn't seem to exist anymore, ignore
}
for (let column of Object.values(relatedTable.schema)) {
for (let column of Object.values(relatedTable!.schema)) {
// needs updated in related rows
if (
column.type === FieldTypes.FORMULA &&
@ -57,7 +58,7 @@ exports.updateRelatedFormula = async (table, enrichedRows) => {
// re-enrich rows for all the related, don't update the related formula for them
promises = promises.concat(
relatedRows[tableId].map(related =>
exports.finaliseRow(relatedTable, related, {
finaliseRow(relatedTable, related, {
updateFormula: false,
})
)
@ -70,8 +71,8 @@ exports.updateRelatedFormula = async (table, enrichedRows) => {
await Promise.all(promises)
}
exports.updateAllFormulasInTable = async table => {
const db = getAppDB()
export async function updateAllFormulasInTable(table: Table) {
const db = context.getAppDB()
// start by getting the raw rows (which will be written back to DB after update)
let rows = (
await db.allDocs(
@ -88,7 +89,9 @@ exports.updateAllFormulasInTable = async table => {
const updatedRows = []
for (let row of rows) {
// find the enriched row, if found process the formulas
const enrichedRow = enrichedRows.find(enriched => enriched._id === row._id)
const enrichedRow = enrichedRows.find(
(enriched: any) => enriched._id === row._id
)
if (enrichedRow) {
const processed = processFormulas(table, cloneDeep(row), {
dynamic: false,
@ -109,12 +112,14 @@ exports.updateAllFormulasInTable = async table => {
* row. The reason we need to return the enriched row is that the automation row created trigger
* expects the row to be totally enriched/contain all relationships.
*/
exports.finaliseRow = async (
table,
row,
{ oldTable, updateFormula } = { updateFormula: true }
) => {
const db = getAppDB()
export async function finaliseRow(
table: Table,
row: Row,
{ oldTable, updateFormula }: { oldTable?: Table; updateFormula: boolean } = {
updateFormula: true,
}
) {
const db = context.getAppDB()
row.type = "row"
// process the row before return, to include relationships
let enrichedRow = await outputProcessing(table, cloneDeep(row), {
@ -131,7 +136,7 @@ exports.finaliseRow = async (
if (oldTable && !isEqual(oldTable, table)) {
try {
await db.put(table)
} catch (err) {
} catch (err: any) {
if (err.status === 409) {
const updatedTable = await db.get(table._id)
let response = processAutoColumn(null, updatedTable, row, {

View File

@ -1,33 +1,32 @@
import { InternalTables } from "../../../db/utils"
import userController from "../user"
import { FieldTypes } from "../../../constants"
import { context } from "@budibase/backend-core"
import { makeExternalQuery } from "../../../integrations/base/query"
import { BBContext, Row, Table } from "@budibase/types"
export { removeKeyNumbering } from "../../../integrations/base/utils"
const validateJs = require("validate.js")
const { cloneDeep } = require("lodash/fp")
const { InternalTables } = require("../../../db/utils")
const userController = require("../user")
const { FieldTypes } = require("../../../constants")
const { getAppDB } = require("@budibase/backend-core/context")
const { makeExternalQuery } = require("../../../integrations/base/query")
const { removeKeyNumbering } = require("../../../integrations/base/utils")
validateJs.extend(validateJs.validators.datetime, {
parse: function (value) {
parse: function (value: string) {
return new Date(value).getTime()
},
// Input is a unix timestamp
format: function (value) {
format: function (value: string) {
return new Date(value).toISOString()
},
})
exports.removeKeyNumbering = removeKeyNumbering
exports.getDatasourceAndQuery = async json => {
export async function getDatasourceAndQuery(json: any) {
const datasourceId = json.endpoint.datasourceId
const db = getAppDB()
const db = context.getAppDB()
const datasource = await db.get(datasourceId)
return makeExternalQuery(datasource, json)
}
exports.findRow = async (ctx, tableId, rowId) => {
const db = getAppDB()
export async function findRow(ctx: BBContext, tableId: string, rowId: string) {
const db = context.getAppDB()
let row
// TODO remove special user case in future
if (tableId === InternalTables.USER_METADATA) {
@ -45,12 +44,20 @@ exports.findRow = async (ctx, tableId, rowId) => {
return row
}
exports.validate = async ({ tableId, row, table }) => {
export async function validate({
tableId,
row,
table,
}: {
tableId?: string
row: Row
table: Table
}) {
if (!table) {
const db = getAppDB()
const db = context.getAppDB()
table = await db.get(tableId)
}
const errors = {}
const errors: any = {}
for (let fieldName of Object.keys(table.schema)) {
const constraints = cloneDeep(table.schema[fieldName].constraints)
const type = table.schema[fieldName].type
@ -70,7 +77,7 @@ exports.validate = async ({ tableId, row, table }) => {
if (!Array.isArray(row[fieldName])) {
row[fieldName] = row[fieldName].split(",")
}
row[fieldName].map(val => {
row[fieldName].map((val: any) => {
if (
!constraints.inclusion.includes(val) &&
constraints.inclusion.length !== 0

View File

@ -7,9 +7,9 @@ import {
roles,
} from "@budibase/backend-core"
import { updateAppPackage } from "./application"
import { Plugin, ScreenProps } from "@budibase/types"
import { Plugin, ScreenProps, BBContext } from "@budibase/types"
exports.fetch = async (ctx: any) => {
export async function fetch(ctx: BBContext) {
const db = context.getAppDB()
const screens = (
@ -20,13 +20,17 @@ exports.fetch = async (ctx: any) => {
)
).rows.map((el: any) => el.doc)
const roleId = ctx.user?.role?._id as string
if (!roleId) {
ctx.throw("Unable to retrieve users role ID.")
}
ctx.body = await new roles.AccessController().checkScreensAccess(
screens,
ctx.user.role._id
roleId
)
}
exports.save = async (ctx: any) => {
export async function save(ctx: BBContext) {
const db = context.getAppDB()
let screen = ctx.request.body
@ -92,7 +96,7 @@ exports.save = async (ctx: any) => {
}
}
exports.destroy = async (ctx: any) => {
export async function destroy(ctx: BBContext) {
const db = context.getAppDB()
const id = ctx.params.screenId
const screen = await db.get(id)
@ -106,7 +110,7 @@ exports.destroy = async (ctx: any) => {
ctx.status = 200
}
const findPlugins = (component: ScreenProps, foundPlugins: string[]) => {
function findPlugins(component: ScreenProps, foundPlugins: string[]) {
if (!component) {
return
}

View File

@ -1,11 +0,0 @@
const ScriptRunner = require("../../utilities/scriptRunner")
exports.execute = async function (ctx) {
const { script, context } = ctx.request.body
const runner = new ScriptRunner(script, context)
ctx.body = runner.execute()
}
exports.save = async function (ctx) {
ctx.throw(501, "Not currently implemented")
}

View File

@ -0,0 +1,12 @@
import ScriptRunner from "../../utilities/scriptRunner"
import { BBContext } from "@budibase/types"
export async function execute(ctx: BBContext) {
const { script, context } = ctx.request.body
const runner = new ScriptRunner(script, context)
ctx.body = runner.execute()
}
export async function save(ctx: BBContext) {
ctx.throw(501, "Not currently implemented")
}

View File

@ -1,17 +1,20 @@
const fetch = require("node-fetch")
const { downloadTemplate } = require("../../utilities/fileSystem")
const env = require("../../environment")
import nodeFetch from "node-fetch"
import { downloadTemplate as dlTemplate } from "../../utilities/fileSystem"
import env from "../../environment"
import { BBContext } from "@budibase/types"
// development flag, can be used to test against templates exported locally
const DEFAULT_TEMPLATES_BUCKET =
"prod-budi-templates.s3-eu-west-1.amazonaws.com"
exports.fetch = async function (ctx) {
export async function fetch(ctx: BBContext) {
let type = env.TEMPLATE_REPOSITORY
let response,
error = false
try {
response = await fetch(`https://${DEFAULT_TEMPLATES_BUCKET}/manifest.json`)
response = await nodeFetch(
`https://${DEFAULT_TEMPLATES_BUCKET}/manifest.json`
)
if (response.status !== 200) {
error = true
}
@ -29,10 +32,10 @@ exports.fetch = async function (ctx) {
// can't currently test this, have to ignore from coverage
/* istanbul ignore next */
exports.downloadTemplate = async function (ctx) {
export async function downloadTemplate(ctx: BBContext) {
const { type, name } = ctx.params
await downloadTemplate(type, name)
await dlTemplate(type, name)
ctx.body = {
message: `template ${type}:${name} downloaded successfully.`,

View File

@ -1,9 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/analytics")
const router = new Router()
router.get("/api/bbtel", controller.isEnabled)
router.post("/api/bbtel/ping", controller.ping)
module.exports = router

View File

@ -0,0 +1,9 @@
import Router from "@koa/router"
import * as controller from "../controllers/analytics"
const router: Router = new Router()
router.get("/api/bbtel", controller.isEnabled)
router.post("/api/bbtel/ping", controller.ping)
export = router

View File

@ -1,12 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/apikeys")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router
.get("/api/keys", authorized(BUILDER), controller.fetch)
.put("/api/keys/:key", authorized(BUILDER), controller.update)
module.exports = router

View File

@ -0,0 +1,12 @@
import Router from "@koa/router"
import * as controller from "../controllers/apikeys"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.get("/api/keys", authorized(permissions.BUILDER), controller.fetch)
.put("/api/keys/:key", authorized(permissions.BUILDER), controller.update)
export = router

View File

@ -1,16 +1,20 @@
import Router from "@koa/router"
import * as controller from "../controllers/application"
import authorized from "../../middleware/authorized"
import { BUILDER } from "@budibase/backend-core/permissions"
import { permissions } from "@budibase/backend-core"
import { applicationValidator } from "./utils/validators"
const router: Router = new Router()
router
.post("/api/applications/:appId/sync", authorized(BUILDER), controller.sync)
.post(
"/api/applications/:appId/sync",
authorized(permissions.BUILDER),
controller.sync
)
.post(
"/api/applications",
authorized(BUILDER),
authorized(permissions.BUILDER),
applicationValidator(),
controller.create
)
@ -19,20 +23,24 @@ router
.get("/api/applications/:appId/appPackage", controller.fetchAppPackage)
.put(
"/api/applications/:appId",
authorized(BUILDER),
authorized(permissions.BUILDER),
applicationValidator({ isCreate: false }),
controller.update
)
.post(
"/api/applications/:appId/client/update",
authorized(BUILDER),
authorized(permissions.BUILDER),
controller.updateClient
)
.post(
"/api/applications/:appId/client/revert",
authorized(BUILDER),
authorized(permissions.BUILDER),
controller.revertClient
)
.delete("/api/applications/:appId", authorized(BUILDER), controller.destroy)
.delete(
"/api/applications/:appId",
authorized(permissions.BUILDER),
controller.destroy
)
export default router
export = router

View File

@ -5,4 +5,4 @@ const router: Router = new Router()
router.get("/api/self", controller.fetchSelf)
export default router
export = router

View File

@ -1,85 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/automation")
const authorized = require("../../middleware/authorized")
const {
BUILDER,
PermissionLevel,
PermissionType,
} = require("@budibase/backend-core/permissions")
const { bodyResource, paramResource } = require("../../middleware/resourceId")
const {
middleware: appInfoMiddleware,
AppType,
} = require("../../middleware/appInfo")
const { automationValidator } = require("./utils/validators")
const router = new Router()
router
.get(
"/api/automations/trigger/list",
authorized(BUILDER),
controller.getTriggerList
)
.get(
"/api/automations/action/list",
authorized(BUILDER),
controller.getActionList
)
.get(
"/api/automations/definitions/list",
authorized(BUILDER),
controller.getDefinitionList
)
.get("/api/automations", authorized(BUILDER), controller.fetch)
.get(
"/api/automations/:id",
paramResource("id"),
authorized(BUILDER),
controller.find
)
.put(
"/api/automations",
bodyResource("_id"),
authorized(BUILDER),
automationValidator(true),
controller.update
)
.post(
"/api/automations",
authorized(BUILDER),
automationValidator(false),
controller.create
)
.post(
"/api/automations/logs/search",
authorized(BUILDER),
controller.logSearch
)
.delete(
"/api/automations/logs",
authorized(BUILDER),
controller.clearLogError
)
.delete(
"/api/automations/:id/:rev",
paramResource("id"),
authorized(BUILDER),
controller.destroy
)
.post(
"/api/automations/:id/trigger",
appInfoMiddleware({ appType: AppType.PROD }),
paramResource("id"),
authorized(PermissionType.AUTOMATION, PermissionLevel.EXECUTE),
controller.trigger
)
.post(
"/api/automations/:id/test",
appInfoMiddleware({ appType: AppType.DEV }),
paramResource("id"),
authorized(PermissionType.AUTOMATION, PermissionLevel.EXECUTE),
controller.test
)
module.exports = router

View File

@ -0,0 +1,87 @@
import Router from "@koa/router"
import * as controller from "../controllers/automation"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import { bodyResource, paramResource } from "../../middleware/resourceId"
import {
middleware as appInfoMiddleware,
AppType,
} from "../../middleware/appInfo"
import { automationValidator } from "./utils/validators"
const router: Router = new Router()
router
.get(
"/api/automations/trigger/list",
authorized(permissions.BUILDER),
controller.getTriggerList
)
.get(
"/api/automations/action/list",
authorized(permissions.BUILDER),
controller.getActionList
)
.get(
"/api/automations/definitions/list",
authorized(permissions.BUILDER),
controller.getDefinitionList
)
.get("/api/automations", authorized(permissions.BUILDER), controller.fetch)
.get(
"/api/automations/:id",
paramResource("id"),
authorized(permissions.BUILDER),
controller.find
)
.put(
"/api/automations",
bodyResource("_id"),
authorized(permissions.BUILDER),
automationValidator(true),
controller.update
)
.post(
"/api/automations",
authorized(permissions.BUILDER),
automationValidator(false),
controller.create
)
.post(
"/api/automations/logs/search",
authorized(permissions.BUILDER),
controller.logSearch
)
.delete(
"/api/automations/logs",
authorized(permissions.BUILDER),
controller.clearLogError
)
.delete(
"/api/automations/:id/:rev",
paramResource("id"),
authorized(permissions.BUILDER),
controller.destroy
)
.post(
"/api/automations/:id/trigger",
appInfoMiddleware({ appType: AppType.PROD }),
paramResource("id"),
authorized(
permissions.PermissionType.AUTOMATION,
permissions.PermissionLevel.EXECUTE
),
controller.trigger
)
.post(
"/api/automations/:id/test",
appInfoMiddleware({ appType: AppType.DEV }),
paramResource("id"),
authorized(
permissions.PermissionType.AUTOMATION,
permissions.PermissionLevel.EXECUTE
),
controller.test
)
export = router

View File

@ -1,10 +1,14 @@
import Router from "@koa/router"
import * as controller from "../controllers/backup"
import authorized from "../../middleware/authorized"
import { BUILDER } from "@budibase/backend-core/permissions"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router.get("/api/backups/export", authorized(BUILDER), controller.exportAppDump)
router.get(
"/api/backups/export",
authorized(permissions.BUILDER),
controller.exportAppDump
)
export default router
export = router

View File

@ -1,14 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/cloud")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router
.get("/api/cloud/export", authorized(BUILDER), controller.exportApps)
// has to be public, only run if apps don't exist
.post("/api/cloud/import", controller.importApps)
.get("/api/cloud/import/complete", controller.hasBeenImported)
module.exports = router

View File

@ -0,0 +1,18 @@
import Router from "@koa/router"
import * as controller from "../controllers/cloud"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.get(
"/api/cloud/export",
authorized(permissions.BUILDER),
controller.exportApps
)
// has to be public, only run if apps don't exist
.post("/api/cloud/import", controller.importApps)
.get("/api/cloud/import/complete", controller.hasBeenImported)
export = router

View File

@ -1,14 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/component")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router.get(
"/api/:appId/components/definitions",
authorized(BUILDER),
controller.fetchAppComponentDefinitions
)
module.exports = router

View File

@ -0,0 +1,14 @@
import Router from "@koa/router"
import * as controller from "../controllers/component"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router.get(
"/api/:appId/components/definitions",
authorized(permissions.BUILDER),
controller.fetchAppComponentDefinitions
)
export = router

View File

@ -1,51 +0,0 @@
const Router = require("@koa/router")
const datasourceController = require("../controllers/datasource")
const authorized = require("../../middleware/authorized")
const {
BUILDER,
PermissionLevel,
PermissionType,
} = require("@budibase/backend-core/permissions")
const {
datasourceValidator,
datasourceQueryValidator,
} = require("./utils/validators")
const router = new Router()
router
.get("/api/datasources", authorized(BUILDER), datasourceController.fetch)
.get(
"/api/datasources/:datasourceId",
authorized(PermissionType.TABLE, PermissionLevel.READ),
datasourceController.find
)
.put(
"/api/datasources/:datasourceId",
authorized(PermissionType.TABLE, PermissionLevel.READ),
datasourceController.update
)
.post(
"/api/datasources/query",
authorized(PermissionType.TABLE, PermissionLevel.READ),
datasourceQueryValidator(),
datasourceController.query
)
.post(
"/api/datasources/:datasourceId/schema",
authorized(BUILDER),
datasourceController.buildSchemaFromDb
)
.post(
"/api/datasources",
authorized(BUILDER),
datasourceValidator(),
datasourceController.save
)
.delete(
"/api/datasources/:datasourceId/:revId",
authorized(BUILDER),
datasourceController.destroy
)
module.exports = router

View File

@ -0,0 +1,60 @@
import Router from "@koa/router"
import * as datasourceController from "../controllers/datasource"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import {
datasourceValidator,
datasourceQueryValidator,
} from "./utils/validators"
const router: Router = new Router()
router
.get(
"/api/datasources",
authorized(permissions.BUILDER),
datasourceController.fetch
)
.get(
"/api/datasources/:datasourceId",
authorized(
permissions.PermissionType.TABLE,
permissions.PermissionLevel.READ
),
datasourceController.find
)
.put(
"/api/datasources/:datasourceId",
authorized(
permissions.PermissionType.TABLE,
permissions.PermissionLevel.READ
),
datasourceController.update
)
.post(
"/api/datasources/query",
authorized(
permissions.PermissionType.TABLE,
permissions.PermissionLevel.READ
),
datasourceQueryValidator(),
datasourceController.query
)
.post(
"/api/datasources/:datasourceId/schema",
authorized(permissions.BUILDER),
datasourceController.buildSchemaFromDb
)
.post(
"/api/datasources",
authorized(permissions.BUILDER),
datasourceValidator(),
datasourceController.save
)
.delete(
"/api/datasources/:datasourceId/:revId",
authorized(permissions.BUILDER),
datasourceController.destroy
)
export = router

View File

@ -18,4 +18,4 @@ router
)
.post("/api/deploy", authorized(permissions.BUILDER), controller.deployApp)
export default router
export = router

View File

@ -1,26 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/dev")
const env = require("../../environment")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
function redirectPath(path) {
router
.get(`/api/${path}/:devPath(.*)`, controller.buildRedirectGet(path))
.post(`/api/${path}/:devPath(.*)`, controller.buildRedirectPost(path))
.delete(`/api/${path}/:devPath(.*)`, controller.buildRedirectDelete(path))
}
if (env.isDev() || env.isTest()) {
redirectPath("global")
redirectPath("system")
}
router
.get("/api/dev/version", authorized(BUILDER), controller.getBudibaseVersion)
.delete("/api/dev/:appId/lock", authorized(BUILDER), controller.clearLock)
.post("/api/dev/:appId/revert", authorized(BUILDER), controller.revert)
module.exports = router

View File

@ -0,0 +1,38 @@
import Router from "@koa/router"
import * as controller from "../controllers/dev"
import env from "../../environment"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
function redirectPath(path: string) {
router
.get(`/api/${path}/:devPath(.*)`, controller.buildRedirectGet(path))
.post(`/api/${path}/:devPath(.*)`, controller.buildRedirectPost(path))
.delete(`/api/${path}/:devPath(.*)`, controller.buildRedirectDelete(path))
}
if (env.isDev() || env.isTest()) {
redirectPath("global")
redirectPath("system")
}
router
.get(
"/api/dev/version",
authorized(permissions.BUILDER),
controller.getBudibaseVersion
)
.delete(
"/api/dev/:appId/lock",
authorized(permissions.BUILDER),
controller.clearLock
)
.post(
"/api/dev/:appId/revert",
authorized(permissions.BUILDER),
controller.revert
)
export = router

View File

@ -1,12 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/integration")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router
.get("/api/integrations", authorized(BUILDER), controller.fetch)
.get("/api/integrations/:type", authorized(BUILDER), controller.find)
module.exports = router

View File

@ -0,0 +1,16 @@
import Router from "@koa/router"
import controller from "../controllers/integration"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.get("/api/integrations", authorized(permissions.BUILDER), controller.fetch)
.get(
"/api/integrations/:type",
authorized(permissions.BUILDER),
controller.find
)
export = router

View File

@ -1,16 +0,0 @@
const Router = require("@koa/router")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const controller = require("../controllers/layout")
const router = new Router()
router
.post("/api/layouts", authorized(BUILDER), controller.save)
.delete(
"/api/layouts/:layoutId/:layoutRev",
authorized(BUILDER),
controller.destroy
)
module.exports = router

View File

@ -0,0 +1,16 @@
import Router from "@koa/router"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import controller from "../controllers/layout"
const router: Router = new Router()
router
.post("/api/layouts", authorized(permissions.BUILDER), controller.save)
.delete(
"/api/layouts/:layoutId/:layoutRev",
authorized(permissions.BUILDER),
controller.destroy
)
export = router

View File

@ -1,38 +1,38 @@
const Router = require("@koa/router")
const controller = require("../controllers/metadata")
const {
middleware: appInfoMiddleware,
import Router from "@koa/router"
import * as controller from "../controllers/metadata"
import {
middleware as appInfoMiddleware,
AppType,
} = require("../../middleware/appInfo")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
} from "../../middleware/appInfo"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router = new Router()
const router: Router = new Router()
router
.post(
"/api/metadata/:type/:entityId",
authorized(BUILDER),
authorized(permissions.BUILDER),
appInfoMiddleware({ appType: AppType.DEV }),
controller.saveMetadata
)
.delete(
"/api/metadata/:type/:entityId",
authorized(BUILDER),
authorized(permissions.BUILDER),
appInfoMiddleware({ appType: AppType.DEV }),
controller.deleteMetadata
)
.get(
"/api/metadata/type",
authorized(BUILDER),
authorized(permissions.BUILDER),
appInfoMiddleware({ appType: AppType.DEV }),
controller.getTypes
)
.get(
"/api/metadata/:type/:entityId",
authorized(BUILDER),
authorized(permissions.BUILDER),
appInfoMiddleware({ appType: AppType.DEV }),
controller.getMetadata
)
module.exports = router
export = router

View File

@ -1,14 +0,0 @@
const Router = require("@koa/router")
const migrationsController = require("../controllers/migrations")
const router = new Router()
const { internalApi } = require("@budibase/backend-core/auth")
router
.post("/api/migrations/run", internalApi, migrationsController.migrate)
.get(
"/api/migrations/definitions",
internalApi,
migrationsController.fetchDefinitions
)
module.exports = router

View File

@ -0,0 +1,14 @@
import Router from "@koa/router"
import * as migrationsController from "../controllers/migrations"
import { auth } from "@budibase/backend-core"
const router: Router = new Router()
router
.post("/api/migrations/run", auth.internalApi, migrationsController.migrate)
.get(
"/api/migrations/definitions",
auth.internalApi,
migrationsController.fetchDefinitions
)
export = router

View File

@ -1,33 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/permission")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const { permissionValidator } = require("./utils/validators")
const router = new Router()
router
.get("/api/permission/builtin", authorized(BUILDER), controller.fetchBuiltin)
.get("/api/permission/levels", authorized(BUILDER), controller.fetchLevels)
.get("/api/permission", authorized(BUILDER), controller.fetch)
.get(
"/api/permission/:resourceId",
authorized(BUILDER),
controller.getResourcePerms
)
// adding a specific role/level for the resource overrides the underlying access control
.post(
"/api/permission/:roleId/:resourceId/:level",
authorized(BUILDER),
permissionValidator(),
controller.addPermission
)
// deleting the level defaults it back the underlying access control for the resource
.delete(
"/api/permission/:roleId/:resourceId/:level",
authorized(BUILDER),
permissionValidator(),
controller.removePermission
)
module.exports = router

View File

@ -0,0 +1,41 @@
import Router from "@koa/router"
import * as controller from "../controllers/permission"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import { permissionValidator } from "./utils/validators"
const router: Router = new Router()
router
.get(
"/api/permission/builtin",
authorized(permissions.BUILDER),
controller.fetchBuiltin
)
.get(
"/api/permission/levels",
authorized(permissions.BUILDER),
controller.fetchLevels
)
.get("/api/permission", authorized(permissions.BUILDER), controller.fetch)
.get(
"/api/permission/:resourceId",
authorized(permissions.BUILDER),
controller.getResourcePerms
)
// adding a specific role/level for the resource overrides the underlying access control
.post(
"/api/permission/:roleId/:resourceId/:level",
authorized(permissions.BUILDER),
permissionValidator(),
controller.addPermission
)
// deleting the level defaults it back the underlying access control for the resource
.delete(
"/api/permission/:roleId/:resourceId/:level",
authorized(permissions.BUILDER),
permissionValidator(),
controller.removePermission
)
export = router

View File

@ -1,14 +1,22 @@
import Router from "@koa/router"
import * as controller from "../controllers/plugin"
import authorized from "../../middleware/authorized"
import { BUILDER } from "@budibase/backend-core/permissions"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.post("/api/plugin/upload", authorized(BUILDER), controller.upload)
.post("/api/plugin", authorized(BUILDER), controller.create)
.get("/api/plugin", authorized(BUILDER), controller.fetch)
.delete("/api/plugin/:pluginId", authorized(BUILDER), controller.destroy)
.post(
"/api/plugin/upload",
authorized(permissions.BUILDER),
controller.upload
)
.post("/api/plugin", authorized(permissions.BUILDER), controller.create)
.get("/api/plugin", authorized(permissions.BUILDER), controller.fetch)
.delete(
"/api/plugin/:pluginId",
authorized(permissions.BUILDER),
controller.destroy
)
export default router

View File

@ -1,22 +1,19 @@
const Router = require("@koa/router")
const queryController = require("../controllers/query")
const authorized = require("../../middleware/authorized")
const {
PermissionLevel,
PermissionType,
BUILDER,
} = require("@budibase/backend-core/permissions")
const {
import Router from "@koa/router"
import * as queryController from "../controllers/query"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import {
bodyResource,
bodySubResource,
paramResource,
} = require("../../middleware/resourceId")
const {
} from "../../middleware/resourceId"
import {
generateQueryPreviewValidation,
generateQueryValidation,
} = require("../controllers/query/validation")
} from "../controllers/query/validation"
const { BUILDER, PermissionType, PermissionLevel } = permissions
const router = new Router()
const router: Router = new Router()
router
.get("/api/queries", authorized(BUILDER), queryController.fetch)
@ -48,17 +45,17 @@ router
authorized(PermissionType.QUERY, PermissionLevel.WRITE),
queryController.executeV1
)
.post(
"/api/v2/queries/:queryId",
paramResource("queryId"),
authorized(PermissionType.QUERY, PermissionLevel.WRITE),
queryController.executeV2
)
.delete(
"/api/queries/:queryId/:revId",
paramResource("queryId"),
authorized(BUILDER),
queryController.destroy
)
.post(
"/api/v2/queries/:queryId",
paramResource("queryId"),
authorized(PermissionType.QUERY, PermissionLevel.WRITE),
queryController.executeV2 as any
)
module.exports = router
export = router

View File

@ -1,15 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/role")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const { roleValidator } = require("./utils/validators")
const router = new Router()
router
.post("/api/roles", authorized(BUILDER), roleValidator(), controller.save)
.get("/api/roles", authorized(BUILDER), controller.fetch)
.get("/api/roles/:roleId", authorized(BUILDER), controller.find)
.delete("/api/roles/:roleId/:rev", authorized(BUILDER), controller.destroy)
module.exports = router

View File

@ -0,0 +1,24 @@
import Router from "@koa/router"
import * as controller from "../controllers/role"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import { roleValidator } from "./utils/validators"
const router: Router = new Router()
router
.post(
"/api/roles",
authorized(permissions.BUILDER),
roleValidator(),
controller.save
)
.get("/api/roles", authorized(permissions.BUILDER), controller.fetch)
.get("/api/roles/:roleId", authorized(permissions.BUILDER), controller.find)
.delete(
"/api/roles/:roleId/:rev",
authorized(permissions.BUILDER),
controller.destroy
)
export = router

View File

@ -1,14 +0,0 @@
const Router = require("@koa/router")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const controller = require("../controllers/routing")
const router = new Router()
router
// gets correct structure for user role
.get("/api/routing/client", controller.clientFetch)
// gets the full structure, not just the correct screen ID for user role
.get("/api/routing", authorized(BUILDER), controller.fetch)
module.exports = router

View File

@ -0,0 +1,14 @@
import Router from "@koa/router"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import * as controller from "../controllers/routing"
const router: Router = new Router()
router
// gets correct structure for user role
.get("/api/routing/client", controller.clientFetch)
// gets the full structure, not just the correct screen ID for user role
.get("/api/routing", authorized(permissions.BUILDER), controller.fetch)
export = router

View File

@ -2,11 +2,9 @@ import Router from "@koa/router"
import * as rowController from "../controllers/row"
import authorized from "../../middleware/authorized"
import { paramResource, paramSubResource } from "../../middleware/resourceId"
const {
PermissionLevel,
PermissionType,
} = require("@budibase/backend-core/permissions")
import { permissions } from "@budibase/backend-core"
const { internalSearchValidator } = require("./utils/validators")
const { PermissionType, PermissionLevel } = permissions
const router: Router = new Router()

View File

@ -1,18 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/screen")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const { screenValidator } = require("./utils/validators")
const router = new Router()
router
.get("/api/screens", authorized(BUILDER), controller.fetch)
.post("/api/screens", authorized(BUILDER), screenValidator(), controller.save)
.delete(
"/api/screens/:screenId/:screenRev",
authorized(BUILDER),
controller.destroy
)
module.exports = router

View File

@ -0,0 +1,23 @@
import Router from "@koa/router"
import * as controller from "../controllers/screen"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
import { screenValidator } from "./utils/validators"
const router: Router = new Router()
router
.get("/api/screens", authorized(permissions.BUILDER), controller.fetch)
.post(
"/api/screens",
authorized(permissions.BUILDER),
screenValidator(),
controller.save
)
.delete(
"/api/screens/:screenId/:screenRev",
authorized(permissions.BUILDER),
controller.destroy
)
export = router

View File

@ -1,10 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/script")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router.post("/api/script", authorized(BUILDER), controller.save)
module.exports = router

View File

@ -0,0 +1,10 @@
import Router from "@koa/router"
import * as controller from "../controllers/script"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core/permissions"
const router: Router = new Router()
router.post("/api/script", authorized(permissions.BUILDER), controller.save)
export = router

View File

@ -2,13 +2,10 @@ import Router from "@koa/router"
import * as controller from "../controllers/static"
import { budibaseTempDir } from "../../utilities/budibaseDir"
import authorized from "../../middleware/authorized"
import {
BUILDER,
PermissionType,
PermissionLevel,
} from "@budibase/backend-core/permissions"
import { permissions } from "@budibase/backend-core"
import * as env from "../../environment"
import { paramResource } from "../../middleware/resourceId"
const { BUILDER, PermissionType, PermissionLevel } = permissions
const router: Router = new Router()
@ -65,4 +62,4 @@ router
controller.getSignedUploadURL
)
export default router
export = router

View File

@ -1,15 +1,12 @@
const Router = require("@koa/router")
const tableController = require("../controllers/table")
const authorized = require("../../middleware/authorized")
const { paramResource, bodyResource } = require("../../middleware/resourceId")
const {
BUILDER,
PermissionLevel,
PermissionType,
} = require("@budibase/backend-core/permissions")
const { tableValidator } = require("./utils/validators")
import Router from "@koa/router"
import * as tableController from "../controllers/table"
import authorized from "../../middleware/authorized"
import { paramResource, bodyResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core"
import { tableValidator } from "./utils/validators"
const { BUILDER, PermissionLevel, PermissionType } = permissions
const router = new Router()
const router: Router = new Router()
router
/**
@ -193,4 +190,4 @@ router
tableController.bulkImport
)
module.exports = router
export = router

View File

@ -1,16 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/templates")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("@budibase/backend-core/permissions")
const router = new Router()
router
.get("/api/templates", authorized(BUILDER), controller.fetch)
.get(
"/api/templates/:type/:name",
authorized(BUILDER),
controller.downloadTemplate
)
module.exports = router

View File

@ -0,0 +1,16 @@
import Router from "@koa/router"
import * as controller from "../controllers/templates"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.get("/api/templates", authorized(permissions.BUILDER), controller.fetch)
.get(
"/api/templates/:type/:name",
authorized(permissions.BUILDER),
controller.downloadTemplate
)
export = router

View File

@ -1,12 +1,10 @@
const Router = require("@koa/router")
const controller = require("../controllers/user")
const authorized = require("../../middleware/authorized")
const {
PermissionLevel,
PermissionType,
} = require("@budibase/backend-core/permissions")
import Router from "@koa/router"
import controller from "../controllers/user"
import authorized from "../../middleware/authorized"
import { permissions } from "@budibase/backend-core"
const { PermissionType, PermissionLevel } = permissions
const router = new Router()
const router: Router = new Router()
router
.get(
@ -50,4 +48,4 @@ router
controller.getFlags
)
module.exports = router
export = router

View File

@ -1,31 +0,0 @@
const Router = require("@koa/router")
const viewController = require("../controllers/view")
const rowController = require("../controllers/row")
const authorized = require("../../middleware/authorized")
const { paramResource } = require("../../middleware/resourceId")
const {
BUILDER,
PermissionType,
PermissionLevel,
} = require("@budibase/backend-core/permissions")
const router = new Router()
router
.get("/api/views/export", authorized(BUILDER), viewController.exportView)
.get(
"/api/views/:viewName",
paramResource("viewName"),
authorized(PermissionType.VIEW, PermissionLevel.READ),
rowController.fetchView
)
.get("/api/views", authorized(BUILDER), viewController.fetch)
.delete(
"/api/views/:viewName",
paramResource("viewName"),
authorized(BUILDER),
viewController.destroy
)
.post("/api/views", authorized(BUILDER), viewController.save)
module.exports = router

View File

@ -0,0 +1,34 @@
import Router from "@koa/router"
import * as viewController from "../controllers/view"
import * as rowController from "../controllers/row"
import authorized from "../../middleware/authorized"
import { paramResource } from "../../middleware/resourceId"
import { permissions } from "@budibase/backend-core"
const router: Router = new Router()
router
.get(
"/api/views/export",
authorized(permissions.BUILDER),
viewController.exportView
)
.get(
"/api/views/:viewName",
paramResource("viewName"),
authorized(
permissions.PermissionType.VIEW,
permissions.PermissionLevel.READ
),
rowController.fetchView
)
.get("/api/views", authorized(permissions.BUILDER), viewController.fetch)
.delete(
"/api/views/:viewName",
paramResource("viewName"),
authorized(permissions.BUILDER),
viewController.destroy
)
.post("/api/views", authorized(permissions.BUILDER), viewController.save)
export = router

View File

@ -24,4 +24,4 @@ router
// this shouldn't have authorisation, right now its always public
.post("/api/webhooks/trigger/:instance/:id", controller.trigger)
export default router
export = router

View File

@ -49,7 +49,7 @@ function getRemovedAttachmentKeys(
* for automatic ID purposes.
*/
export function processAutoColumn(
user: User,
user: User | null,
table: Table,
row: Row,
opts: AutoColumnProcessingOpts
@ -71,7 +71,7 @@ export function processAutoColumn(
}
switch (schema.subtype) {
case AutoFieldSubTypes.CREATED_BY:
if (creating && shouldUpdateUserFields) {
if (creating && shouldUpdateUserFields && user) {
row[key] = [user.userId]
}
break
@ -81,7 +81,7 @@ export function processAutoColumn(
}
break
case AutoFieldSubTypes.UPDATED_BY:
if (shouldUpdateUserFields) {
if (shouldUpdateUserFields && user) {
row[key] = [user.userId]
}
break

View File

@ -35,13 +35,16 @@ export function fixAutoColumnSubType(column: FieldSchema) {
*/
export function processFormulas(
table: Table,
rows: Row[],
rows: Row[] | Row,
{ dynamic, contextRows }: any = { dynamic: true }
) {
const single = !Array.isArray(rows)
let rowArray: Row[]
if (single) {
rows = [rows]
rowArray = [rows]
contextRows = contextRows ? [contextRows] : contextRows
} else {
rowArray = rows
}
for (let [column, schema] of Object.entries(table.schema)) {
const isStatic = schema.formulaType === FormulaTypes.STATIC
@ -53,18 +56,18 @@ export function processFormulas(
continue
}
// iterate through rows and process formula
for (let i = 0; i < rows.length; i++) {
for (let i = 0; i < rowArray.length; i++) {
if (schema.formula) {
let row = rows[i]
let row = rowArray[i]
let context = contextRows ? contextRows[i] : row
rows[i] = {
rowArray[i] = {
...row,
[column]: processStringSync(schema.formula, context),
}
}
}
}
return single ? rows[0] : rows
return single ? rowArray[0] : rowArray
}
/**