@azure-utils/storybooks
Version:
Utils to upload and manage Storybooks via Azure Functions and storage.
98 lines (96 loc) • 3.29 kB
JavaScript
const require_chunk = require('./chunk-DWy1uDak.cjs');
const require_constants = require('./constants-94H7Co6A.cjs');
const require_openapi_utils = require('./openapi-utils-CMVFCUk1.cjs');
const require_store = require('./store-CYxr095K.cjs');
const zod_openapi = require_chunk.__toESM(require("zod-openapi"));
//#region src/handlers/openapi-handler.ts
async function openAPIHandler(request, context) {
const { authLevel, openapi, serviceName } = require_store.getStore();
const { title = serviceName.toUpperCase(), version = process.env["NODE_ENV"] || "TEST", servers } = openapi || {};
context.log("Serving OpenAPI schema...", authLevel);
try {
const securitySchemes = {};
if (authLevel) securitySchemes["functionsKey"] = require_openapi_utils.openAPISecuritySchemas.functionsKey;
const openAPISpec = (0, zod_openapi.createDocument)({
openapi: "3.1.0",
info: {
title,
version
},
security: authLevel ? [{ functionsKey: [] }] : [],
servers,
tags: Object.values(require_openapi_utils.openAPITags),
paths: require_openapi_utils.openAPIPaths,
components: {
schemas: require_openapi_utils.openAPISchemas,
securitySchemes
}
});
const { searchParams } = new URL(request.url);
const isDownloadJSON = searchParams.get("download") === "json";
if (isDownloadJSON) {
const headers = new Headers({ "Content-Disposition": `attachment; filename="${title}_${version}_openapi.json"` });
return {
status: 200,
jsonBody: openAPISpec,
headers
};
}
const accept = request.headers.get("accept");
if (!accept || accept.includes(require_constants.CONTENT_TYPES.JSON)) return {
status: 200,
jsonBody: openAPISpec
};
if (accept.includes(require_constants.CONTENT_TYPES.HTML)) {
const html = generateSwaggerUI(title, openAPISpec);
return {
status: 200,
headers: { "Content-Type": require_constants.CONTENT_TYPES.HTML },
body: html
};
}
return {
status: 406,
body: require_constants.SUPPORTED_CONTENT_TYPES_MSG
};
} catch (error) {
return require_store.responseError(error, context);
}
}
function generateSwaggerUI(title, openAPISpec) {
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="${title} SwaggerUI" />
<title>${title}</title>
<link rel="stylesheet" type="text/css" href="https://unpkg.com/swagger-ui-dist/swagger-ui.css" />
<script src="https://unpkg.com/swagger-ui-dist/swagger-ui-bundle.js" crossorigin><\/script>
</head>
<body style="position: relative;">
<div id="swagger-ui"></div>
<div style="position: absolute; top: 0; right: 0; padding-right: 16px; display: flex; gap: 0.5rem;">
<form>
<input type="hidden" name="download" value="json" />
<button type="submit">Download</button>
</form>
<button type="button" onClick="window.location.reload();">Refresh</button>
</div>
<script async defer>
window.swaggerUI = SwaggerUIBundle(${JSON.stringify({
dom_id: "#swagger-ui",
spec: openAPISpec
})});
<\/script>
</body>
</html>`;
}
//#endregion
Object.defineProperty(exports, 'openAPIHandler', {
enumerable: true,
get: function () {
return openAPIHandler;
}
});