Przeglądaj źródła

Support basic auth in opencode sdk (#4697)

Andrew Joslin 2 miesięcy temu
rodzic
commit
b9253d0b3b
1 zmienionych plików z 13 dodań i 0 usunięć
  1. 13 0
      packages/sdk/js/src/client.ts

+ 13 - 0
packages/sdk/js/src/client.ts

@@ -24,6 +24,19 @@ export function createOpencodeClient(config?: Config & { directory?: string }) {
     }
   }
 
+  if (config?.baseUrl) {
+    const baseUrl = new URL(config.baseUrl)
+    if (baseUrl.username || baseUrl.password) {
+      config.headers = {
+        ...config.headers,
+        Authorization: `Basic ${btoa(`${baseUrl.username}:${baseUrl.password}`)}`,
+      }
+      baseUrl.username = ""
+      baseUrl.password = ""
+      config.baseUrl = baseUrl.toString()
+    }
+  }
+
   const client = createClient(config)
   return new OpencodeClient({ client })
 }