project.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { Hono } from "hono"
  2. import { describeRoute, validator } from "hono-openapi"
  3. import { resolver } from "hono-openapi"
  4. import { Instance } from "../project/instance"
  5. import { Project } from "../project/project"
  6. import z from "zod"
  7. import { errors } from "./error"
  8. export const ProjectRoute = new Hono()
  9. .get(
  10. "/",
  11. describeRoute({
  12. summary: "List all projects",
  13. description: "Get a list of projects that have been opened with OpenCode.",
  14. operationId: "project.list",
  15. responses: {
  16. 200: {
  17. description: "List of projects",
  18. content: {
  19. "application/json": {
  20. schema: resolver(Project.Info.array()),
  21. },
  22. },
  23. },
  24. },
  25. }),
  26. async (c) => {
  27. const projects = await Project.list()
  28. return c.json(projects)
  29. },
  30. )
  31. .get(
  32. "/current",
  33. describeRoute({
  34. summary: "Get current project",
  35. description: "Retrieve the currently active project that OpenCode is working with.",
  36. operationId: "project.current",
  37. responses: {
  38. 200: {
  39. description: "Current project information",
  40. content: {
  41. "application/json": {
  42. schema: resolver(Project.Info),
  43. },
  44. },
  45. },
  46. },
  47. }),
  48. async (c) => {
  49. return c.json(Instance.project)
  50. },
  51. )
  52. .patch(
  53. "/:projectID",
  54. describeRoute({
  55. summary: "Update project",
  56. description: "Update project properties such as name, icon and color.",
  57. operationId: "project.update",
  58. responses: {
  59. 200: {
  60. description: "Updated project information",
  61. content: {
  62. "application/json": {
  63. schema: resolver(Project.Info),
  64. },
  65. },
  66. },
  67. ...errors(400, 404),
  68. },
  69. }),
  70. validator("param", z.object({ projectID: z.string() })),
  71. validator("json", Project.update.schema.omit({ projectID: true })),
  72. async (c) => {
  73. const projectID = c.req.valid("param").projectID
  74. const body = c.req.valid("json")
  75. const project = await Project.update({ ...body, projectID })
  76. return c.json(project)
  77. },
  78. )