UNPKG

@azure-utils/storybooks

Version:

Utils to upload and manage Storybooks via Azure Functions and storage.

137 lines (134 loc) 4.58 kB
import { DocumentLayout, __toESM, getStore, require_jsx_runtime, responseHTML, urlBuilder } from "./store-BL4RNiEp.mjs"; import { CACHE_CONTROL_PUBLIC_WEEK, CONTENT_TYPES, commonErrorResponses } from "./constants-CsV1N9r4.mjs"; import { getMimeType } from "./mime-utils-CD98LBMP.mjs"; import { ProjectModel } from "./projects-DDVy3_77.mjs"; import { openAPITags, registerOpenAPIPath } from "./openapi-utils-CAJ85ahl.mjs"; import { joinUrl } from "./url-utils-B9Pl4bQ7.mjs"; import { ProjectsTable } from "./projects-table-B_qW44SW.mjs"; import { openAPIHandler } from "./openapi-handler-BNcJ1aSZ.mjs"; import { app } from "@azure/functions"; import z from "zod"; import fs from "node:fs"; import path from "node:path"; //#region src/handlers/web-ui-handlers.tsx var import_jsx_runtime = __toESM(require_jsx_runtime()); async function rootHandler(_request, context) { context.log("Serving SB root..."); const { connectionString, openapi } = getStore(); const projectModel = new ProjectModel(context, connectionString); const projects = await projectModel.list(); return responseHTML(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DocumentLayout, { title: "Home", toolbar: !openapi?.disabled ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: urlBuilder.root("openapi"), target: "_blank", children: "OpenAPI" }) : null, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ProjectsTable, { projects, toolbar: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: urlBuilder.allProjects(), children: "View all" }) }) })); } async function staticFileHandler(request, context) { const { filepath = "" } = request.params; const { staticDirs } = getStore(); context.log("Serving static file '%s' from '%s' dirs...", filepath, staticDirs.join(",")); const staticFilepaths = staticDirs.map((dir) => path.join(path.relative(process.cwd(), dir), filepath)); const staticFilepath = staticFilepaths.find(fs.existsSync); if (!staticFilepath) return { status: 404 }; context.debug("Static file '%s' found.", staticFilepath); return { status: 200, body: fs.createReadStream(staticFilepath, { encoding: "utf8", autoClose: true }), headers: { "Content-Type": getMimeType(staticFilepath) || "application/octet-stream", "Cache-Control": CACHE_CONTROL_PUBLIC_WEEK } }; } //#endregion //#region src/routers/web-ui-router.ts const TAG = openAPITags.webUI.name; function registerWebUIRouter(options) { const { authLevel, baseRoute, basePathParamsSchema, handlerWrapper, openAPIEnabled, serviceName } = options; app.get(`${serviceName}-root`, { authLevel, route: joinUrl(baseRoute), handler: handlerWrapper(rootHandler, [{ resource: "ui", action: "read" }]) }); const healthRoute = joinUrl(baseRoute, "health"); app.get(`${serviceName}-health-check`, { route: healthRoute, handler: handlerWrapper(() => ({ status: 200 }), []) }); const staticFileRoute = joinUrl(baseRoute, "{**filepath}"); app.get(`${serviceName}-static-files`, { authLevel, route: staticFileRoute, handler: handlerWrapper(staticFileHandler, [{ resource: "ui", action: "read" }]) }); if (openAPIEnabled) { app.get(`${serviceName}-openapi`, { authLevel, route: joinUrl(baseRoute, "openapi"), handler: handlerWrapper(openAPIHandler, [{ resource: "openapi", action: "read" }]) }); registerOpenAPIPath(baseRoute, { get: { tags: [TAG], summary: "Render homepage", requestParams: { path: basePathParamsSchema }, responses: { ...commonErrorResponses, 200: { description: "Root endpoint", content: { [CONTENT_TYPES.HTML]: { example: "<!DOCTYPE html>" } } }, 303: { description: "Redirects to all projects", headers: { Location: { description: "The URL to redirect to", schema: { type: "string", format: "uri" } } } } } } }); registerOpenAPIPath(healthRoute, { get: { tags: [TAG], summary: "Health check", description: "Checks the health of the service.", requestParams: { path: basePathParamsSchema }, responses: { 200: { description: "Service is healthy." } } } }); } registerOpenAPIPath(staticFileRoute, { get: { tags: [TAG], summary: "Serve static files", requestParams: { path: basePathParamsSchema.extend({ "**filepath": z.string() }) }, responses: { 200: { description: "Static file served successfully." }, 404: { description: "File not found." } } } }); } //#endregion export { registerWebUIRouter }; //# sourceMappingURL=web-ui-router-zy-hkmhO.mjs.map