File size: 711 Bytes
a86df80
 
 
65cfba9
 
a86df80
 
 
 
 
65cfba9
 
 
a86df80
 
6c2bcb4
a86df80
 
e5ae926
a86df80
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { toOpenAI } from "@/services/api";
import { NextApiRequest, NextApiResponse } from "next";
import { AxiosError } from "axios";
import process from "node:process";
import { createClient } from "@/services/api/openai";

export default async function handler(request: NextApiRequest, response: NextApiResponse) {
	switch (request.method) {
		case "POST":
			try {
				const client = createClient(process.env.OPENAI_API_KEY as string);

				const answer = await toOpenAI({ ...request.body, client });
				return response.status(200).json(answer);
			} catch (error) {
				return response.status((error as AxiosError).status ?? 500).json(error);
			}
		default:
			return response.status(405).json({});
	}
}