38 lines
861 B
TypeScript
38 lines
861 B
TypeScript
import {
|
|
FastifyInstance,
|
|
FastifyPluginAsync,
|
|
FastifyReply,
|
|
FastifyRequest,
|
|
} from "fastify";
|
|
import fastifyPlugin from "fastify-plugin";
|
|
|
|
/**
|
|
* 将post 和 get 的参数统一到 req.params
|
|
*/
|
|
declare module "fastify" {
|
|
interface FastifyInstance {
|
|
zReqParser: (request: FastifyRequest, reply: FastifyReply) => {};
|
|
}
|
|
}
|
|
const zReqParserPlugin: FastifyPluginAsync = async function (
|
|
fastify: FastifyInstance,
|
|
options?: any
|
|
) {
|
|
fastify.addHook(
|
|
"preValidation",
|
|
async (request: FastifyRequest, reply: FastifyReply) => {
|
|
let params = request.params || {};
|
|
if (request.query) {
|
|
Object.assign(params, request.query);
|
|
}
|
|
if (request.body) {
|
|
Object.assign(params, request.body);
|
|
}
|
|
request.params = params;
|
|
}
|
|
);
|
|
return;
|
|
};
|
|
|
|
export default fastifyPlugin(zReqParserPlugin, "4.x");
|