feat: 添加mock数据
This commit is contained in:
@@ -34,7 +34,7 @@ export async function startMockServiceWorker() {
|
|||||||
// 🎯 严格模式(关键配置)
|
// 🎯 严格模式(关键配置)
|
||||||
// 'bypass': 未定义 Mock 的请求自动转发到真实后端
|
// 'bypass': 未定义 Mock 的请求自动转发到真实后端
|
||||||
// 'warn': 未定义的请求会显示警告(调试用)
|
// 'warn': 未定义的请求会显示警告(调试用)
|
||||||
// 'error': 未定义的请求会抛出错误(严格模式)✅ 当前使用
|
// 'error': 未定义的请求会抛出错误(严格模式)✅ 当前使用 穿透模式 bypass
|
||||||
onUnhandledRequest: 'error',
|
onUnhandledRequest: 'error',
|
||||||
|
|
||||||
// 自定义 Service Worker URL(如果需要)
|
// 自定义 Service Worker URL(如果需要)
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { companyHandlers } from './company';
|
|||||||
import { marketHandlers } from './market';
|
import { marketHandlers } from './market';
|
||||||
import { financialHandlers } from './financial';
|
import { financialHandlers } from './financial';
|
||||||
import { limitAnalyseHandlers } from './limitAnalyse';
|
import { limitAnalyseHandlers } from './limitAnalyse';
|
||||||
|
import { posthogHandlers } from './posthog';
|
||||||
|
|
||||||
// 可以在这里添加更多的 handlers
|
// 可以在这里添加更多的 handlers
|
||||||
// import { userHandlers } from './user';
|
// import { userHandlers } from './user';
|
||||||
@@ -30,5 +31,6 @@ export const handlers = [
|
|||||||
...marketHandlers,
|
...marketHandlers,
|
||||||
...financialHandlers,
|
...financialHandlers,
|
||||||
...limitAnalyseHandlers,
|
...limitAnalyseHandlers,
|
||||||
|
...posthogHandlers,
|
||||||
// ...userHandlers,
|
// ...userHandlers,
|
||||||
];
|
];
|
||||||
|
|||||||
85
src/mocks/handlers/posthog.js
Normal file
85
src/mocks/handlers/posthog.js
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
// src/mocks/handlers/posthog.js
|
||||||
|
// PostHog 埋点请求 Mock Handler
|
||||||
|
|
||||||
|
import { http, HttpResponse } from 'msw';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PostHog 埋点 Mock Handler
|
||||||
|
* 拦截所有发往 PostHog 的埋点请求,避免在 Mock 模式下产生 500 错误
|
||||||
|
*/
|
||||||
|
export const posthogHandlers = [
|
||||||
|
// PostHog 事件追踪接口
|
||||||
|
http.post('https://us.i.posthog.com/e/', async ({ request }) => {
|
||||||
|
try {
|
||||||
|
// 读取埋点数据(可选,用于调试)
|
||||||
|
const body = await request.text();
|
||||||
|
|
||||||
|
// 开发环境输出埋点日志(可选,方便调试)
|
||||||
|
if (process.env.NODE_ENV === 'development' && process.env.REACT_APP_LOG_POSTHOG === 'true') {
|
||||||
|
console.log('[Mock] PostHog 埋点请求:', {
|
||||||
|
url: request.url,
|
||||||
|
bodyPreview: body.substring(0, 150) + (body.length > 150 ? '...' : ''),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回成功响应(模拟 PostHog 服务器响应)
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ status: 1 },
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Mock] PostHog handler error:', error);
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ status: 0, error: 'Mock handler error' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
// PostHog batch 批量事件追踪接口(可选)
|
||||||
|
http.post('https://us.i.posthog.com/batch/', async ({ request }) => {
|
||||||
|
try {
|
||||||
|
const body = await request.text();
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV === 'development' && process.env.REACT_APP_LOG_POSTHOG === 'true') {
|
||||||
|
console.log('[Mock] PostHog 批量埋点请求:', {
|
||||||
|
url: request.url,
|
||||||
|
bodyPreview: body.substring(0, 150) + (body.length > 150 ? '...' : ''),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ status: 1 },
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Mock] PostHog batch handler error:', error);
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ status: 0, error: 'Mock handler error' },
|
||||||
|
{ status: 500 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
|
||||||
|
// PostHog decide 接口(功能开关、特性标志)
|
||||||
|
http.post('https://us.i.posthog.com/decide/', async ({ request }) => {
|
||||||
|
try {
|
||||||
|
if (process.env.NODE_ENV === 'development' && process.env.REACT_APP_LOG_POSTHOG === 'true') {
|
||||||
|
const body = await request.json();
|
||||||
|
console.log('[Mock] PostHog decide 请求:', body);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 返回空的特性标志配置
|
||||||
|
return HttpResponse.json({
|
||||||
|
featureFlags: {},
|
||||||
|
sessionRecording: false,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('[Mock] PostHog decide handler error:', error);
|
||||||
|
return HttpResponse.json(
|
||||||
|
{ featureFlags: {}, sessionRecording: false },
|
||||||
|
{ status: 200 }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
];
|
||||||
Reference in New Issue
Block a user