Skip to content

Commit

Permalink
improve: Rename exposeAPIRouteRules to serverApiProxy and fix type er…
Browse files Browse the repository at this point in the history
…rors (#210)
  • Loading branch information
vloss3 authored Mar 20, 2024
1 parent aca067c commit 799fba4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ 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).
- `serverApiProxy`: 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).

- `passThroughHeaders`: Response headers to pass through from Drupal to the client. Defaults to ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache']. Note: This is only available in SSR mode.

Expand Down
26 changes: 12 additions & 14 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { fileURLToPath } from 'url'
import { defineNuxtModule, addPlugin, createResolver, addImportsDir, addServerHandler } from '@nuxt/kit'
import type { UseFetchOptions } from 'nuxt/dist/app/composables'
import { defu } from 'defu'
import type { NuxtOptionsWithDrupalCe } from './types'

export interface ModuleOptions {
baseURL?: string,
Expand All @@ -13,11 +13,12 @@ export interface ModuleOptions {
addRequestContentFormat?: string,
addRequestFormat: boolean,
customErrorPages: boolean,
fetchOptions: UseFetchOptions<any>,
fetchOptions: Object,
fetchProxyHeaders: string[],
useLocalizedMenuEndpoint: boolean,
exposeAPIRouteRules: boolean,
serverApiProxy: boolean,
passThroughHeaders?: string[],
exposeAPIRouteRules?: boolean,
}

export default defineNuxtModule<ModuleOptions>({
Expand All @@ -39,10 +40,15 @@ export default defineNuxtModule<ModuleOptions>({
fetchProxyHeaders: ['cookie'],
useLocalizedMenuEndpoint: true,
addRequestFormat: false,
exposeAPIRouteRules: true,
serverApiProxy: true,
passThroughHeaders: ['cache-control', 'content-language', 'set-cookie', 'x-drupal-cache', 'x-drupal-dynamic-cache'],
},
setup (options, nuxt) {
const nuxtOptions = nuxt.options as NuxtOptionsWithDrupalCe
// Keep backwards compatibility for exposeAPIRouteRules(deprecated).
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)
Expand All @@ -60,7 +66,7 @@ export default defineNuxtModule<ModuleOptions>({

// Disable the server routes for static sites OR when baseURL is not a full URL.
if (nuxt.options._generate || !options.baseURL.startsWith('http')) {
options.exposeAPIRouteRules = false
options.serverApiProxy = false
}

const { resolve } = createResolver(import.meta.url)
Expand All @@ -71,7 +77,7 @@ export default defineNuxtModule<ModuleOptions>({

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

if (options.exposeAPIRouteRules === true) {
if (options.serverApiProxy === true) {
addServerHandler({
route: '/api/drupal-ce',
handler: resolve(runtimeDir, 'server/api/drupalCe')
Expand All @@ -87,11 +93,3 @@ export default defineNuxtModule<ModuleOptions>({
}
}
})

// Define the type for the runtime-config,.
// see https://nuxt.com/docs/guide/going-further/runtime-config#manually-typing-runtime-config
declare module 'nuxt/schema' {
interface PublicRuntimeConfig {
drupalCe: ModuleOptions,
}
}
7 changes: 4 additions & 3 deletions src/runtime/composables/useDrupalCe.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { callWithNuxt } from '#app'
import { defu } from 'defu'
import { appendResponseHeader } from 'h3'
import { useRuntimeConfig, useRequestURL, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, UseFetchOptions, ref, watch } from '#imports'
import type { UseFetchOptions } from '#app'
import { useRuntimeConfig, useState, useFetch, navigateTo, createError, h, resolveComponent, setResponseStatus, useNuxtApp, useRequestHeaders, ref, watch } from '#imports'

export const useDrupalCe = () => {

Expand All @@ -13,7 +14,7 @@ export const useDrupalCe = () => {
* @returns UseFetchOptions<any>
*/
const processFetchOptions = (fetchOptions:UseFetchOptions<any> = {}) => {
if (config.exposeAPIRouteRules) {
if (config.serverApiProxy) {
fetchOptions.baseURL = '/api/drupal-ce'
} else {
fetchOptions.baseURL = fetchOptions.baseURL ?? config.baseURL
Expand Down Expand Up @@ -121,7 +122,7 @@ export const useDrupalCe = () => {
})
}

if (config.exposeAPIRouteRules) {
if (config.serverApiProxy) {
useFetchOptions.baseURL = '/api/menu'
// menuPath should not start with a slash.
if (menuPath.value.startsWith('/')) {
Expand Down
14 changes: 14 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { ModuleOptions } from './module'

// Define the type for the runtime-config,.
// see https://nuxt.com/docs/guide/going-further/runtime-config#manually-typing-runtime-config
declare module 'nuxt/schema' {
interface PublicRuntimeConfig {
drupalCe: ModuleOptions,
}
}

export interface NuxtOptionsWithDrupalCe extends NuxtOptions {
drupalCe?: ModuleOptions
}

4 changes: 2 additions & 2 deletions test/routeRules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, expect } from 'vitest'
import { setup, $fetch } from '@nuxt/test-utils'
import DrupalCe from '..'

describe('Site works with exposeAPIRouteRules disabled', async () => {
describe('Site works with serverApiProxy disabled', async () => {
await setup({
rootDir: fileURLToPath(new URL('../playground', import.meta.url)),
nuxtConfig: {
Expand All @@ -12,7 +12,7 @@ describe('Site works with exposeAPIRouteRules disabled', async () => {
],
drupalCe: {
baseURL: '/api',
exposeAPIRouteRules: false
serverApiProxy: false
}
}
})
Expand Down

0 comments on commit 799fba4

Please sign in to comment.