Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add debug option #67

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cool-readers-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nuxt-mock-server": patch
---

Replace `suppressAllLogs` with `debug` option
2 changes: 1 addition & 1 deletion playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default defineNuxtConfig({
mocks: {
enabled: true,
auto: false,
supressAllLogs: true,
debug: true,
generate: {
routes: [
"/api/pages/product?slug=/product/Cheese",
Expand Down
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default defineNuxtModule<ModuleOptions>({
devtools: true,
preset: DEFAULT_PRESET,
auto: true,
supressAllLogs: false,
debug: false,
},
async setup(_options, nuxt) {
const resolver = createResolver(import.meta.url);
Expand All @@ -68,7 +68,7 @@ export default defineNuxtModule<ModuleOptions>({
return;
}

if (!options.supressAllLogs) {
if (options.debug) {
logger.info(`Mock server is enabled for ${options.pathMatch}`);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/server/management/delete-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function deletePreset(event: H3Event, name: string, options: Delete
throw new TypeError("Mock server is not enabled");
}

if (!event.context.preset && !mockServer.supressAllLogs) {
if (!event.context.preset) {
consola.warn("[mock-server] No preset is set in the event context, maybe you forgot to use `definePresetHandler`?");
}

Expand Down
12 changes: 6 additions & 6 deletions src/runtime/server/management/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { NitroApp } from "nitropack";
import { MAIN_HEADER_KEY, PRESET_GENERATION_HEADER_KEY } from "../../utils";
import { useNitroApp } from "#imports";

async function request(localFetch: ReturnType<typeof createLocalFetch>, route: string, preset: string, isAutoMode: boolean, suppressLogs: boolean) {
async function request(localFetch: ReturnType<typeof createLocalFetch>, route: string, preset: string, isAutoMode: boolean, debug: boolean) {
try {
const response = await localFetch(route, {
method: "GET",
Expand All @@ -15,7 +15,7 @@ async function request(localFetch: ReturnType<typeof createLocalFetch>, route: s
},
});

if (!response.headers.has(MAIN_HEADER_KEY) && !suppressLogs) {
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\`.`,
Expand All @@ -36,7 +36,7 @@ export const generatePreset = async (
) => {
const nitro = _nitro || useNitroApp();

const { mockServer: { preset: DefaultPreset, generate, auto, supressAllLogs } = {} } = runtimeConfig;
const { mockServer: { preset: DefaultPreset, generate, auto, debug } = {} } = runtimeConfig;
const preset = _preset || DefaultPreset;

if (!generate || !generate.routes?.length) {
Expand All @@ -51,18 +51,18 @@ export const generatePreset = async (
await nitro.hooks.callHook("mock-server:extendRoutes", { routes, preset });

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

const successfullCalls = routeCalls.filter(call => call.status === "fulfilled");
const failedCalls = routeCalls.filter(call => call.status === "rejected");

if (routeCalls.length && !supressAllLogs) {
if (routeCalls.length && debug) {
consola.info(`[mock-server] Generated ${successfullCalls.length} routes${failedCalls.length ? `and failed for ${failedCalls.length} routes` : ""}`);
}
}
else {
for (const route of routes) {
await request(nitro.localFetch, route, preset, !!auto, !!supressAllLogs);
await request(nitro.localFetch, route, preset, !!auto, !!debug);
}
}
};
4 changes: 2 additions & 2 deletions src/runtime/types/module-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ export interface ModuleOptions {
*/
auto?: boolean;
/**
* Suppresses all logs emitted by the module except errors.
* Enables all logs emitted by the module otherwise only errors are emitted.
*
* @default false
*/
supressAllLogs?: boolean;
debug?: boolean;
/**
* If defined generates mock files on the specified routes.
*
Expand Down