UNPKG

@azure-utils/storybooks

Version:

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

92 lines (89 loc) 4.26 kB
const require_chunk = require('./chunk-DWy1uDak.cjs'); const require_constants = require('./constants-94H7Co6A.cjs'); const require_azure_storage_blob = require('./azure-storage-blob-BAHnImGv.cjs'); const require_shared = require('./shared-BSQDPNdH.cjs'); const require_openapi_utils = require('./openapi-utils-CMVFCUk1.cjs'); const require_store = require('./store-CYxr095K.cjs'); const require_url_utils = require('./url-utils-Dy7KiQmB.cjs'); const __azure_functions = require_chunk.__toESM(require("@azure/functions")); const zod = require_chunk.__toESM(require("zod")); const __azure_storage_blob = require_chunk.__toESM(require("@azure/storage-blob")); const node_path = require_chunk.__toESM(require("node:path")); const node_stream = require_chunk.__toESM(require("node:stream")); //#region src/handlers/storybook-handler.tsx async function serveStorybook(request, context) { const { projectId = "", buildSHA = "", filepath = "" } = request.params; const blobName = node_path.default.posix.join(buildSHA, filepath); context.log("Serving SB (%s) - %s...", projectId, blobName); const { connectionString } = require_store.getStore(); const blockBlobClient = __azure_storage_blob.BlobServiceClient.fromConnectionString(connectionString).getContainerClient(require_azure_storage_blob.generateAzureStorageContainerName(projectId)).getBlockBlobClient(blobName); if (!await blockBlobClient.exists()) return require_store.responseError(`File '${blobName}' not found.`, context, 404); if (blobName.endsWith("index.html")) { const buffer = await blockBlobClient.downloadToBuffer(0); const bodyWithBackButton = buffer.toString("utf8").replace(`</body>`, ` <div><a id="view-all" href="${require_store.urlBuilder.allBuilds(projectId)}" style="position: fixed; bottom: 0.5rem; left: 0.5rem; z-index: 9999; padding: 0.25rem 0.5rem; background-color: black; color: white; border-radius: 0.25rem; text-decoration: none; font-size: 1rem; font-face: sans-serif; font-weight: 400;"> ← View all </a></div></body>`); return { headers: { "Cache-Control": require_constants.CACHE_CONTROL_PUBLIC_YEAR, "Content-Type": "text/html; charset=utf8", "Content-Length": Buffer.byteLength(bodyWithBackButton).toString() }, body: bodyWithBackButton, status: 200 }; } const downloadResponse = await blockBlobClient.download(0); const headers = new Headers(); headers.set("Cache-Control", require_constants.CACHE_CONTROL_PUBLIC_YEAR); headers.set("Content-Type", downloadResponse.contentType || "application/octet-stream"); if (downloadResponse.contentLength) headers.set("Content-Length", downloadResponse.contentLength.toString()); if (downloadResponse.contentEncoding) headers.set("Content-Encoding", downloadResponse.contentEncoding); return { body: node_stream.Readable.toWeb(downloadResponse.readableStreamBody), headers, status: 200 }; } //#endregion //#region src/routers/storybook-router.ts const TAG = require_openapi_utils.openAPITags.storybook.name; function registerStorybookRouter(options) { const { authLevel, baseRoute, basePathParamsSchema, handlerWrapper, openAPIEnabled, serviceName } = options; const storybookRoute = require_url_utils.joinUrl(baseRoute, "{projectId}", "{buildSHA}", "{**filepath}"); __azure_functions.app.get(`${serviceName}-serve-storybook`, { authLevel, route: storybookRoute, handler: handlerWrapper(serveStorybook, [{ resource: "build", action: "read" }]) }); if (openAPIEnabled) require_openapi_utils.registerOpenAPIPath(storybookRoute, { get: { tags: [TAG], summary: "List all projects", description: "Retrieves a list of projects.", requestParams: { path: basePathParamsSchema.extend({ projectId: require_shared.ProjectIdSchema, buildSHA: require_shared.BuildSHASchema, "**filepath": zod.default.string().meta({ description: "The path to the storybook files.", example: "index.html" }) }) }, responses: { ...require_constants.commonErrorResponses, 200: { description: "Serve the file." }, 404: { description: "File not found." } } } }); } //#endregion Object.defineProperty(exports, 'registerStorybookRouter', { enumerable: true, get: function () { return registerStorybookRouter; } });