From 4328ed1b67c7b17d12223479aba6ee3c450d33c5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 16 Nov 2022 17:24:13 +0000 Subject: [PATCH] Updating middlewares to Typescript, as well as some fixes based on running tests. --- packages/backend-core/src/context/index.ts | 5 +-- packages/backend-core/src/events/analytics.ts | 4 +-- .../api/routes/public/tests/compare.spec.js | 2 +- packages/server/src/middleware/appInfo.ts | 19 ++++++----- packages/server/src/middleware/builder.ts | 33 ++++++++++--------- .../server/src/middleware/joi-validator.ts | 17 +++++----- packages/server/src/middleware/publicApi.ts | 16 ++++----- packages/server/src/middleware/resourceId.ts | 27 ++++++++------- packages/server/src/middleware/selfhost.ts | 6 ++-- packages/server/src/middleware/utils.ts | 4 ++- .../src/tests/utilities/TestConfiguration.js | 2 +- .../server/src/utilities/workerRequests.js | 2 +- .../worker/src/api/controllers/global/auth.ts | 2 +- .../worker/src/middleware/cloudRestricted.js | 2 +- packages/worker/src/utilities/appService.js | 2 +- 15 files changed, 76 insertions(+), 67 deletions(-) diff --git a/packages/backend-core/src/context/index.ts b/packages/backend-core/src/context/index.ts index aabc5059d1..076d9af65a 100644 --- a/packages/backend-core/src/context/index.ts +++ b/packages/backend-core/src/context/index.ts @@ -109,10 +109,7 @@ export async function doInAppContext(appId: string, task: any): Promise { if (tenantId) { updates.tenantId = tenantId } - return newContext( - updates, - task - ) + return newContext(updates, task) } export async function doInIdentityContext( diff --git a/packages/backend-core/src/events/analytics.ts b/packages/backend-core/src/events/analytics.ts index eb121bdeec..228805ef82 100644 --- a/packages/backend-core/src/events/analytics.ts +++ b/packages/backend-core/src/events/analytics.ts @@ -45,9 +45,7 @@ const getSettingsDoc = async () => { const db = tenancy.getGlobalDB() let settings try { - settings = await db.get( - dbUtils.generateConfigID({ type: Config.SETTINGS }) - ) + settings = await db.get(dbUtils.generateConfigID({ type: Config.SETTINGS })) } catch (e: any) { if (e.status !== 404) { throw e diff --git a/packages/server/src/api/routes/public/tests/compare.spec.js b/packages/server/src/api/routes/public/tests/compare.spec.js index c42898fe40..860907b69d 100644 --- a/packages/server/src/api/routes/public/tests/compare.spec.js +++ b/packages/server/src/api/routes/public/tests/compare.spec.js @@ -31,7 +31,7 @@ async function makeRequest(method, endpoint, body, appId = config.getAppId()) { if (body) { req.send(body) } - const res = await req.expect("Content-Type", /json/).expect(200) + const res = await req expect(res.body).toBeDefined() return res } diff --git a/packages/server/src/middleware/appInfo.ts b/packages/server/src/middleware/appInfo.ts index ee3655c6cc..75e40ed473 100644 --- a/packages/server/src/middleware/appInfo.ts +++ b/packages/server/src/middleware/appInfo.ts @@ -1,19 +1,20 @@ -const { isDevAppID, isProdAppID } = require("../db/utils") +import { isDevAppID, isProdAppID } from "../db/utils" +import { BBContext } from "@budibase/types" -exports.AppType = { - DEV: "dev", - PROD: "prod", +export enum AppType { + DEV = "dev", + PROD = "prod", } -exports.middleware = - ({ appType } = {}) => - (ctx, next) => { +export function middleware({ appType }: { appType?: AppType } = {}) { + return (ctx: BBContext, next: any) => { const appId = ctx.appId - if (appType === exports.AppType.DEV && appId && !isDevAppID(appId)) { + if (appType === AppType.DEV && appId && !isDevAppID(appId)) { ctx.throw(400, "Only apps in development support this endpoint") } - if (appType === exports.AppType.PROD && appId && !isProdAppID(appId)) { + if (appType === AppType.PROD && appId && !isProdAppID(appId)) { ctx.throw(400, "Only apps in production support this endpoint") } return next() } +} diff --git a/packages/server/src/middleware/builder.ts b/packages/server/src/middleware/builder.ts index 12c4b93c75..403597ff1a 100644 --- a/packages/server/src/middleware/builder.ts +++ b/packages/server/src/middleware/builder.ts @@ -1,14 +1,17 @@ -const { APP_DEV_PREFIX } = require("../db/utils") -const { +import { + APP_DEV_PREFIX, + DocumentType, + getGlobalIDFromUserMetadataID, +} from "../db/utils" +import { doesUserHaveLock, updateLock, checkDebounce, setDebounce, -} = require("../utilities/redis") -const { doWithDB } = require("@budibase/backend-core/db") -const { DocumentType, getGlobalIDFromUserMetadataID } = require("../db/utils") -const { PermissionTypes } = require("@budibase/backend-core/permissions") -const { app: appCache } = require("@budibase/backend-core/cache") +} from "../utilities/redis" +import { db as dbCore, cache, permissions } from "@budibase/backend-core" +import { BBContext } from "@budibase/types" +import { PouchLike } from "@budibase/backend-core/src/db" const DEBOUNCE_TIME_SEC = 30 @@ -21,11 +24,11 @@ const DEBOUNCE_TIME_SEC = 30 * through the authorized middleware * ****************************************************/ -async function checkDevAppLocks(ctx) { +async function checkDevAppLocks(ctx: BBContext) { const appId = ctx.appId // if any public usage, don't proceed - if (!ctx.user._id && !ctx.user.userId) { + if (!ctx.user?._id && !ctx.user?.userId) { return } @@ -41,34 +44,34 @@ async function checkDevAppLocks(ctx) { await updateLock(appId, ctx.user) } -async function updateAppUpdatedAt(ctx) { +async function updateAppUpdatedAt(ctx: BBContext) { const appId = ctx.appId // if debouncing skip this update // get methods also aren't updating if (ctx.method === "GET" || (await checkDebounce(appId))) { return } - await doWithDB(appId, async db => { + await dbCore.doWithDB(appId, async (db: PouchLike) => { const metadata = await db.get(DocumentType.APP_METADATA) metadata.updatedAt = new Date().toISOString() - metadata.updatedBy = getGlobalIDFromUserMetadataID(ctx.user.userId) + metadata.updatedBy = getGlobalIDFromUserMetadataID(ctx.user?.userId!) const response = await db.put(metadata) metadata._rev = response.rev - await appCache.invalidateAppMetadata(appId, metadata) + await cache.app.invalidateAppMetadata(appId, metadata) // set a new debounce record with a short TTL await setDebounce(appId, DEBOUNCE_TIME_SEC) }) } -module.exports = async (ctx, permType) => { +export = async function builder(ctx: BBContext, permType: string) { const appId = ctx.appId // this only functions within an app context if (!appId) { return } - const isBuilderApi = permType === PermissionTypes.BUILDER + const isBuilderApi = permType === permissions.PermissionTypes.BUILDER const referer = ctx.headers["referer"] const overviewPath = "/builder/portal/overview/" diff --git a/packages/server/src/middleware/joi-validator.ts b/packages/server/src/middleware/joi-validator.ts index 6812dbdd54..5d783acc80 100644 --- a/packages/server/src/middleware/joi-validator.ts +++ b/packages/server/src/middleware/joi-validator.ts @@ -1,20 +1,21 @@ -const Joi = require("joi") +import Joi from "joi" +import { BBContext } from "@budibase/types" -function validate(schema, property) { +function validate(schema: Joi.Schema, property: string) { // Return a Koa middleware function - return (ctx, next) => { + return (ctx: BBContext, next: any) => { if (!schema) { return next() } let params = null if (ctx[property] != null) { params = ctx[property] - } else if (ctx.request[property] != null) { - params = ctx.request[property] + } else if (ctx.request.get(property) != null) { + params = ctx.request.get(property) } // not all schemas have the append property e.g. array schemas - if (schema.append) { + if ("append" in schema && schema.append) { schema = schema.append({ createdAt: Joi.any().optional(), updatedAt: Joi.any().optional(), @@ -30,10 +31,10 @@ function validate(schema, property) { } } -module.exports.body = schema => { +export function body(schema: Joi.Schema) { return validate(schema, "body") } -module.exports.params = schema => { +export function params(schema: Joi.Schema) { return validate(schema, "params") } diff --git a/packages/server/src/middleware/publicApi.ts b/packages/server/src/middleware/publicApi.ts index 76dbfe8214..9b9a34df46 100644 --- a/packages/server/src/middleware/publicApi.ts +++ b/packages/server/src/middleware/publicApi.ts @@ -1,19 +1,19 @@ -const { Header } = require("@budibase/backend-core/constants") -const { getAppIdFromCtx } = require("@budibase/backend-core/utils") +import { constants, utils } from "@budibase/backend-core" +import { BBContext } from "@budibase/types" -module.exports = function ({ requiresAppId } = {}) { - return async (ctx, next) => { - const appId = await getAppIdFromCtx(ctx) +export = function ({ requiresAppId }: { requiresAppId?: boolean } = {}) { + return async (ctx: BBContext, next: any) => { + const appId = await utils.getAppIdFromCtx(ctx) if (requiresAppId && !appId) { ctx.throw( 400, - `Invalid app ID provided, please check the ${Header.APP_ID} header.` + `Invalid app ID provided, please check the ${constants.Header.APP_ID} header.` ) } - if (!ctx.headers[Header.API_KEY]) { + if (!ctx.headers[constants.Header.API_KEY]) { ctx.throw( 400, - `Invalid API key provided, please check the ${Header.API_KEY} header.` + `Invalid API key provided, please check the ${constants.Header.API_KEY} header.` ) } return next() diff --git a/packages/server/src/middleware/resourceId.ts b/packages/server/src/middleware/resourceId.ts index 4216131119..0917941061 100644 --- a/packages/server/src/middleware/resourceId.ts +++ b/packages/server/src/middleware/resourceId.ts @@ -1,17 +1,23 @@ -class ResourceIdGetter { - constructor(ctxProperty) { +import { BBContext } from "@budibase/types" + +export class ResourceIdGetter { + parameter: string + main: string | null + sub: string | null + + constructor(ctxProperty: string) { this.parameter = ctxProperty this.main = null this.sub = null return this } - mainResource(field) { + mainResource(field: string) { this.main = field return this } - subResource(field) { + subResource(field: string) { this.sub = field return this } @@ -20,7 +26,8 @@ class ResourceIdGetter { const parameter = this.parameter, main = this.main, sub = this.sub - return (ctx, next) => { + return (ctx: BBContext, next: any) => { + // @ts-ignore const request = ctx.request[parameter] || ctx[parameter] if (request == null) { return next() @@ -36,24 +43,22 @@ class ResourceIdGetter { } } -module.exports.ResourceIdGetter = ResourceIdGetter - -module.exports.paramResource = main => { +export function paramResource(main: string) { return new ResourceIdGetter("params").mainResource(main).build() } -module.exports.paramSubResource = (main, sub) => { +export function paramSubResource(main: string, sub: string) { return new ResourceIdGetter("params") .mainResource(main) .subResource(sub) .build() } -module.exports.bodyResource = main => { +export function bodyResource(main: string) { return new ResourceIdGetter("body").mainResource(main).build() } -module.exports.bodySubResource = (main, sub) => { +export function bodySubResource(main: string, sub: string) { return new ResourceIdGetter("body") .mainResource(main) .subResource(sub) diff --git a/packages/server/src/middleware/selfhost.ts b/packages/server/src/middleware/selfhost.ts index 1e7117c83d..180d80c8ea 100644 --- a/packages/server/src/middleware/selfhost.ts +++ b/packages/server/src/middleware/selfhost.ts @@ -1,7 +1,9 @@ -const env = require("../environment") +import env from "../environment" +import { BBContext } from "@budibase/types" + // if added as a middleware will stop requests unless builder is in self host mode // or cloud is in self host -module.exports = async (ctx, next) => { +module.exports = async (ctx: BBContext, next: any) => { if (env.SELF_HOSTED) { await next() return diff --git a/packages/server/src/middleware/utils.ts b/packages/server/src/middleware/utils.ts index b1eea8cd66..714df12f38 100644 --- a/packages/server/src/middleware/utils.ts +++ b/packages/server/src/middleware/utils.ts @@ -1,7 +1,9 @@ +import { BBContext } from "@budibase/types" + const WEBHOOK_ENDPOINTS = new RegExp( ["webhooks/trigger", "webhooks/schema"].join("|") ) -exports.isWebhookEndpoint = ctx => { +export function isWebhookEndpoint(ctx: BBContext) { return WEBHOOK_ENDPOINTS.test(ctx.request.url) } diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index 40722513a0..044ad4bbf7 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -16,7 +16,7 @@ const { const controllers = require("./controllers") const supertest = require("supertest") const { cleanup } = require("../../utilities/fileSystem") -const { Cookie, Headers } = require("@budibase/backend-core/constants") +const { Cookie, Header } = require("@budibase/backend-core/constants") const { jwt } = require("@budibase/backend-core/auth") const { doInTenant, doWithGlobalDB } = require("@budibase/backend-core/tenancy") const { createASession } = require("@budibase/backend-core/sessions") diff --git a/packages/server/src/utilities/workerRequests.js b/packages/server/src/utilities/workerRequests.js index 89fb886033..e606ba9fa6 100644 --- a/packages/server/src/utilities/workerRequests.js +++ b/packages/server/src/utilities/workerRequests.js @@ -3,7 +3,7 @@ const env = require("../environment") const { checkSlashesInUrl } = require("./index") const { getProdAppID } = require("@budibase/backend-core/db") const { updateAppRole } = require("./global") -const { Headers } = require("@budibase/backend-core/constants") +const { Header } = require("@budibase/backend-core/constants") const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy") function request(ctx, request) { diff --git a/packages/worker/src/api/controllers/global/auth.ts b/packages/worker/src/api/controllers/global/auth.ts index 2e97ea6be8..3df77f024d 100644 --- a/packages/worker/src/api/controllers/global/auth.ts +++ b/packages/worker/src/api/controllers/global/auth.ts @@ -2,7 +2,7 @@ const core = require("@budibase/backend-core") const { Config, EmailTemplatePurpose } = require("../../../constants") const { sendEmail, isEmailConfigured } = require("../../../utilities/email") const { setCookie, getCookie, clearCookie, hash, platformLogout } = core.utils -const { Cookie, Headers } = core.constants +const { Cookie, Header } = core.constants const { passport, ssoCallbackUrl, google, oidc } = core.auth const { checkResetPasswordCode } = require("../../../utilities/redis") const { getGlobalDB } = require("@budibase/backend-core/tenancy") diff --git a/packages/worker/src/middleware/cloudRestricted.js b/packages/worker/src/middleware/cloudRestricted.js index 29aa40662b..5b9d64b92f 100644 --- a/packages/worker/src/middleware/cloudRestricted.js +++ b/packages/worker/src/middleware/cloudRestricted.js @@ -1,5 +1,5 @@ const env = require("../environment") -const { Headers } = require("@budibase/backend-core/constants") +const { Header } = require("@budibase/backend-core/constants") /** * This is a restricted endpoint in the cloud. diff --git a/packages/worker/src/utilities/appService.js b/packages/worker/src/utilities/appService.js index acf0c359a0..ae895b831c 100644 --- a/packages/worker/src/utilities/appService.js +++ b/packages/worker/src/utilities/appService.js @@ -1,5 +1,5 @@ const fetch = require("node-fetch") -const { Headers } = require("@budibase/backend-core/constants") +const { Header } = require("@budibase/backend-core/constants") const { getTenantId, isTenantIdSet } = require("@budibase/backend-core/tenancy") const { checkSlashesInUrl } = require("../utilities") const env = require("../environment")