add experimental file HttpApi read slice
Move the shared file DTOs to Effect Schema, add a parallel experimental file HttpApi surface for list/content/status, and cover the new read-only slice with a server test.
This commit is contained in:
@@ -126,7 +126,7 @@ export const FileRoutes = lazy(() =>
|
||||
description: "Files and directories",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(File.Node.array()),
|
||||
schema: resolver(z.array(File.Node.zod)),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -159,7 +159,7 @@ export const FileRoutes = lazy(() =>
|
||||
description: "File content",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(File.Content),
|
||||
schema: resolver(File.Content.zod),
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -192,7 +192,7 @@ export const FileRoutes = lazy(() =>
|
||||
description: "File status",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: resolver(File.Info.array()),
|
||||
schema: resolver(z.array(File.Info.zod)),
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
import { AppLayer } from "@/effect/app-runtime"
|
||||
import { memoMap } from "@/effect/run-service"
|
||||
import { File } from "@/file"
|
||||
import { lazy } from "@/util/lazy"
|
||||
import { Effect, Layer, Schema } from "effect"
|
||||
import { HttpRouter, HttpServer } from "effect/unstable/http"
|
||||
import { HttpApi, HttpApiBuilder, HttpApiEndpoint, HttpApiGroup, OpenApi } from "effect/unstable/httpapi"
|
||||
import type { Handler } from "hono"
|
||||
|
||||
const root = "/experimental/httpapi/file"
|
||||
|
||||
const Api = HttpApi.make("file")
|
||||
.add(
|
||||
HttpApiGroup.make("file")
|
||||
.add(
|
||||
HttpApiEndpoint.get("list", root, {
|
||||
query: { path: Schema.optional(Schema.String) },
|
||||
success: Schema.Array(File.Node),
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "file.list",
|
||||
summary: "List files",
|
||||
description: "List files and directories in a specified path.",
|
||||
}),
|
||||
),
|
||||
HttpApiEndpoint.get("content", `${root}/content`, {
|
||||
query: { path: Schema.String },
|
||||
success: File.Content,
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "file.read",
|
||||
summary: "Read file",
|
||||
description: "Read the content of a specified file.",
|
||||
}),
|
||||
),
|
||||
HttpApiEndpoint.get("status", `${root}/status`, {
|
||||
success: Schema.Array(File.Info),
|
||||
}).annotateMerge(
|
||||
OpenApi.annotations({
|
||||
identifier: "file.status",
|
||||
summary: "Get file status",
|
||||
description: "Get the git status of all files in the project.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
.annotateMerge(
|
||||
OpenApi.annotations({
|
||||
title: "file",
|
||||
description: "Experimental HttpApi file routes.",
|
||||
}),
|
||||
),
|
||||
)
|
||||
.annotateMerge(
|
||||
OpenApi.annotations({
|
||||
title: "opencode experimental HttpApi",
|
||||
version: "0.0.1",
|
||||
description: "Experimental HttpApi surface for selected instance routes.",
|
||||
}),
|
||||
)
|
||||
|
||||
const list = Effect.fn("FileHttpApi.list")(function* (ctx: { query: { path?: string } }) {
|
||||
const svc = yield* File.Service
|
||||
return Schema.decodeUnknownSync(Schema.Array(File.Node))(yield* svc.list(ctx.query.path))
|
||||
})
|
||||
|
||||
const content = Effect.fn("FileHttpApi.content")(function* (ctx: { query: { path: string } }) {
|
||||
const svc = yield* File.Service
|
||||
return Schema.decodeUnknownSync(File.Content)(yield* svc.read(ctx.query.path))
|
||||
})
|
||||
|
||||
const status = Effect.fn("FileHttpApi.status")(function* () {
|
||||
const svc = yield* File.Service
|
||||
return Schema.decodeUnknownSync(Schema.Array(File.Info))(yield* svc.status())
|
||||
})
|
||||
|
||||
const FileLive = HttpApiBuilder.group(Api, "file", (handlers) =>
|
||||
handlers.handle("list", list).handle("content", content).handle("status", status),
|
||||
)
|
||||
|
||||
const web = lazy(() =>
|
||||
HttpRouter.toWebHandler(
|
||||
Layer.mergeAll(
|
||||
AppLayer,
|
||||
HttpApiBuilder.layer(Api, { openapiPath: `${root}/doc` }).pipe(
|
||||
Layer.provide(FileLive),
|
||||
Layer.provide(HttpServer.layerServices),
|
||||
),
|
||||
),
|
||||
{
|
||||
disableLogger: true,
|
||||
memoMap,
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
export const FileHttpApiHandler: Handler = (c, _next) => web().handler(c.req.raw)
|
||||
@@ -1,7 +1,12 @@
|
||||
import { lazy } from "@/util/lazy"
|
||||
import { Hono } from "hono"
|
||||
import { FileHttpApiHandler } from "./file"
|
||||
import { QuestionHttpApiHandler } from "./question"
|
||||
|
||||
export const HttpApiRoutes = lazy(() =>
|
||||
new Hono().all("/question", QuestionHttpApiHandler).all("/question/*", QuestionHttpApiHandler),
|
||||
new Hono()
|
||||
.all("/question", QuestionHttpApiHandler)
|
||||
.all("/question/*", QuestionHttpApiHandler)
|
||||
.all("/file", FileHttpApiHandler)
|
||||
.all("/file/*", FileHttpApiHandler),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user