From ed6a6ab8ce8643a05c3233300351f263262cb4a1 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 22 Nov 2022 18:49:19 +0000 Subject: [PATCH] Updating all of the route files to typescript, as well as some controllers. --- .../server/src/api/controllers/component.ts | 3 +- .../deploy/{Deployment.js => Deployment.ts} | 23 +-- .../src/api/controllers/plugin/index.ts | 9 +- .../api/controllers/row/ExternalRequest.ts | 12 +- .../{internalSearch.js => internalSearch.ts} | 160 ++++++++++-------- .../{staticFormula.js => staticFormula.ts} | 51 +++--- .../controllers/row/{utils.js => utils.ts} | 43 +++-- packages/server/src/api/controllers/screen.ts | 16 +- packages/server/src/api/controllers/script.js | 11 -- packages/server/src/api/controllers/script.ts | 12 ++ .../{templates.js => templates.ts} | 17 +- packages/server/src/api/routes/analytics.js | 9 - packages/server/src/api/routes/analytics.ts | 9 + packages/server/src/api/routes/apikeys.js | 12 -- packages/server/src/api/routes/apikeys.ts | 12 ++ packages/server/src/api/routes/application.ts | 24 ++- packages/server/src/api/routes/auth.ts | 2 +- packages/server/src/api/routes/automation.js | 85 ---------- packages/server/src/api/routes/automation.ts | 87 ++++++++++ packages/server/src/api/routes/backup.ts | 10 +- packages/server/src/api/routes/cloud.js | 14 -- packages/server/src/api/routes/cloud.ts | 18 ++ packages/server/src/api/routes/component.js | 14 -- packages/server/src/api/routes/component.ts | 14 ++ packages/server/src/api/routes/datasource.js | 51 ------ packages/server/src/api/routes/datasource.ts | 60 +++++++ packages/server/src/api/routes/deploy.ts | 2 +- packages/server/src/api/routes/dev.js | 26 --- packages/server/src/api/routes/dev.ts | 38 +++++ packages/server/src/api/routes/integration.js | 12 -- packages/server/src/api/routes/integration.ts | 16 ++ packages/server/src/api/routes/layout.js | 16 -- packages/server/src/api/routes/layout.ts | 16 ++ .../api/routes/{metadata.js => metadata.ts} | 26 +-- packages/server/src/api/routes/migrations.js | 14 -- packages/server/src/api/routes/migrations.ts | 14 ++ packages/server/src/api/routes/permission.js | 33 ---- packages/server/src/api/routes/permission.ts | 41 +++++ packages/server/src/api/routes/plugin.ts | 18 +- .../src/api/routes/{query.js => query.ts} | 37 ++-- packages/server/src/api/routes/role.js | 15 -- packages/server/src/api/routes/role.ts | 24 +++ packages/server/src/api/routes/routing.js | 14 -- packages/server/src/api/routes/routing.ts | 14 ++ packages/server/src/api/routes/row.ts | 6 +- packages/server/src/api/routes/screen.js | 18 -- packages/server/src/api/routes/screen.ts | 23 +++ packages/server/src/api/routes/script.js | 10 -- packages/server/src/api/routes/script.ts | 10 ++ packages/server/src/api/routes/static.ts | 9 +- .../src/api/routes/{table.js => table.ts} | 21 +-- packages/server/src/api/routes/templates.js | 16 -- packages/server/src/api/routes/templates.ts | 16 ++ .../src/api/routes/{user.js => user.ts} | 16 +- packages/server/src/api/routes/view.js | 31 ---- packages/server/src/api/routes/view.ts | 34 ++++ packages/server/src/api/routes/webhook.ts | 2 +- .../src/utilities/rowProcessor/index.ts | 6 +- .../src/utilities/rowProcessor/utils.ts | 15 +- 59 files changed, 748 insertions(+), 639 deletions(-) rename packages/server/src/api/controllers/deploy/{Deployment.js => Deployment.ts} (70%) rename packages/server/src/api/controllers/row/{internalSearch.js => internalSearch.ts} (76%) rename packages/server/src/api/controllers/row/{staticFormula.js => staticFormula.ts} (80%) rename packages/server/src/api/controllers/row/{utils.js => utils.ts} (75%) delete mode 100644 packages/server/src/api/controllers/script.js create mode 100644 packages/server/src/api/controllers/script.ts rename packages/server/src/api/controllers/{templates.js => templates.ts} (64%) delete mode 100644 packages/server/src/api/routes/analytics.js create mode 100644 packages/server/src/api/routes/analytics.ts delete mode 100644 packages/server/src/api/routes/apikeys.js create mode 100644 packages/server/src/api/routes/apikeys.ts delete mode 100644 packages/server/src/api/routes/automation.js create mode 100644 packages/server/src/api/routes/automation.ts delete mode 100644 packages/server/src/api/routes/cloud.js create mode 100644 packages/server/src/api/routes/cloud.ts delete mode 100644 packages/server/src/api/routes/component.js create mode 100644 packages/server/src/api/routes/component.ts delete mode 100644 packages/server/src/api/routes/datasource.js create mode 100644 packages/server/src/api/routes/datasource.ts delete mode 100644 packages/server/src/api/routes/dev.js create mode 100644 packages/server/src/api/routes/dev.ts delete mode 100644 packages/server/src/api/routes/integration.js create mode 100644 packages/server/src/api/routes/integration.ts delete mode 100644 packages/server/src/api/routes/layout.js create mode 100644 packages/server/src/api/routes/layout.ts rename packages/server/src/api/routes/{metadata.js => metadata.ts} (52%) delete mode 100644 packages/server/src/api/routes/migrations.js create mode 100644 packages/server/src/api/routes/migrations.ts delete mode 100644 packages/server/src/api/routes/permission.js create mode 100644 packages/server/src/api/routes/permission.ts rename packages/server/src/api/routes/{query.js => query.ts} (73%) delete mode 100644 packages/server/src/api/routes/role.js create mode 100644 packages/server/src/api/routes/role.ts delete mode 100644 packages/server/src/api/routes/routing.js create mode 100644 packages/server/src/api/routes/routing.ts delete mode 100644 packages/server/src/api/routes/screen.js create mode 100644 packages/server/src/api/routes/screen.ts delete mode 100644 packages/server/src/api/routes/script.js create mode 100644 packages/server/src/api/routes/script.ts rename packages/server/src/api/routes/{table.js => table.ts} (95%) delete mode 100644 packages/server/src/api/routes/templates.js create mode 100644 packages/server/src/api/routes/templates.ts rename packages/server/src/api/routes/{user.js => user.ts} (78%) delete mode 100644 packages/server/src/api/routes/view.js create mode 100644 packages/server/src/api/routes/view.ts diff --git a/packages/server/src/api/controllers/component.ts b/packages/server/src/api/controllers/component.ts index 6d65d43db6..9b196ca289 100644 --- a/packages/server/src/api/controllers/component.ts +++ b/packages/server/src/api/controllers/component.ts @@ -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) diff --git a/packages/server/src/api/controllers/deploy/Deployment.js b/packages/server/src/api/controllers/deploy/Deployment.ts similarity index 70% rename from packages/server/src/api/controllers/deploy/Deployment.js rename to packages/server/src/api/controllers/deploy/Deployment.ts index 65cca97d07..765f22acc1 100644 --- a/packages/server/src/api/controllers/deploy/Deployment.js +++ b/packages/server/src/api/controllers/deploy/Deployment.ts @@ -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 diff --git a/packages/server/src/api/controllers/plugin/index.ts b/packages/server/src/api/controllers/plugin/index.ts index 7d1b1291ab..907fcf65a6 100644 --- a/packages/server/src/api/controllers/plugin/index.ts +++ b/packages/server/src/api/controllers/plugin/index.ts @@ -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) { diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index 852f124ee5..2ef7953437 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -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." diff --git a/packages/server/src/api/controllers/row/internalSearch.js b/packages/server/src/api/controllers/row/internalSearch.ts similarity index 76% rename from packages/server/src/api/controllers/row/internalSearch.js rename to packages/server/src/api/controllers/row/internalSearch.ts index 051a55aa9f..7068aabc5a 100644 --- a/packages/server/src/api/controllers/row/internalSearch.js +++ b/packages/server/src/api/controllers/row/internalSearch.ts @@ -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) { - this.version = 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) { - this.bookmark = 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 { 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 diff --git a/packages/server/src/api/controllers/row/staticFormula.js b/packages/server/src/api/controllers/row/staticFormula.ts similarity index 80% rename from packages/server/src/api/controllers/row/staticFormula.js rename to packages/server/src/api/controllers/row/staticFormula.ts index 2de3ae0e4f..ee09264af1 100644 --- a/packages/server/src/api/controllers/row/staticFormula.js +++ b/packages/server/src/api/controllers/row/staticFormula.ts @@ -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[] = [] for (let enrichedRow of Array.isArray(enrichedRows) ? enrichedRows : [enrichedRows]) { // the related rows by tableId - let relatedRows = {} + let relatedRows: Record = {} 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, { diff --git a/packages/server/src/api/controllers/row/utils.js b/packages/server/src/api/controllers/row/utils.ts similarity index 75% rename from packages/server/src/api/controllers/row/utils.js rename to packages/server/src/api/controllers/row/utils.ts index ca2ad02a30..eea80a3cd8 100644 --- a/packages/server/src/api/controllers/row/utils.js +++ b/packages/server/src/api/controllers/row/utils.ts @@ -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 diff --git a/packages/server/src/api/controllers/screen.ts b/packages/server/src/api/controllers/screen.ts index 08040351dd..9cbd019d6e 100644 --- a/packages/server/src/api/controllers/screen.ts +++ b/packages/server/src/api/controllers/screen.ts @@ -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 } diff --git a/packages/server/src/api/controllers/script.js b/packages/server/src/api/controllers/script.js deleted file mode 100644 index 38211ebee7..0000000000 --- a/packages/server/src/api/controllers/script.js +++ /dev/null @@ -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") -} diff --git a/packages/server/src/api/controllers/script.ts b/packages/server/src/api/controllers/script.ts new file mode 100644 index 0000000000..8af339b0b5 --- /dev/null +++ b/packages/server/src/api/controllers/script.ts @@ -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") +} diff --git a/packages/server/src/api/controllers/templates.js b/packages/server/src/api/controllers/templates.ts similarity index 64% rename from packages/server/src/api/controllers/templates.js rename to packages/server/src/api/controllers/templates.ts index 3a1cafdf5a..6774fa2766 100644 --- a/packages/server/src/api/controllers/templates.js +++ b/packages/server/src/api/controllers/templates.ts @@ -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.`, diff --git a/packages/server/src/api/routes/analytics.js b/packages/server/src/api/routes/analytics.js deleted file mode 100644 index 610d6d0c7f..0000000000 --- a/packages/server/src/api/routes/analytics.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/analytics.ts b/packages/server/src/api/routes/analytics.ts new file mode 100644 index 0000000000..87dcb73eab --- /dev/null +++ b/packages/server/src/api/routes/analytics.ts @@ -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 diff --git a/packages/server/src/api/routes/apikeys.js b/packages/server/src/api/routes/apikeys.js deleted file mode 100644 index ddbd35c23c..0000000000 --- a/packages/server/src/api/routes/apikeys.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/apikeys.ts b/packages/server/src/api/routes/apikeys.ts new file mode 100644 index 0000000000..3afa1a0950 --- /dev/null +++ b/packages/server/src/api/routes/apikeys.ts @@ -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 diff --git a/packages/server/src/api/routes/application.ts b/packages/server/src/api/routes/application.ts index 38d857ac8d..486953ea8b 100644 --- a/packages/server/src/api/routes/application.ts +++ b/packages/server/src/api/routes/application.ts @@ -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 diff --git a/packages/server/src/api/routes/auth.ts b/packages/server/src/api/routes/auth.ts index c4e65a4c25..3b2aaca79e 100644 --- a/packages/server/src/api/routes/auth.ts +++ b/packages/server/src/api/routes/auth.ts @@ -5,4 +5,4 @@ const router: Router = new Router() router.get("/api/self", controller.fetchSelf) -export default router +export = router diff --git a/packages/server/src/api/routes/automation.js b/packages/server/src/api/routes/automation.js deleted file mode 100644 index 544d94abb2..0000000000 --- a/packages/server/src/api/routes/automation.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/automation.ts b/packages/server/src/api/routes/automation.ts new file mode 100644 index 0000000000..1beb8e7d76 --- /dev/null +++ b/packages/server/src/api/routes/automation.ts @@ -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 diff --git a/packages/server/src/api/routes/backup.ts b/packages/server/src/api/routes/backup.ts index cc7eb25c8b..63e9fee5b6 100644 --- a/packages/server/src/api/routes/backup.ts +++ b/packages/server/src/api/routes/backup.ts @@ -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 diff --git a/packages/server/src/api/routes/cloud.js b/packages/server/src/api/routes/cloud.js deleted file mode 100644 index c183ffb5ba..0000000000 --- a/packages/server/src/api/routes/cloud.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/cloud.ts b/packages/server/src/api/routes/cloud.ts new file mode 100644 index 0000000000..acc6b9e9c9 --- /dev/null +++ b/packages/server/src/api/routes/cloud.ts @@ -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 diff --git a/packages/server/src/api/routes/component.js b/packages/server/src/api/routes/component.js deleted file mode 100644 index 275f58bd6c..0000000000 --- a/packages/server/src/api/routes/component.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/component.ts b/packages/server/src/api/routes/component.ts new file mode 100644 index 0000000000..20b3e54c81 --- /dev/null +++ b/packages/server/src/api/routes/component.ts @@ -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 diff --git a/packages/server/src/api/routes/datasource.js b/packages/server/src/api/routes/datasource.js deleted file mode 100644 index 402e464e16..0000000000 --- a/packages/server/src/api/routes/datasource.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/datasource.ts b/packages/server/src/api/routes/datasource.ts new file mode 100644 index 0000000000..c9de7f51fb --- /dev/null +++ b/packages/server/src/api/routes/datasource.ts @@ -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 diff --git a/packages/server/src/api/routes/deploy.ts b/packages/server/src/api/routes/deploy.ts index d091581ec1..b0e4960acb 100644 --- a/packages/server/src/api/routes/deploy.ts +++ b/packages/server/src/api/routes/deploy.ts @@ -18,4 +18,4 @@ router ) .post("/api/deploy", authorized(permissions.BUILDER), controller.deployApp) -export default router +export = router diff --git a/packages/server/src/api/routes/dev.js b/packages/server/src/api/routes/dev.js deleted file mode 100644 index 0103219246..0000000000 --- a/packages/server/src/api/routes/dev.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/dev.ts b/packages/server/src/api/routes/dev.ts new file mode 100644 index 0000000000..d61c56d5ee --- /dev/null +++ b/packages/server/src/api/routes/dev.ts @@ -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 diff --git a/packages/server/src/api/routes/integration.js b/packages/server/src/api/routes/integration.js deleted file mode 100644 index 5469aaa27d..0000000000 --- a/packages/server/src/api/routes/integration.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/integration.ts b/packages/server/src/api/routes/integration.ts new file mode 100644 index 0000000000..835cc5a896 --- /dev/null +++ b/packages/server/src/api/routes/integration.ts @@ -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 diff --git a/packages/server/src/api/routes/layout.js b/packages/server/src/api/routes/layout.js deleted file mode 100644 index 76103f9cfc..0000000000 --- a/packages/server/src/api/routes/layout.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/layout.ts b/packages/server/src/api/routes/layout.ts new file mode 100644 index 0000000000..6ec48b9263 --- /dev/null +++ b/packages/server/src/api/routes/layout.ts @@ -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 diff --git a/packages/server/src/api/routes/metadata.js b/packages/server/src/api/routes/metadata.ts similarity index 52% rename from packages/server/src/api/routes/metadata.js rename to packages/server/src/api/routes/metadata.ts index 0c2867c45a..e46e0eeb01 100644 --- a/packages/server/src/api/routes/metadata.js +++ b/packages/server/src/api/routes/metadata.ts @@ -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 diff --git a/packages/server/src/api/routes/migrations.js b/packages/server/src/api/routes/migrations.js deleted file mode 100644 index a40111cf25..0000000000 --- a/packages/server/src/api/routes/migrations.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/migrations.ts b/packages/server/src/api/routes/migrations.ts new file mode 100644 index 0000000000..689373cf75 --- /dev/null +++ b/packages/server/src/api/routes/migrations.ts @@ -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 diff --git a/packages/server/src/api/routes/permission.js b/packages/server/src/api/routes/permission.js deleted file mode 100644 index 4736769f61..0000000000 --- a/packages/server/src/api/routes/permission.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/permission.ts b/packages/server/src/api/routes/permission.ts new file mode 100644 index 0000000000..8a26deb9ba --- /dev/null +++ b/packages/server/src/api/routes/permission.ts @@ -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 diff --git a/packages/server/src/api/routes/plugin.ts b/packages/server/src/api/routes/plugin.ts index d5bd7607f7..70838c499e 100644 --- a/packages/server/src/api/routes/plugin.ts +++ b/packages/server/src/api/routes/plugin.ts @@ -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 diff --git a/packages/server/src/api/routes/query.js b/packages/server/src/api/routes/query.ts similarity index 73% rename from packages/server/src/api/routes/query.js rename to packages/server/src/api/routes/query.ts index 1195e55113..439e3a6ec8 100644 --- a/packages/server/src/api/routes/query.js +++ b/packages/server/src/api/routes/query.ts @@ -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 diff --git a/packages/server/src/api/routes/role.js b/packages/server/src/api/routes/role.js deleted file mode 100644 index a6e04e81fa..0000000000 --- a/packages/server/src/api/routes/role.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/role.ts b/packages/server/src/api/routes/role.ts new file mode 100644 index 0000000000..48933a505b --- /dev/null +++ b/packages/server/src/api/routes/role.ts @@ -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 diff --git a/packages/server/src/api/routes/routing.js b/packages/server/src/api/routes/routing.js deleted file mode 100644 index d7e971d507..0000000000 --- a/packages/server/src/api/routes/routing.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/routing.ts b/packages/server/src/api/routes/routing.ts new file mode 100644 index 0000000000..56f2b40192 --- /dev/null +++ b/packages/server/src/api/routes/routing.ts @@ -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 diff --git a/packages/server/src/api/routes/row.ts b/packages/server/src/api/routes/row.ts index e75b7d6e20..f4462b3595 100644 --- a/packages/server/src/api/routes/row.ts +++ b/packages/server/src/api/routes/row.ts @@ -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() diff --git a/packages/server/src/api/routes/screen.js b/packages/server/src/api/routes/screen.js deleted file mode 100644 index 426b89fd0f..0000000000 --- a/packages/server/src/api/routes/screen.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/screen.ts b/packages/server/src/api/routes/screen.ts new file mode 100644 index 0000000000..c8bab17b31 --- /dev/null +++ b/packages/server/src/api/routes/screen.ts @@ -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 diff --git a/packages/server/src/api/routes/script.js b/packages/server/src/api/routes/script.js deleted file mode 100644 index a4b4e4a7f5..0000000000 --- a/packages/server/src/api/routes/script.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/script.ts b/packages/server/src/api/routes/script.ts new file mode 100644 index 0000000000..44f1069c9e --- /dev/null +++ b/packages/server/src/api/routes/script.ts @@ -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 diff --git a/packages/server/src/api/routes/static.ts b/packages/server/src/api/routes/static.ts index 7dbd998583..992575fb74 100644 --- a/packages/server/src/api/routes/static.ts +++ b/packages/server/src/api/routes/static.ts @@ -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 diff --git a/packages/server/src/api/routes/table.js b/packages/server/src/api/routes/table.ts similarity index 95% rename from packages/server/src/api/routes/table.js rename to packages/server/src/api/routes/table.ts index 8d280e06d3..0b6d80cdaf 100644 --- a/packages/server/src/api/routes/table.js +++ b/packages/server/src/api/routes/table.ts @@ -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 diff --git a/packages/server/src/api/routes/templates.js b/packages/server/src/api/routes/templates.js deleted file mode 100644 index 61a185b5c8..0000000000 --- a/packages/server/src/api/routes/templates.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/templates.ts b/packages/server/src/api/routes/templates.ts new file mode 100644 index 0000000000..a05e1f12ac --- /dev/null +++ b/packages/server/src/api/routes/templates.ts @@ -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 diff --git a/packages/server/src/api/routes/user.js b/packages/server/src/api/routes/user.ts similarity index 78% rename from packages/server/src/api/routes/user.js rename to packages/server/src/api/routes/user.ts index a290ced829..95010bf8f2 100644 --- a/packages/server/src/api/routes/user.js +++ b/packages/server/src/api/routes/user.ts @@ -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 diff --git a/packages/server/src/api/routes/view.js b/packages/server/src/api/routes/view.js deleted file mode 100644 index 0a8bd7d4ce..0000000000 --- a/packages/server/src/api/routes/view.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/view.ts b/packages/server/src/api/routes/view.ts new file mode 100644 index 0000000000..3705e74984 --- /dev/null +++ b/packages/server/src/api/routes/view.ts @@ -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 diff --git a/packages/server/src/api/routes/webhook.ts b/packages/server/src/api/routes/webhook.ts index 3aa9525ed5..8067ecac7c 100644 --- a/packages/server/src/api/routes/webhook.ts +++ b/packages/server/src/api/routes/webhook.ts @@ -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 diff --git a/packages/server/src/utilities/rowProcessor/index.ts b/packages/server/src/utilities/rowProcessor/index.ts index 9c91ea070b..2b4f4e05a6 100644 --- a/packages/server/src/utilities/rowProcessor/index.ts +++ b/packages/server/src/utilities/rowProcessor/index.ts @@ -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 diff --git a/packages/server/src/utilities/rowProcessor/utils.ts b/packages/server/src/utilities/rowProcessor/utils.ts index 7853775f19..c22bc59419 100644 --- a/packages/server/src/utilities/rowProcessor/utils.ts +++ b/packages/server/src/utilities/rowProcessor/utils.ts @@ -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 } /**