@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
120 lines (119 loc) • 5 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __typeError = (msg) => {
throw TypeError(msg);
};
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) => member.has(obj) || __typeError("Cannot " + msg);
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
var __privateAdd = (obj, member, value) => member.has(obj) ? __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), value);
var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
var http_transport_exports = {};
__export(http_transport_exports, {
SuiHTTPTransport: () => SuiHTTPTransport
});
module.exports = __toCommonJS(http_transport_exports);
var import_version = require("../version.js");
var import_errors = require("./errors.js");
var import_rpc_websocket_client = require("./rpc-websocket-client.js");
var _requestId, _options, _websocketClient, _SuiHTTPTransport_instances, getWebsocketClient_fn;
class SuiHTTPTransport {
constructor(options) {
__privateAdd(this, _SuiHTTPTransport_instances);
__privateAdd(this, _requestId, 0);
__privateAdd(this, _options);
__privateAdd(this, _websocketClient);
__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 SuiHTTPTransport."
);
}
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",
signal: input.signal,
headers: {
"Content-Type": "application/json",
"Client-Sdk-Type": "typescript",
"Client-Sdk-Version": import_version.PACKAGE_VERSION,
"Client-Target-Api-Version": import_version.TARGETED_RPC_VERSION,
"Client-Request-Method": input.method,
...__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_errors.SuiHTTPStatusError(
`Unexpected status code: ${res.status}`,
res.status,
res.statusText
);
}
const data = await res.json();
if ("error" in data && data.error != null) {
throw new import_errors.JsonRpcError(data.error.message, data.error.code);
}
return data.result;
}
async subscribe(input) {
const unsubscribe = await __privateMethod(this, _SuiHTTPTransport_instances, getWebsocketClient_fn).call(this).subscribe(input);
if (input.signal) {
input.signal.throwIfAborted();
input.signal.addEventListener("abort", () => {
unsubscribe();
});
}
return async () => !!await unsubscribe();
}
}
_requestId = new WeakMap();
_options = new WeakMap();
_websocketClient = new WeakMap();
_SuiHTTPTransport_instances = 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_rpc_websocket_client.WebsocketClient(
__privateGet(this, _options).websocket?.url ?? __privateGet(this, _options).url,
{
WebSocketConstructor,
...__privateGet(this, _options).websocket
}
));
}
return __privateGet(this, _websocketClient);
};
//# sourceMappingURL=http-transport.js.map
;