v0.3.3: resolve fetch at call time, not factory-creation
Tests that vi.stubGlobal('fetch', ...) AFTER module import (standard
vitest pattern) couldn't see their stub because the factory captured
globalThis.fetch at createCoreFetch() time. Switch to a thin wrapper
that does the lookup per call.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
de150e790d
commit
84693a7af5
2 changed files with 7 additions and 3 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@uschuster/webapp-scaffold",
|
"name": "@uschuster/webapp-scaffold",
|
||||||
"version": "0.3.2",
|
"version": "0.3.3",
|
||||||
"description": "Shared build scripts + Vite config factories for webapp-template-derived projects.",
|
"description": "Shared build scripts + Vite config factories for webapp-template-derived projects.",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
|
|
||||||
|
|
@ -96,7 +96,11 @@ const MUTATING = new Set(['POST', 'PUT', 'PATCH', 'DELETE']);
|
||||||
|
|
||||||
export function createCoreFetch(opts: CoreFetchOptions = {}): CoreFetch {
|
export function createCoreFetch(opts: CoreFetchOptions = {}): CoreFetch {
|
||||||
const base = (opts.baseUrl ?? '').replace(/\/$/, '');
|
const base = (opts.baseUrl ?? '').replace(/\/$/, '');
|
||||||
const fetchImpl = opts.fetchImpl ?? fetch;
|
// Resolve fetch at call time, not construction time, so tests that
|
||||||
|
// `vi.stubGlobal('fetch', ...)` after module import see the stub.
|
||||||
|
const callFetch: typeof fetch = opts.fetchImpl
|
||||||
|
? opts.fetchImpl
|
||||||
|
: ((...args) => fetch(...(args as Parameters<typeof fetch>))) as typeof fetch;
|
||||||
const shape: ResponseShape = opts.responseShape ?? 'wrapped';
|
const shape: ResponseShape = opts.responseShape ?? 'wrapped';
|
||||||
|
|
||||||
function wrap(data: unknown, status: number, headers: Headers): unknown {
|
function wrap(data: unknown, status: number, headers: Headers): unknown {
|
||||||
|
|
@ -129,7 +133,7 @@ export function createCoreFetch(opts: CoreFetchOptions = {}): CoreFetch {
|
||||||
|
|
||||||
let r: Response;
|
let r: Response;
|
||||||
try {
|
try {
|
||||||
r = await fetchImpl(`${base}${url}`, {
|
r = await callFetch(`${base}${url}`, {
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
...init,
|
...init,
|
||||||
headers,
|
headers,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue