import { NextApiRequest, NextApiResponse } from "next"
import logging, { Logging } from "../logging"

export interface Result<T> {
  message: string
  statusCode?: number
  body?: T
  type?: Logging[]
}

export default function sendResponse<T>(payload: {
  req: NextApiRequest
  res: NextApiResponse<T>
  result: Result<T>
}) {
  const { req, res, result } = payload
  res.status(result.statusCode ?? 200)
  result.body ? res.json(result.body) : res.end()
  logging(result.message, result.type ?? ["debug"], req)
}
