route.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { restApiFetch } from "@/utils/bagisto";
  3. import { isBagistoError } from "@/utils/type-guards";
  4. import { getAuthToken } from "@/utils/helper";
  5. // import type { GiftListBody,FetchWrap } from '@/types/api/gift/lists';
  6. export async function GET(req: NextRequest) {
  7. try {
  8. const guestToken = getAuthToken(req);
  9. // 从url取查询参数 id
  10. const addressId = req.nextUrl.searchParams.get("id");
  11. const apiUrl = addressId
  12. ? `/customer/token/address/${addressId}`
  13. : `/customer/token/address`;
  14. console.log("11111----:", apiUrl);
  15. const response = await restApiFetch<any>({
  16. api: apiUrl, //
  17. method: "GET",
  18. cache: "no-store",
  19. guestToken,
  20. });
  21. return NextResponse.json({
  22. status: response.status,
  23. data: response.body,
  24. });
  25. } catch (error) {
  26. if (isBagistoError(error)) {
  27. return NextResponse.json(
  28. {
  29. data: null,
  30. error: error.cause ?? error,
  31. },
  32. { status: 200 },
  33. );
  34. }
  35. return NextResponse.json(
  36. {
  37. message: "Network error",
  38. error: error instanceof Error ? error.message : error,
  39. },
  40. { status: 500 },
  41. );
  42. }
  43. }
  44. export async function POST(req: NextRequest) {
  45. try {
  46. const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
  47. const params = await req.json();
  48. const response = await restApiFetch<{
  49. data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
  50. variables: {
  51. first_name: string;
  52. last_name: string;
  53. email: string;
  54. phone: string;
  55. address: string;
  56. country: string;
  57. state: string;
  58. city: string;
  59. postcode: string;
  60. default_address: number;
  61. };
  62. }>({
  63. api: "/customer/token/address",
  64. method: "POST",
  65. cache: "no-store",
  66. variables: params,
  67. guestToken: authorizationToken,
  68. });
  69. // 打印后端原始返回结构
  70. console.log(
  71. "接口原始response.body =",
  72. JSON.stringify(response.body, null, 2),
  73. );
  74. return NextResponse.json({
  75. status: response.status,
  76. data: response.body,
  77. });
  78. } catch (error) {
  79. console.log("/customer/token/address---", error); // 调试用
  80. if (isBagistoError(error)) {
  81. return NextResponse.json(
  82. {
  83. data: null,
  84. error: error.cause ?? error,
  85. },
  86. { status: 200 },
  87. );
  88. }
  89. return NextResponse.json(
  90. {
  91. message: "Network error",
  92. error: error instanceof Error ? error.message : error,
  93. },
  94. { status: 500 },
  95. );
  96. }
  97. }
  98. export async function PUT(req: NextRequest) {
  99. try {
  100. const authorizationToken = getAuthToken(req); // 获取headers中的Authorization的值
  101. const params = await req.json();
  102. // 从url取查询参数 id
  103. const addressId = req.nextUrl.searchParams.get("id");
  104. const response = await restApiFetch<{
  105. data: any; // 这个是返回结果的数据类型,暂时写成any,具体看后端反的数据结构再改成确定的类型1
  106. variables: {
  107. first_name: string;
  108. last_name: string;
  109. email: string;
  110. phone: string;
  111. address: string;
  112. country: string;
  113. state: string;
  114. city: string;
  115. postcode: string;
  116. default_address: number;
  117. };
  118. }>({
  119. api: `/customer/token/address/${addressId}`,
  120. method: "PUT",
  121. cache: "no-store",
  122. variables: params,
  123. guestToken: authorizationToken,
  124. });
  125. // 打印后端原始返回结构
  126. console.log(
  127. "接口原始response.body =",
  128. JSON.stringify(response.body, null, 2),
  129. );
  130. return NextResponse.json({
  131. status: response.status,
  132. data: response.body,
  133. });
  134. } catch (error) {
  135. console.log("/customer/token/address---", error); // 调试用
  136. if (isBagistoError(error)) {
  137. return NextResponse.json(
  138. {
  139. data: null,
  140. error: error.cause ?? error,
  141. },
  142. { status: 200 },
  143. );
  144. }
  145. return NextResponse.json(
  146. {
  147. message: "Network error",
  148. error: error instanceof Error ? error.message : error,
  149. },
  150. { status: 500 },
  151. );
  152. }
  153. }
  154. export async function DELETE(req: NextRequest) {
  155. try {
  156. const guestToken = getAuthToken(req);
  157. const addressId = req.nextUrl.searchParams.get("id");
  158. const response = await restApiFetch<any>({
  159. api: `/customer/token/address/${addressId}`, //
  160. method: "DELETE",
  161. cache: "no-store",
  162. guestToken,
  163. });
  164. return NextResponse.json({
  165. status: response.status,
  166. data: response.body,
  167. });
  168. } catch (error) {
  169. if (isBagistoError(error)) {
  170. return NextResponse.json(
  171. {
  172. data: null,
  173. error: error.cause ?? error,
  174. },
  175. { status: 200 },
  176. );
  177. }
  178. return NextResponse.json(
  179. {
  180. message: "Network error",
  181. error: error instanceof Error ? error.message : error,
  182. },
  183. { status: 500 },
  184. );
  185. }
  186. }