133 lines
4.1 KiB
JavaScript
133 lines
4.1 KiB
JavaScript
// 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 }
|
|
);
|
|
}
|
|
}),
|
|
|
|
// PostHog session recording 接口(会话录制)
|
|
http.post('https://us.i.posthog.com/s/', 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 session recording 请求:', {
|
|
url: request.url,
|
|
bodyPreview: body.substring(0, 100) + (body.length > 100 ? '...' : ''),
|
|
});
|
|
}
|
|
|
|
// 返回成功响应
|
|
return HttpResponse.json(
|
|
{ status: 1 },
|
|
{ status: 200 }
|
|
);
|
|
} catch (error) {
|
|
console.error('[Mock] PostHog session recording handler error:', error);
|
|
return HttpResponse.json(
|
|
{ status: 0, error: 'Mock handler error' },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}),
|
|
|
|
// PostHog feature flags 接口(特性标志查询)
|
|
http.post('https://us.i.posthog.com/flags/', async ({ request }) => {
|
|
try {
|
|
if (process.env.NODE_ENV === 'development' && process.env.REACT_APP_LOG_POSTHOG === 'true') {
|
|
console.log('[Mock] PostHog feature flags 请求:', request.url);
|
|
}
|
|
|
|
// 返回空的特性标志
|
|
return HttpResponse.json({
|
|
featureFlags: {},
|
|
featureFlagPayloads: {},
|
|
});
|
|
} catch (error) {
|
|
console.error('[Mock] PostHog flags handler error:', error);
|
|
return HttpResponse.json(
|
|
{ featureFlags: {}, featureFlagPayloads: {} },
|
|
{ status: 200 }
|
|
);
|
|
}
|
|
}),
|
|
];
|