]> git.codecow.com Git - libnemo.git/commitdiff
Start scaffolding support for websocket. Provide read-only access to RPC info.
authorChris Duncan <chris@zoso.dev>
Fri, 26 Sep 2025 14:11:16 +0000 (07:11 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 26 Sep 2025 14:11:16 +0000 (07:11 -0700)
src/lib/rpc.ts

index 404c5a1e52cd0b5ef16efbfd7883ac9245bac154..0ab7e22b3cc43c6afcfab41e1602a1ec676ea8cf 100644 (file)
@@ -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,