mirror of
https://github.com/simstudioai/sim.git
synced 2026-02-13 07:55:09 -05:00
* feat(internal): added internal api base url for internal calls * make validation on http more lax
43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import { generateInternalToken } from '@/lib/auth/internal'
|
|
import { getBaseUrl, getInternalApiBaseUrl } from '@/lib/core/utils/urls'
|
|
import { HTTP } from '@/executor/constants'
|
|
|
|
export async function buildAuthHeaders(): Promise<Record<string, string>> {
|
|
const headers: Record<string, string> = {
|
|
'Content-Type': HTTP.CONTENT_TYPE.JSON,
|
|
}
|
|
|
|
if (typeof window === 'undefined') {
|
|
const token = await generateInternalToken()
|
|
headers.Authorization = `Bearer ${token}`
|
|
}
|
|
|
|
return headers
|
|
}
|
|
|
|
export function buildAPIUrl(path: string, params?: Record<string, string>): URL {
|
|
const baseUrl = path.startsWith('/api/') ? getInternalApiBaseUrl() : getBaseUrl()
|
|
const url = new URL(path, baseUrl)
|
|
|
|
if (params) {
|
|
for (const [key, value] of Object.entries(params)) {
|
|
if (value !== undefined && value !== null) {
|
|
url.searchParams.set(key, value)
|
|
}
|
|
}
|
|
}
|
|
|
|
return url
|
|
}
|
|
|
|
export async function extractAPIErrorMessage(response: Response): Promise<string> {
|
|
const defaultMessage = `API request failed with status ${response.status}`
|
|
|
|
try {
|
|
const errorData = await response.json()
|
|
return errorData.error || defaultMessage
|
|
} catch {
|
|
return defaultMessage
|
|
}
|
|
}
|