| import { config } from '$lib/stores/settings.svelte'; |
| import { REDACTED_HEADERS } from '$lib/constants'; |
| import { redactValue } from './redact'; |
|
|
| |
| |
| |
| |
| export function getAuthHeaders(): Record<string, string> { |
| const currentConfig = config(); |
| const apiKey = currentConfig.apiKey?.toString().trim(); |
|
|
| return apiKey ? { Authorization: `Bearer ${apiKey}` } : {}; |
| } |
|
|
| |
| |
| |
| export function getJsonHeaders(): Record<string, string> { |
| return { |
| 'Content-Type': 'application/json', |
| ...getAuthHeaders() |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function sanitizeHeaders( |
| headers?: HeadersInit, |
| extraRedactedHeaders?: Iterable<string>, |
| partialRedactHeaders?: Map<string, number> |
| ): Record<string, string> { |
| if (!headers) { |
| return {}; |
| } |
|
|
| const normalized = new Headers(headers); |
| const sanitized: Record<string, string> = {}; |
| const redactedHeaders = new Set( |
| Array.from(extraRedactedHeaders ?? [], (header) => header.toLowerCase()) |
| ); |
|
|
| for (const [key, value] of normalized.entries()) { |
| const normalizedKey = key.toLowerCase(); |
| const partialChars = partialRedactHeaders?.get(normalizedKey); |
|
|
| if (partialChars !== undefined) { |
| sanitized[key] = redactValue(value, partialChars); |
| } else if (REDACTED_HEADERS.has(normalizedKey) || redactedHeaders.has(normalizedKey)) { |
| sanitized[key] = redactValue(value); |
| } else { |
| sanitized[key] = value; |
| } |
| } |
|
|
| return sanitized; |
| } |
|
|