@roochnetwork/rooch-sdk
Version:
145 lines (144 loc) • 5.42 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
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 httpTransport_exports = {};
__export(httpTransport_exports, {
RoochHTTPTransport: () => RoochHTTPTransport
});
module.exports = __toCommonJS(httpTransport_exports);
var import_error = require("./error.js");
var import_sseTransport = require("./sseTransport.js");
var import_wsTransport = require("./wsTransport.js");
var _requestId, _options, _websocketClient, _sseClient, _getRoochSSETransport, getRoochSSETransport_fn, _getWebsocketClient, getWebsocketClient_fn;
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 import_error.RoochHTTPStatusError(
`Unexpected status code: ${res.status}`,
res.status,
res.statusText
);
}
const data = await res.json();
if ("error" in data && data.error != null) {
throw new import_error.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 import_sseTransport.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 import_wsTransport.WebsocketClient(
__privateGet(this, _options).websocket?.url ?? __privateGet(this, _options).url,
{
WebSocketConstructor,
...__privateGet(this, _options).websocket
}
));
}
return __privateGet(this, _websocketClient);
};
//# sourceMappingURL=httpTransport.js.map