(
url: string,
accessToken: string,
body: unknown,
opts: { timeoutMs?: number },
)
| 81 | } |
| 82 | |
| 83 | async function postJson( |
| 84 | url: string, |
| 85 | accessToken: string, |
| 86 | body: unknown, |
| 87 | opts: { timeoutMs?: number }, |
| 88 | ): Promise<{ readonly kind: 'ok'; readonly payload: unknown } | FetchFeedbackUploadError> { |
| 89 | const controller = new AbortController(); |
| 90 | const timer = setTimeout(() => { |
| 91 | controller.abort(); |
| 92 | }, opts.timeoutMs ?? 8000); |
| 93 | try { |
| 94 | const res = await fetch(url, { |
| 95 | method: 'POST', |
| 96 | headers: { |
| 97 | Authorization: `Bearer ${accessToken}`, |
| 98 | Accept: 'application/json', |
| 99 | 'Content-Type': 'application/json', |
| 100 | }, |
| 101 | body: JSON.stringify(body), |
| 102 | signal: controller.signal, |
| 103 | }); |
| 104 | if (!res.ok) { |
| 105 | return { |
| 106 | kind: 'error', |
| 107 | status: res.status, |
| 108 | message: await readApiErrorMessage(res, `Feedback upload request failed: HTTP ${res.status}`), |
| 109 | }; |
| 110 | } |
| 111 | const text = await res.text(); |
| 112 | return { kind: 'ok', payload: text.length > 0 ? JSON.parse(text) : {} }; |
| 113 | } catch (error) { |
| 114 | if (error instanceof Error && error.name === 'AbortError') { |
| 115 | return { kind: 'error', message: 'Feedback upload request timed out.' }; |
| 116 | } |
| 117 | const msg = error instanceof Error ? error.message : String(error); |
| 118 | return { kind: 'error', message: `Feedback upload request failed: ${msg}` }; |
| 119 | } finally { |
| 120 | clearTimeout(timer); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | function feedbackBaseUrl(baseUrl?: string): string { |
| 125 | return (baseUrl ?? kimiCodeBaseUrl()).replace(/\/+$/, ''); |
no test coverage detected