Some checks failed
Cloudflare Worker API Template / Deploy to ${{ github.ref_name }} environment (push) Failing after 15s
94 lines
2.8 KiB
JavaScript
94 lines
2.8 KiB
JavaScript
import { swaggerConfig } from './swagger';
|
|
|
|
// HTML template for Swagger UI
|
|
const swaggerHtml = `
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>API Documentation</title>
|
|
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css">
|
|
<style>
|
|
html { box-sizing: border-box; overflow: -moz-scrollbars-vertical; overflow-y: scroll; }
|
|
*, *:before, *:after { box-sizing: inherit; }
|
|
body { margin: 0; background: #fafafa; }
|
|
.topbar { display: none; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="swagger-ui"></div>
|
|
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js"></script>
|
|
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-standalone-preset.js"></script>
|
|
<script>
|
|
window.onload = function() {
|
|
const ui = SwaggerUIBundle({
|
|
spec: SWAGGER_SPEC,
|
|
dom_id: '#swagger-ui',
|
|
deepLinking: true,
|
|
presets: [
|
|
SwaggerUIBundle.presets.apis,
|
|
SwaggerUIStandalonePreset
|
|
],
|
|
plugins: [
|
|
SwaggerUIBundle.plugins.DownloadUrl
|
|
],
|
|
layout: "StandaloneLayout"
|
|
});
|
|
window.ui = ui;
|
|
};
|
|
</script>
|
|
</body>
|
|
</html>
|
|
`;
|
|
|
|
/**
|
|
* Handles Swagger UI requests
|
|
* @param {Request} request - The incoming request
|
|
* @param {string} basePath - The base path for the API
|
|
* @returns {Response} - The response with Swagger UI or JSON
|
|
*/
|
|
export function handleSwaggerRequest(request, basePath = '') {
|
|
try {
|
|
const url = new URL(request.url);
|
|
const path = url.pathname;
|
|
|
|
// Serve the OpenAPI/Swagger JSON specification
|
|
if (path === `${basePath}/openapi.json` || path === `${basePath}/swagger.json`) {
|
|
return new Response(JSON.stringify(swaggerConfig), {
|
|
headers: { 'Content-Type': 'application/json' },
|
|
});
|
|
}
|
|
|
|
// Serve the Swagger UI HTML
|
|
if (path === `${basePath}/docs` || path === `${basePath}/docs/` || path === basePath) {
|
|
// Replace the placeholder with the actual Swagger definition
|
|
const html = swaggerHtml.replace(
|
|
'SWAGGER_SPEC',
|
|
JSON.stringify(swaggerConfig)
|
|
);
|
|
|
|
return new Response(html, {
|
|
headers: { 'Content-Type': 'text/html' },
|
|
});
|
|
}
|
|
|
|
// If not a valid Swagger UI path, return a JSON error
|
|
return new Response(JSON.stringify({
|
|
error: 'Not Found',
|
|
message: 'The requested Swagger UI resource was not found'
|
|
}), {
|
|
status: 404,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
} catch (error) {
|
|
// Return a proper JSON error response instead of HTML
|
|
return new Response(JSON.stringify({
|
|
error: 'Swagger UI Error',
|
|
details: error.message
|
|
}), {
|
|
status: 500,
|
|
headers: { 'Content-Type': 'application/json' }
|
|
});
|
|
}
|
|
}
|