]> git.codecow.com Git - libnemo.git/commitdiff
Refactor environment parsing for RPC values.
authorChris Duncan <chris@codecow.com>
Thu, 6 Aug 2026 10:04:57 +0000 (03:04 -0700)
committerChris Duncan <chris@codecow.com>
Thu, 6 Aug 2026 10:04:57 +0000 (03:04 -0700)
sample.env
sample.env.mjs
src/lib/rpc/index.ts

index b1f6ab24831b19c2c11cd6c5da9ea71e27e547c8..229237e216387b973782c5db11e47204b9875f60 100644 (file)
@@ -1,6 +1,22 @@
 # SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 # SPDX-License-Identifier: GPL-3.0-or-later
 
-# Save this file as `.env` and replace the following with real values
-LIBNEMO_RPC_URL="https://rpc.example.com"
-LIBNEMO_RPC_AUTHORIZATION="fedcba9876543210fedcba9876543210"
+## Save this file as `.env` and replace the examples with real values.
+
+## Specify a valid URL pointing to a live Nano node.
+##
+LIBNEMO_RPC_URL='https://rpc.example.com'
+
+## Pass a plain string to use the default "Authorization" HTTP header. Examples:
+##
+## 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE='
+## 'Bearer fedcba9876543210fedcba9876543210'
+##
+## Alternatively, pass a JSON string to use a custom header for authentication.
+## The expected JSON format is as follows:
+##
+## '{ "header": "custom-auth-header", "value": "fedcba9876543210fedcba9876543210" }'
+##
+## Replace the example values for `"header"` and `"value"` with your own.
+##
+LIBNEMO_RPC_AUTHORIZATION='Bearer fedcba9876543210fedcba9876543210'
index 1b61f5f2243f2b8354fd74d125642fc3017feaeb..dde66e3ebaa8a16e7eed8b4e8a358d3e9d750abc 100644 (file)
@@ -1,10 +1,24 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-'use strict'
-
-// Save this file as `env.mjs` and replace the following with real values
+// Save this file as `env.mjs` and replace the examples with real values
 export const env = {
-       LIBNEMO_RPC_URL: "https://rpc.example.com",
-       LIBNEMO_RPC_AUTHORIZATION: "fedcba9876543210fedcba9876543210"
+
+       // Specify a valid URL pointing to a live Nano node.
+       //
+       LIBNEMO_RPC_URL: 'https://rpc.example.com',
+
+       // Pass a string to use the default "Authorization" HTTP header. Examples:
+       //
+       // 'Basic dXNlckBleGFtcGxlLmNvbTpwYXNzd29yZDEyMyE='
+       // 'Bearer fedcba9876543210fedcba9876543210'
+       //
+       // Alternatively, pass an object to use a custom header for authentication.
+       // The expected format is as follows:
+       //
+       // { header: "custom-auth-header", value: "fedcba9876543210fedcba9876543210" }
+       //
+       // Replace the example values for`header` and `value` with your own.
+       //
+       LIBNEMO_RPC_AUTHORIZATION: 'Bearer fedcba9876543210fedcba9876543210'
 }
index c38c2ad1b02548096db4ff334e44b6199a3f310e..afe1767c59f0aa8bd9fdd824f662e1354aa35710 100644 (file)
@@ -15,13 +15,21 @@ export { Schema }
  */
 export class Rpc {
        #url: URL
-       #auth?: string
+       #auth?: string | { header: string, value: string }
 
        /**
         * @param {(string|URL)} url Nano network node
-        * @param {string} [auth] optional API key
+        * @param {string} [auth] "Authorization" token HTTP header for an API key
         */
-       constructor (url: string | URL, auth?: string) {
+       constructor (url: string | URL, auth?: string)
+       /**
+        * @param {(string|URL)} url Nano network node
+        * @param {object} [auth] custom HTTP header for an API key
+        * @param {string} auth.header custom authorization HTTP header name
+        * @param {string} auth.value authorization token value
+        */
+       constructor (url: string | URL, auth?: { header: string, value: string })
+       constructor (url: string | URL, auth?: string | { header: string, value: string }) {
                this.#url = new URL(url)
                this.#url.protocol = 'https:'
                this.#auth = auth
@@ -42,8 +50,13 @@ export class Rpc {
                        'Content-Type': 'application/json'
                }
                const auth = this.#auth ?? env?.LIBNEMO_RPC_AUTHORIZATION
-               if (auth) {
-                       headers['Authorization'] = auth
+               if (typeof auth === 'string') {
+                       const token = /^\{.*\}$/.test(auth.trim()) ? JSON.parse(auth) : auth
+                       headers['Authorization'] = token
+               } else if (typeof auth?.header === 'string' && typeof auth.value === 'string') {
+                       headers[auth.header] = auth.value
+               } else {
+                       console.warn('Unrecognized RPC authorization format')
                }
                if (data !== undefined && typeof data !== 'object') {
                        throw new TypeError('Invalid RPC post data')