route.ts 5.6 KB

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