| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- import { NextRequest, NextResponse } from "next/server";
- import { restApiFetch } from "@/utils/bagisto";
- import { isBagistoError } from "@/utils/type-guards";
- import { getAuthToken } from "@/utils/helper";
- // import type { GiftListBody,FetchWrap } from '@/types/api/gift/lists';
- export async function GET(req: NextRequest) {
- try {
- const guestToken = getAuthToken(req);
- // 从url取查询参数 id
- const addressId = req.nextUrl.searchParams.get("id");
- const apiUrl = addressId
- ? `/customer/token/address/${addressId}`
- : `/customer/token/address`;
- console.log("11111----:", apiUrl);
-
- const response = await restApiFetch<any>({
- api: apiUrl, //
- method: "GET",
- cache: "no-store",
- guestToken,
- });
- return NextResponse.json({
- status: response.status,
- data: response.body,
- });
- } catch (error) {
- if (isBagistoError(error)) {
- return NextResponse.json(
- {
- data: null,
- error: error.cause ?? error,
- },
- { status: 200 },
- );
- }
- return NextResponse.json(
- {
- message: "Network error",
- error: error instanceof Error ? error.message : error,
- },
- { status: 500 },
- );
- }
- }
- export async function POST(req: NextRequest) {
- try {
- const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
- const params = await req.json();
- const response = await restApiFetch<{
- data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
- variables: {
- first_name: string;
- last_name: string;
- email: string;
- phone: string;
- address: string;
- country: string;
- state: string;
- city: string;
- postcode: string;
- default_address: number;
- };
- }>({
- api: "/customer/token/address",
- method: "POST",
- cache: "no-store",
- variables: params,
- guestToken: authorizationToken,
- });
- // 打印后端原始返回结构
- console.log(
- "接口原始response.body =",
- JSON.stringify(response.body, null, 2),
- );
- return NextResponse.json({
- status: response.status,
- data: response.body,
- });
- } catch (error) {
- console.log("/customer/token/address---", error); // 调试用
- if (isBagistoError(error)) {
- return NextResponse.json(
- {
- data: null,
- error: error.cause ?? error,
- },
- { status: 200 },
- );
- }
- return NextResponse.json(
- {
- message: "Network error",
- error: error instanceof Error ? error.message : error,
- },
- { status: 500 },
- );
- }
- }
- export async function PUT(req: NextRequest) {
- try {
- const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
- const params = await req.json();
- // 从url取查询参数 id
- const addressId = req.nextUrl.searchParams.get("id");
- const response = await restApiFetch<{
- data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
- variables: {
- first_name: string;
- last_name: string;
- email: string;
- phone: string;
- address: string;
- country: string;
- state: string;
- city: string;
- postcode: string;
- default_address: number;
- };
- }>({
- api: `/customer/token/address/${addressId}`,
- method: "PUT",
- cache: "no-store",
- variables: params,
- guestToken: authorizationToken,
- });
- // 打印后端原始返回结构
- console.log(
- "接口原始response.body =",
- JSON.stringify(response.body, null, 2),
- );
- return NextResponse.json({
- status: response.status,
- data: response.body,
- });
- } catch (error) {
- console.log("/customer/token/address---", error); // 调试用
- if (isBagistoError(error)) {
- return NextResponse.json(
- {
- data: null,
- error: error.cause ?? error,
- },
- { status: 200 },
- );
- }
- return NextResponse.json(
- {
- message: "Network error",
- error: error instanceof Error ? error.message : error,
- },
- { status: 500 },
- );
- }
- }
- export async function DELETE(req: NextRequest) {
- try {
- const guestToken = getAuthToken(req);
- const addressId = req.nextUrl.searchParams.get("id");
- const response = await restApiFetch<any>({
- api: `/customer/token/address/${addressId}`, //
- method: "DELETE",
- cache: "no-store",
- guestToken,
- });
- return NextResponse.json({
- status: response.status,
- data: response.body,
- });
- } catch (error) {
- if (isBagistoError(error)) {
- return NextResponse.json(
- {
- data: null,
- error: error.cause ?? error,
- },
- { status: 200 },
- );
- }
- return NextResponse.json(
- {
- message: "Network error",
- error: error instanceof Error ? error.message : error,
- },
- { status: 500 },
- );
- }
- }
|