Skip to content

Commit

Permalink
fix: forward headers when generating (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
SoaresMG authored Aug 14, 2024
1 parent ddff480 commit 3ff885f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/tidy-readers-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nuxt-mock-server": patch
---

Forward headers when requesting during mock generation
71 changes: 44 additions & 27 deletions src/runtime/server/management/generate-preset.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,9 @@
import consola from "consola";
import type { createFetch as createLocalFetch } from "unenv/runtime/fetch/index";
import type { H3Event } from "h3";
import { MAIN_HEADER_KEY, PRESET_GENERATION_HEADER_KEY } from "../../utils";
import { MAIN_HEADER_KEY, PRESET_GENERATION_HEADER_KEY, transformHeaders } from "../../utils";
import { useNitroApp, useRuntimeConfig } from "#imports";

async function request(localFetch: ReturnType<typeof createLocalFetch>, route: string, preset: string, isAutoMode: boolean, debug: boolean) {
try {
const response = await localFetch(route, {
method: "GET",
cache: "no-cache",
headers: {
[PRESET_GENERATION_HEADER_KEY]: preset,
},
});

if (!response.headers.has(MAIN_HEADER_KEY) && debug) {
consola.warn(isAutoMode
? `[mock-server] Route ${route} was set but not catched by \`pathMatch\` nor by any \`defineMockInterceptorHandler\`.`
: `[mock-server] Route ${route} was set but not catched by an handler with \`defineMockInterceptorHandler\`.`,
);
return;
}
}
catch (e) {
consola.error(`[mock-server] Failed to generate ${route}`, e);
throw e;
}
}

export const generatePreset = async (
event: H3Event,
_preset: string | undefined = undefined,
Expand All @@ -50,7 +26,16 @@ export const generatePreset = async (
}

if (generate?.parallel) {
const routeCalls = await Promise.allSettled(routes.map(route => request(nitro.localFetch, route, preset, !!auto, !!debug)));
const routeCalls = await Promise.allSettled(
routes.map(route => request(
nitro.localFetch,
event.headers,
route,
preset,
!!auto,
!!debug,
)),
);

const successfullCalls = routeCalls.filter(call => call.status === "fulfilled");
const failedCalls = routeCalls.filter(call => call.status === "rejected");
Expand All @@ -61,7 +46,39 @@ export const generatePreset = async (
}
else {
for (const route of routes) {
await request(nitro.localFetch, route, preset, !!auto, !!debug);
await request(nitro.localFetch, event.headers, route, preset, !!auto, !!debug);
}
}
};

async function request(
localFetch: ReturnType<typeof createLocalFetch>,
headers: Headers,
route: string,
preset: string,
isAutoMode: boolean,
debug: boolean,
) {
try {
const response = await localFetch(route, {
method: "GET",
cache: "no-cache",
headers: {
...transformHeaders(headers),
[PRESET_GENERATION_HEADER_KEY]: preset,
},
});

if (!response.headers.has(MAIN_HEADER_KEY) && debug) {
consola.warn(isAutoMode
? `[mock-server] Route ${route} was set but not catched by \`pathMatch\` nor by any \`defineMockInterceptorHandler\`.`
: `[mock-server] Route ${route} was set but not catched by an handler with \`defineMockInterceptorHandler\`.`,
);
return;
}
}
catch (e) {
consola.error(`[mock-server] Failed to generate ${route}`, e);
throw e;
}
}

0 comments on commit 3ff885f

Please sign in to comment.