Skip to content
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

fix(openapi): use new Nitro 2.10 format #365

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"std-env": "^3.7.0",
"ufo": "^1.5.4",
"uncrypto": "^0.1.3",
"unstorage": "^1.12.0",
"unstorage": "^1.13.1",
"zod": "^3.23.8"
},
"devDependencies": {
Expand All @@ -65,13 +65,13 @@
"@nuxt/schema": "^3.13.2",
"@nuxt/test-utils": "^3.14.4",
"@nuxthub/core": "link:",
"@types/node": "^22.8.4",
"@types/node": "^22.8.7",
"changelogen": "^0.5.7",
"eslint": "^9.13.0",
"eslint": "^9.14.0",
"nuxt": "^3.13.2",
"typescript": "^5.6.3",
"vitest": "^2.1.4",
"wrangler": "^3.83.0"
"wrangler": "^3.84.1"
},
"packageManager": "pnpm@9.12.3"
}
6 changes: 6 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export default defineNuxtConfig({

compatibilityDate: '2024-08-08',

nitro: {
experimental: {
openAPI: true
}
},

hub: {
ai: true,
database: true,
Expand Down
4,134 changes: 1,507 additions & 2,627 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface HubConfig {
}

migrationsPath?: string
openAPIRoute?: string
}

export function setupBase(nuxt: Nuxt, hub: HubConfig) {
Expand Down Expand Up @@ -237,12 +238,15 @@ export function vectorizeRemoteCheck(hub: HubConfig) {
}
}

export function setupOpenAPI(nuxt: Nuxt) {
// Fallback to custom placeholder when openAPI is disabled
nuxt.options.alias['#hub/openapi'] = nuxt.options.nitro?.experimental?.openAPI === true
? 'nitropack/runtime/routes/openapi'
: resolve('./runtime/openapi/server/templates/openapi')

export function setupOpenAPI(nuxt: Nuxt, hub: HubConfig) {
nuxt.options.nitro ||= {}
nuxt.options.nitro.openAPI ||= {}
nuxt.options.nitro.openAPI.production ||= 'runtime'
nuxt.options.nitro.openAPI.route ||= '/api/_hub/openapi.json'
nuxt.options.nitro.openAPI.ui ||= {}
nuxt.options.nitro.openAPI.ui.scalar ||= false
nuxt.options.nitro.openAPI.ui.swagger ||= false
hub.openAPIRoute = nuxt.options.nitro.openAPI.route
addServerScanDir(resolve('./runtime/openapi/server'))
}

Expand Down
6 changes: 1 addition & 5 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export default defineNuxtModule<ModuleOptions>({
// Extra bindings for the project
bindings: {
hyperdrive: {},
// @ts-expect-error nitro.cloudflare.wrangler is not yet typed
compatibilityFlags: nuxt.options.nitro.cloudflare?.wrangler?.compatibility_flags
},
// Cloudflare Access
Expand All @@ -79,12 +78,9 @@ export default defineNuxtModule<ModuleOptions>({
runtimeConfig.hub = hub
runtimeConfig.public.hub = {}
// Make sure to tell Nitro to not generate the wrangler.toml file
// @ts-expect-error nitro.cloudflare.wrangler is not yet typed
delete nuxt.options.nitro.cloudflare?.wrangler?.compatibility_flags
// @ts-expect-error nitro.cloudflare.wrangler is not yet typed
if (nuxt.options.nitro.cloudflare?.wrangler && Object.keys(nuxt.options.nitro.cloudflare.wrangler).length) {
log.warn('The `nitro.cloudflare.wrangler` defined options are not supported by NuxtHub, ignoring...')
// @ts-expect-error nitro.cloudflare.wrangler is not yet typed
nuxt.options.nitro.cloudflare.wrangler = {}
}
// validate remote option
Expand All @@ -107,7 +103,7 @@ export default defineNuxtModule<ModuleOptions>({
}

setupBase(nuxt, hub as HubConfig)
setupOpenAPI(nuxt)
hub.openapi && setupOpenAPI(nuxt, hub as HubConfig)
hub.ai && await setupAI(nuxt, hub as HubConfig)
hub.analytics && setupAnalytics(nuxt)
hub.blob && setupBlob(nuxt)
Expand Down
16 changes: 2 additions & 14 deletions src/runtime/openapi/server/api/_hub/openapi.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { eventHandler, createError, type H3Event } from 'h3'
import { requireNuxtHubAuthorization } from '../../../../utils/auth'
import { useRuntimeConfig } from '#imports'

export default eventHandler(async (event) => {
export default eventHandler(async (event: H3Event) => {
await requireNuxtHubAuthorization(event)
const hub = useRuntimeConfig().hub

Expand All @@ -13,17 +13,5 @@ export default eventHandler(async (event) => {
})
}

// @ts-expect-error #hub/openapi has no exported types
const openapi: (event: H3Event) => any = await import('#hub/openapi')
.then(mod => mod.default)
.catch(() => undefined)

if (typeof openapi !== 'function') {
throw createError({
statusCode: 404,
message: 'not found'
})
}

return openapi(event)
return $fetch(hub.openAPIRoute).catch(() => ({}))
})
1 change: 0 additions & 1 deletion src/runtime/openapi/server/templates/openapi.ts

This file was deleted.

Loading