feat: refactor useCustomFetch to use createUseFetch#207
feat: refactor useCustomFetch to use createUseFetch#207maximepvrt wants to merge 1 commit intonuxt:mainfrom
Conversation
|
@maximepvrt is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThe pull request refactors a custom fetch composable from an explicit function declaration to a factory-based approach using Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@examples/advanced/use-custom-fetch-composable/composables/useCustomFetch.ts`:
- Around line 1-4: The factory callback in useCustomFetch currently spreads
callerOptions into the returned object which allows a caller-provided $fetch to
override the intended useNuxtApp().$customFetch; update the callback for
createUseFetch (in useCustomFetch) to return only $fetch:
useNuxtApp().$customFetch (and any other fixed defaults) and remove the
...callerOptions spread so $customFetch is enforced via the callback override
mechanism (keep callerOptions passed to createUseFetch as-is but do not merge it
into the factory return), ensuring plugins/customFetch.ts auth/401 handling
remains in effect.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9f8f0b69-f8b7-46d2-8e88-d970edb0031c
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (3)
examples/advanced/use-custom-fetch-composable/composables/useCustomFetch.tspackage.jsonshared/package.json
| export const useCustomFetch = createUseFetch(callerOptions => ({ | ||
| $fetch: useNuxtApp().$customFetch as typeof $fetch, | ||
| ...callerOptions, | ||
| })) No newline at end of file |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# Description: Inspect the installed/resolved Nuxt version's createUseFetch implementation.
# Expected: callback/factory options are applied after caller opts, so factory-returned `$fetch` overrides caller `$fetch`.
set -euo pipefail
version="$(jq -r '
[
.dependencies.nuxt?,
.devDependencies.nuxt?,
.resolutions.nuxt?,
.overrides.nuxt?
] | map(select(. != null)) | first // empty
' package.json | sed 's/^[^0-9]*//')"
if [ -z "$version" ]; then
echo "No Nuxt version found in root package.json"
exit 1
fi
curl -fsSL "https://raw.githubusercontent.com/nuxt/nuxt/v${version}/packages/nuxt/src/app/composables/fetch.ts" \
| rg -n -C 6 'Merge factory options|typeof options === .function|factoryOptions'Repository: nuxt/examples
Length of output: 2081
Keep $customFetch as the enforced fetch implementation.
Spreading callerOptions after $fetch in the factory callback allows a caller-provided $fetch to override $customFetch, bypassing auth header and 401 handling from plugins/customFetch.ts.
In Nuxt's callback mode (function option to createUseFetch), the factory-returned object merges after user options (applying as overrides). By spreading callerOptions inside the return, you're including the user's options in factoryOptions, which means if the user passed $fetch, it ends up in both places—with the later spread in factoryOptions retaining the user's value rather than the intended $customFetch.
Remove the callerOptions spread so the factory return enforces $customFetch via the callback override mechanism:
Proposed fix
-export const useCustomFetch = createUseFetch(callerOptions => ({
+export const useCustomFetch = createUseFetch(() => ({
$fetch: useNuxtApp().$customFetch as typeof $fetch,
- ...callerOptions,
}))🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@examples/advanced/use-custom-fetch-composable/composables/useCustomFetch.ts`
around lines 1 - 4, The factory callback in useCustomFetch currently spreads
callerOptions into the returned object which allows a caller-provided $fetch to
override the intended useNuxtApp().$customFetch; update the callback for
createUseFetch (in useCustomFetch) to return only $fetch:
useNuxtApp().$customFetch (and any other fixed defaults) and remove the
...callerOptions spread so $customFetch is enforced via the callback override
mechanism (keep callerOptions passed to createUseFetch as-is but do not merge it
into the factory return), ensuring plugins/customFetch.ts auth/401 handling
remains in effect.
use the new
createUseFetch🔗 Linked issue
📚 Description