viem
Version:
121 lines • 5.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHttpRpcClient = getHttpRpcClient;
exports.parseUrl = parseUrl;
const request_js_1 = require("../../errors/request.js");
const utils_js_1 = require("../../errors/utils.js");
const withTimeout_js_1 = require("../promise/withTimeout.js");
const stringify_js_1 = require("../stringify.js");
const id_js_1 = require("./id.js");
function getHttpRpcClient(url_, options = {}) {
const { url, headers: headers_url } = parseUrl(url_);
return {
async request(params) {
const { body, fetchFn = options.fetchFn ?? fetch, onRequest = options.onRequest, onResponse = options.onResponse, timeout = options.timeout ?? 10_000, } = params;
const fetchOptions = {
...(options.fetchOptions ?? {}),
...(params.fetchOptions ?? {}),
};
const { headers, method, signal: signal_ } = fetchOptions;
try {
const response = await (0, withTimeout_js_1.withTimeout)(async ({ signal }) => {
const init = {
...fetchOptions,
body: Array.isArray(body)
? (0, stringify_js_1.stringify)(body.map((body) => ({
jsonrpc: '2.0',
id: body.id ?? id_js_1.idCache.take(),
...body,
})))
: (0, stringify_js_1.stringify)({
jsonrpc: '2.0',
id: body.id ?? id_js_1.idCache.take(),
...body,
}),
headers: {
...headers_url,
'Content-Type': 'application/json',
...headers,
},
method: method || 'POST',
signal: signal_ || (timeout > 0 ? signal : null),
};
const request = new Request(url, init);
const args = (await onRequest?.(request, init)) ?? { ...init, url };
const response = await fetchFn(args.url ?? url, args);
return response;
}, {
errorInstance: new request_js_1.TimeoutError({ body, url }),
timeout,
signal: true,
});
if (onResponse)
await onResponse(response);
let data;
if (response.headers.get('Content-Type')?.startsWith('application/json'))
data = await response.json();
else {
data = await response.text();
try {
data = JSON.parse(data || '{}');
}
catch (err) {
if (response.ok)
throw err;
data = { error: data };
}
}
if (!response.ok) {
if (typeof data.error?.code === 'number' &&
typeof data.error?.message === 'string')
return data;
throw new request_js_1.HttpRequestError({
body,
details: (0, stringify_js_1.stringify)(data.error) || response.statusText,
headers: response.headers,
status: response.status,
url,
});
}
return data;
}
catch (err) {
if (signal_?.aborted)
throw (0, utils_js_1.getAbortError)(signal_);
if ((0, utils_js_1.isAbortError)(err))
throw err;
if (err instanceof request_js_1.HttpRequestError)
throw err;
if (err instanceof request_js_1.TimeoutError)
throw err;
throw new request_js_1.HttpRequestError({
body,
cause: err,
url,
});
}
},
};
}
function parseUrl(url_) {
try {
const url = new URL(url_);
const result = (() => {
if (url.username) {
const credentials = `${decodeURIComponent(url.username)}:${decodeURIComponent(url.password)}`;
url.username = '';
url.password = '';
return {
url: url.toString(),
headers: { Authorization: `Basic ${btoa(credentials)}` },
};
}
return;
})();
return { url: url.toString(), ...result };
}
catch {
return { url: url_ };
}
}
//# sourceMappingURL=http.js.map