Skip to content

Commit 77ba18c

Browse files
authored
improve!: LDP-2461: remove deprecated baseURL option and fix configuration fallbacks to apply at runtime (#204)
* improve: LDP-2461: Rename routeRules to serverApiProxy * LDP-2461: Add BC for exposeAPIRouteRules * LDP-2461: Remove check for absolute URL in serverApiProxy * LDP-2461: Add dist for testing * LDP-2461: Remove baseURL, add utility functions for base urls * LDP-2461: Update tests * improve: Rename exposeAPIRouteRules to serverApiProxy and fix type errors * LDP-2461: Remove dist * LDP-2461: Add useFetchDrupal helper * LDP-2461: Add getCeApiEndpoint helper * LDP-2461: Create composable for base urls * LDP-2461: Add dist * LDP-2461: Add server compatible utils for getBaseUrls * LDP-2461: Add useDrupalCe server composables * LDP-2461: Improve variable name in menu route * LDP-2461: Remove dist
1 parent 799fba4 commit 77ba18c

File tree

14 files changed

+64
-44
lines changed

14 files changed

+64
-44
lines changed

playground/nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ export default defineNuxtConfig({
66
DrupalCe
77
],
88
drupalCe: {
9-
baseURL: 'https://8080-drunomics-lupusdecouple-ih6hr5d3dwc.ws-eu106.gitpod.io/ce-api'
9+
drupalBaseUrl: 'https://8080-drunomics-lupusdecouple-x5qe3h51r0o.ws-eu110.gitpod.io'
1010
}
1111
})

playground/nuxt.config4test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export default defineNuxtConfig({
66
DrupalCe
77
],
88
drupalCe: {
9-
baseURL: '/api'
9+
drupalBaseUrl: '',
10+
ceApiEndpoint: '/api',
11+
serverApiProxy: false // to-do: improve tests to cover this option
1012
}
1113
})

src/module.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { fileURLToPath } from 'url'
2-
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit'
2+
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler, addServerImports, addImports } from '@nuxt/kit'
33
import { defu } from 'defu'
44
import type { NuxtOptionsWithDrupalCe } from './types'
55

66
export interface ModuleOptions {
7-
baseURL?: string,
87
drupalBaseUrl: string,
98
serverDrupalBaseUrl?: string,
109
ceApiEndpoint: string,
@@ -49,31 +48,17 @@ export default defineNuxtModule<ModuleOptions>({
4948
if (!nuxtOptions.drupalCe?.serverApiProxy && options.exposeAPIRouteRules !== undefined) {
5049
options.serverApiProxy = options.exposeAPIRouteRules
5150
}
52-
// Keep backwards compatibility for baseURL(deprecated).
53-
if (options.baseURL && options.baseURL.startsWith('http')) {
54-
const baseURL = new URL(options.baseURL)
55-
if (!options.drupalBaseUrl) {
56-
options.drupalBaseUrl = baseURL.origin
57-
}
58-
options.ceApiEndpoint = baseURL.pathname
59-
} else if (!options.baseURL) {
60-
options.baseURL = options.drupalBaseUrl + options.ceApiEndpoint
61-
}
62-
63-
if (!options.menuBaseUrl) {
64-
options.menuBaseUrl = options.drupalBaseUrl + options.ceApiEndpoint
65-
}
6651

67-
// Disable the server routes for static sites OR when baseURL is not a full URL.
68-
if (nuxt.options._generate || !options.baseURL.startsWith('http')) {
52+
// Disable the server routes for static sites.
53+
if (nuxt.options._generate) {
6954
options.serverApiProxy = false
7055
}
7156

7257
const { resolve } = createResolver(import.meta.url)
7358
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
7459
nuxt.options.build.transpile.push(runtimeDir)
7560
addPlugin(resolve(runtimeDir, 'plugin'))
76-
addImportsDir(resolve(runtimeDir, 'composables'))
61+
addImportsDir(resolve(runtimeDir, 'composables/useDrupalCe'))
7762

7863
nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, options)
7964

src/runtime/composables/useDrupalCe.ts renamed to src/runtime/composables/useDrupalCe/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ import { callWithNuxt } from '#app'
22
import { defu } from 'defu'
33
import { appendResponseHeader } from 'h3'
44
import type { UseFetchOptions } from '#app'
5+
import { getDrupalBaseUrl, getMenuBaseUrl } from './server'
56
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch } from '#imports'
67

78
export const useDrupalCe = () => {
8-
99
const config = useRuntimeConfig().public.drupalCe
1010

1111
/**
1212
* Processes the given fetchOptions to apply module defaults
1313
* @param fetchOptions Optional Nuxt useFetch options
1414
* @returns UseFetchOptions<any>
1515
*/
16-
const processFetchOptions = (fetchOptions:UseFetchOptions<any> = {}) => {
16+
const processFetchOptions = (fetchOptions: UseFetchOptions<any> = {}) => {
1717
if (config.serverApiProxy) {
1818
fetchOptions.baseURL = '/api/drupal-ce'
1919
} else {
20-
fetchOptions.baseURL = fetchOptions.baseURL ?? config.baseURL
20+
fetchOptions.baseURL = fetchOptions.baseURL ?? getDrupalBaseUrl() + config.ceApiEndpoint
2121
}
2222
fetchOptions = defu(fetchOptions, config.fetchOptions)
2323

@@ -28,6 +28,17 @@ export const useDrupalCe = () => {
2828
return fetchOptions
2929
}
3030

31+
/**
32+
* Returns the API endpoint with localization (if available)
33+
*/
34+
const getCeApiEndpoint = (localize: boolean = true) => {
35+
const nuxtApp = useNuxtApp()
36+
if (localize && nuxtApp.$i18n?.locale && nuxtApp.$i18n.locale.value !== nuxtApp.$i18n.defaultLocale) {
37+
return `${config.ceApiEndpoint}/${nuxtApp.$i18n.locale.value}`
38+
}
39+
return config.ceApiEndpoint
40+
}
41+
3142
/**
3243
* Fetches page data from Drupal, handles redirects, errors and messages
3344
* @param path Path of the Drupal page to fetch
@@ -186,7 +197,10 @@ export const useDrupalCe = () => {
186197
getMessages,
187198
getPage,
188199
renderCustomElements,
189-
passThroughHeaders
200+
passThroughHeaders,
201+
getCeApiEndpoint,
202+
getDrupalBaseUrl,
203+
getMenuBaseUrl
190204
}
191205
}
192206

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { useRuntimeConfig } from '#imports'
2+
3+
export const getDrupalBaseUrl = () => {
4+
const config = useRuntimeConfig().public.drupalCe
5+
return import.meta.server && config.serverDrupalBaseUrl ? config.serverDrupalBaseUrl : config.drupalBaseUrl
6+
}
7+
8+
export const getMenuBaseUrl = () => {
9+
const config = useRuntimeConfig().public.drupalCe
10+
return config.menuBaseUrl ? config.menuBaseUrl : getDrupalBaseUrl() + config.ceApiEndpoint
11+
}

src/runtime/server/api/drupalCe.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { defineEventHandler, proxyRequest, getRouterParams } from 'h3'
2+
import { getDrupalBaseUrl } from '../../composables/useDrupalCe/server'
23
import { useRuntimeConfig } from '#imports'
34

45
export default defineEventHandler(async (event) => {
56
const params = getRouterParams(event)._
67
const path = params ? '/' + params : ''
7-
const drupalCe = useRuntimeConfig().public.drupalCe
8-
const drupalUrl = (drupalCe.serverDrupalBaseUrl || drupalCe.drupalBaseUrl) + drupalCe.ceApiEndpoint
8+
const { ceApiEndpoint } = useRuntimeConfig().public.drupalCe
99
// Remove x-forwarded-proto header as it causes issues with the request.
1010
delete event.req.headers['x-forwarded-proto']
11-
return await proxyRequest(event, drupalUrl + path)
11+
return await proxyRequest(event, getDrupalBaseUrl() + ceApiEndpoint + path)
1212
})

src/runtime/server/api/menu.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { defineEventHandler, proxyRequest, getRouterParams } from 'h3'
2-
import { useRuntimeConfig } from '#imports'
2+
import { getMenuBaseUrl } from '../../composables/useDrupalCe/server'
33

44
export default defineEventHandler(async (event) => {
5-
const drupalCe = useRuntimeConfig().public.drupalCe
6-
const { serverDrupalBaseUrl, drupalBaseUrl, menuBaseUrl: ceMenuBaseUrl, ceApiEndpoint } = drupalCe
7-
const menuBaseUrl = ceMenuBaseUrl || (serverDrupalBaseUrl || drupalBaseUrl) + ceApiEndpoint
8-
const menu = getRouterParams(event)._
9-
return await proxyRequest(event, `${menuBaseUrl}/${menu}`, {
5+
// Get the menu path along with the localization prefix.
6+
const menuPath = getRouterParams(event)._
7+
return await proxyRequest(event, `${getMenuBaseUrl()}/${menuPath}`, {
108
headers: {
119
'Cache-Control': 'max-age=300'
1210
}

src/types.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ declare module 'nuxt/schema' {
1111
export interface NuxtOptionsWithDrupalCe extends NuxtOptions {
1212
drupalCe?: ModuleOptions
1313
}
14-

test/addRequestContentFormat/json.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ describe('Module addRequestContentFormat option set to json', async () => {
1111
DrupalCe
1212
],
1313
drupalCe: {
14-
baseURL: '/api',
15-
addRequestContentFormat: 'json'
14+
drupalBaseUrl: '',
15+
ceApiEndpoint: '/api',
16+
addRequestContentFormat: 'json',
17+
serverApiProxy: false
1618
}
1719
}
1820
})

test/addRequestContentFormat/markup.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ describe('Module addRequestContentFormat set to markup', async () => {
1111
DrupalCe
1212
],
1313
drupalCe: {
14-
baseURL: '/api',
15-
addRequestContentFormat: 'markup'
14+
drupalBaseUrl: '',
15+
ceApiEndpoint: '/api',
16+
addRequestContentFormat: 'markup',
17+
serverApiProxy: false
1618
}
1719
}
1820
})

0 commit comments

Comments
 (0)