976bde37cc
与 REST/OpenAPI 并存,按实例权限自动 tools/list,免手填工具。 Co-authored-by: Cursor <cursoragent@cursor.com>
35 lines
1002 B
TypeScript
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,
|
|
};
|
|
}),
|
|
);
|
|
}
|
|
}
|
|
|