refactor(server): use JSON response for OpenAPI doc route (#26447)

This commit is contained in:
Kit Langton
2026-05-08 23:41:20 -04:00
committed by GitHub
parent f73a56c223
commit eadda11ec9

View File

@@ -155,13 +155,15 @@ const instanceRoutes = Layer.mergeAll(rawInstanceRoutes, instanceApiRoutes).pipe
]),
)
const openApiDocument = OpenApi.fromApi(PublicApi)
const openApiDocumentJson = JSON.stringify(openApiDocument)
// `OpenApi.fromApi` is non-trivial; defer until /doc is actually hit so
// processes that never serve it (CLI, scripts) don't pay at module load.
// `HttpServerResponse.jsonUnsafe` runs JSON.stringify eagerly, so caching
// the response also caches the serialized body — every /doc request reuses
// the same Uint8Array instead of re-stringifying the spec.
const docResponse = lazy(() => HttpServerResponse.jsonUnsafe(OpenApi.fromApi(PublicApi)))
const docRoute = HttpRouter.use((router) =>
router.add("GET", "/doc", () =>
Effect.succeed(HttpServerResponse.text(openApiDocumentJson, { headers: { "content-type": "application/json" } })),
),
router.add("GET", "/doc", () => Effect.succeed(docResponse())),
).pipe(Layer.provide(authOnlyRouterLayer))
const uiRoute = HttpRouter.use((router) =>