UNPKG

msw-mock-gen

Version:

A Vite plugin that automatically generates MSW (Mock Service Worker) handlers from your API calls by watching TypeScript/JavaScript files and parsing URL patterns. Supports Vite 2+ and Node.js 12+ for maximum compatibility.

30 lines (27 loc) 1.07 kB
/** * Creates the content for a mock data file * * @param hookName - Name of the hook (e.g., useAssetsQuery) * @param dataType - The data type for the hook * @param type - Whether this is a query or mutation * @returns The TypeScript content for the mock data file */ export function createMockDataContent(hookName, dataType, type) { const importStatement = `import { ${hookName} } from "./${hookName}";`; const typeDefinition = `type QueryData = ReturnType<typeof ${hookName}>["data"];`; const exportStatement = `export const ${getMockDataName(hookName)}: QueryData = undefined; // TODO: Replace with mock ${type} data of type: ${dataType}`; return `${importStatement} ${typeDefinition} ${exportStatement} `; } /** * Gets the mock data variable name from a hook name * * @param hookName - Name of the hook (e.g., useAssetsQuery) * @returns The mock data variable name */ function getMockDataName(hookName) { const baseName = hookName.replace(/^use/, "mock"); return baseName.charAt(0).toLowerCase() + baseName.slice(1) + "Data"; }