build.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bun
  2. import { fileURLToPath } from "url"
  3. const dir = fileURLToPath(new URL("..", import.meta.url))
  4. process.chdir(dir)
  5. import { $ } from "bun"
  6. import path from "path"
  7. import { createClient } from "@hey-api/openapi-ts"
  8. const opencode = path.resolve(dir, "../../opencode")
  9. await $`bun dev generate > ${dir}/openapi.json`.cwd(opencode)
  10. await createClient({
  11. input: "./openapi.json",
  12. output: {
  13. path: "./src/v2/gen",
  14. tsConfigPath: path.join(dir, "tsconfig.json"),
  15. clean: true,
  16. },
  17. plugins: [
  18. {
  19. name: "@hey-api/typescript",
  20. exportFromIndex: false,
  21. },
  22. {
  23. name: "@hey-api/sdk",
  24. instance: "OpencodeClient",
  25. exportFromIndex: false,
  26. auth: false,
  27. paramsStructure: "flat",
  28. },
  29. {
  30. name: "@hey-api/client-fetch",
  31. exportFromIndex: false,
  32. baseUrl: "http://localhost:4096",
  33. },
  34. ],
  35. })
  36. // Patch a @hey-api/openapi-ts codegen bug: SseFn incorrectly passes the
  37. // endpoint's TError into the second generic of ServerSentEventsResult, which
  38. // is the AsyncGenerator's TReturn slot. Iterator return values have nothing
  39. // to do with HTTP errors, and any consumer that calls `.return()` or returns
  40. // from a mock generator gets type-checked against the wrong shape. Drop the
  41. // arg so TReturn defaults to void.
  42. const sseTypesPath = "./src/v2/gen/client/types.gen.ts"
  43. const sseTypesFile = Bun.file(sseTypesPath)
  44. const sseTypesSource = await sseTypesFile.text()
  45. const sseTypesPatched = sseTypesSource.replace(
  46. "=> Promise<ServerSentEventsResult<TData, TError>>",
  47. "=> Promise<ServerSentEventsResult<TData>>",
  48. )
  49. if (sseTypesPatched === sseTypesSource) {
  50. throw new Error(`SseFn patch did not apply; @hey-api/openapi-ts output may have changed (${sseTypesPath})`)
  51. }
  52. await Bun.write(sseTypesPath, sseTypesPatched)
  53. await $`bun prettier --write src/gen`
  54. await $`bun prettier --write src/v2`
  55. await $`rm -rf dist`
  56. await $`bun tsc`
  57. await $`rm openapi.json`