Files
llm-in-text/src/utils/fetch.js
T
“ydy0615” 2283020e51 Refactor and enhance OCR and API functionalities
- Removed obsolete unit tests for TTS/ASR module.
- Deleted unused sample video file.
- Introduced OCRImageWrapper component for better OCR image handling with loading, success, and failure states.
- Updated copilot plugin to improve transaction handling and added new types for better type safety.
- Enhanced web search block plugin to support streaming content updates.
- Refactored API utility functions for better error handling and consistency across requests.
- Added new configuration for OCR API endpoint.
- Consolidated SSE event parsing into a shared utility.
- Created string utility functions to reduce code duplication.
- Removed outdated test documents related to compression functionality.
2026-06-10 14:47:51 +08:00

55 lines
1.4 KiB
JavaScript

/**
* Unified safeFetch wrapper for API requests.
* Handles authentication headers, credentials, and error mapping consistently across all endpoints.
*/
import { API_KEY } from './config.js'
/** Build headers for JSON API requests with optional X-API-Key */
function buildHeaders(extra = {}) {
return API_KEY
? { 'X-API-Key': API_KEY, ...extra }
: { ...extra }
}
/** Parse JSON response from fetch result, handling non-OK status codes */
async function parseJsonResponse(res) {
if (!res.ok) {
let message = `HTTP ${res.status}`
try {
const data = await res.json()
message = data.detail || data.error || message
} catch {
const text = await res.text()
if (text) message = text
}
throw new Error(message)
}
return res.json()
}
/**
* Fetch wrapper that handles:
* - Authentication headers (X-API-Key)
* - Credentials (include cookies)
* - Error mapping (HTTP status → meaningful message)
*/
export async function safeFetch(url, options = {}) {
const headers = buildHeaders(options.headers || {})
const res = await fetch(url, {
...options,
headers,
credentials: 'include',
})
if (!res.ok) {
const errorText = await res.text()
throw new Error(`HTTP ${res.status}: ${errorText}`)
}
return res
}
/** Export helper functions for use in other modules */
export { buildHeaders, parseJsonResponse }