-
Notifications
You must be signed in to change notification settings - Fork 533
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: experimental /_nitro/openapi.json
and /_nitro/swagger
endpoints for dev mode
#1162
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if this were configurable or bundled for offline usage - though I guess that could be archived by manually recreating this route?