1
0
Fork 0
mirror of synced 2024-07-07 15:25:52 +12:00

Merge branch 'type-worker-requests' of github.com:Budibase/budibase into type-worker-requests

This commit is contained in:
mike12345567 2024-02-15 16:19:14 +00:00
commit 66fed60e44
5 changed files with 53 additions and 1571 deletions

View file

@ -1,21 +1,13 @@
import { HeaderInit, Headers } from "node-fetch"
import { Header } from "../../constants" import { Header } from "../../constants"
const correlator = require("correlation-id") const correlator = require("correlation-id")
export const setHeader = (headers: HeaderInit) => { export const setHeader = (headers: Record<string, string>) => {
const correlationId = correlator.getId() const correlationId = correlator.getId()
if (!correlationId) { if (!correlationId) {
return return
} }
headers[Header.CORRELATION_ID] = correlationId
if (headers instanceof Headers) {
headers.set(Header.CORRELATION_ID, correlationId)
} else if (Array.isArray(headers)) {
headers.push([Header.CORRELATION_ID, correlationId])
} else {
headers[Header.CORRELATION_ID] = correlationId
}
} }
export function getId() { export function getId() {

File diff suppressed because it is too large Load diff

View file

@ -92,12 +92,13 @@ const resetBuilderHistory = () => {
export const initialise = async pkg => { export const initialise = async pkg => {
const { application } = pkg const { application } = pkg
// must be first operation to make sure subsequent requests have correct app ID
appStore.syncAppPackage(pkg)
await Promise.all([ await Promise.all([
appStore.syncAppRoutes(), appStore.syncAppRoutes(),
componentStore.refreshDefinitions(application?.appId), componentStore.refreshDefinitions(application?.appId),
]) ])
builderStore.init(application) builderStore.init(application)
appStore.syncAppPackage(pkg)
navigationStore.syncAppNavigation(application?.navigation) navigationStore.syncAppNavigation(application?.navigation)
themeStore.syncAppTheme(application) themeStore.syncAppTheme(application)
screenStore.syncAppScreens(pkg) screenStore.syncAppScreens(pkg)

View file

@ -1,7 +1,7 @@
import fetch from "node-fetch" import fetch from "node-fetch"
import env from "../../environment" import env from "../../environment"
import { checkSlashesInUrl } from "../../utilities" import { checkSlashesInUrl } from "../../utilities"
import { request } from "../../utilities/workerRequests" import { createRequest } from "../../utilities/workerRequests"
import { clearLock as redisClearLock } from "../../utilities/redis" import { clearLock as redisClearLock } from "../../utilities/redis"
import { DocumentType } from "../../db/utils" import { DocumentType } from "../../db/utils"
import { import {
@ -13,14 +13,19 @@ import {
} from "@budibase/backend-core" } from "@budibase/backend-core"
import { App } from "@budibase/types" import { App } from "@budibase/types"
async function redirect(ctx: any, method: string, path: string = "global") { async function redirect(
ctx: any,
method: "GET" | "POST" | "DELETE",
path: string = "global"
) {
const { devPath } = ctx.params const { devPath } = ctx.params
const queryString = ctx.originalUrl.split("?")[1] || "" const queryString = ctx.originalUrl.split("?")[1] || ""
const response = await fetch( const response = await fetch(
checkSlashesInUrl( checkSlashesInUrl(
`${env.WORKER_URL}/api/${path}/${devPath}?${queryString}` `${env.WORKER_URL}/api/${path}/${devPath}?${queryString}`
), ),
request(ctx, { createRequest({
ctx,
method, method,
body: ctx.request.body, body: ctx.request.body,
}) })

View file

@ -39,15 +39,24 @@ function ensureHeadersIsObject(headers: HeadersInit | undefined): Headers {
return headersObj return headersObj
} }
export function request(request: RequestInit & { ctx?: Ctx }): RequestInit { interface Request {
const ctx = request.ctx ctx?: Ctx
request.headers = ensureHeadersIsObject(request.headers) method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
headers?: { [key: string]: string }
body?: { [key: string]: any }
}
if (!ctx) { export function createRequest(request: Request): RequestInit {
if (coreEnv.INTERNAL_API_KEY) { const headers: Record<string, string> = {}
request.headers.set(constants.Header.API_KEY, coreEnv.INTERNAL_API_KEY) const requestInit: RequestInit = {
} method: request.method,
} else if (ctx.headers) { }
const ctx = request.ctx
if (!ctx && coreEnv.INTERNAL_API_KEY) {
headers[constants.Header.API_KEY] = coreEnv.INTERNAL_API_KEY
} else if (ctx && ctx.headers) {
// copy all Budibase utilised headers over - copying everything can have // copy all Budibase utilised headers over - copying everything can have
// side effects like requests being rejected due to odd content types etc // side effects like requests being rejected due to odd content types etc
for (let header of Object.values(constants.Header)) { for (let header of Object.values(constants.Header)) {
@ -57,42 +66,37 @@ export function request(request: RequestInit & { ctx?: Ctx }): RequestInit {
} }
if (Array.isArray(value)) { if (Array.isArray(value)) {
for (let v of value) { headers[header] = value[0]
request.headers.append(header, v)
}
} else { } else {
request.headers.set(header, value) headers[header] = value
} }
} }
// be specific about auth headers // be specific about auth headers
const cookie = ctx.headers[constants.Header.COOKIE], const cookie = ctx.headers[constants.Header.COOKIE],
apiKey = ctx.headers[constants.Header.API_KEY] apiKey = ctx.headers[constants.Header.API_KEY]
if (cookie) { if (cookie) {
request.headers[constants.Header.COOKIE] = cookie headers[constants.Header.COOKIE] = cookie
} else if (apiKey) { } else if (apiKey) {
request.headers[constants.Header.API_KEY] = apiKey if (Array.isArray(apiKey)) {
headers[constants.Header.API_KEY] = apiKey[0]
} else {
headers[constants.Header.API_KEY] = apiKey
}
} }
} }
// apply tenancy if its available // apply tenancy if its available
if (tenancy.isTenantIdSet()) { if (tenancy.isTenantIdSet()) {
request.headers.set(constants.Header.TENANT_ID, tenancy.getTenantId()) headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
} }
if (request.body && Object.keys(request.body).length > 0) { if (request.body && Object.keys(request.body).length > 0) {
request.headers.set("Content-Type", "application/json") headers["Content-Type"] = "application/json"
request.body = requestInit.body = JSON.stringify(request.body)
typeof request.body === "object"
? JSON.stringify(request.body)
: request.body
} else {
delete request.body
} }
// add x-budibase-correlation-id header logging.correlation.setHeader(headers)
logging.correlation.setHeader(request.headers) return requestInit
delete request.ctx
return request
} }
async function checkResponse( async function checkResponse(
@ -141,9 +145,9 @@ export async function sendSmtpEmail({
// tenant ID will be set in header // tenant ID will be set in header
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`), checkSlashesInUrl(env.WORKER_URL + `/api/global/email/send`),
request({ createRequest({
method: "POST", method: "POST",
body: JSON.stringify({ body: {
email: to, email: to,
from, from,
contents, contents,
@ -153,7 +157,7 @@ export async function sendSmtpEmail({
purpose: "custom", purpose: "custom",
automation, automation,
invite, invite,
}), },
}) })
) )
return checkResponse(response, "send email") return checkResponse(response, "send email")
@ -163,7 +167,7 @@ export async function removeAppFromUserRoles(ctx: Ctx, appId: string) {
const prodAppId = dbCore.getProdAppID(appId) const prodAppId = dbCore.getProdAppID(appId)
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`), checkSlashesInUrl(env.WORKER_URL + `/api/global/roles/${prodAppId}`),
request({ createRequest({
ctx, ctx,
method: "DELETE", method: "DELETE",
}) })
@ -175,7 +179,7 @@ export async function allGlobalUsers(ctx: Ctx) {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"), checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
// we don't want to use API key when getting self // we don't want to use API key when getting self
request({ ctx, method: "GET" }) createRequest({ ctx, method: "GET" })
) )
return checkResponse(response, "get users", { ctx }) return checkResponse(response, "get users", { ctx })
} }
@ -184,7 +188,7 @@ export async function saveGlobalUser(ctx: Ctx) {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/users"), checkSlashesInUrl(env.WORKER_URL + "/api/global/users"),
// we don't want to use API key when getting self // we don't want to use API key when getting self
request({ ctx, method: "POST", body: ctx.request.body }) createRequest({ ctx, method: "POST", body: ctx.request.body })
) )
return checkResponse(response, "save user", { ctx }) return checkResponse(response, "save user", { ctx })
} }
@ -195,7 +199,7 @@ export async function deleteGlobalUser(ctx: Ctx) {
env.WORKER_URL + `/api/global/users/${ctx.params.userId}` env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
), ),
// we don't want to use API key when getting self // we don't want to use API key when getting self
request({ ctx, method: "DELETE" }) createRequest({ ctx, method: "DELETE" })
) )
return checkResponse(response, "delete user", { ctx }) return checkResponse(response, "delete user", { ctx })
} }
@ -206,7 +210,7 @@ export async function readGlobalUser(ctx: Ctx): Promise<User> {
env.WORKER_URL + `/api/global/users/${ctx.params.userId}` env.WORKER_URL + `/api/global/users/${ctx.params.userId}`
), ),
// we don't want to use API key when getting self // we don't want to use API key when getting self
request({ ctx, method: "GET" }) createRequest({ ctx, method: "GET" })
) )
return checkResponse(response, "get user", { ctx }) return checkResponse(response, "get user", { ctx })
} }
@ -216,7 +220,7 @@ export async function getChecklist(): Promise<{
}> { }> {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"), checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
request({ method: "GET" }) createRequest({ method: "GET" })
) )
return checkResponse(response, "get checklist") return checkResponse(response, "get checklist")
} }
@ -224,7 +228,7 @@ export async function getChecklist(): Promise<{
export async function generateApiKey(userId: string) { export async function generateApiKey(userId: string) {
const response = await fetch( const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/self/api_key"), checkSlashesInUrl(env.WORKER_URL + "/api/global/self/api_key"),
request({ method: "POST", body: JSON.stringify({ userId }) }) createRequest({ method: "POST", body: { userId } })
) )
return checkResponse(response, "generate API key") return checkResponse(response, "generate API key")
} }