MCPcopy Create free account
hub / github.com/langgenius/dify / createHttpClient

Function createHttpClient

cli/src/http/client.ts:276–348  ·  view source on GitHub ↗
(opts: ClientOptions)

Source from the content-addressed store, hash-verified

274}
275
276export 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

Callers 7

login.test.tsFile · 0.90
runLoginFunction · 0.90
runMethod · 0.90
buildAuthedContextFunction · 0.90
runCompatNudgeFunction · 0.90
defaultProbeFunction · 0.90
extendFunction · 0.70

Calls 2

compileStateFunction · 0.85
typedCallFunction · 0.85

Tested by

no test coverage detected