@@ -57,21 +57,34 @@ export default defineNuxtModule<ModuleOptions>({
5757 } ,
5858 } ,
5959 defaults : {
60- kirbyUrl : process . env . KIRBY_BASE_URL ,
60+ kirbyUrl : process . env . KIRBY_BASE_URL as string ,
6161 kirbyEndpoint : 'api/query' ,
6262 kirbyAuth : 'basic' ,
63- token : process . env . KIRBY_API_TOKEN ,
63+ token : process . env . KIRBY_API_TOKEN as string ,
6464 credentials : {
65- username : process . env . KIRBY_API_USERNAME ,
66- password : process . env . KIRBY_API_PASSWORD ,
65+ username : process . env . KIRBY_API_USERNAME as string ,
66+ password : process . env . KIRBY_API_PASSWORD as string ,
6767 } ,
6868 clientRequests : false ,
6969 } ,
7070 async setup ( options , nuxt ) {
7171 const { resolve } = createResolver ( import . meta. url )
72- const { clientRequests } = options
7372 const apiRoute = '/api/__kql__' as const
7473
74+ // Make sure Kirby URL and KQL endpoint are set
75+ if ( ! options . kirbyUrl )
76+ console . warn ( 'Missing `KIRBY_BASE_URL` in `.env`' )
77+
78+ if ( ! options . kirbyEndpoint )
79+ console . warn ( 'Missing `kql.kirbyEndpoint` option in Nuxt config' )
80+
81+ // Make sure authentication credentials are set
82+ if ( options . kirbyAuth === 'basic' && ( ! options . credentials || ! options . credentials . username || ! options . credentials . password ) )
83+ console . warn ( 'Missing `KIRBY_API_USERNAME` and `KIRBY_API_PASSWORD` in `.env` for basic authentication' )
84+
85+ if ( options . kirbyAuth === 'bearer' && ! options . token )
86+ console . warn ( 'Missing `KIRBY_API_TOKEN` in `.env` for bearer authentication' )
87+
7588 // Private runtime config
7689 nuxt . options . runtimeConfig . kql = defu (
7790 nuxt . options . runtimeConfig . kql ,
@@ -83,35 +96,58 @@ export default defineNuxtModule<ModuleOptions>({
8396 nuxt . options . runtimeConfig . public . kql ,
8497 // Protect authorization data if no public requests are enabled
8598 {
86- kirbyUrl : clientRequests ? options . kirbyUrl : undefined ,
87- kirbyEndpoint : clientRequests ? options . kirbyEndpoint : undefined ,
88- kirbyAuth : clientRequests ? options . kirbyAuth : undefined ,
89- token : clientRequests ? options . token : undefined ,
90- credentials : clientRequests ? options . credentials : undefined ,
91- clientRequests,
99+ kirbyUrl : options . clientRequests ? options . kirbyUrl : undefined ,
100+ kirbyEndpoint : options . clientRequests ? options . kirbyEndpoint : undefined ,
101+ kirbyAuth : options . clientRequests ? options . kirbyAuth : undefined ,
102+ token : options . clientRequests ? options . token : undefined ,
103+ credentials : options . clientRequests ? options . credentials : undefined ,
104+ clientRequests : options . clientRequests ,
92105 } as ModuleOptions ,
93106 )
94107
108+ // Transpile runtime
95109 const runtimeDir = fileURLToPath ( new URL ( './runtime' , import . meta. url ) )
96110 nuxt . options . build . transpile . push ( runtimeDir )
97111
112+ // Add KQL proxy endpoint to fetch queries on server-side
113+ addServerHandler ( {
114+ route : apiRoute ,
115+ handler : resolve ( runtimeDir , 'server/api/kql' ) ,
116+ } )
117+
118+ // Add KQL composables
98119 nuxt . hook ( 'autoImports:dirs' , ( dirs ) => {
99120 dirs . push ( resolve ( runtimeDir , 'composables' ) )
100121 } )
101122
102- addServerHandler ( {
103- route : apiRoute ,
104- handler : resolve ( runtimeDir , 'server/api' ) ,
123+ nuxt . hook ( 'nitro:config' , ( nitroConfig ) => {
124+ // Inline module runtime in Nitro bundle
125+ nitroConfig . externals = defu ( typeof nitroConfig . externals === 'object' ? nitroConfig . externals : { } , {
126+ inline : [ resolve ( './runtime' ) ] ,
127+ } )
105128 } )
106129
107130 addTemplate ( {
108- filename : 'nuxt-kql- options.ts ' ,
131+ filename : 'nuxt-kql/ options.mjs ' ,
109132 write : true ,
110133 getContents ( ) {
111134 return `
112135export const apiRoute = '${ apiRoute } '
113136` . trimStart ( )
114137 } ,
115138 } )
139+
140+ addTemplate ( {
141+ filename : 'types/nuxt-kql.d.ts' ,
142+ getContents : ( ) => [
143+ 'declare module \'#build/nuxt-kql/options\' {' ,
144+ ' const apiRoute: string' ,
145+ '}' ,
146+ ] . join ( '\n' ) ,
147+ } )
148+
149+ nuxt . hook ( 'prepare:types' , ( options ) => {
150+ options . references . push ( { path : resolve ( nuxt . options . buildDir , 'types/nuxt-kql.d.ts' ) } )
151+ } )
116152 } ,
117153} )
0 commit comments