UNPKG

@azure-utils/storybooks

Version:

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

439 lines (435 loc) 14.8 kB
import { DocumentLayout, ErrorMessage, __toESM, checkIsEditMode, checkIsHTMLRequest, checkIsHXRequest, checkIsNewMode, getStore, require_jsx_runtime, responseError, responseHTML, responseRedirect, urlBuilder } from "./store-BL4RNiEp.mjs"; import { CONTENT_TYPES, commonErrorResponses } from "./constants-CsV1N9r4.mjs"; import { LabelSlugSchema } from "./shared-BAE3ceND.mjs"; import { LabelCreateSchema, LabelModel, LabelSchema, LabelUpdateSchema, labelTypes } from "./projects-DDVy3_77.mjs"; import { openAPITags, registerOpenAPIPath } from "./openapi-utils-CAJ85ahl.mjs"; import { joinUrl, urlSearchParamsToObject } from "./url-utils-B9Pl4bQ7.mjs"; import { BuildTable, RawDataPreview } from "./builds-table-D78yU-UW.mjs"; import { LabelsTable } from "./labels-table-DoOmA3fT.mjs"; import { app } from "@azure/functions"; import z from "zod"; //#region src/components/label-form.tsx var import_jsx_runtime$1 = __toESM(require_jsx_runtime()); async function LabelForm({ label, projectId }) { return /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("form", { "hx-ext": "response-targets", "hx-patch": label ? urlBuilder.labelSlug(projectId, label.slug) : void 0, "hx-post": label ? void 0 : urlBuilder.allLabels(projectId), "hx-target-error": "#form-error", style: { maxWidth: "60ch" }, children: [ /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("fieldset", { children: [ /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("legend", { children: "Details" }), label ? /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("input", { type: "hidden", name: "slug", value: label.slug }) : null, /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("div", { class: "field", children: [/* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("label", { for: "value", children: "Label" }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("input", { id: "value", name: "value", required: true, value: label?.value })] }) ] }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("fieldset", { children: [ /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("legend", { children: "Type" }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("div", { style: { display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: "0.5rem" }, children: labelTypes.map((type) => { const id = `type-${type}`; return /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("div", { children: [/* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("input", { id, name: "type", type: "radio", required: true, value: type, checked: type === label?.type }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("label", { for: id, children: type })] }); }) }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("span", { class: "description", children: "Type of label defines behaviour of the label." }) ] }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("div", { style: { display: "flex", gap: "1rem" }, children: [/* @__PURE__ */ (0, import_jsx_runtime$1.jsxs)("button", { type: "submit", children: [label ? "Update" : "Create", " Label"] }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)("button", { type: "reset", children: "Reset" })] }), /* @__PURE__ */ (0, import_jsx_runtime$1.jsx)(ErrorMessage, { id: "form-error" }) ] }); } //#endregion //#region src/handlers/label-handlers.tsx var import_jsx_runtime = __toESM(require_jsx_runtime()); async function listLabels(request, context) { const { projectId = "" } = request.params; try { if (checkIsNewMode()) return responseHTML(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DocumentLayout, { title: "Create Label", breadcrumbs: [{ label: projectId, href: urlBuilder.projectId(projectId) }, { label: "Labels", href: urlBuilder.allLabels(projectId) }], children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LabelForm, { projectId, label: void 0 }) })); const { connectionString } = getStore(); const labelModel = new LabelModel(context, connectionString, projectId); const labels = await labelModel.list(); if (checkIsHTMLRequest()) return responseHTML(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DocumentLayout, { title: "All Labels", breadcrumbs: [projectId], toolbar: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: urlBuilder.labelCreate(projectId), children: "+ Create" }), children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LabelsTable, { labels, projectId, caption: `Labels (${labels.length})` }) })); return { status: 200, jsonBody: labels }; } catch (error) { return responseError(error, context); } } async function createLabel(request, context) { try { const { projectId = "" } = request.params; const { connectionString } = getStore(); const model = new LabelModel(context, connectionString, projectId); if (!await model.projectModel.has(projectId)) return responseError(`The project '${projectId}' does not exist.`, context, 404); const contentType = request.headers.get("content-type"); if (!contentType) return responseError("Content-Type header is required", context, 400); if (!contentType.includes(CONTENT_TYPES.FORM_ENCODED)) return responseError(`Invalid Content-Type, expected ${CONTENT_TYPES.FORM_ENCODED}`, context, 415); const data = LabelCreateSchema.parse(urlSearchParamsToObject(await request.formData())); await model.create(data); const labelUrl = urlBuilder.labelSlug(projectId, LabelModel.createSlug(data.value)); if (checkIsHTMLRequest() || checkIsHXRequest()) return responseRedirect(labelUrl, 303); return { status: 201, headers: { Location: labelUrl }, jsonBody: { data, links: { self: labelUrl } } }; } catch (error) { return responseError(error, context); } } async function getLabel(request, context) { const { projectId = "", labelSlug = "" } = request.params; try { const { connectionString } = getStore(); const labelModel = new LabelModel(context, connectionString, projectId); const label = await labelModel.get(labelSlug); if (checkIsEditMode()) return responseHTML(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DocumentLayout, { title: "Edit Label", breadcrumbs: [ { label: projectId, href: urlBuilder.projectId(projectId) }, { label: "Labels", href: urlBuilder.allLabels(projectId) }, { label: label.slug, href: urlBuilder.labelSlug(projectId, labelSlug) } ], children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LabelForm, { projectId, label }) })); const projectModel = labelModel.projectModel; const project = await projectModel.get(projectId); const builds = await projectModel.buildModel(projectId).list({ filter: `PartitionKey eq '${labelSlug}'` }); if (checkIsHTMLRequest()) return responseHTML(/* @__PURE__ */ (0, import_jsx_runtime.jsx)(DocumentLayout, { title: `[${label.type}] ${label.value}`, breadcrumbs: [projectId, "Labels"], toolbar: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { style: { display: "flex", gap: "1rem", alignItems: "center" }, children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)("a", { href: urlBuilder.labelSlugEdit(projectId, labelSlug), children: "Edit" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("form", { "hx-delete": request.url, "hx-confirm": "Are you sure about deleting the label?", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { children: "Delete" }) })] }), children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(RawDataPreview, { data: label, summary: "Label details" }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(BuildTable, { caption: `Builds (${builds.length})`, builds, labels: void 0, project })] }) })); return { status: 202, jsonBody: { ...label, builds } }; } catch (error) { return responseError(error, context); } } async function updateLabel(request, context) { const { projectId = "", labelSlug = "" } = request.params; try { const { connectionString } = getStore(); const contentType = request.headers.get("content-type"); if (!contentType) return responseError("Content-Type header is required", context, 400); if (!contentType.includes(CONTENT_TYPES.FORM_ENCODED)) return responseError(`Invalid Content-Type, expected ${CONTENT_TYPES.FORM_ENCODED}`, context, 415); const data = LabelUpdateSchema.partial().parse(urlSearchParamsToObject(await request.formData())); const labelModel = new LabelModel(context, connectionString, projectId); await labelModel.update(labelSlug, data); const labelUrl = urlBuilder.labelSlug(projectId, labelSlug); if (checkIsHTMLRequest() || checkIsHXRequest()) return responseRedirect(request.url, 303); return { status: 202, headers: { Location: labelUrl }, jsonBody: { data: await labelModel.get(labelSlug), links: { self: request.url } } }; } catch (error) { return responseError(error, context); } } async function deleteLabel(request, context) { const { projectId = "", labelSlug = "" } = request.params; try { const { connectionString } = getStore(); const labelModel = new LabelModel(context, connectionString, projectId); await labelModel.delete(labelSlug); const labelsUrl = urlBuilder.allLabels(projectId); if (checkIsHTMLRequest() || checkIsHXRequest()) return responseRedirect(labelsUrl, 303); return { status: 204, headers: { Location: labelsUrl } }; } catch (error) { return responseError(error, context); } } async function getLabelLatestBuild(request, context) { const { projectId = "", labelSlug = "" } = request.params; context.log("Getting latest build for label '%s' in project '%s'...", labelSlug, projectId); try { const { connectionString } = getStore(); const labelModel = new LabelModel(context, connectionString, projectId); const { buildSHA: latestBuildSHA } = await labelModel.get(labelSlug); if (!latestBuildSHA) return { status: 404, jsonBody: { error: "No builds found for this label." } }; return { status: 303, headers: { Location: urlBuilder.buildSHA(projectId, latestBuildSHA) } }; } catch (error) { return responseError(error, context); } } //#endregion //#region src/routers/labels-router.ts const TAG = openAPITags.labels.name; function registerLabelsRouter(options) { const { authLevel, baseRoute, basePathParamsSchema, handlerWrapper, openAPIEnabled, serviceName } = options; app.get(`${serviceName}-labels-list`, { authLevel, route: baseRoute, handler: handlerWrapper(listLabels, [{ resource: "label", action: "read" }, { resource: "ui", action: "read" }]) }); app.post(`${serviceName}-label-create`, { authLevel, route: baseRoute, handler: handlerWrapper(createLabel, [{ resource: "label", action: "create" }]) }); const routeWithLabel = joinUrl(baseRoute, "{labelSlug}"); app.get(`${serviceName}-label-get`, { authLevel, route: routeWithLabel, handler: handlerWrapper(getLabel, [{ resource: "label", action: "read" }, { resource: "ui", action: "read" }]) }); app.patch(`${serviceName}-label-update`, { authLevel, route: routeWithLabel, handler: handlerWrapper(updateLabel, [{ resource: "label", action: "update" }]) }); app.deleteRequest(`${serviceName}-label-delete`, { authLevel, route: routeWithLabel, handler: handlerWrapper(deleteLabel, [{ resource: "label", action: "delete" }]) }); const routeWithLabelLatest = joinUrl(routeWithLabel, "latest"); app.get(`${serviceName}-label-latest`, { authLevel, route: routeWithLabelLatest, handler: handlerWrapper(getLabelLatestBuild, [{ resource: "label", action: "read" }]) }); if (openAPIEnabled) { const labelPathParameterSchema = basePathParamsSchema.extend({ labelSlug: LabelSlugSchema }); registerOpenAPIPath(baseRoute, { get: { tags: [TAG], summary: "List all labels for the project.", description: "Retrieves a list of labels.", requestParams: { path: basePathParamsSchema }, responses: { ...commonErrorResponses, 200: { description: "A list of labels.", content: { [CONTENT_TYPES.JSON]: { schema: LabelSchema.array(), example: [{ slug: "label-slug", value: "label/slug" }] }, [CONTENT_TYPES.HTML]: { example: "<!DOCTYPE html>" } } } } }, post: { tags: [TAG], summary: "Create a new label.", description: "Create a new label with slug and type.", requestParams: { path: basePathParamsSchema }, responses: { ...commonErrorResponses, 202: { description: "Data about label", content: { [CONTENT_TYPES.JSON]: { schema: LabelSchema } } }, 303: { description: "Label created, redirecting...", headers: { Location: z.url() } } } } }); registerOpenAPIPath(routeWithLabel, { get: { tags: [TAG], summary: "Get label details", description: "Retrieves the details of a specific label and all builds associated with it.", requestParams: { path: labelPathParameterSchema }, responses: { ...commonErrorResponses, 200: { description: "Label details retrieved successfully", content: { [CONTENT_TYPES.JSON]: { schema: LabelSchema }, [CONTENT_TYPES.HTML]: { example: "<!DOCTYPE html>" } } }, 404: { description: "Matching label not found." } } }, delete: { tags: [TAG], summary: "Delete a label", description: "Deletes a specific label and builds associated with it.", requestParams: { path: labelPathParameterSchema }, responses: { ...commonErrorResponses, 204: { description: "Label deleted successfully" }, 404: { description: "Matching label not found." } } } }); registerOpenAPIPath(routeWithLabelLatest, { get: { tags: [TAG], summary: "Redirect to latest build for a label", description: "Redirects to the latest build associated with a specific label.", requestParams: { path: labelPathParameterSchema }, responses: { ...commonErrorResponses, 308: { description: "Redirecting to latest build.", headers: { Location: { content: z.string().meta({ description: "URL of the latest build" }) } } }, 404: { description: "Matching label or build not found." } } } }); } } //#endregion export { registerLabelsRouter }; //# sourceMappingURL=labels-router-DWXBHCOP.mjs.map