-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent object merging with HeadersList
- Loading branch information
1 parent
aff1d39
commit 393ddca
Showing
4 changed files
with
62 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,34 @@ | ||
import type { FetchContext } from 'ofetch' | ||
import type { ConsolaInstance } from 'consola' | ||
import type { NuxtApp } from '#app' | ||
|
||
/** | ||
* Modify request before sending it to the Laravel API | ||
* @param app Nuxt application instance | ||
* @param ctx Fetch context | ||
* @param logger Module logger instance | ||
*/ | ||
export default async function handleRequestHeaders( | ||
app: NuxtApp, | ||
ctx: FetchContext, | ||
) { | ||
logger: ConsolaInstance, | ||
): Promise<void> { | ||
const method = ctx.options.method?.toLowerCase() ?? 'get' | ||
|
||
ctx.options.headers = { | ||
Accept: 'application/json', | ||
...ctx.options.headers, | ||
if (!ctx.options.headers) { | ||
ctx.options.headers = {} | ||
} | ||
|
||
Object.assign(ctx.options.headers!, { Accept: 'application/json' }) | ||
|
||
// https://laravel.com/docs/10.x/routing#form-method-spoofing | ||
if (method === 'put' && ctx.options.body instanceof FormData) { | ||
ctx.options.method = 'POST' | ||
ctx.options.body.append('_method', 'PUT') | ||
} | ||
|
||
logger.debug( | ||
'[handleRequestHeaders] headers modified', | ||
Object.keys(ctx.options.headers!), | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters