route.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { bagistoFetch } from "@/utils/bagisto";
  3. import { isBagistoError } from "@/utils/type-guards";
  4. import { getAuthToken } from "@/utils/helper";
  5. import {
  6. CREATE_ADD_PRODUCT_IN_CART,
  7. REMOVE_CART_ITEM,
  8. UPDATE_CART_ITEM,
  9. GET_CART_ITEM,
  10. CREATE_CART_TOKEN,
  11. CREATE_MERGE_CART,
  12. GET_CHECKOUT_ADDRESSES,
  13. GET_CHECKOUT_SHIPPING_RATES,
  14. GET_CHECKOUT_PAYMENT_METHODS,
  15. CREATE_CHECKOUT_ADDRESS,
  16. CREATE_CHECKOUT_SHIPPING_METHODS,
  17. CREATE_CHECKOUT_PAYMENT_METHODS,
  18. CREATE_CHECKOUT_ORDER,
  19. CREATE_PRODUCT_REVIEW,
  20. CREATE_PAYMENT_INITIATE,
  21. CREATE_SAVE_CHECKOUT_CART,
  22. GET_ORDER_DETAILS,
  23. CREATE_CANCEL_ORDER,
  24. // GET_PRODUCT_BY_URL_KEY,
  25. } from "@/graphql";
  26. const ALLOWED_OPERATIONS: Record<string, any> = {
  27. createAddProductInCart: CREATE_ADD_PRODUCT_IN_CART,
  28. RemoveCartItem: REMOVE_CART_ITEM,
  29. UpdateCartItem: UPDATE_CART_ITEM,
  30. GetCartItem: GET_CART_ITEM,
  31. CreateCart: CREATE_CART_TOKEN,
  32. createMergeCart: CREATE_MERGE_CART,
  33. collectionGetCheckoutAddresses: GET_CHECKOUT_ADDRESSES,
  34. CheckoutShippingRates: GET_CHECKOUT_SHIPPING_RATES,
  35. CheckoutPaymentMethods: GET_CHECKOUT_PAYMENT_METHODS,
  36. createCheckoutAddress: CREATE_CHECKOUT_ADDRESS,
  37. CreateCheckoutShippingMethod: CREATE_CHECKOUT_SHIPPING_METHODS,
  38. CreateCheckoutPaymentMethod: CREATE_CHECKOUT_PAYMENT_METHODS,
  39. CreateCheckoutOrder: CREATE_CHECKOUT_ORDER,
  40. CreateProductReview: CREATE_PRODUCT_REVIEW,
  41. CreatePaymentInitiate: CREATE_PAYMENT_INITIATE, // 创建订单
  42. CreateSaveCheckoutCart: CREATE_SAVE_CHECKOUT_CART,
  43. GetOrderDetails: GET_ORDER_DETAILS, // 获取订单详情
  44. CreateCancelOrder: CREATE_CANCEL_ORDER
  45. };
  46. // const QUERY_OPERATIONS: Record<string, any> = {
  47. // GetProductById: GET_PRODUCT_BY_URL_KEY,
  48. // }
  49. interface FetchOption {
  50. query: string;
  51. variables?: Record<string, any>;
  52. cache?: RequestCache;
  53. guestToken?: string;
  54. operationName?: string;
  55. }
  56. // 需要authorization的operation
  57. function authorizationOperations(body: Record<string, any>,req:NextRequest): FetchOption {
  58. const { operationName, variables } = body;
  59. const guestToken = getAuthToken(req);
  60. const query = ALLOWED_OPERATIONS[operationName];
  61. let finalVariables = variables;
  62. /*if (operationName === 'CheckoutPaymentMethods' || operationName === 'CheckoutShippingRates') {
  63. finalVariables = { ...variables };
  64. }
  65. if (operationName === 'CreateCheckoutPaymentMethod') {
  66. finalVariables = {
  67. ...variables,
  68. successUrl: variables?.successUrl ?? `payment/success`,
  69. failureUrl: variables?.failureUrl ?? `payment/failure`,
  70. cancelUrl: variables?.cancelUrl ?? `payment/cancel`
  71. };
  72. }*/
  73. /*if (operationName === 'createCheckoutAddress' && body.billingFirstName) {
  74. finalVariables = {
  75. billingFirstName: body.billingFirstName,
  76. billingLastName: body.billingLastName,
  77. billingEmail: body.billingEmail,
  78. billingAddress: body.billingAddress,
  79. billingCity: body.billingCity,
  80. billingCountry: body.billingCountry,
  81. billingState: body.billingState,
  82. billingPostcode: body.billingPostcode,
  83. billingPhoneNumber: body.billingPhoneNumber,
  84. billingCompanyName: body.billingCompanyName,
  85. useForShipping: body.useForShipping,
  86. ...(!body.useForShipping && {
  87. shippingFirstName: body.shippingFirstName,
  88. shippingLastName: body.shippingLastName,
  89. shippingEmail: body.billingEmail,
  90. shippingAddress: body.shippingAddress,
  91. shippingCity: body.shippingCity,
  92. shippingCountry: body.shippingCountry,
  93. shippingState: body.shippingState,
  94. shippingPostcode: body.shippingPostcode,
  95. shippingPhoneNumber: body.shippingPhoneNumber,
  96. shippingCompanyName: body.shippingCompanyName,
  97. })
  98. };
  99. }*/
  100. /*if (operationName === 'createAddProductInCart' && body.productId) {
  101. finalVariables = {
  102. cartId: body.cartId ?? null,
  103. productId: body.productId,
  104. quantity: body.quantity,
  105. };
  106. }*/
  107. return {
  108. query,
  109. variables: finalVariables,
  110. cache: "no-store",
  111. guestToken,
  112. operationName
  113. }
  114. }
  115. // 不需要authorization的operation
  116. function notAuthorizationOperations(body: Record<string, any>): FetchOption {
  117. const { operationName, query: bodyGraphqlQuery, variables } = body;
  118. const query = bodyGraphqlQuery;
  119. return {
  120. query,
  121. variables,
  122. cache: "no-store",
  123. operationName
  124. }
  125. }
  126. export async function POST(req: NextRequest) {
  127. try {
  128. const body = await req.json();
  129. const { operationName } = body;
  130. if (!operationName) {
  131. return NextResponse.json(
  132. { message: "Invalid or unauthorized operation: " + (operationName || "missing") },
  133. { status: 400 }
  134. );
  135. }
  136. let fetchOption: FetchOption = notAuthorizationOperations(body);
  137. if(ALLOWED_OPERATIONS[operationName]) {
  138. fetchOption = authorizationOperations(body,req);
  139. }
  140. const response = await bagistoFetch<any>(fetchOption);
  141. // console.log('response ------ ', response);
  142. return NextResponse.json({
  143. data: response.body.data,
  144. });
  145. } catch (error) {
  146. if (isBagistoError(error)) {
  147. return NextResponse.json(
  148. {
  149. data: null,
  150. error: error.cause ?? error,
  151. },
  152. { status: 200 }
  153. );
  154. }
  155. return NextResponse.json(
  156. {
  157. message: "Network error",
  158. error: error instanceof Error ? error.message : error,
  159. },
  160. { status: 500 }
  161. );
  162. }
  163. }