返回 AiToEarn
express-http.adapter.ts
根目录 / project / aitoearn-backend / libs / nest-mcp / src / adapters / express-http.adapter.ts
1 import type { Request, Response } from 'express'
2 import {
3 HttpAdapter,
4 HttpRequest,
5 HttpResponse,
6 } from '../interfaces/http-adapter.interface'
7
8 /**
9 * Express HTTP adapter that implements the generic HTTP interface
10 */
11 export class ExpressHttpAdapter implements HttpAdapter {
12 adaptRequest(req: Request): HttpRequest {
13 return {
14 url: req.url,
15 method: req.method,
16 headers: req.headers as Record<string, string | string[] | undefined>,
17 query: req.query,
18 body: req.body,
19 params: req.params as Record<string, string>,
20 get: (name: string) => req.get(name),
21 raw: req,
22 }
23 }
24
25 adaptResponse(res: Response): HttpResponse {
26 return {
27 status: (code: number) => {
28 res.status(code)
29 return this.adaptResponse(res)
30 },
31 json: (body: any) => {
32 res.json(body)
33 return this.adaptResponse(res)
34 },
35 send: (body: string) => {
36 res.send(body)
37 return this.adaptResponse(res)
38 },
39 write: (chunk: any) => res.write(chunk),
40 setHeader: (name: string, value: string | string[]) =>
41 res.setHeader(name, value),
42 get headersSent() {
43 return res.headersSent
44 },
45 get writable() {
46 return res.writable
47 },
48 get closed() {
49 return res.destroyed || res.writableEnded
50 },
51 on: (event: string, listener: (...args: any[]) => void) => {
52 res.on(event, listener)
53 },
54 raw: res,
55 }
56 }
57 }
58
58 lines TYPESCRIPT