Skip to content

Commit

Permalink
add-api-proxy: Add README and add BC baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
vloss3 committed Nov 23, 2023
1 parent 8409bce commit 003b311
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' }`.

Expand All @@ -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.

Expand Down
25 changes: 22 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -25,6 +29,8 @@ export default defineNuxtModule<ModuleOptions>({
},
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: {
Expand All @@ -41,6 +47,22 @@ export default defineNuxtModule<ModuleOptions>({
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)
Expand All @@ -50,10 +72,7 @@ export default defineNuxtModule<ModuleOptions>({
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<string, { proxy: string, swr?: number }> = {
'/api/drupal/**': { proxy: baseURLOrigin + '/**' },
'/api/drupal-ce/**': { proxy: options.baseURL + '/**' },
'/api/menu/**': { proxy: options.baseURL + '/**', swr: nuxt.options.dev ? 0 : 300 }
}
Expand Down

0 comments on commit 003b311

Please sign in to comment.