diff --git a/README.md b/README.md index f6ec5dcc..300e11fb 100755 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ is added automatically to requests. Defaults to `false`. - `useLocalizedMenuEndpoint`: If enabled, the menu endpoint will use a language prefix as configured by [nuxtjs/i18n](https://v8.i18n.nuxtjs.org) module. Defaults to `true`. -- `exposeAPIRouteRules`: If enabled, the module will create a Nitro server handler that proxies API requests to Drupal backend. Defaults to `true` for SSR (it's disabled for SSG). +- `serverApiProxy`: If enabled, the module will create a Nitro server handler that proxies API requests to Drupal backend. Defaults to `true` for SSR (it's disabled for SSG). - `passThroughHeaders`: Response headers to pass through from Drupal to the client. Defaults to ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache']. Note: This is only available in SSR mode. diff --git a/src/module.ts b/src/module.ts index c392a421..1749ba3f 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,7 +1,7 @@ import { fileURLToPath } from 'url' import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit' -import type { UseFetchOptions } from 'nuxt/dist/app/composables' import { defu } from 'defu' +import type { NuxtOptionsWithDrupalCe } from './types' export interface ModuleOptions { baseURL?: string, @@ -13,11 +13,12 @@ export interface ModuleOptions { addRequestContentFormat?: string, addRequestFormat: boolean, customErrorPages: boolean, - fetchOptions: UseFetchOptions, + fetchOptions: Object, fetchProxyHeaders: string[], useLocalizedMenuEndpoint: boolean, - exposeAPIRouteRules: boolean, + serverApiProxy: boolean, passThroughHeaders?: string[], + exposeAPIRouteRules?: boolean, } export default defineNuxtModule({ @@ -39,10 +40,15 @@ export default defineNuxtModule({ fetchProxyHeaders: ['cookie'], useLocalizedMenuEndpoint: true, addRequestFormat: false, - exposeAPIRouteRules: true, + serverApiProxy: true, passThroughHeaders: ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache'], }, setup (options, nuxt) { + const nuxtOptions = nuxt.options as NuxtOptionsWithDrupalCe + // Keep backwards compatibility for exposeAPIRouteRules(deprecated). + if (!nuxtOptions.drupalCe?.serverApiProxy && options.exposeAPIRouteRules !== undefined) { + options.serverApiProxy = options.exposeAPIRouteRules + } // Keep backwards compatibility for baseURL(deprecated). if (options.baseURL && options.baseURL.startsWith('http')) { const baseURL = new URL(options.baseURL) @@ -60,7 +66,7 @@ export default defineNuxtModule({ // Disable the server routes for static sites OR when baseURL is not a full URL. if (nuxt.options._generate || !options.baseURL.startsWith('http')) { - options.exposeAPIRouteRules = false + options.serverApiProxy = false } const { resolve } = createResolver(import.meta.url) @@ -71,7 +77,7 @@ export default defineNuxtModule({ nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, options) - if (options.exposeAPIRouteRules === true) { + if (options.serverApiProxy === true) { addServerHandler({ route: '/api/drupal-ce', handler: resolve(runtimeDir, 'server/api/drupalCe') @@ -87,11 +93,3 @@ export default defineNuxtModule({ } } }) - -// Define the type for the runtime-config,. -// see https://nuxt.com/docs/guide/going-further/runtime-config#manually-typing-runtime-config -declare module 'nuxt/schema' { - interface PublicRuntimeConfig { - drupalCe: ModuleOptions, - } -} diff --git a/src/runtime/composables/useDrupalCe.ts b/src/runtime/composables/useDrupalCe.ts index d181c9a5..d4bc53f3 100644 --- a/src/runtime/composables/useDrupalCe.ts +++ b/src/runtime/composables/useDrupalCe.ts @@ -1,7 +1,8 @@ import { callWithNuxt } from '#app' import { defu } from 'defu' import { appendResponseHeader } from 'h3' -import { useRuntimeConfig, useRequestURL, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports' +import type { UseFetchOptions } from '#app' +import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch } from '#imports' export const useDrupalCe = () => { @@ -13,7 +14,7 @@ export const useDrupalCe = () => { * @returns UseFetchOptions */ const processFetchOptions = (fetchOptions:UseFetchOptions = {}) => { - if (config.exposeAPIRouteRules) { + if (config.serverApiProxy) { fetchOptions.baseURL = '/api/drupal-ce' } else { fetchOptions.baseURL = fetchOptions.baseURL ?? config.baseURL @@ -121,7 +122,7 @@ export const useDrupalCe = () => { }) } - if (config.exposeAPIRouteRules) { + if (config.serverApiProxy) { useFetchOptions.baseURL = '/api/menu' // menuPath should not start with a slash. if (menuPath.value.startsWith('/')) { diff --git a/src/types.d.ts b/src/types.d.ts new file mode 100644 index 00000000..7b5eb1b6 --- /dev/null +++ b/src/types.d.ts @@ -0,0 +1,14 @@ +import type { ModuleOptions } from './module' + +// Define the type for the runtime-config,. +// see https://nuxt.com/docs/guide/going-further/runtime-config#manually-typing-runtime-config +declare module 'nuxt/schema' { + interface PublicRuntimeConfig { + drupalCe: ModuleOptions, + } +} + +export interface NuxtOptionsWithDrupalCe extends NuxtOptions { + drupalCe?: ModuleOptions +} + diff --git a/test/routeRules.test.ts b/test/routeRules.test.ts index 0ea7d5b9..b9d0913a 100644 --- a/test/routeRules.test.ts +++ b/test/routeRules.test.ts @@ -3,7 +3,7 @@ import { describe, it, expect } from 'vitest' import { setup, $fetch } from '@nuxt/test-utils' import DrupalCe from '..' -describe('Site works with exposeAPIRouteRules disabled', async () => { +describe('Site works with serverApiProxy disabled', async () => { await setup({ rootDir: fileURLToPath(new URL('../playground', import.meta.url)), nuxtConfig: { @@ -12,7 +12,7 @@ describe('Site works with exposeAPIRouteRules disabled', async () => { ], drupalCe: { baseURL: '/api', - exposeAPIRouteRules: false + serverApiProxy: false } } })