"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.successHandlerPlugin = void 0;
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
exports.successHandlerPlugin = (0, fastify_plugin_1.default)(async (app) => {
    app.addHook("preSerialization", async (req, reply, payload) => {
        if (reply.statusCode < 200 || reply.statusCode >= 300) {
            return payload;
        }
        if (payload === undefined || payload === null) {
            return payload;
        }
        const type = reply.getHeader("content-type");
        if (typeof type === "string" && !type.includes("application/json")) {
            return payload;
        }
        return {
            ok: true,
            data: payload,
            meta: {
                requestId: req.id,
                ts: Date.now(),
            },
        };
    });
});
exports.default = exports.successHandlerPlugin;
