From 2dd2495c175c877fadb16e5c74290a2b9f68281f Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 26 Sep 2025 07:11:16 -0700 Subject: [PATCH] Start scaffolding support for websocket. Provide read-only access to RPC info. --- src/lib/rpc.ts | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/lib/rpc.ts b/src/lib/rpc.ts index 404c5a1..0ab7e22 100644 --- a/src/lib/rpc.ts +++ b/src/lib/rpc.ts @@ -9,17 +9,26 @@ * other value will be changed automatically. */ export class Rpc { - #u: URL - #n?: string + #apiUrl: URL + #apiKeyName?: string + #wsUrl?: URL + + get apiUrl (): URL { return this.#apiUrl } + get apiKeyName (): string | undefined { return this.#apiKeyName } + get websocketUrl (): URL | undefined { return this.#wsUrl } /** - * @param {(string|URL)} url + * @param {(string | URL)} url * @param {string} [apiKeyName] */ - constructor (url: string | URL, apiKeyName?: string) { - this.#u = new URL(url) - this.#u.protocol = 'https:' - this.#n = apiKeyName + constructor (apiUrl: string | URL, apiKeyName?: string, websocketUrl?: string | URL) { + this.#apiUrl = new URL(apiUrl) + this.#apiUrl.protocol = 'https:' + this.#apiKeyName = apiKeyName + if (websocketUrl != null) { + this.#wsUrl = new URL(websocketUrl) + this.#wsUrl.protocol = 'wss:' + } } /** @@ -34,8 +43,8 @@ export class Rpc { this.#validate(action) const headers: { [key: string]: string } = {} headers['Content-Type'] = 'application/json' - if (this.#n && process?.env?.LIBNEMO_RPC_API_KEY) { - headers[this.#n] = process.env.LIBNEMO_RPC_API_KEY + if (this.#apiKeyName && process?.env?.LIBNEMO_RPC_API_KEY) { + headers[this.#apiKeyName] = process.env.LIBNEMO_RPC_API_KEY } data ??= {} @@ -47,7 +56,7 @@ export class Rpc { .replaceAll('\\', '\\u005c') const aborter = new AbortController() - const req = new Request(this.#u, { + const req = new Request(this.#apiUrl, { signal: aborter.signal, method: 'POST', headers, -- 2.47.3