summaryrefslogtreecommitdiffhomepage
path: root/cloud/web/src/pages/components/context-api.tsx
blob: 0a348f48f9c26668f9280699222b36d162ef3b7f (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
import { hc } from "hono/client"
import { ApiType } from "@opencode/cloud-function/src/gateway"
import { useWorkspace } from "./context-workspace"
import { useOpenAuth } from "../../components/context-openauth"

export function useApi() {
  const workspace = useWorkspace()
  const auth = useOpenAuth()
  return hc<ApiType>(import.meta.env.VITE_API_URL, {
    async fetch(...args: Parameters<typeof fetch>): Promise<Response> {
      const [input, init] = args
      const request = input instanceof Request ? input : new Request(input, init)
      const headers = new Headers(request.headers)
      headers.set("authorization", `Bearer ${await auth.access()}`)
      headers.set("x-opencode-workspace", workspace.id)
      return fetch(
        new Request(request, {
          ...init,
          headers,
        }),
      )
    },
  })
}