mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Add a docked desktop tool panel with an icon rail, driven by the shared panel URL param, and keep the mobile dialogs and drawers. Extract dialog bodies into reusable content components, rename the walkthrough dialog to a sheet, and make walkthrough generation asynchronous with client polling.
133 lines
3.8 KiB
TypeScript
133 lines
3.8 KiB
TypeScript
import { FetchError } from '@opencode-manager/shared'
|
|
import type { ApiErrorResponse } from '@opencode-manager/shared'
|
|
|
|
export { FetchError }
|
|
|
|
interface FetchWrapperOptions extends RequestInit {
|
|
timeout?: number
|
|
params?: Record<string, string | number | boolean | undefined>
|
|
acceptedStatuses?: number[]
|
|
}
|
|
|
|
function formatDetails(details: unknown): string | undefined {
|
|
if (Array.isArray(details)) {
|
|
return details
|
|
.map((d) => {
|
|
if (typeof d !== 'object' || d === null) return null
|
|
const path = Array.isArray((d as Record<string, unknown>).path)
|
|
? ((d as Record<string, unknown>).path as string[])
|
|
: undefined
|
|
const message = typeof (d as Record<string, unknown>).message === 'string'
|
|
? (d as Record<string, unknown>).message as string
|
|
: undefined
|
|
return path?.length ? `${path.join('.')}: ${message}` : message
|
|
})
|
|
.filter(Boolean)
|
|
.join('; ')
|
|
}
|
|
if (typeof details === 'string') return details
|
|
return undefined
|
|
}
|
|
|
|
async function handleResponse(response: Response): Promise<never> {
|
|
const text = await response.text().catch(() => '')
|
|
const data: ApiErrorResponse = (() => {
|
|
if (!text) return { error: 'An error occurred' }
|
|
try {
|
|
return JSON.parse(text) as ApiErrorResponse
|
|
} catch {
|
|
return { error: /^\s*</.test(text) ? `Request failed with status ${response.status}` : text }
|
|
}
|
|
})()
|
|
const errorData = data as ApiErrorResponse & { message?: string; data?: { message?: unknown } }
|
|
const openCodeMessage = typeof errorData.data?.message === 'string'
|
|
? errorData.data.message
|
|
: undefined
|
|
const detail = data.detail || formatDetails(data.details)
|
|
throw new FetchError(
|
|
data.error || errorData.message || openCodeMessage || 'Request failed',
|
|
response.status,
|
|
data.code,
|
|
detail,
|
|
{
|
|
details: data.details,
|
|
validationIssues: data.validationIssues,
|
|
}
|
|
)
|
|
}
|
|
|
|
function buildUrl(url: string, params?: Record<string, string | number | boolean | undefined>): URL {
|
|
const urlObj = new URL(url, window.location.origin)
|
|
if (params) {
|
|
Object.entries(params).forEach(([key, value]) => {
|
|
if (value !== undefined) {
|
|
urlObj.searchParams.append(key, String(value))
|
|
}
|
|
})
|
|
}
|
|
return urlObj
|
|
}
|
|
|
|
async function fetchWithTimeout(
|
|
url: string,
|
|
options: FetchWrapperOptions = {}
|
|
): Promise<Response> {
|
|
const { timeout = 30000, params, acceptedStatuses, ...fetchOptions } = options
|
|
const urlObj = buildUrl(url, params)
|
|
|
|
const controller = new AbortController()
|
|
const timeoutId = timeout > 0 ? setTimeout(() => controller.abort(), timeout) : null
|
|
|
|
try {
|
|
const response = await fetch(urlObj.toString(), {
|
|
credentials: 'include',
|
|
...fetchOptions,
|
|
signal: controller.signal,
|
|
})
|
|
|
|
if (timeoutId) clearTimeout(timeoutId)
|
|
|
|
if (!response.ok && !acceptedStatuses?.includes(response.status)) {
|
|
await handleResponse(response)
|
|
}
|
|
|
|
return response
|
|
} catch (error) {
|
|
if (timeoutId) clearTimeout(timeoutId)
|
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
throw new FetchError('Request timeout', 408, 'TIMEOUT')
|
|
}
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async function fetchWrapper<T = unknown>(
|
|
url: string,
|
|
options: FetchWrapperOptions = {}
|
|
): Promise<T> {
|
|
const response = await fetchWithTimeout(url, options)
|
|
|
|
try {
|
|
return await response.json()
|
|
} catch {
|
|
throw new FetchError('Invalid JSON response', response.status, 'INVALID_JSON')
|
|
}
|
|
}
|
|
|
|
async function fetchWrapperVoid(
|
|
url: string,
|
|
options: FetchWrapperOptions = {}
|
|
): Promise<void> {
|
|
await fetchWithTimeout(url, options)
|
|
}
|
|
|
|
async function fetchWrapperBlob(
|
|
url: string,
|
|
options: FetchWrapperOptions = {}
|
|
): Promise<Blob> {
|
|
const response = await fetchWithTimeout(url, options)
|
|
return response.blob()
|
|
}
|
|
|
|
export { fetchWrapper, fetchWrapperVoid, fetchWrapperBlob }
|