Files
nekonest-cloud/app/api/hosts/pairing/route.ts
T

65 lines
1.9 KiB
TypeScript

import { getCloudViewer } from "@/app/cloud-auth";
import {
createPairingRequest,
getOrCreateAccount,
getOwnedPairingProgress,
} from "@/db/repository";
import { apiError, readJsonMutation } from "../../respond";
export async function GET(request: Request) {
try {
const viewer = await getCloudViewer();
if (!viewer) {
return Response.json(
{ error: "authentication_required", message: "请先登录" },
{ status: 401, headers: { "cache-control": "no-store" } },
);
}
const pairingId = new URL(request.url).searchParams.get("pairing_id") ?? "";
const account = await getOrCreateAccount(viewer);
const pairing = await getOwnedPairingProgress({
accountId: account.id,
pairingId,
});
return Response.json(
{ pairing },
{ status: 200, headers: { "cache-control": "no-store" } },
);
} catch (error) {
const response = apiError(error);
response.headers.set("cache-control", "no-store");
return response;
}
}
export async function POST(request: Request) {
try {
const viewer = await getCloudViewer();
if (!viewer) {
return Response.json(
{ error: "authentication_required", message: "请先登录" },
{ status: 401 },
);
}
const payload = await readJsonMutation<{
requestedName?: string;
os?: "windows" | "linux";
}>(request);
const account = await getOrCreateAccount(viewer);
const pairing = await createPairingRequest({
accountId: account.id,
requestedName: payload.requestedName ?? "",
os: payload.os ?? "windows",
actorId: viewer.userId,
});
return Response.json(
{ pairing },
{ status: 201, headers: { "cache-control": "no-store" } },
);
} catch (error) {
const response = apiError(error);
response.headers.set("cache-control", "no-store");
return response;
}
}