返回 AiToEarn
http-adapter.factory.ts
根目录 / project / aitoearn-backend / libs / nest-mcp / src / adapters / http-adapter.factory.ts
1 import { HttpAdapter } from '../interfaces/http-adapter.interface'
2 import { ExpressHttpAdapter } from './express-http.adapter'
3
4 /**
5 * Factory for creating HTTP adapters based on the detected framework
6 */
7 export class HttpAdapterFactory {
8 private static expressAdapter: ExpressHttpAdapter | null = null
9
10 /**
11 * Get the appropriate HTTP adapter for the given request/response objects
12 */
13 static getAdapter(req: any, res: any): HttpAdapter {
14 // Check if it's Express by looking for Express-specific properties
15 if (this.isExpressRequest(req) && this.isExpressResponse(res)) {
16 if (!this.expressAdapter) {
17 this.expressAdapter = new ExpressHttpAdapter()
18 }
19 return this.expressAdapter
20 }
21
22 // Default to Express adapter for backward compatibility
23 if (!this.expressAdapter) {
24 this.expressAdapter = new ExpressHttpAdapter()
25 }
26 return this.expressAdapter
27 }
28
29 /**
30 * Check if the request object is from Express
31 */
32 private static isExpressRequest(req: any): boolean {
33 return Boolean(
34 req
35 && typeof req === 'object'
36 && typeof req.get === 'function'
37 && req.method !== undefined
38 && req.url !== undefined
39 && !req.routeOptions, // Fastify-specific property
40 )
41 }
42
43 /**
44 * Check if the response object is from Express
45 */
46 private static isExpressResponse(res: any): boolean {
47 return Boolean(
48 res
49 && typeof res === 'object'
50 && typeof res.status === 'function'
51 && typeof res.json === 'function'
52 && typeof res.send === 'function'
53 && res.headersSent !== undefined
54 && !res.sent, // Fastify-specific property
55 )
56 }
57 }
58
58 lines TYPESCRIPT