summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSadie Powell <sadie@witchery.services>2025-02-26 17:16:57 +0000
committerSadie Powell <sadie@witchery.services>2025-02-26 17:18:03 +0000
commitfb03d745c5c7fab4a1e97ea0cbd944629ecc2584 (patch)
tree144b977985cc80b9597c97098528a0f7813f250f
parent752f5e269e99acdd0c2795a2c2eedc44875d5fed (diff)
Add a JavaScript library for accessing the JSON-RPC API.
-rw-r--r--docs/RPC/jsonrpc.js131
1 files changed, 131 insertions, 0 deletions
diff --git a/docs/RPC/jsonrpc.js b/docs/RPC/jsonrpc.js
new file mode 100644
index 000000000..29291ad7d
--- /dev/null
+++ b/docs/RPC/jsonrpc.js
@@ -0,0 +1,131 @@
+// SPDX-License-Identifier: CC0-1.0
+"use strict";
+
+/** Implements methods for accessing an Anope JSON-RPC server. */
+class AnopeRPC {
+
+ /**
+ * Initializes a new AnopeRPC instance with the specified RPC host.
+ *
+ * @param {string} The RPC host base URL.
+ */
+ constructor(host) {
+ this.host = host;
+ }
+
+ /**
+ * Executes an arbitrary RPC query.
+ *
+ * @param {string} method The name of the method to execute.
+ * @param {...*} params The parameters pass to the method.
+ * @returns {*} The result of the RPC query.
+ */
+ async run(method, ...params) {
+ const request = JSON.stringify({
+ "jsonrpc": "2.0",
+ "method": method,
+ "params": params,
+ "id": Math.random().toString(36).slice(2)
+ });
+ const response = await fetch(this.host, {
+ method: 'POST',
+ body: request
+ });
+ if (!response.ok) {
+ throw new Error(`HTTP returned ${response.status}`)
+ }
+ const json = await response.json();
+ if ("error" in json) {
+ throw new Error(`JSON-RPC returned ${json.error.code}: ${json.error.message}`)
+ }
+ if ("result" in json) {
+ return json.result;
+ }
+ return null;
+ }
+
+ /**
+ * Retrieves a list of channels.
+ *
+ * @returns {array} An array of channel names.
+ */
+ listChannels() {
+ return this.run("anope.listChannels");
+ }
+
+ /**
+ * Retrieves information about the specified channel.
+ *
+ * @param {string} name The name of the channel.
+ * @returns {object} An object containing information about the channel.
+ */
+ channel(name) {
+ return this.run("anope.channel", name);
+ }
+
+ /**
+ * Retrieves a list of services operators.
+ *
+ * @returns {array} An array of channel names.
+ */
+ listOpers() {
+ return this.run("anope.listOpers");
+ }
+
+ /**
+ * Retrieves information about the specified services operator.
+ *
+ * @param {string} name The name of the services operator.
+ * @returns {object} An object containing information about the services operator.
+ */
+ oper(name) {
+ return this.run("anope.oper", name);
+ }
+
+ /**
+ * Retrieves a list of servers.
+ *
+ * @returns {array} An array of servers names.
+ */
+ listServers() {
+ return this.run("anope.listServers");
+ }
+
+ /**
+ * Retrieves information about the specified server.
+ *
+ * @param {string} name The name of the server.
+ * @returns {object} An object containing information about the server.
+ */
+ server(name) {
+ return this.run("anope.server", name);
+ }
+
+ /**
+ * Retrieves a list of users.
+ *
+ * @returns {array} An array of channel names.
+ */
+ listUsers() {
+ return this.run("anope.listUsers");
+ }
+
+ /**
+ * Retrieves information about the specified user.
+ *
+ * @param {string} nick The nick of the user.
+ * @returns {object} An object containing information about the user.
+ */
+ user(nick) {
+ return this.run("anope.user", nick);
+ }
+}
+
+/*
+const arpc = new AnopeRPC("http://127.0.0.1:8080/jsonrpc");
+arpc.listServers().then(servers => {
+ console.log(servers);
+}).catch (error => {
+ console.log(error);
+});
+*/