File size: 776 Bytes
fb38ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import fp from "fastify-plugin";
import { type FastifyInstance, type FastifyPluginAsync } from "fastify";

const customBodyParser: FastifyPluginAsync = async (fastify: FastifyInstance) => {
  fastify.addContentTypeParser(
    "application/json",
    { parseAs: "buffer" },
    function (req, body, done) {
      try {
        switch (true) {
          case req.url.includes("/release"):
            // Skip parsing for release endpoints
            done(null, null);
            break;

          default:
            // Parse JSON for all other requests
            done(null, JSON.parse(body.toString()));
        }
      } catch (error) {
        done(error as Error, undefined);
      }
    },
  );
};

export default fp(customBodyParser, { name: "custom-body-parser" });