UNPKG

@azure-utils/storybooks

Version:

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

1 lines 7.93 kB
{"version":3,"file":"web-ui-router-zy-hkmhO.mjs","names":["_request: HttpRequest","context: InvocationContext","request: HttpRequest","options: RouterOptions"],"sources":["../src/handlers/web-ui-handlers.tsx","../src/routers/web-ui-router.ts"],"sourcesContent":["import type {\n HttpRequest,\n HttpResponseInit,\n InvocationContext,\n} from \"@azure/functions\";\nimport { CACHE_CONTROL_PUBLIC_WEEK } from \"../utils/constants\";\nimport { getStore } from \"../utils/store\";\nimport path from \"node:path\";\nimport fs from \"node:fs\";\nimport { getMimeType } from \"../utils/mime-utils\";\nimport { responseHTML } from \"../utils/response-utils\";\nimport { DocumentLayout } from \"../components/layout\";\nimport { ProjectsTable } from \"../components/projects-table\";\nimport { ProjectModel } from \"../models/projects\";\nimport { urlBuilder } from \"../utils/url-builder\";\n\nexport async function rootHandler(\n _request: HttpRequest,\n context: InvocationContext\n): Promise<HttpResponseInit> {\n context.log(\"Serving SB root...\");\n const { connectionString, openapi } = getStore();\n const projectModel = new ProjectModel(context, connectionString);\n const projects = await projectModel.list();\n\n return responseHTML(\n <DocumentLayout\n title=\"Home\"\n toolbar={\n !openapi?.disabled ? (\n <a href={urlBuilder.root(\"openapi\")} target=\"_blank\">\n OpenAPI\n </a>\n ) : null\n }\n >\n <ProjectsTable\n projects={projects}\n toolbar={<a href={urlBuilder.allProjects()}>View all</a>}\n />\n </DocumentLayout>\n );\n}\n\nexport async function staticFileHandler(\n request: HttpRequest,\n context: InvocationContext\n): Promise<HttpResponseInit> {\n const { filepath = \"\" } = request.params;\n const { staticDirs } = getStore();\n context.log(\n \"Serving static file '%s' from '%s' dirs...\",\n filepath,\n staticDirs.join(\",\")\n );\n\n const staticFilepaths = staticDirs.map((dir) =>\n path.join(path.relative(process.cwd(), dir), filepath)\n );\n\n const staticFilepath = staticFilepaths.find(fs.existsSync);\n\n if (!staticFilepath) {\n return { status: 404 };\n }\n\n context.debug(\"Static file '%s' found.\", staticFilepath);\n\n return {\n status: 200,\n body: fs.createReadStream(staticFilepath, {\n encoding: \"utf8\",\n autoClose: true,\n }),\n headers: {\n \"Content-Type\": getMimeType(staticFilepath) || \"application/octet-stream\",\n \"Cache-Control\": CACHE_CONTROL_PUBLIC_WEEK,\n },\n };\n}\n","import { app } from \"@azure/functions\";\nimport { commonErrorResponses, CONTENT_TYPES } from \"../utils/constants\";\nimport { openAPITags, registerOpenAPIPath } from \"../utils/openapi-utils\";\nimport z from \"zod\";\nimport type { RouterOptions } from \"../utils/types\";\nimport { joinUrl } from \"../utils/url-utils\";\nimport { openAPIHandler } from \"../handlers/openapi-handler\";\nimport { rootHandler, staticFileHandler } from \"../handlers/web-ui-handlers\";\n\nconst TAG = openAPITags.webUI.name;\n\nexport function registerWebUIRouter(options: RouterOptions) {\n const {\n authLevel,\n baseRoute,\n basePathParamsSchema,\n handlerWrapper,\n openAPIEnabled,\n serviceName,\n } = options;\n\n app.get(`${serviceName}-root`, {\n authLevel,\n route: joinUrl(baseRoute),\n handler: handlerWrapper(rootHandler, [{ resource: \"ui\", action: \"read\" }]),\n });\n\n const healthRoute = joinUrl(baseRoute, \"health\");\n app.get(`${serviceName}-health-check`, {\n route: healthRoute,\n handler: handlerWrapper(() => ({ status: 200 }), []),\n });\n\n const staticFileRoute = joinUrl(baseRoute, \"{**filepath}\");\n app.get(`${serviceName}-static-files`, {\n authLevel,\n route: staticFileRoute,\n handler: handlerWrapper(staticFileHandler, [\n { resource: \"ui\", action: \"read\" },\n ]),\n });\n\n if (openAPIEnabled) {\n app.get(`${serviceName}-openapi`, {\n authLevel,\n route: joinUrl(baseRoute, \"openapi\"),\n handler: handlerWrapper(openAPIHandler, [\n { resource: \"openapi\", action: \"read\" },\n ]),\n });\n\n registerOpenAPIPath(baseRoute, {\n get: {\n tags: [TAG],\n summary: \"Render homepage\",\n requestParams: { path: basePathParamsSchema },\n responses: {\n ...commonErrorResponses,\n 200: {\n description: \"Root endpoint\",\n content: { [CONTENT_TYPES.HTML]: { example: \"<!DOCTYPE html>\" } },\n },\n 303: {\n description: \"Redirects to all projects\",\n headers: {\n Location: {\n description: \"The URL to redirect to\",\n schema: { type: \"string\", format: \"uri\" },\n },\n },\n },\n },\n },\n });\n\n registerOpenAPIPath(healthRoute, {\n get: {\n tags: [TAG],\n summary: \"Health check\",\n description: \"Checks the health of the service.\",\n requestParams: { path: basePathParamsSchema },\n responses: {\n 200: { description: \"Service is healthy.\" },\n },\n },\n });\n }\n\n registerOpenAPIPath(staticFileRoute, {\n get: {\n tags: [TAG],\n summary: \"Serve static files\",\n requestParams: {\n path: basePathParamsSchema.extend({ \"**filepath\": z.string() }),\n },\n responses: {\n 200: { description: \"Static file served successfully.\" },\n 404: { description: \"File not found.\" },\n },\n },\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;AAgBA,eAAsB,YACpBA,UACAC,SAC2B;CAC3B,QAAQ,IAAI,qBAAqB;CACjC,MAAM,EAAE,kBAAkB,SAAS,GAAG,UAAU;CAChD,MAAM,eAAe,IAAI,aAAa,SAAS;CAC/C,MAAM,WAAW,MAAM,aAAa,MAAM;AAE1C,QAAO,yDACJ;EACC,OAAM;EACN,SACE,CAAC,SAAS,uDACP;GAAE,MAAM,WAAW,KAAK,UAAU;GAAE,QAAO;aAAS;IAEjD,GACF;wDAGL;GACW;GACV,qDAAU;IAAE,MAAM,WAAW,aAAa;cAAE;KAAY;IACxD;GACa,CAClB;AACF;AAED,eAAsB,kBACpBC,SACAD,SAC2B;CAC3B,MAAM,EAAE,WAAW,IAAI,GAAG,QAAQ;CAClC,MAAM,EAAE,YAAY,GAAG,UAAU;CACjC,QAAQ,IACN,8CACA,UACA,WAAW,KAAK,IAAI,CACrB;CAED,MAAM,kBAAkB,WAAW,IAAI,CAAC,QACtC,KAAK,KAAK,KAAK,SAAS,QAAQ,KAAK,EAAE,IAAI,EAAE,SAAS,CACvD;CAED,MAAM,iBAAiB,gBAAgB,KAAK,GAAG,WAAW;AAE1D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