( response: Response, operation: string )
| 256 | * by signal/cancel/terminate) parse to an empty object. |
| 257 | */ |
| 258 | export async function parseTemporalResponse<T extends object>( |
| 259 | response: Response, |
| 260 | operation: string |
| 261 | ): Promise<T> { |
| 262 | const text = await response.text() |
| 263 | let data: Record<string, unknown> = {} |
| 264 | if (text) { |
| 265 | try { |
| 266 | data = JSON.parse(text) as Record<string, unknown> |
| 267 | } catch { |
| 268 | data = { message: truncate(text, 300) } |
| 269 | } |
| 270 | } |
| 271 | if (!response.ok) { |
| 272 | const message = |
| 273 | typeof data.message === 'string' && data.message ? data.message : `HTTP ${response.status}` |
| 274 | throw new Error(`Temporal ${operation} failed: ${message}`) |
| 275 | } |
| 276 | return data as T |
| 277 | } |
no test coverage detected