From 003b311ef69372b681d72dbac9b0a3a7cd926cec Mon Sep 17 00:00:00 2001 From: Alexandru Date: Thu, 23 Nov 2023 15:13:46 +0200 Subject: [PATCH] add-api-proxy: Add README and add BC baseUrl --- README.md | 8 ++++++++ src/module.ts | 25 ++++++++++++++++++++++--- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 43ed82e9..e492938e 100755 --- a/README.md +++ b/README.md @@ -65,6 +65,12 @@ rm -f app.vue && npx nuxt-drupal-ce-init - `baseURL`: The Drupal base URL. Defaults to the `DRUPAL_BASE_URL` environment variable if provided, otherwise to `http://localhost:8888`. +- `drupalBaseUrl`: The Drupal origin. Defaults to parsing the origin from `baseURL` if unset. + +- `serverDrupalBaseUrl`: The Node.js server base URL(optional). + +- `ceApiEndpoint`: The custom elements API endpoint. Defaults to `/ce-api`. + - `fetchOptions`: The default [fetchOptions](https://nuxt.com/docs/api/composables/use-fetch#params) to apply when fetching from the Drupal. Defaults to `{ credentials: 'include' }`. @@ -74,6 +80,8 @@ rm -f app.vue && npx nuxt-drupal-ce-init to the API provided by the [Rest menu items](https://www.drupal.org/project/rest_menu_items) Drupal module. `$$$NAME$$$` is replaced by the menu name being fetched. +- `menuBaseUrl`: The menu base URL. Defaults to drupalBaseUrl + ceApiEndpoint. + - `addRequestContentFormat`: If specified, the given value is added as `_content_format` URL parameter to requests. Disabled by default. diff --git a/src/module.ts b/src/module.ts index 166e54c5..1883898b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -5,7 +5,11 @@ import { defu } from 'defu' export interface ModuleOptions { baseURL: string, + drupalBaseUrl?: string, + serverDrupalBaseUrl?: string, + ceApiEndpoint?: string, menuEndpoint: string, + menuBaseUrl?: string, addRequestContentFormat?: string, addRequestFormat: boolean, customErrorPages: boolean, @@ -25,6 +29,8 @@ export default defineNuxtModule({ }, defaults: { baseURL: 'https://8080-shaal-drupalpod-xxxxxxxxxxx.ws-xxxx.gitpod.io/ce-api', + drupalBaseUrl: 'https://8080-shaal-drupalpod-xxxxxxxxxxx.ws-xxxx.gitpod.io', + ceApiEndpoint: '/ce-api', menuEndpoint: 'api/menu_items/$$$NAME$$$', customErrorPages: false, fetchOptions: { @@ -41,6 +47,22 @@ export default defineNuxtModule({ options.exposeAPIRouteRules = false } + if (options.baseURL) { + const baseURL = new URL(options.baseURL) + if (!options.drupalBaseUrl) { + options.drupalBaseUrl = baseURL.origin + } + if (!options.ceApiEndpoint) { + options.ceApiEndpoint = baseURL.pathname + } + } else { + options.baseURL = options.drupalBaseUrl + options.ceApiEndpoint + } + + if (!options.menuBaseUrl) { + options.menuBaseUrl = options.drupalBaseUrl + options.ceApiEndpoint + } + const { resolve } = createResolver(import.meta.url) const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url)) nuxt.options.build.transpile.push(runtimeDir) @@ -50,10 +72,7 @@ export default defineNuxtModule({ nuxt.options.runtimeConfig.public.drupalCe = defu(nuxt.options.runtimeConfig.public.drupalCe ?? {}, options) if (options.exposeAPIRouteRules === true) { - // Check if absolute URL or path, if an absolute then extract the origin. - const baseURLOrigin = /^(http|https):\/\//.test(options.baseURL) ? new URL(options.baseURL).origin : options.baseURL const defaultRouteRules: Record = { - '/api/drupal/**': { proxy: baseURLOrigin + '/**' }, '/api/drupal-ce/**': { proxy: options.baseURL + '/**' }, '/api/menu/**': { proxy: options.baseURL + '/**', swr: nuxt.options.dev ? 0 : 300 } }