-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental
/_nitro/openapi.json
and /_nitro/swagger
for …
…dev mode (#1162)
- Loading branch information
Showing
8 changed files
with
183 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { eventHandler } from "h3"; | ||
import type { | ||
OpenAPI3, | ||
PathItemObject, | ||
OperationObject, | ||
ParameterObject, | ||
} from "openapi-typescript"; | ||
import { handlersMeta } from "#internal/nitro/virtual/server-handlers"; | ||
|
||
// Served as /_nitro/openapi.json | ||
export default eventHandler((event) => { | ||
return <OpenAPI3>{ | ||
openapi: "3.0.0", | ||
info: { | ||
title: "Nitro Server Routes", | ||
version: null, | ||
}, | ||
servers: [ | ||
{ | ||
url: "http://localhost:3000", | ||
description: "Local Development Server", | ||
variables: {}, | ||
}, | ||
], | ||
schemes: ["http"], | ||
paths: { | ||
...Object.fromEntries( | ||
handlersMeta.map((h) => { | ||
const parameters: ParameterObject[] = []; | ||
|
||
let anonymouseCtr = 0; | ||
const route = h.route | ||
.replace(/:(\w+)/g, (_, name) => `{${name}}`) | ||
.replace(/\/(\*)\//g, () => `/{param${++anonymouseCtr}}/`) | ||
.replace(/\*\*{/, "{") | ||
.replace(/\/(\*\*)$/g, () => `/{*param${++anonymouseCtr}}`); | ||
|
||
const paramMatches = route.matchAll(/{(\*?\w+)}/g); | ||
for (const match of paramMatches) { | ||
const name = match[1]; | ||
if (!parameters.some((p) => p.name === name)) { | ||
parameters.push({ name, in: "path", required: true }); | ||
} | ||
} | ||
|
||
const tags: string[] = []; | ||
if (route.startsWith("/api/")) { | ||
tags.push("API Routes"); | ||
} else if (route.startsWith("/_")) { | ||
tags.push("Internal"); | ||
} else { | ||
tags.push("App Routes"); | ||
} | ||
|
||
const item: PathItemObject = { | ||
[(h.method || "get").toLowerCase()]: <OperationObject>{ | ||
tags, | ||
parameters, | ||
responses: { | ||
200: { description: "OK" }, | ||
}, | ||
}, | ||
}; | ||
return [route, item]; | ||
}) | ||
), | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { eventHandler } from "h3"; | ||
|
||
// https://github.com/swagger-api/swagger-ui | ||
|
||
// Served as /_nitro/swagger | ||
export default eventHandler((event) => { | ||
const title = "Nitro Swagger UI"; | ||
const CDN_BASE = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@^4"; | ||
return html`<!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}" /> | ||
<title>${title}</title> | ||
<link rel="stylesheet" href="${CDN_BASE}/swagger-ui.css" /> | ||
</head> | ||
<body> | ||
<div id="swagger-ui"></div> | ||
<script src="${CDN_BASE}/swagger-ui-bundle.js" crossorigin></script> | ||
<script | ||
src="${CDN_BASE}/swagger-ui-standalone-preset.js" | ||
crossorigin | ||
></script> | ||
<script> | ||
window.onload = () => { | ||
window.ui = SwaggerUIBundle({ | ||
url: "./openapi.json", | ||
dom_id: "#swagger-ui", | ||
presets: [ | ||
SwaggerUIBundle.presets.apis, | ||
SwaggerUIStandalonePreset, | ||
], | ||
layout2: "StandaloneLayout", | ||
}); | ||
}; | ||
</script> | ||
</body> | ||
</html> `; | ||
}); | ||
|
||
function html(str, ...args) { | ||
return String.raw(str, ...args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters