Skip to content

Commit 91d5016

Browse files
committed
feat: add Open API tab in Nuxt Devtools with Scalar
1 parent 53d375a commit 91d5016

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

docs/content/1.docs/2.features/open-api.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,26 @@ After you deploy your project, NuxtHub Admin will showcase your API documentatio
2828

2929
:nuxt-img{src="/images/landing/nuxthub-admin-open-api.png" alt="Nuxt Open API Scalar integration" width="915" height="515" data-zoom-src="/images/landing/nuxthub-admin-open-api.png"}
3030

31+
You can define route handler meta (at build time) using the `defineRouteMeta` macro:
32+
33+
```ts [pages/api/ok.ts]
34+
defineRouteMeta({
35+
openAPI: {
36+
description: 'Test route description',
37+
parameters: [{ in: "query", name: "test", required: true }],
38+
},
39+
});
40+
41+
export default defineEventHandler(() => "OK");
42+
```
43+
44+
::note{to="https://swagger.io/specification/v3/"}
45+
See swagger specification for available OpenAPI options.
46+
::
47+
3148
## Nuxt DevTools
3249

33-
In development, you can use Nuxt DevTools to access your API routes. using the `Server Routes` tab.
50+
In development, you can use Nuxt DevTools to access your API routes using the `Open API` or `Server Routes` tabs.
3451

3552
It list all the API routes in your project as well as providing a playground to send and test your endpoints.
3653

playground/server/api/chat.post.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import { AIStream, formatStreamPart } from 'ai'
22

3+
defineRouteMeta({
4+
openAPI: {
5+
description: 'Chat with AI.',
6+
tags: ['ai']
7+
}
8+
})
9+
310
export default defineEventHandler(async (event) => {
411
const { messages } = await readBody(event)
512

src/features.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,11 @@ export function setupOpenAPI(nuxt: Nuxt, hub: HubConfig) {
248248
nuxt.options.nitro.openAPI.production ||= 'runtime'
249249
nuxt.options.nitro.openAPI.route ||= '/api/_hub/openapi.json'
250250
nuxt.options.nitro.openAPI.ui ||= {}
251-
nuxt.options.nitro.openAPI.ui.scalar ||= false
251+
if (nuxt.options.dev) {
252+
nuxt.options.nitro.openAPI.ui.scalar = {
253+
route: '/api/_hub/scalar'
254+
}
255+
}
252256
nuxt.options.nitro.openAPI.ui.swagger ||= false
253257
hub.openAPIRoute = nuxt.options.nitro.openAPI.route
254258
addServerScanDir(resolve('./runtime/openapi/server'))

src/utils/devtools.ts

+11
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,16 @@ export function addDevToolsCustomTabs(nuxt: Nuxt, hub: HubConfig) {
4747
src: `https://admin.hub.nuxt.com/embed/cache?url=${url}`
4848
}
4949
})
50+
51+
hub.openAPIRoute && addCustomTab({
52+
category: 'server',
53+
name: 'hub-open-api',
54+
title: 'OpenAPI',
55+
icon: 'i-lucide-file-text',
56+
view: {
57+
type: 'iframe',
58+
src: `/api/_hub/scalar`
59+
}
60+
})
5061
})
5162
}

0 commit comments

Comments
 (0)