Skip to content

Commit

Permalink
feat: add-api-proxy: Add Nitro server API routes (#179)
Browse files Browse the repository at this point in the history
* feat: add-api-proxy: Add Nitro server API routes

* Remove debug code

* Fix tests

* Fix menu route rules and add docs

* Add test for disabled exposeAPIRouteRules

* Improve docs

* Improve docs

* Fix module build error.

* Fix menu proxy not working.

* add-api-proxy: Add README and add BC baseUrl

* add-api-proxy: Replace routeRules with server handlers

* add-api-proxy: Remove console log

* add-api-proxy: Handle cases where baseURL is not full URL

* add-api-proxy: Add back README commit

* add-api-proxy: Support i18n in menus and add README for runtime overrides

* add-api-proxy: Add override examples and use serverDrupalBaseUrl

* add-api-proxy: Improve README

* Improve README

* Fix a build error

* Fix build errors

* Fix menu endpoint proxy

* Improve README

* Fix default options

* Update README.md

* correctly test BC

* add-api-proxy: Update README and menu fallback

* add-api-proxy: Allow env overrides for drupalBaseUrl

* add-api-proxy: Update vitest

* add-api-proxy: Improve menu route and README

* add-api-proxy: Small refactor

* add-api-proxy: Refactor menu api code

* add-api-proxy: Add handler for base path

---------

Co-authored-by: Matic Fink <[email protected]>
Co-authored-by: Wolfgang Ziegler <[email protected]>
  • Loading branch information
3 people authored Jan 31, 2024
1 parent af06bf4 commit af80b10
Show file tree
Hide file tree
Showing 9 changed files with 6,134 additions and 7,060 deletions.
41 changes: 30 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ export default defineNuxtConfig({
'nuxtjs-drupal-ce',
],
drupalCe: {
baseURL: 'https://your-drupal.example.com/ce-api',
drupalBaseUrl: 'https://your-drupal.example.com',
// more options...
}
})
```

The module defaults work well with [Lupus Decoupled Drupal](https://www.drupal.org/project/lupus_decoupled), so setting the `baseURL` is usually enough.
The module defaults work well with [Lupus Decoupled Drupal](https://www.drupal.org/project/lupus_decoupled) - in that case setting the
`drupalBaseUrl` is enough to get started.

3. Get started quickly by scaffolding initial files:
```bash
Expand All @@ -60,10 +61,14 @@ rm -f app.vue && npx nuxt-drupal-ce-init
* Provides unstyled skeleton components for getting started quickly.
* Supports fetching and display of Drupal menus via the [Rest menu items](https://www.drupal.org/project/rest_menu_items) module.


## Options

- `baseURL`: The Drupal base URL. Defaults to the `DRUPAL_BASE_URL`
environment variable if provided, otherwise to `http://localhost:8888`.
- `drupalBaseUrl`: The Drupal base URL, e.g. `https://example.com:8080`. Required.

- `serverDrupalBaseUrl`: Optionally, an alternative drupal base URL to apply in server context.

- `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 +79,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 All @@ -86,6 +93,25 @@ 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).

## Overriding options with environment variables

Runtime config values can be overridden with environment variables via `NUXT_PUBLIC_` prefix. Supported runtime overrides:

- `drupalBaseUrl` -> `NUXT_PUBLIC_DRUPAL_CE_DRUPAL_BASE_URL`
- `serverDrupalBaseUrl` -> `NUXT_PUBLIC_DRUPAL_CE_SERVER_DRUPAL_BASE_URL`
- `menuBaseUrl` -> `NUXT_PUBLIC_DRUPAL_CE_MENU_BASE_URL`
- `ceApiEndpoint` -> `NUXT_PUBLIC_DRUPAL_CE_CE_API_ENDPOINT`

## Deprecated options

The following options are deprecated and only there for improved backwards compatibility.

- `baseURL`: The base URL of the Drupal /ce-api endpoint, e.g. http://localhost:8888/ce-api.
If set, `drupalBaseUrl` is set with the origin of the provided URL.


## Error handling

The module provides a default error handler for the `fetchPage` and `fetchMenu` methods:
Expand Down Expand Up @@ -139,13 +165,6 @@ The following options were support in 1.x but got dropped:
- `axios`: Options to pass-through to the `drupal-ce`
[axios](https://github.com/nuxt-community/axios-module) instance. Use `fetchOptions` instead.

- `useProxy`: If set to `dev-only` and nuxt is in dev-mode, the module automatically
configures `/api` to the Drupal backend via
[@nuxtjs/proxy](https://github.com/nuxt-community/proxy-module) and uses it instead of
the Drupal backend, such that there are no CORS issues. Other values supported are
`always` or false.
Note: When using `always` the module must be added to the nuxt `modules` section instead
of the `buildModules` section.

## Development

Expand Down
Loading

0 comments on commit af80b10

Please sign in to comment.