All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
43 lines
971 B
TypeScript
43 lines
971 B
TypeScript
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<Context.Data>} 返回包含请求上下文的对象。
|
|
*/
|
|
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
|