@roochnetwork/rooch-sdk
Version:
125 lines (124 loc) • 4.43 kB
JavaScript
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateGet = (obj, member, getter) => {
__accessCheck(obj, member, "read from private field");
return getter ? getter.call(obj) : member.get(obj);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateSet = (obj, member, value, setter) => {
__accessCheck(obj, member, "write to private field");
setter ? setter.call(obj, value) : member.set(obj, value);
return value;
};
var __privateMethod = (obj, member, method) => {
__accessCheck(obj, member, "access private method");
return method;
};
var _requestId, _options, _websocketClient, _sseClient, _getRoochSSETransport, getRoochSSETransport_fn, _getWebsocketClient, getWebsocketClient_fn;
import { JsonRpcError, RoochHTTPStatusError } from "./error.js";
import { SSEClient } from "./sseTransport.js";
import { WebsocketClient } from "./wsTransport.js";
class RoochHTTPTransport {
constructor(options) {
__privateAdd(this, _getRoochSSETransport);
__privateAdd(this, _getWebsocketClient);
__privateAdd(this, _requestId, 0);
__privateAdd(this, _options, void 0);
__privateAdd(this, _websocketClient, void 0);
__privateAdd(this, _sseClient, void 0);
__privateSet(this, _options, options);
}
fetch(input, init) {
const fetchFn = __privateGet(this, _options).fetch ?? fetch;
if (!fetchFn) {
throw new Error(
"The current environment does not support fetch, you can provide a fetch implementation in the options for RoochHTTPTransport."
);
}
return fetchFn(input, init);
}
async request(input) {
__privateSet(this, _requestId, __privateGet(this, _requestId) + 1);
const res = await this.fetch(__privateGet(this, _options).rpc?.url ?? __privateGet(this, _options).url, {
method: "POST",
headers: {
"Content-Type": "application/json",
...__privateGet(this, _options).rpc?.headers
},
body: JSON.stringify({
jsonrpc: "2.0",
id: __privateGet(this, _requestId),
method: input.method,
params: input.params
})
});
if (!res.ok) {
throw new RoochHTTPStatusError(
`Unexpected status code: ${res.status}`,
res.status,
res.statusText
);
}
const data = await res.json();
if ("error" in data && data.error != null) {
throw new JsonRpcError(data.error.message, data.error.code);
}
return data.result;
}
async subscribeWithSSE(input) {
const unsubscribe = await __privateMethod(this, _getRoochSSETransport, getRoochSSETransport_fn).call(this).subscribe(input);
return async () => !!await unsubscribe();
}
async subscribe(input) {
const unsubscribe = await __privateMethod(this, _getWebsocketClient, getWebsocketClient_fn).call(this).subscribe(input);
if (input.signal) {
input.signal.throwIfAborted();
input.signal.addEventListener("abort", () => {
unsubscribe();
});
}
return async () => !!await unsubscribe();
}
destroy() {
}
}
_requestId = new WeakMap();
_options = new WeakMap();
_websocketClient = new WeakMap();
_sseClient = new WeakMap();
_getRoochSSETransport = new WeakSet();
getRoochSSETransport_fn = function() {
if (!__privateGet(this, _sseClient)) {
__privateSet(this, _sseClient, new SSEClient(__privateGet(this, _options).url));
}
return __privateGet(this, _sseClient);
};
_getWebsocketClient = new WeakSet();
getWebsocketClient_fn = function() {
if (!__privateGet(this, _websocketClient)) {
const WebSocketConstructor = __privateGet(this, _options).WebSocketConstructor ?? WebSocket;
if (!WebSocketConstructor) {
throw new Error(
"The current environment does not support WebSocket, you can provide a WebSocketConstructor in the options for SuiHTTPTransport."
);
}
__privateSet(this, _websocketClient, new WebsocketClient(
__privateGet(this, _options).websocket?.url ?? __privateGet(this, _options).url,
{
WebSocketConstructor,
...__privateGet(this, _options).websocket
}
));
}
return __privateGet(this, _websocketClient);
};
export {
RoochHTTPTransport
};
//# sourceMappingURL=httpTransport.js.map