1
0
Fork 0
mirror of synced 2024-10-01 09:38:55 +13:00

Type workerRequests.ts

This commit is contained in:
Sam Rose 2024-02-15 14:48:47 +00:00
parent 36e1a20c03
commit dde8f77877
No known key found for this signature in database
2 changed files with 70 additions and 21 deletions

View file

@ -1,10 +1,19 @@
import { HeaderInit, Headers } from "node-fetch"
import { Header } from "../../constants"
const correlator = require("correlation-id")
export const setHeader = (headers: any) => {
export const setHeader = (headers: HeaderInit) => {
const correlationId = correlator.getId()
if (correlationId) {
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
}
}

View file

@ -1,4 +1,10 @@
import { Response, default as fetch } from "node-fetch"
import {
Response,
default as fetch,
type RequestInit,
Headers,
HeadersInit,
} from "node-fetch"
import env from "../environment"
import { checkSlashesInUrl } from "./index"
import {
@ -10,29 +16,61 @@ import {
} from "@budibase/backend-core"
import { Ctx, User, EmailInvite } from "@budibase/types"
export function request(ctx?: Ctx, request?: any) {
if (!request.headers) {
request.headers = {}
function ensureHeadersIsObject(headers: HeadersInit | undefined): Headers {
if (headers instanceof Headers) {
return headers
}
const headersObj = new Headers()
if (headers === undefined) {
return headersObj
}
if (Array.isArray(headers)) {
for (const [key, value] of headers) {
headersObj.append(key, value)
}
} else {
for (const key in headers) {
headersObj.append(key, headers[key])
}
}
return headersObj
}
export function request(request: RequestInit & { ctx?: Ctx }): RequestInit {
const ctx = request.ctx
request.headers = ensureHeadersIsObject(request.headers)
if (!ctx) {
request.headers[constants.Header.API_KEY] = coreEnv.INTERNAL_API_KEY
if (coreEnv.INTERNAL_API_KEY) {
request.headers.set(constants.Header.API_KEY, coreEnv.INTERNAL_API_KEY)
}
} else if (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)) {
if (ctx.headers[header]) {
request.headers[header] = ctx.headers[header]
const value = ctx.headers[header]
if (value === undefined) {
continue
}
if (Array.isArray(value)) {
for (let v of value) {
request.headers.append(header, v)
}
} else {
request.headers.set(header, value)
}
}
}
// apply tenancy if its available
if (tenancy.isTenantIdSet()) {
request.headers[constants.Header.TENANT_ID] = tenancy.getTenantId()
request.headers.set(constants.Header.TENANT_ID, tenancy.getTenantId())
}
if (request.body && Object.keys(request.body).length > 0) {
request.headers["Content-Type"] = "application/json"
request.headers.set("Content-Type", "application/json")
request.body =
typeof request.body === "object"
? JSON.stringify(request.body)
@ -44,6 +82,7 @@ export function request(ctx?: Ctx, request?: any) {
// add x-budibase-correlation-id header
logging.correlation.setHeader(request.headers)
delete request.ctx
return request
}
@ -93,9 +132,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(undefined, {
request({
method: "POST",
body: {
body: JSON.stringify({
email: to,
from,
contents,
@ -105,7 +144,7 @@ export async function sendSmtpEmail({
purpose: "custom",
automation,
invite,
},
}),
})
)
return checkResponse(response, "send email")
@ -115,7 +154,8 @@ 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(ctx, {
request({
ctx,
method: "DELETE",
})
)
@ -126,7 +166,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" })
request({ ctx, method: "GET" })
)
return checkResponse(response, "get users", { ctx })
}
@ -135,7 +175,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 })
request({ ctx, method: "POST", body: ctx.request.body })
)
return checkResponse(response, "save user", { ctx })
}
@ -146,7 +186,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" })
request({ ctx, method: "DELETE" })
)
return checkResponse(response, "delete user", { ctx })
}
@ -157,7 +197,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" })
request({ ctx, method: "GET" })
)
return checkResponse(response, "get user", { ctx })
}
@ -167,7 +207,7 @@ export async function getChecklist(): Promise<{
}> {
const response = await fetch(
checkSlashesInUrl(env.WORKER_URL + "/api/global/configs/checklist"),
request(undefined, { method: "GET" })
request({ method: "GET" })
)
return checkResponse(response, "get checklist")
}
@ -175,7 +215,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(undefined, { method: "POST", body: { userId } })
request({ method: "POST", body: JSON.stringify({ userId }) })
)
return checkResponse(response, "generate API key")
}