@@ -2,6 +2,7 @@ import { callWithNuxt } from '#app'
22import { defu } from 'defu'
33import { appendResponseHeader } from 'h3'
44import type { UseFetchOptions } from '#app'
5+ import type { $Fetch , NitroFetchRequest } from 'nitropack'
56import { getDrupalBaseUrl , getMenuBaseUrl } from './server'
67import { useRuntimeConfig , useState , useFetch , navigateTo , createError , h , resolveComponent , setResponseStatus , useNuxtApp , useRequestHeaders , ref , watch } from '#imports'
78
@@ -25,9 +26,56 @@ export const useDrupalCe = () => {
2526 if ( config . fetchProxyHeaders ) {
2627 fetchOptions . headers = defu ( fetchOptions . headers ?? { } , useRequestHeaders ( config . fetchProxyHeaders ) )
2728 }
29+
30+ // If fetchOptions.query._content_format is undefined, use config.addRequestContentFormat.
31+ // If fetchOptions.query._content_format is false, keep that.
32+ fetchOptions . query = fetchOptions . query ?? { }
33+
34+ fetchOptions . query . _content_format = fetchOptions . query . _content_format ?? config . addRequestContentFormat
35+ if ( ! fetchOptions . query . _content_format ) {
36+ // Remove _content_format if set to a falsy value (e.g. fetchOptions.query._content_format was set to false)
37+ delete fetchOptions . query . _content_format
38+ }
39+
40+ if ( config . addRequestFormat ) {
41+ fetchOptions . query . _format = 'custom_elements'
42+ }
2843 return fetchOptions
2944 }
3045
46+ /**
47+ * Custom $fetch instance
48+ * @param fetchOptions UseFetchOptions<any>
49+ */
50+ const $ceApi = ( fetchOptions : UseFetchOptions < any > = { } ) : $Fetch < unknown , NitroFetchRequest > => {
51+ const useFetchOptions = processFetchOptions ( fetchOptions )
52+
53+ return $fetch . create ( {
54+ ...useFetchOptions
55+ } )
56+ }
57+
58+ /**
59+ * Fetch data from Drupal ce-API endpoint using $ceApi
60+ * @param path Path of the Drupal ce-API endpoint to fetch
61+ * @param fetchOptions UseFetchOptions<any>
62+ * @param passThroughHeaders Whether to pass through headers from Drupal to the client
63+ */
64+ const useCeApi = ( path : string | Ref < string > , fetchOptions : UseFetchOptions < any > = { } , doPassThroughHeaders ?: boolean ) : Promise < any > => {
65+ const nuxtApp = useNuxtApp ( )
66+ fetchOptions . onResponse = ( context ) => {
67+ if ( doPassThroughHeaders && config . passThroughHeaders && import . meta. server ) {
68+ const headersObject = Object . fromEntries ( [ ...context . response . headers . entries ( ) ] )
69+ passThroughHeaders ( nuxtApp , headersObject )
70+ }
71+ }
72+
73+ return useFetch ( path , {
74+ ...fetchOptions ,
75+ $fetch : $ceApi ( fetchOptions )
76+ } )
77+ }
78+
3179 /**
3280 * Returns the API endpoint with localization (if available)
3381 */
@@ -68,25 +116,8 @@ export const useDrupalCe = () => {
68116 title : ''
69117 } ) )
70118 useFetchOptions . key = `page-${ path } `
71- useFetchOptions = processFetchOptions ( useFetchOptions )
72- useFetchOptions . query = useFetchOptions . query ?? { }
73-
74- useFetchOptions . onResponse = ( context ) => {
75- if ( config . passThroughHeaders && import . meta. server ) {
76- const headersObject = Object . fromEntries ( [ ...context . response . headers . entries ( ) ] )
77- passThroughHeaders ( nuxtApp , headersObject )
78- }
79- }
80-
81- if ( config . addRequestContentFormat ) {
82- useFetchOptions . query . _content_format = config . addRequestContentFormat
83- }
84-
85- if ( config . addRequestFormat ) {
86- useFetchOptions . query . _format = 'custom_elements'
87- }
88119
89- const { data : page , error } = await useFetch ( path , useFetchOptions )
120+ const { data : page , error } = await useCeApi ( path , useFetchOptions , true )
90121
91122 if ( page ?. value ?. redirect ) {
92123 await callWithNuxt ( nuxtApp , navigateTo , [
@@ -146,7 +177,7 @@ export const useDrupalCe = () => {
146177 }
147178 }
148179
149- const { data : menu , error } = await useFetch ( menuPath , useFetchOptions )
180+ const { data : menu , error } = await useCeApi ( menuPath , useFetchOptions )
150181
151182 if ( error . value ) {
152183 overrideErrorHandler ? overrideErrorHandler ( error ) : menuErrorHandler ( error )
@@ -197,6 +228,8 @@ export const useDrupalCe = () => {
197228 }
198229
199230 return {
231+ $ceApi,
232+ useCeApi,
200233 fetchPage,
201234 fetchMenu,
202235 getMessages,
0 commit comments