Skip to content

Commit

Permalink
refactor: refactor get endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pauloh-fm committed Jul 15, 2024
1 parent 32d54d1 commit 22c2d59
Showing 1 changed file with 30 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/app/api/sheets/route.ts
Original file line number Diff line number Diff line change
@@ -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" },
},
);
}
};
}

0 comments on commit 22c2d59

Please sign in to comment.