diff --git a/playground/nuxt.config.ts b/playground/nuxt.config.ts index 36d474cb..106cd5c8 100644 --- a/playground/nuxt.config.ts +++ b/playground/nuxt.config.ts @@ -6,6 +6,6 @@ export default defineNuxtConfig({ DrupalCe ], drupalCe: { - baseURL: 'https://8080-drunomics-lupusdecouple-ih6hr5d3dwc.ws-eu106.gitpod.io/ce-api' + drupalBaseUrl: 'https://8080-drunomics-lupusdecouple-x5qe3h51r0o.ws-eu110.gitpod.io' } }) diff --git a/playground/nuxt.config4test.ts b/playground/nuxt.config4test.ts index b2e67f13..c8534016 100644 --- a/playground/nuxt.config4test.ts +++ b/playground/nuxt.config4test.ts @@ -6,6 +6,8 @@ export default defineNuxtConfig({ DrupalCe ], drupalCe: { - baseURL: '/api' + drupalBaseUrl: '', + ceApiEndpoint: '/api', + serverApiProxy: false // to-do: improve tests to cover this option } }) diff --git a/src/module.ts b/src/module.ts index 1749ba3f..7c4e2fe0 100644 --- a/src/module.ts +++ b/src/module.ts @@ -1,10 +1,9 @@ import { fileURLToPath } from 'url' -import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit' +import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler, addServerImports, addImports } from '@nuxt/kit' import { defu } from 'defu' import type { NuxtOptionsWithDrupalCe } from './types' export interface ModuleOptions { - baseURL?: string, drupalBaseUrl: string, serverDrupalBaseUrl?: string, ceApiEndpoint: string, @@ -49,23 +48,9 @@ export default defineNuxtModule({ 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) - if (!options.drupalBaseUrl) { - options.drupalBaseUrl = baseURL.origin - } - options.ceApiEndpoint = baseURL.pathname - } else if (!options.baseURL) { - options.baseURL = options.drupalBaseUrl + options.ceApiEndpoint - } - - if (!options.menuBaseUrl) { - options.menuBaseUrl = options.drupalBaseUrl + options.ceApiEndpoint - } - // Disable the server routes for static sites OR when baseURL is not a full URL. - if (nuxt.options._generate || !options.baseURL.startsWith('http')) { + // Disable the server routes for static sites. + if (nuxt.options._generate) { options.serverApiProxy = false } @@ -73,7 +58,7 @@ export default defineNuxtModule({ const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) nuxt.options.build.transpile.push(runtimeDir) addPlugin(resolve(runtimeDir, 'plugin')) - addImportsDir(resolve(runtimeDir, 'composables')) + addImportsDir(resolve(runtimeDir, 'composables/useDrupalCe')) nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, options) diff --git a/src/runtime/composables/useDrupalCe.ts b/src/runtime/composables/useDrupalCe/index.ts similarity index 90% rename from src/runtime/composables/useDrupalCe.ts rename to src/runtime/composables/useDrupalCe/index.ts index d4bc53f3..cdba4aa6 100644 --- a/src/runtime/composables/useDrupalCe.ts +++ b/src/runtime/composables/useDrupalCe/index.ts @@ -2,10 +2,10 @@ import { callWithNuxt } from '#app' import { defu } from 'defu' import { appendResponseHeader } from 'h3' import type { UseFetchOptions } from '#app' +import { getDrupalBaseUrl, getMenuBaseUrl } from './server' import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch } from '#imports' export const useDrupalCe = () => { - const config = useRuntimeConfig().public.drupalCe /** @@ -13,11 +13,11 @@ export const useDrupalCe = () => { * @param fetchOptions Optional Nuxt useFetch options * @returns UseFetchOptions */ - const processFetchOptions = (fetchOptions:UseFetchOptions = {}) => { + const processFetchOptions = (fetchOptions: UseFetchOptions = {}) => { if (config.serverApiProxy) { fetchOptions.baseURL = '/api/drupal-ce' } else { - fetchOptions.baseURL = fetchOptions.baseURL ?? config.baseURL + fetchOptions.baseURL = fetchOptions.baseURL ?? getDrupalBaseUrl() + config.ceApiEndpoint } fetchOptions = defu(fetchOptions, config.fetchOptions) @@ -28,6 +28,17 @@ export const useDrupalCe = () => { return fetchOptions } + /** + * Returns the API endpoint with localization (if available) + */ + const getCeApiEndpoint = (localize: boolean = true) => { + const nuxtApp = useNuxtApp() + if (localize && nuxtApp.$i18n?.locale && nuxtApp.$i18n.locale.value !== nuxtApp.$i18n.defaultLocale) { + return `${config.ceApiEndpoint}/${nuxtApp.$i18n.locale.value}` + } + return config.ceApiEndpoint + } + /** * Fetches page data from Drupal, handles redirects, errors and messages * @param path Path of the Drupal page to fetch @@ -186,7 +197,10 @@ export const useDrupalCe = () => { getMessages, getPage, renderCustomElements, - passThroughHeaders + passThroughHeaders, + getCeApiEndpoint, + getDrupalBaseUrl, + getMenuBaseUrl } } diff --git a/src/runtime/composables/useDrupalCe/server.ts b/src/runtime/composables/useDrupalCe/server.ts new file mode 100644 index 00000000..d653a93a --- /dev/null +++ b/src/runtime/composables/useDrupalCe/server.ts @@ -0,0 +1,11 @@ +import { useRuntimeConfig } from '#imports' + +export const getDrupalBaseUrl = () => { + const config = useRuntimeConfig().public.drupalCe + return import.meta.server && config.serverDrupalBaseUrl ? config.serverDrupalBaseUrl : config.drupalBaseUrl +} + +export const getMenuBaseUrl = () => { + const config = useRuntimeConfig().public.drupalCe + return config.menuBaseUrl ? config.menuBaseUrl : getDrupalBaseUrl() + config.ceApiEndpoint +} diff --git a/src/runtime/server/api/drupalCe.ts b/src/runtime/server/api/drupalCe.ts index 3a535ed1..e32667a8 100644 --- a/src/runtime/server/api/drupalCe.ts +++ b/src/runtime/server/api/drupalCe.ts @@ -1,12 +1,12 @@ import { defineEventHandler, proxyRequest, getRouterParams } from 'h3' +import { getDrupalBaseUrl } from '../../composables/useDrupalCe/server' import { useRuntimeConfig } from '#imports' export default defineEventHandler(async (event) => { const params = getRouterParams(event)._ const path = params ? '/' + params : '' - const drupalCe = useRuntimeConfig().public.drupalCe - const drupalUrl = (drupalCe.serverDrupalBaseUrl || drupalCe.drupalBaseUrl) + drupalCe.ceApiEndpoint + const { ceApiEndpoint } = useRuntimeConfig().public.drupalCe // Remove x-forwarded-proto header as it causes issues with the request. delete event.req.headers['x-forwarded-proto'] - return await proxyRequest(event, drupalUrl + path) + return await proxyRequest(event, getDrupalBaseUrl() + ceApiEndpoint + path) }) diff --git a/src/runtime/server/api/menu.ts b/src/runtime/server/api/menu.ts index c1aad38c..68c12831 100644 --- a/src/runtime/server/api/menu.ts +++ b/src/runtime/server/api/menu.ts @@ -1,12 +1,10 @@ import { defineEventHandler, proxyRequest, getRouterParams } from 'h3' -import { useRuntimeConfig } from '#imports' +import { getMenuBaseUrl } from '../../composables/useDrupalCe/server' export default defineEventHandler(async (event) => { - const drupalCe = useRuntimeConfig().public.drupalCe - const { serverDrupalBaseUrl, drupalBaseUrl, menuBaseUrl: ceMenuBaseUrl, ceApiEndpoint } = drupalCe - const menuBaseUrl = ceMenuBaseUrl || (serverDrupalBaseUrl || drupalBaseUrl) + ceApiEndpoint - const menu = getRouterParams(event)._ - return await proxyRequest(event, `${menuBaseUrl}/${menu}`, { + // Get the menu path along with the localization prefix. + const menuPath = getRouterParams(event)._ + return await proxyRequest(event, `${getMenuBaseUrl()}/${menuPath}`, { headers: { 'Cache-Control': 'max-age=300' } diff --git a/src/types.d.ts b/src/types.d.ts index 7b5eb1b6..588a4a88 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -11,4 +11,3 @@ declare module 'nuxt/schema' { export interface NuxtOptionsWithDrupalCe extends NuxtOptions { drupalCe?: ModuleOptions } - diff --git a/test/addRequestContentFormat/json.test.ts b/test/addRequestContentFormat/json.test.ts index b5d6971a..a18e51d1 100644 --- a/test/addRequestContentFormat/json.test.ts +++ b/test/addRequestContentFormat/json.test.ts @@ -11,8 +11,10 @@ describe('Module addRequestContentFormat option set to json', async () => { DrupalCe ], drupalCe: { - baseURL: '/api', - addRequestContentFormat: 'json' + drupalBaseUrl: '', + ceApiEndpoint: '/api', + addRequestContentFormat: 'json', + serverApiProxy: false } } }) diff --git a/test/addRequestContentFormat/markup.test.ts b/test/addRequestContentFormat/markup.test.ts index 9a0f3ca7..fb8e5287 100644 --- a/test/addRequestContentFormat/markup.test.ts +++ b/test/addRequestContentFormat/markup.test.ts @@ -11,8 +11,10 @@ describe('Module addRequestContentFormat set to markup', async () => { DrupalCe ], drupalCe: { - baseURL: '/api', - addRequestContentFormat: 'markup' + drupalBaseUrl: '', + ceApiEndpoint: '/api', + addRequestContentFormat: 'markup', + serverApiProxy: false } } }) diff --git a/test/addRequestContentFormat/unset.test.ts b/test/addRequestContentFormat/unset.test.ts index 4947cffb..78ac2c79 100644 --- a/test/addRequestContentFormat/unset.test.ts +++ b/test/addRequestContentFormat/unset.test.ts @@ -11,7 +11,9 @@ describe('Module addRequestContentFormat not set', async () => { DrupalCe ], drupalCe: { - baseURL: '/api' + drupalBaseUrl: '', + ceApiEndpoint: '/api', + serverApiProxy: false } } }) diff --git a/test/addRequestFormat/set.test.ts b/test/addRequestFormat/set.test.ts index 9c8ffa6e..021fbce0 100644 --- a/test/addRequestFormat/set.test.ts +++ b/test/addRequestFormat/set.test.ts @@ -11,8 +11,10 @@ describe('Module addRequestFormat option set to true', async () => { DrupalCe ], drupalCe: { - baseURL: '/api', - addRequestFormat: true + drupalBaseUrl: '', + ceApiEndpoint: '/api', + addRequestFormat: true, + serverApiProxy: false } } }) diff --git a/test/addRequestFormat/unset.test.ts b/test/addRequestFormat/unset.test.ts index 1c4a2924..b36b8cae 100644 --- a/test/addRequestFormat/unset.test.ts +++ b/test/addRequestFormat/unset.test.ts @@ -11,7 +11,9 @@ describe('Module addRequestFormat not set', async () => { DrupalCe ], drupalCe: { - baseURL: '/api' + drupalBaseUrl: '', + ceApiEndpoint: '/api', + serverApiProxy: false } } }) diff --git a/test/routeRules.test.ts b/test/routeRules.test.ts index b9d0913a..d5194996 100644 --- a/test/routeRules.test.ts +++ b/test/routeRules.test.ts @@ -11,7 +11,8 @@ describe('Site works with serverApiProxy disabled', async () => { DrupalCe ], drupalCe: { - baseURL: '/api', + drupalBaseUrl: '', + ceApiEndpoint: '/api', serverApiProxy: false } }