summaryrefslogtreecommitdiffhomepage
path: root/packages/sdk/js/src/client.ts
blob: 55cdeba1879c1aa408aeab0a2c7bd774217467f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export * from "./gen/types.gen.js"
export { type Config as OpencodeClientConfig, OpencodeClient }

import { createClient } from "./gen/client/client.gen.js"
import { type Config } from "./gen/client/types.gen.js"
import { OpencodeClient } from "./gen/sdk.gen.js"

export function createOpencodeClient(config?: Config & { directory?: string }) {
  if (!config?.fetch) {
    config = {
      ...config,
      fetch: (req) => {
        // @ts-ignore
        req.timeout = false
        return fetch(req)
      },
    }
  }

  if (config?.directory) {
    config.headers = {
      ...config.headers,
      "x-opencode-directory": config.directory,
    }
  }

  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 })
}