import { v4 as uuid } from "uuid" import loggerIns from "../log" import { AttachService, LarkService } from "../services" import { Context } from "../types" import NetTool from "./netTool" /** * 生成请求上下文。 * * @param {Request} req - 请求对象。 * @returns {Promise} 返回包含请求上下文的对象。 */ const genContext = async (req: Request) => { const requestId = uuid() const logger = loggerIns.child({ requestId }) const genResp = new NetTool({ requestId }) const larkService = new LarkService("egg", requestId) const attachService = new AttachService({ requestId }) let body: any = null let text: string = "" try { text = await req.text() body = JSON.parse(text) } catch { /* empty */ } logger.debug(`req body: ${text}`) return { req, requestId, logger, genResp, body, text, larkService, attachService, } as Context.Data } export default genContext