1
0
Fork 0
mirror of synced 2024-07-02 21:10:43 +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"
const correlator = require("correlation-id")
export const setHeader = (headers: HeaderInit) => {
export const setHeader = (headers: Record<string, string>) => {
const correlationId = correlator.getId()
if (!correlationId) {
return
}
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
}
headers[Header.CORRELATION_ID] = correlationId
}
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 => {
const { application } = pkg
// must be first operation to make sure subsequent requests have correct app ID
appStore.syncAppPackage(pkg)
await Promise.all([
appStore.syncAppRoutes(),
componentStore.refreshDefinitions(application?.appId),
])
builderStore.init(application)
appStore.syncAppPackage(pkg)
navigationStore.syncAppNavigation(application?.navigation)
themeStore.syncAppTheme(application)
screenStore.syncAppScreens(pkg)

View file

@ -1,7 +1,7 @@
import fetch from "node-fetch"
import env from "../../environment"
import { checkSlashesInUrl } from "../../utilities"
import { request } from "../../utilities/workerRequests"
import { createRequest } from "../../utilities/workerRequests"
import { clearLock as redisClearLock } from "../../utilities/redis"
import { DocumentType } from "../../db/utils"
import {
@ -13,14 +13,19 @@ import {
} from "@budibase/backend-core"
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 queryString = ctx.originalUrl.split("?")[1] || ""
const response = await fetch(
checkSlashesInUrl(
`${env.WORKER_URL}/api/${path}/${devPath}?${queryString}`
),
request(ctx, {
createRequest({
ctx,
method,
body: ctx.request.body,
})

View file

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