Skip to content

Commit 5f7b885

Browse files
authored
improve: LDP-2497: Add $ceApi helper and include query in server routes (#224)
* improve: custom-fetch-instance: Add helper and include query in server routes * custom-fetch-instance: Add useCeApi helper * custom-fetch-instance: Improve code * custom-fetch-instance: Add dist * custom-fetch-instance: Remove query and add dist * custom-fetch-instance: Remove dist
1 parent 0e9c3e8 commit 5f7b885

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

src/runtime/composables/useDrupalCe/index.ts

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { callWithNuxt } from '#app'
22
import { defu } from 'defu'
33
import { appendResponseHeader } from 'h3'
44
import type { UseFetchOptions } from '#app'
5+
import type { $Fetch, NitroFetchRequest } from 'nitropack'
56
import { getDrupalBaseUrl, getMenuBaseUrl } from './server'
67
import { 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,

src/runtime/server/api/drupalCe.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { defineEventHandler, proxyRequest, getRouterParams } from 'h3'
1+
import { defineEventHandler, proxyRequest, getRouterParams, getQuery } from 'h3'
22
import { getDrupalBaseUrl } from '../../composables/useDrupalCe/server'
33
import { useRuntimeConfig } from '#imports'
44

55
export default defineEventHandler(async (event) => {
66
const params = getRouterParams(event)._
77
const path = params ? '/' + params : ''
8+
const query = getQuery(event) ? '?' + new URLSearchParams(getQuery(event)) : ''
89
const { ceApiEndpoint } = useRuntimeConfig().public.drupalCe
910
// Remove x-forwarded-proto header as it causes issues with the request.
1011
delete event.req.headers['x-forwarded-proto']
11-
return await proxyRequest(event, getDrupalBaseUrl() + ceApiEndpoint + path)
12+
return await proxyRequest(event, getDrupalBaseUrl() + ceApiEndpoint + path + query)
1213
})

0 commit comments

Comments
 (0)