/**
* @since 1.0.0
*/
import * as Effect from "effect/Effect"
import * as Layer from "effect/Layer"
import { Api } from "./HttpApi.js"
import type * as HttpApi from "./HttpApi.js"
import { Router } from "./HttpApiBuilder.js"
import * as HttpLayerRouter from "./HttpLayerRouter.js"
import * as HttpServerResponse from "./HttpServerResponse.js"
import * as Html from "./internal/html.js"
import * as internal from "./internal/httpApiSwagger.js"
import * as OpenApi from "./OpenApi.js"
const makeHandler = (options: {
readonly api: HttpApi.HttpApi.Any
}) => {
const spec = OpenApi.fromApi(options.api as any)
const response = HttpServerResponse.html(`
${Html.escape(spec.info.title)} Documentation
`)
return Effect.succeed(response)
}
/**
* Exported layer mounting Swagger/OpenAPI documentation UI.
*
* @param options.path Optional mount path (default "/docs").
*
* @since 1.0.0
* @category layers
*/
export const layer = (options?: {
readonly path?: `/${string}` | undefined
}): Layer.Layer =>
Router.use((router) =>
Effect.gen(function*() {
const { api } = yield* Api
const handler = makeHandler({ api })
yield* router.get(options?.path ?? "/docs", handler)
})
)
/**
* @since 1.0.0
* @category layers
*/
export const layerHttpLayerRouter: (
options: {
readonly api: HttpApi.HttpApi.Any
readonly path: `/${string}`
}
) => Layer.Layer<
never,
never,
HttpLayerRouter.HttpRouter
> = Effect.fnUntraced(function*(options: {
readonly api: HttpApi.HttpApi.Any
readonly path: `/${string}`
}) {
const router = yield* HttpLayerRouter.HttpRouter
const handler = makeHandler(options)
yield* router.add("GET", options.path, handler)
}, Layer.effectDiscard)