asu_tools_service/request.js
2026-03-02 16:30:21 +03:00

30 lines
677 B
JavaScript

/**
* @param {import('node:http').IncomingMessage} req
* @returns {HTTPRequest}
*/
export function createRequest(req) {
return {
get req() {
return req;
},
async parseJson() {
return new Promise((resolve, reject) => {
/** @type {Buffer[]} */
const body = [];
this.req.on("data", (chunk) => body.push(chunk));
this.req.on("end", () => {
try {
const parsedJson = JSON.parse(Buffer.concat(body).toString());
resolve(parsedJson);
} catch (error) {
reject(error);
}
});
this.req.on("error", (err) => reject(err));
});
}
};
}