Skip to content

Commit

Permalink
fix: 🎨 better config with useOaConfig()
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgbn committed Jun 19, 2024
1 parent 29c5606 commit 54dbeed
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 20 deletions.
13 changes: 4 additions & 9 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export default defineNuxtModule<ModuleOptions>({
from: resolve('runtime/server/helpers/db'),
imports: ['useMongoClient', 'useDb', 'useCol', 'useObjectId']
})
nuxt.options.nitro.virtual = defu(
{ '#oa-config': () => `export default ${JSON.stringify(nuxt.options.runtimeConfig.oa)}` },
nuxt.options.nitro.virtual
)

// Add doc routes
if (options.openApiPath) {
Expand Down Expand Up @@ -225,12 +229,3 @@ export default defineNuxtModule<ModuleOptions>({
})
}
})

declare module 'nuxt/schema' {
interface NuxtOptions {
oa: ModuleOptions
}
interface RuntimeConfig {
oa: ModuleOptions
}
}
5 changes: 5 additions & 0 deletions src/runtime/server/helpers/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { ModuleOptions } from '../../types'
// @ts-expect-error : we are importing from the virtual file system
import config from '#oa-config'

export const useOaConfig = () => config as ModuleOptions
5 changes: 2 additions & 3 deletions src/runtime/server/helpers/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import { MongoClient, ObjectId } from 'mongodb'
import { createError } from 'h3'
import { consola } from 'consola'
import type { Db, Collection, Document } from 'mongodb'
import { useOaConfig } from './config'

import { useRuntimeConfig } from '#imports'

const { dbUrl, dbOptions } = useRuntimeConfig().oa
const { dbUrl, dbOptions } = useOaConfig()

const client = new MongoClient(dbUrl ?? '', dbOptions)
client.connect()
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/helpers/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import type { Schema } from '../../types'
import { useCol, useObjectId } from './db'
import { pluralize } from './pluralize'
import { decrypt, encrypt } from './cipher'
import { useRuntimeConfig } from '#imports'
import { useOaConfig } from './config'
import { useOaServerSchema, type OaModelName } from '~/.nuxt/oa/nitro.js'

const { cipherAlgo, cipherKey, cipherIvSize } = useRuntimeConfig().oa
const { cipherAlgo, cipherKey, cipherIvSize } = useOaConfig()
const { schemasByName, defsSchemas } = useOaServerSchema()

const ajv = new Ajv({ removeAdditional: true, schemas: defsSchemas })
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/openApiPage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { defineEventHandler, setHeader } from 'h3'
import type { Schema } from '../types'
import { cleanSchema } from './helpers/model'
import { useOaConfig } from './helpers/config'
import { paths, components } from './helpers/router'
import { useRuntimeConfig } from '#imports'
import { oaDefsSchemas, oaSchemasByName } from '~/.nuxt/oa/nitro.js'

const { openApiGeneralInfo, openApiServers } = useRuntimeConfig().oa
const { openApiGeneralInfo, openApiServers } = useOaConfig()

const hasMultiDefsId = oaDefsSchemas.length > 1
const defsComponents = oaDefsSchemas.reduce<Record<string, Schema>>((o, schema) => {
Expand Down
4 changes: 2 additions & 2 deletions src/runtime/server/swaggerPage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineEventHandler } from 'h3'
import { useRuntimeConfig } from '#imports'
import { useOaConfig } from './helpers/config'

const { openApiPath } = useRuntimeConfig().oa
const { openApiPath } = useOaConfig()

export default defineEventHandler(event => `<html>
<head>
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export type Schema = { [key: string]: any }

export type DefsSchema = { $id: string, definitions: Record<string, Schema> }

export interface OaModels {
[key: string]: Schema & { _id?: ObjectId, createdAt?: string | Date, updatedAt?: string | Date, createdBy?: string | ObjectId, updatedBy?: string | ObjectId, updates?: unknown[] }
declare module '@nuxt/schema' {
interface NuxtOptions {
oa: Partial<ModuleOptions>
}
interface RuntimeConfig {
oa: Partial<ModuleOptions>
}
}

0 comments on commit 54dbeed

Please sign in to comment.