(opts: ClientOptions)
| 274 | } |
| 275 | |
| 276 | export function createHttpClient(opts: ClientOptions): HttpClient { |
| 277 | const state = compileState(opts) |
| 278 | |
| 279 | const typedCall = async <T>(method: HttpMethod, path: string, callOpts?: RequestOptions): Promise<T> => { |
| 280 | const finalOpts: RequestOptions = { ...callOpts, method } |
| 281 | const built = buildRequest(state, path, finalOpts, true) |
| 282 | const res = await execute(state, built.request, built.resolved, built.effectiveTimeoutMs, built.userSignal) |
| 283 | return parseJsonBody<T>(res) |
| 284 | } |
| 285 | |
| 286 | const rawFetch = (path: string, callOpts?: RequestOptions): Promise<Response> => { |
| 287 | const finalOpts: RequestOptions = { ...callOpts, method: callOpts?.method ?? 'GET' } |
| 288 | const built = buildRequest(state, path, finalOpts, false) |
| 289 | return execute(state, built.request, built.resolved, built.effectiveTimeoutMs, built.userSignal) |
| 290 | } |
| 291 | |
| 292 | const streamFetch = (path: string, callOpts?: RequestOptions): Promise<Response> => { |
| 293 | // SSE bodies must not be aborted by a request-level timeout — `0` is the buildRequest |
| 294 | // sentinel for "no timeout" and also overrides the client default. |
| 295 | // |
| 296 | // A stream normally never retries (a mid-stream replay would double-send). When the caller |
| 297 | // opts into 429 retry, allow a bounded budget: the 429 admission rejection arrives as a plain |
| 298 | // body before the stream opens, and execute()'s 429 branch is the only path that fires for a |
| 299 | // POST — shouldRetry still rejects POST for transport / 5xx, so nothing else replays. |
| 300 | const retryAttempts = callOpts?.retryOnRateLimit === true |
| 301 | ? (callOpts.retryAttempts ?? RATE_LIMIT_MAX_ATTEMPTS) |
| 302 | : 0 |
| 303 | const finalOpts: RequestOptions = { |
| 304 | ...callOpts, |
| 305 | method: callOpts?.method ?? 'GET', |
| 306 | retryAttempts, |
| 307 | timeoutMs: 0, |
| 308 | } |
| 309 | const built = buildRequest(state, path, finalOpts, false) |
| 310 | return execute(state, built.request, built.resolved, built.effectiveTimeoutMs, built.userSignal) |
| 311 | } |
| 312 | |
| 313 | // Low-level entrypoint for oRPC's OpenAPILink: executes an already-built, absolute-URL |
| 314 | // Request through the same transport (UA+bearer hooks, retry, timeout, error-map) while |
| 315 | // skipping joinURL. Policy comes from the client instance defaults — there is no per-call |
| 316 | // override, so this stays a drop-in for OpenAPILink's `(req, init) => Promise<Response>`. |
| 317 | // Returns the raw Response for every status; the oRPC fetch wrapper (orpc.ts) inspects the |
| 318 | // status and raises classifyResponse for non-2xx, so error mapping stays in one place. |
| 319 | const requestFetch = (req: Request, init?: RequestInit): Promise<Response> => { |
| 320 | const method = req.method.toUpperCase() as HttpMethod |
| 321 | const resolved: ResolvedOptions = { |
| 322 | method, |
| 323 | headers: req.headers, |
| 324 | body: undefined, |
| 325 | timeoutMs: state.defaultTimeoutMs, |
| 326 | retryAttempts: state.defaultRetryAttempts, |
| 327 | throwOnError: false, |
| 328 | retryOnRateLimit: false, |
| 329 | } |
| 330 | const userSignal = init?.signal ?? req.signal |
| 331 | return execute(state, req, resolved, state.defaultTimeoutMs, userSignal) |
| 332 | } |
| 333 |
no test coverage detected