Skip to content

Commit

Permalink
Merge pull request #192 from manchenkoff/fix-ofetch-headers-normaliza…
Browse files Browse the repository at this point in the history
…tion

fix: use normalized ofetch headers
  • Loading branch information
manchenkoff authored Oct 2, 2024
2 parents cf16ed8 + 22602b6 commit 77ca461
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"changelogen": "^0.5.5",
"eslint": "^9.9.1",
"nuxt": "^3.13.2",
"ofetch": "^1.4.0",
"typescript": "^5.5.4",
"vitest": "^2.0.5",
"vue-tsc": "^2.1.2"
Expand Down
21 changes: 12 additions & 9 deletions pnpm-lock.yaml

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

4 changes: 2 additions & 2 deletions src/runtime/httpFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function createHttpClient(nuxtApp: NuxtApp, logger: ConsolaInstance): $Fe

logger.trace(
`Request headers for "${context.request.toString()}"`,
context.options.headers,
Object.fromEntries(context.options.headers.entries()),
)
},

Expand All @@ -94,7 +94,7 @@ export function createHttpClient(nuxtApp: NuxtApp, logger: ConsolaInstance): $Fe

logger.trace(
`Response headers for "${context.request.toString()}"`,
context.response?.headers,
context.response ? Object.fromEntries(context.response.headers.entries()) : {},
)
},

Expand Down
6 changes: 2 additions & 4 deletions src/runtime/interceptors/common/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { FetchContext } from 'ofetch'
import type { ConsolaInstance } from 'consola'
import { appendRequestHeaders } from '../../utils/headers'
import type { NuxtApp } from '#app'

/**
Expand All @@ -16,10 +17,7 @@ export default async function handleRequestHeaders(
const method = ctx.options.method?.toLowerCase() ?? 'get'
const headersToAdd = { Accept: 'application/json' }

ctx.options.headers = Object.assign(
ctx.options.headers || {},
headersToAdd,
)
ctx.options.headers = appendRequestHeaders(ctx.options.headers, headersToAdd)

// https://laravel.com/docs/10.x/routing#form-method-spoofing
if (method === 'put' && ctx.options.body instanceof FormData) {
Expand Down
15 changes: 8 additions & 7 deletions src/runtime/interceptors/cookie/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { FetchContext } from 'ofetch'
import type { ConsolaInstance } from 'consola'
import { useSanctumConfig } from '../../composables/useSanctumConfig'
import type { ModuleOptions } from '../../types/options'
import { appendRequestHeaders } from '../../utils/headers'
import { type NuxtApp, useCookie, useRequestHeaders, useRequestURL } from '#app'

const SECURE_METHODS = new Set(['post', 'delete', 'put', 'patch'])
Expand All @@ -15,10 +16,10 @@ const COOKIE_OPTIONS: { readonly: true } = { readonly: true }
* @returns Headers collection to pass to the API
*/
function useServerHeaders(
headers: HeadersInit | undefined,
headers: Headers,
config: ModuleOptions,
logger: ConsolaInstance,
): HeadersInit {
): Headers {
const clientHeaders = useRequestHeaders(['cookie', 'user-agent'])
const origin = config.origin ?? useRequestURL().origin

Expand All @@ -34,7 +35,7 @@ function useServerHeaders(
Object.keys(headersToAdd),
)

return Object.assign(headers || {}, headersToAdd)
return appendRequestHeaders(headers, headersToAdd)
}

/**
Expand Down Expand Up @@ -67,10 +68,10 @@ async function initCsrfCookie(
* @returns Headers collection to pass to the API
*/
async function useCsrfHeader(
headers: HeadersInit | undefined,
headers: Headers,
config: ModuleOptions,
logger: ConsolaInstance,
): Promise<HeadersInit> {
): Promise<Headers> {
if (config.csrf.cookie === undefined) {
throw new Error('`sanctum.csrf.cookie` is not defined')
}
Expand All @@ -88,14 +89,14 @@ async function useCsrfHeader(

if (!csrfToken.value) {
logger.warn(`${config.csrf.cookie} cookie is missing, unable to set ${config.csrf.header} header`)
return headers || {}
return headers
}

const headersToAdd = { [config.csrf.header]: csrfToken.value }

logger.debug(`[request] added csrf token header`, Object.keys(headersToAdd))

return Object.assign(headers || {}, headersToAdd)
return appendRequestHeaders(headers, headersToAdd)
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/interceptors/token/request.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { FetchContext } from 'ofetch'
import type { ConsolaInstance } from 'consola'
import { useSanctumAppConfig } from '../../composables/useSanctumAppConfig'
import { appendRequestHeaders } from '../../utils/headers'
import type { NuxtApp } from '#app'

/**
Expand Down Expand Up @@ -34,5 +35,5 @@ export default async function handleRequestTokenHeader(
Object.keys(headersToAdd),
)

ctx.options.headers = Object.assign(ctx.options.headers || {}, headersToAdd)
ctx.options.headers = appendRequestHeaders(ctx.options.headers, headersToAdd)
}
7 changes: 7 additions & 0 deletions src/runtime/utils/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function appendRequestHeaders(headers: Headers, append: Record<string, string>): Headers {
for (const [key, value] of Object.entries(append)) {
headers.set(key, value)
}

return headers
}

0 comments on commit 77ca461

Please sign in to comment.