Files

34 lines
1.1 KiB
TypeScript

import { getCloudViewer } from "@/app/cloud-auth";
import {
getAccountControlPlaneExport,
getOrCreateAccount,
} from "@/db/repository";
import { apiError } from "../../respond";
export async function GET() {
try {
const viewer = await getCloudViewer();
if (!viewer) {
return Response.json(
{ error: "authentication_required", message: "请先登录" },
{ status: 401, headers: { "cache-control": "no-store" } },
);
}
const account = await getOrCreateAccount(viewer);
const exportData = await getAccountControlPlaneExport(account);
const date = exportData.exported_at.slice(0, 10);
return new Response(`${JSON.stringify(exportData, null, 2)}\n`, {
headers: {
"cache-control": "no-store",
"content-disposition": `attachment; filename="nekonest-cloud-${date}.json"`,
"content-type": "application/json; charset=utf-8",
"x-content-type-options": "nosniff",
},
});
} catch (error) {
const response = apiError(error);
response.headers.set("cache-control", "no-store");
return response;
}
}