route.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { restApiFetch } from "@/utils/bagisto";
  3. import { getAuthToken } from "@/utils/helper";
  4. export async function POST(req: NextRequest) {
  5. try {
  6. const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
  7. const params = await req.json();
  8. const response = await restApiFetch<{
  9. data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
  10. variables: {
  11. id: number; // 到时候你自己改看是number还是string
  12. }
  13. }>({
  14. api: '/gift/add',
  15. method:'POST',
  16. cache:'no-store',
  17. variables: params,
  18. guestToken: authorizationToken,
  19. });
  20. // 打印后端原始返回结构
  21. console.log('接口原始response.body =', JSON.stringify(response.body, null, 2));
  22. return NextResponse.json(response.body,{
  23. status: response.status,
  24. });
  25. } catch (error) {
  26. console.log('/gift/add --- ', error); // 调试用
  27. return NextResponse.json(
  28. {
  29. message: error instanceof Error ? error.message : "Network error",
  30. success: false,
  31. data: []
  32. },
  33. { status:500 }
  34. );
  35. }
  36. }