mirror of
https://github.com/simstudioai/sim.git
synced 2026-01-25 06:48:12 -05:00
* fix(import): fixed trigger save on export/import flow * optimized test runners * ack PR comments
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { generateInternalToken } from '@/lib/auth/internal'
|
|
import { getBaseUrl } 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 url = new URL(path, getBaseUrl())
|
|
|
|
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
|
|
}
|
|
}
|