Replacing BBContext where its still used in backend-core and middlewares.
This commit is contained in:
parent
ecf4ea5826
commit
cb49beb317
|
@ -1,6 +1,6 @@
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
export default async (ctx: BBContext | any, next: any) => {
|
export default async (ctx: Ctx, next: any) => {
|
||||||
// Placeholder for audit log middleware
|
// Placeholder for audit log middleware
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Header } from "../constants"
|
import { Header } from "../constants"
|
||||||
import { buildMatcherRegex, matches } from "./matchers"
|
import { buildMatcherRegex, matches } from "./matchers"
|
||||||
import { BBContext, EndpointMatcher } from "@budibase/types"
|
import { Ctx, EndpointMatcher } from "@budibase/types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET, HEAD and OPTIONS methods are considered safe operations
|
* GET, HEAD and OPTIONS methods are considered safe operations
|
||||||
|
@ -36,7 +36,7 @@ export default function (
|
||||||
opts: { noCsrfPatterns: EndpointMatcher[] } = { noCsrfPatterns: [] }
|
opts: { noCsrfPatterns: EndpointMatcher[] } = { noCsrfPatterns: [] }
|
||||||
) {
|
) {
|
||||||
const noCsrfOptions = buildMatcherRegex(opts.noCsrfPatterns)
|
const noCsrfOptions = buildMatcherRegex(opts.noCsrfPatterns)
|
||||||
return async (ctx: BBContext | any, next: any) => {
|
return async (ctx: Ctx, next: any) => {
|
||||||
// don't apply for excluded paths
|
// don't apply for excluded paths
|
||||||
const found = matches(ctx, noCsrfOptions)
|
const found = matches(ctx, noCsrfOptions)
|
||||||
if (found) {
|
if (found) {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import { Header } from "../constants"
|
import { Header } from "../constants"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
import { isValidInternalAPIKey } from "../utils"
|
import { isValidInternalAPIKey } from "../utils"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* API Key only endpoint.
|
* API Key only endpoint.
|
||||||
*/
|
*/
|
||||||
export default async (ctx: BBContext, next: any) => {
|
export default async (ctx: Ctx, next: any) => {
|
||||||
const apiKey = ctx.request.headers[Header.API_KEY]
|
const apiKey = ctx.request.headers[Header.API_KEY]
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
ctx.throw(403, "Unauthorized")
|
ctx.throw(403, "Unauthorized")
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { BBContext, EndpointMatcher, RegexMatcher } from "@budibase/types"
|
import { Ctx, EndpointMatcher, RegexMatcher } from "@budibase/types"
|
||||||
|
|
||||||
const PARAM_REGEX = /\/:(.*?)(\/.*)?$/g
|
const PARAM_REGEX = /\/:(.*?)(\/.*)?$/g
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ export const buildMatcherRegex = (
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export const matches = (ctx: BBContext, options: RegexMatcher[]) => {
|
export const matches = (ctx: Ctx, options: RegexMatcher[]) => {
|
||||||
return options.find(({ regex, method }) => {
|
return options.find(({ regex, method }) => {
|
||||||
const urlMatch = regex.test(ctx.request.url)
|
const urlMatch = regex.test(ctx.request.url)
|
||||||
const methodMatch =
|
const methodMatch =
|
||||||
|
|
|
@ -2,7 +2,7 @@ import { UserStatus } from "../../constants"
|
||||||
import { compare } from "../../utils"
|
import { compare } from "../../utils"
|
||||||
import * as users from "../../users"
|
import * as users from "../../users"
|
||||||
import { authError } from "./utils"
|
import { authError } from "./utils"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
const INVALID_ERR = "Invalid credentials"
|
const INVALID_ERR = "Invalid credentials"
|
||||||
const EXPIRED = "This account has expired. Please reset your password"
|
const EXPIRED = "This account has expired. Please reset your password"
|
||||||
|
@ -20,7 +20,7 @@ export const options = {
|
||||||
* @returns The authenticated user, or errors if they occur
|
* @returns The authenticated user, or errors if they occur
|
||||||
*/
|
*/
|
||||||
export async function authenticate(
|
export async function authenticate(
|
||||||
ctx: BBContext,
|
ctx: Ctx,
|
||||||
email: string,
|
email: string,
|
||||||
password: string,
|
password: string,
|
||||||
done: Function
|
done: Function
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { getTenantIDFromCtx } from "../tenancy"
|
||||||
import { buildMatcherRegex, matches } from "./matchers"
|
import { buildMatcherRegex, matches } from "./matchers"
|
||||||
import { Header } from "../constants"
|
import { Header } from "../constants"
|
||||||
import {
|
import {
|
||||||
BBContext,
|
Ctx,
|
||||||
EndpointMatcher,
|
EndpointMatcher,
|
||||||
GetTenantIdOptions,
|
GetTenantIdOptions,
|
||||||
TenantResolutionStrategy,
|
TenantResolutionStrategy,
|
||||||
|
@ -17,7 +17,7 @@ export default function (
|
||||||
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
|
const allowQsOptions = buildMatcherRegex(allowQueryStringPatterns)
|
||||||
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
|
const noTenancyOptions = buildMatcherRegex(noTenancyPatterns)
|
||||||
|
|
||||||
return async function (ctx: BBContext | any, next: any) {
|
return async function (ctx: Ctx, next: any) {
|
||||||
const allowNoTenant =
|
const allowNoTenant =
|
||||||
opts.noTenancyRequired || !!matches(ctx, noTenancyOptions)
|
opts.noTenancyRequired || !!matches(ctx, noTenancyOptions)
|
||||||
const tenantOpts: GetTenantIdOptions = {
|
const tenantOpts: GetTenantIdOptions = {
|
||||||
|
|
|
@ -6,7 +6,7 @@ import {
|
||||||
getPlatformURL,
|
getPlatformURL,
|
||||||
} from "../context"
|
} from "../context"
|
||||||
import {
|
import {
|
||||||
BBContext,
|
Ctx,
|
||||||
TenantResolutionStrategy,
|
TenantResolutionStrategy,
|
||||||
GetTenantIdOptions,
|
GetTenantIdOptions,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
|
@ -37,7 +37,7 @@ export const isUserInAppTenant = (appId: string, user?: any) => {
|
||||||
const ALL_STRATEGIES = Object.values(TenantResolutionStrategy)
|
const ALL_STRATEGIES = Object.values(TenantResolutionStrategy)
|
||||||
|
|
||||||
export const getTenantIDFromCtx = (
|
export const getTenantIDFromCtx = (
|
||||||
ctx: BBContext,
|
ctx: Ctx,
|
||||||
opts: GetTenantIdOptions
|
opts: GetTenantIdOptions
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
// exit early if not multi-tenant
|
// exit early if not multi-tenant
|
||||||
|
|
|
@ -5,7 +5,7 @@ import * as db from "../../db"
|
||||||
import { Header } from "../../constants"
|
import { Header } from "../../constants"
|
||||||
import { newid } from "../../utils"
|
import { newid } from "../../utils"
|
||||||
import env from "../../environment"
|
import env from "../../environment"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
describe("utils", () => {
|
describe("utils", () => {
|
||||||
const config = new DBTestConfiguration()
|
const config = new DBTestConfiguration()
|
||||||
|
@ -109,7 +109,7 @@ describe("utils", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("isServingBuilder", () => {
|
describe("isServingBuilder", () => {
|
||||||
let ctx: BBContext
|
let ctx: Ctx
|
||||||
|
|
||||||
const expectResult = (result: boolean) =>
|
const expectResult = (result: boolean) =>
|
||||||
expect(utils.isServingBuilder(ctx)).toBe(result)
|
expect(utils.isServingBuilder(ctx)).toBe(result)
|
||||||
|
@ -133,7 +133,7 @@ describe("utils", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("isServingBuilderPreview", () => {
|
describe("isServingBuilderPreview", () => {
|
||||||
let ctx: BBContext
|
let ctx: Ctx
|
||||||
|
|
||||||
const expectResult = (result: boolean) =>
|
const expectResult = (result: boolean) =>
|
||||||
expect(utils.isServingBuilderPreview(ctx)).toBe(result)
|
expect(utils.isServingBuilderPreview(ctx)).toBe(result)
|
||||||
|
@ -157,7 +157,7 @@ describe("utils", () => {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("isPublicAPIRequest", () => {
|
describe("isPublicAPIRequest", () => {
|
||||||
let ctx: BBContext
|
let ctx: Ctx
|
||||||
|
|
||||||
const expectResult = (result: boolean) =>
|
const expectResult = (result: boolean) =>
|
||||||
expect(utils.isPublicApiRequest(ctx)).toBe(result)
|
expect(utils.isPublicApiRequest(ctx)).toBe(result)
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import { createMockContext, createMockCookies } from "@shopify/jest-koa-mocks"
|
import { createMockContext, createMockCookies } from "@shopify/jest-koa-mocks"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
export const newContext = (): BBContext => {
|
export const newContext = (): Ctx => {
|
||||||
const ctx = createMockContext() as any
|
const ctx = createMockContext() as Ctx
|
||||||
return {
|
return {
|
||||||
...ctx,
|
...ctx,
|
||||||
path: "/",
|
path: "/",
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { isDevAppID, isProdAppID } from "../db/utils"
|
import { isDevAppID, isProdAppID } from "../db/utils"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
export enum AppType {
|
export enum AppType {
|
||||||
DEV = "dev",
|
DEV = "dev",
|
||||||
|
@ -7,7 +7,7 @@ export enum AppType {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function middleware({ appType }: { appType?: AppType } = {}) {
|
export function middleware({ appType }: { appType?: AppType } = {}) {
|
||||||
return (ctx: BBContext, next: any) => {
|
return (ctx: Ctx, next: any) => {
|
||||||
const appId = ctx.appId
|
const appId = ctx.appId
|
||||||
if (appType === AppType.DEV && appId && !isDevAppID(appId)) {
|
if (appType === AppType.DEV && appId && !isDevAppID(appId)) {
|
||||||
ctx.throw(400, "Only apps in development support this endpoint")
|
ctx.throw(400, "Only apps in development support this endpoint")
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import Joi from "joi"
|
import Joi from "joi"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
function validate(schema: Joi.Schema, property: string) {
|
function validate(schema: Joi.Schema, property: string) {
|
||||||
// Return a Koa middleware function
|
// Return a Koa middleware function
|
||||||
return (ctx: BBContext, next: any) => {
|
return (ctx: Ctx, next: any) => {
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
export class ResourceIdGetter {
|
export class ResourceIdGetter {
|
||||||
parameter: string
|
parameter: string
|
||||||
|
@ -26,7 +26,7 @@ export class ResourceIdGetter {
|
||||||
const parameter = this.parameter,
|
const parameter = this.parameter,
|
||||||
main = this.main,
|
main = this.main,
|
||||||
sub = this.sub
|
sub = this.sub
|
||||||
return (ctx: BBContext, next: any) => {
|
return (ctx: Ctx, next: any) => {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const request = ctx.request[parameter] || ctx[parameter]
|
const request = ctx.request[parameter] || ctx[parameter]
|
||||||
if (request == null) {
|
if (request == null) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import { BBContext } from "@budibase/types"
|
import { Ctx } from "@budibase/types"
|
||||||
|
|
||||||
// if added as a middleware will stop requests unless builder is in self host mode
|
// if added as a middleware will stop requests unless builder is in self host mode
|
||||||
// or cloud is in self host
|
// or cloud is in self host
|
||||||
export default async (ctx: BBContext, next: any) => {
|
export default async (ctx: Ctx, next: any) => {
|
||||||
if (env.SELF_HOSTED) {
|
if (env.SELF_HOSTED) {
|
||||||
await next()
|
await next()
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { BBContext } from "./koa"
|
import { Ctx } from "./koa"
|
||||||
import { Hosting } from "./hosting"
|
import { Hosting } from "./hosting"
|
||||||
|
|
||||||
export interface AuthToken {
|
export interface AuthToken {
|
||||||
|
@ -32,7 +32,7 @@ export interface ScannedSession {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface PlatformLogoutOpts {
|
export interface PlatformLogoutOpts {
|
||||||
ctx: BBContext
|
ctx: Ctx
|
||||||
userId: string
|
userId: string
|
||||||
keepActiveSession?: boolean
|
keepActiveSession?: boolean
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue