From 22c2d59de7ddfe33238b82d8ec8fe1bc8fa52ae6 Mon Sep 17 00:00:00 2001 From: pauloh-fm Date: Mon, 15 Jul 2024 17:47:19 -0300 Subject: [PATCH] refactor: refactor get endpoint --- src/app/api/sheets/route.ts | 43 ++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/app/api/sheets/route.ts b/src/app/api/sheets/route.ts index db08fa3..99da3dd 100644 --- a/src/app/api/sheets/route.ts +++ b/src/app/api/sheets/route.ts @@ -1,19 +1,36 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import { getSheetData } from "../../../services/googleSheetsService"; -import { NextResponse } from "next/server"; +import { getSheetData } from "@/services/googleSheetsService"; -export const GET = async (req: NextApiRequest, res: NextApiResponse) => { - const url = new URL(req.url as string, `http://${req.headers.host}`); - const range = url.searchParams.get("range"); - const sheetName = url.searchParams.get("sheetName"); +export async function GET(request: Request) { + const { searchParams } = new URL(request.url); + const range = searchParams.get("range"); + const sheetName = searchParams.get("sheetName"); + + if (!range || !sheetName) { + return new Response( + JSON.stringify({ + message: "Parâmetros 'range' e 'sheetName' são obrigatórios", + }), + { + status: 400, + headers: { "Content-Type": "application/json" }, + }, + ); + } try { - const data = await getSheetData(range as string, sheetName as string); - // console.log("Dados obtidos:", data); - return NextResponse.json({ data }, { status: 200 }); - // return res.status(200).json({ data }); + const data = await getSheetData(range, sheetName); + return new Response(JSON.stringify(data), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); } catch (error) { console.error("GET sheets error:", error); - return res.status(500).json({ message: "Erro ao acessar o Google Sheets" }); + return new Response( + JSON.stringify({ message: "Erro ao recuperar dados da planilha" }), + { + status: 500, + headers: { "Content-Type": "application/json" }, + }, + ); } -}; +}