MCPcopy Create free account
hub / github.com/wevm/viem / getHttpRpcClient

Function getHttpRpcClient

src/utils/rpc/http.ts:92–208  ·  view source on GitHub ↗
(
  url_: string,
  options: HttpRpcClientOptions = {},
)

Source from the content-addressed store, hash-verified

90}
91
92export function getHttpRpcClient(
93 url_: string,
94 options: HttpRpcClientOptions = {},
95): HttpRpcClient {
96 const { url, headers: headers_url } = parseUrl(url_)
97
98 return {
99 async request(params) {
100 const {
101 body,
102 fetchFn = options.fetchFn ?? fetch,
103 maxResponseBodySize = options.maxResponseBodySize ??
104 defaultMaxResponseBodySize,
105 onRequest = options.onRequest,
106 onResponse = options.onResponse,
107 timeout = options.timeout ?? 10_000,
108 } = params
109
110 const fetchOptions = {
111 ...(options.fetchOptions ?? {}),
112 ...(params.fetchOptions ?? {}),
113 }
114
115 const { headers, method, signal: signal_ } = fetchOptions
116
117 try {
118 const response = await withTimeout(
119 async ({ signal }) => {
120 const init: RequestInit = {
121 ...fetchOptions,
122 body: Array.isArray(body)
123 ? stringify(
124 body.map((body) => ({
125 jsonrpc: '2.0',
126 id: body.id ?? idCache.take(),
127 ...body,
128 })),
129 )
130 : stringify({
131 jsonrpc: '2.0',
132 id: body.id ?? idCache.take(),
133 ...body,
134 }),
135 headers: {
136 ...headers_url,
137 'Content-Type': 'application/json',
138 ...headers,
139 },
140 method: method || 'POST',
141 signal: signal_ || (timeout > 0 ? signal : null),
142 }
143 const request = new Request(url, init)
144 const args = (await onRequest?.(request, init)) ?? { ...init, url }
145 const response = await fetchFn(args.url ?? url, args)
146 return response
147 },
148 {
149 errorInstance: new TimeoutError({ body, url }),

Callers 10

chains.test.tsFile · 0.85
requestFunction · 0.85
requestFunction · 0.85
requestFunction · 0.85
requestFunction · 0.85
requestFunction · 0.85
http.test.tsFile · 0.85
httpFunction · 0.85
httpFunction · 0.85
requestFunction · 0.85

Calls 1

parseUrlFunction · 0.85

Tested by 6

requestFunction · 0.68
requestFunction · 0.68
requestFunction · 0.68
requestFunction · 0.68
requestFunction · 0.68
requestFunction · 0.68