Skip to content

Commit

Permalink
feat: enable proxying additional headers
Browse files Browse the repository at this point in the history
Closes #323
  • Loading branch information
Diizzayy committed Jul 17, 2023
1 parent 0ae0fab commit 7c2e5cb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/content/1.getting-started/4.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ Pass cookies from the browser to the GraphQL API in SSR mode.
Enabled by default.
::

### `proxyHeaders`

Headers to be passed from the browser to the GraphQL API in SSR mode.

### `corsOptions`

Specify CORS options to be used for client-side requests.
Expand Down
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 18 additions & 10 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@ export default defineNuxtPlugin((nuxtApp) => {

const { clients }: GqlConfig = defu(config?.['graphql-client'], config?.public?.['graphql-client'])

const cookie = (process.server && useRequestHeaders(['cookie'])?.cookie) || undefined
const proxyHeaders = Object.values(clients || {}).flatMap(v => v?.proxyHeaders).filter((v, i, a) => Boolean(v) && a.indexOf(v) === i) as string[]
if (!proxyHeaders.includes('cookie')) { proxyHeaders.push('cookie') }

const requestHeaders = ((process.server && useRequestHeaders(proxyHeaders)) as Record<string, string>) || undefined

for (const [name, v] of Object.entries(clients || {})) {
const host = (process.client && v?.clientHost) || v.host

const proxyCookie = v?.proxyCookies && !!cookie
const proxyCookie = v?.proxyCookies && !!requestHeaders?.cookie

let headers = v?.headers as Record<string, string> | undefined
const serverHeaders = (process.server && (typeof headers?.serverOnly === 'object' && headers?.serverOnly)) || {}
Expand All @@ -30,13 +33,18 @@ export default defineNuxtPlugin((nuxtApp) => {
delete headers.serverOnly
}

for (const header of (v?.proxyHeaders || [])) {
if (!requestHeaders?.[header]) { continue }

headers = { ...headers, [header]: requestHeaders?.[header as keyof typeof requestHeaders] }
}

const opts = {
...((proxyCookie || v?.token?.value || v?.headers) && {
headers: {
...(headers && { ...headers, ...serverHeaders }),
...(proxyCookie && { cookie })
}
}),
headers: {
...headers,
...serverHeaders,
...(proxyCookie && { cookie: requestHeaders?.cookie })
},
...v?.corsOptions
}

Expand All @@ -59,9 +67,9 @@ export default defineNuxtPlugin((nuxtApp) => {
if (v.tokenStorage?.mode === 'cookie') {
if (process.client) {
token.value = useCookie(v.tokenStorage.name!).value
} else if (cookie) {
} else if (requestHeaders?.cookie) {
const cookieName = `${v.tokenStorage.name}=`
token.value = cookie.split(';').find(c => c.trim().startsWith(cookieName))?.split('=')?.[1]
token.value = requestHeaders?.cookie.split(';').find(c => c.trim().startsWith(cookieName))?.split('=')?.[1]
}
} else if (process.client && v.tokenStorage?.mode === 'localStorage') {
const storedToken = localStorage.getItem(v.tokenStorage.name!)
Expand Down
8 changes: 8 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ export interface GqlClient<T = string> {
* */
proxyCookies?: boolean


/**
* Headers to be passed from the browser to the GraphQL API in SSR mode.
*
* @type {string[]}
*/
proxyHeaders?: string[]

/**
* Specify CORS options to be used for client-side requests.
* @type {object}
Expand Down

0 comments on commit 7c2e5cb

Please sign in to comment.