docmee.ts 1.5 KB
import { getServerSideConfig } from "@/app/config/server";
import { NextRequest, NextResponse } from "next/server";

export async function handle(
  req: NextRequest,
  { params }: { params: { path: string[] } },
) {
  const config = getServerSideConfig();
  const baseUrl = config.docmeeUrl;
  const subPath = params.path.join("/");
  // if(subPath==='createApiToken'){const reqUrl = `${baseUrl}/api/user/${subPath}`;}
  const reqUrl = `${baseUrl}/api/user/${subPath}`;
  console.log(reqUrl);
  try {
    // 获取 accessCode
    const body = await new Response(req.body).text();
    const uid = JSON.parse(body);
    const headers = new Headers({
      "Api-Key": config.docmeeApiKey,
      "Content-Type": "application/json",
    });
    console.log("********************" + config.docmeeApiKey);
    // 使用 async/await 处理 fetch 请求
    const response = await fetch(reqUrl, {
      headers,
      method: "POST",
      body: JSON.stringify({ uid: uid, limit: config.docmeeMaxDailyUses }),
    });
    if (!response.ok) {
      throw new Error(
        `HTTP error! status: ${response.status} ${response.text()}`,
      );
    }
    const data = await response.json();
    console.log("-----------------data", data);
    // 返回固定的 token
    return NextResponse.json({
      data: data,
      status: 200,
    });
  } catch (error) {
    console.error("处理请求时出错:", error);
    return NextResponse.json({
      data: "处理请求时出错",
      status: 500,
    });
  }
}