소스 검색

服务端请求函数打印信息优化;详情页主图页码样式

fogwind 1 주 전
부모
커밋
1e2a8e489d
3개의 변경된 파일과 34개의 추가작업 그리고 18개의 파일을 삭제
  1. 1 1
      src/app/(public)/product/_components/ProductMedia.tsx
  2. 2 2
      src/app/api/graphql/route.ts
  3. 31 15
      src/utils/bagisto/index.ts

+ 1 - 1
src/app/(public)/product/_components/ProductMedia.tsx

@@ -100,7 +100,7 @@ export function ProductMedia({
           ))}
           {/* 自定义分页数字 */}
           <div slot="container-end">
-            <div className="product-media-swiper-pagination z-1 w-13 absolute  top-3 right-4 pt-1.5 pb-1.5 pl-3 pr-3 rounded-3xl text-ly-14 text-white bg-ly-deepgreen/50"></div>
+            <div className="product-media-swiper-pagination flex justify-center items-center z-1 w-14 absolute  top-4 right-4 h-6 rounded-3xl text-ly-14 text-white bg-ly-deepgreen/50"></div>
           </div>
         </Swiper>
       </div>

+ 2 - 2
src/app/api/graphql/route.ts

@@ -101,12 +101,12 @@ export async function POST(req: NextRequest) {
 
         
         const response = await bagistoFetch<any>(fetchOption);
-        console.log('bagistoFetch response ------ ', response);
+
         return NextResponse.json(response.body,{
             status: response.status
         });
     } catch (error) {
-        console.log('bagistoFetch ERROR ------ ', error);
+ 
         return NextResponse.json(
             {
                 errors:[

+ 31 - 15
src/utils/bagisto/index.ts

@@ -1,6 +1,6 @@
 import { cookies } from "next/headers";
 import { auth } from "@/utils/auth/auth-helper";
-import { print, type DocumentNode } from "graphql";
+import { print, parse, getOperationAST, type DocumentNode } from "graphql";
 import { 
   GRAPHQL_URL, 
   REST_API_URL,
@@ -77,16 +77,27 @@ export async function getAuthorizationToken(): Promise<{token: string | null; is
   };
 }
 
+function getGraphqlOperationName(query: string | DocumentNode) {
+    const documentAST = typeof query === "string" ? parse(query) : query;
+    // 不传入 operationName 时,如果文档中只有一个操作,会自动返回该操作
+    const operation = getOperationAST(documentAST);
+    const operationName = operation && operation.name
+      ? operation.name.value
+      : undefined;
+    
+    return operationName;
+}
+
 // rest api fetch
 export async function restApiFetch<T>({
   api,
   method,
-  cache = "force-cache",
+  cache = "no-store",
   headers,
   tags,
   variables,
   isRoute = true,
-  revalidate = 60,
+  revalidate = 0,
   takeAuthorization = true, // 是否携带token,默认携带
 }: {
   api: string;
@@ -120,7 +131,10 @@ export async function restApiFetch<T>({
         Object.assign(baseHeaders, headers);
       }
     }
-    console.log('restApiFetch --- baseHeaders:', baseHeaders)
+    console.log('restApiFetch --- baseHeaders:', {
+      ...baseHeaders, 
+      apiUrl
+    })
     const param: RequestInit = {
       method: method,
       headers: baseHeaders,
@@ -145,7 +159,7 @@ export async function restApiFetch<T>({
     const result = await fetch(url, param);
 
     const body = await result.json();
-    console.log('restApiFetch --- body:', body)
+
     if(!isRoute) {
       if(result.status === 401) {
         const err = new Error('Authorization Bearer token is required. Please Login.');//new Error(body.message || 'Authorization Bearer token is required');
@@ -192,6 +206,7 @@ export async function serverGraphqlFetch<
     }
   try {
     const queryString = typeof query === "string" ? query : print(query);
+    const operationName = getGraphqlOperationName(query);
 
     if (headers) {
       if (headers instanceof Headers) {
@@ -200,7 +215,10 @@ export async function serverGraphqlFetch<
         Object.assign(baseHeaders, headers);
       }
     }
-    console.log('serverGraphqlFetch --- baseHeaders:', baseHeaders);
+    console.log('serverGraphqlFetch --- baseHeaders:', {
+      ...baseHeaders,
+      operationName
+    });
     const result = await fetch(GRAPHQL_URL, {
       method: "POST",
       headers: baseHeaders,
@@ -268,8 +286,8 @@ export async function bagistoFetch<T>({
 
     }
   try {
-    const queryString =
-      typeof query === "string" ? query : print(query);
+    const queryString = typeof query === "string" ? query : print(query);
+    const operationName = getGraphqlOperationName(query);
     
     if (headers) {
       if (headers instanceof Headers) {
@@ -278,12 +296,10 @@ export async function bagistoFetch<T>({
         Object.assign(baseHeaders, headers);
       }
     }
-    // let cc = await cookies();
-    // console.log('bagistoFetch --- url:', GRAPHQL_URL,isCookies);
-    // console.log('bagistoFetch --- queryString:', queryString);
-    console.log('bagistoFetch --- baseHeaders:', baseHeaders);
-    // console.log('bagistoFetch --- cookies:', cc.getAll());
-    // console.log('bagistoFetch --- variables:', variables);
+    console.log('bagistoFetch --- baseHeaders:', {
+      ...baseHeaders,
+      operationName
+    });
     const result = await fetch(GRAPHQL_URL, {
       method: "POST",
       headers: baseHeaders,
@@ -299,7 +315,7 @@ export async function bagistoFetch<T>({
     });
 
     const body = await result.json();
-    console.log('bagistoFetch --- body:',body);
+
     return { status: result.status, body };
   } catch (e) {
     throw e;