Files
dukang/server/dukang-api/src/common/interceptors/response.interceptor.ts
T
jacy 976bde37cc feat(wecom): 企微 API 插件增加 MCP Streamable HTTP 端点
与 REST/OpenAPI 并存,按实例权限自动 tools/list,免手填工具。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-08 16:57:42 +08:00

35 lines
1002 B
TypeScript

import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable, map } from 'rxjs';
export function skipWecomPluginResponseWrap(url?: string): boolean {
const path = (url || '').split('?')[0];
return path.endsWith('/wecom/plugin/openapi.json') || path.includes('/wecom/plugin/mcp');
}
@Injectable()
export class ResponseInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const req = context.switchToHttp().getRequest<{ originalUrl?: string; url?: string }>();
const res = context.switchToHttp().getResponse<{ headersSent?: boolean }>();
if (skipWecomPluginResponseWrap(req.originalUrl || req.url)) {
return next.handle();
}
return next.handle().pipe(
map((data) => {
if (res.headersSent) return data;
return {
code: 0,
message: 'ok',
data: data ?? null,
};
}),
);
}
}