|
@@ -50,7 +50,8 @@ import {FetchGraphqlResult} from "@/types/graphqlFetch/type";
|
|
|
|
|
|
|
|
type ExtractVariables<T> = T extends { variables: object }
|
|
type ExtractVariables<T> = T extends { variables: object }
|
|
|
? T["variables"]
|
|
? T["variables"]
|
|
|
- : never;
|
|
|
|
|
|
|
+ : any;
|
|
|
|
|
+type ExtractData<T> = T extends { data: infer D } ? D : any;
|
|
|
|
|
|
|
|
interface PageByUrlKeyResponse {
|
|
interface PageByUrlKeyResponse {
|
|
|
pageByUrlKeypages?: PageData[];
|
|
pageByUrlKeypages?: PageData[];
|
|
@@ -110,9 +111,11 @@ export async function restApiFetch<T>({
|
|
|
isCookies?: boolean;
|
|
isCookies?: boolean;
|
|
|
guestToken?: string;
|
|
guestToken?: string;
|
|
|
revalidate?: number;
|
|
revalidate?: number;
|
|
|
-}): Promise<{ status: number; body: T } | never> {
|
|
|
|
|
|
|
+}): Promise<{ status: number; body: ExtractData<T> } | never> {
|
|
|
try {
|
|
try {
|
|
|
const apiUrl = api.startsWith("http") ? api : `${REST_API_URL}${api}`;
|
|
const apiUrl = api.startsWith("http") ? api : `${REST_API_URL}${api}`;
|
|
|
|
|
+ const url = new URL(apiUrl);
|
|
|
|
|
+
|
|
|
let accessToken: string | undefined = undefined;
|
|
let accessToken: string | undefined = undefined;
|
|
|
|
|
|
|
|
if (isCookies) {
|
|
if (isCookies) {
|
|
@@ -139,7 +142,7 @@ export async function restApiFetch<T>({
|
|
|
Object.assign(baseHeaders, headers);
|
|
Object.assign(baseHeaders, headers);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-console.log('restApiFetch --- baseHeaders:', baseHeaders)
|
|
|
|
|
|
|
+ console.log('restApiFetch --- baseHeaders:', baseHeaders)
|
|
|
const param: RequestInit = {
|
|
const param: RequestInit = {
|
|
|
method: method,
|
|
method: method,
|
|
|
headers: baseHeaders,
|
|
headers: baseHeaders,
|
|
@@ -149,16 +152,22 @@ console.log('restApiFetch --- baseHeaders:', baseHeaders)
|
|
|
...(tags && { tags }),
|
|
...(tags && { tags }),
|
|
|
},
|
|
},
|
|
|
};
|
|
};
|
|
|
- if(variables) {
|
|
|
|
|
|
|
+ if(variables && method === "POST") {
|
|
|
param.body = JSON.stringify({...variables});
|
|
param.body = JSON.stringify({...variables});
|
|
|
}
|
|
}
|
|
|
|
|
+ if(variables && method === "GET") {
|
|
|
|
|
+ const entries = Object.entries(variables as Record<string, unknown>);
|
|
|
|
|
+ for (const [key, val] of entries) {
|
|
|
|
|
+ if (val !== undefined && val !== null) {
|
|
|
|
|
+ url.searchParams.set(key, String(val));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- const result = await fetch(apiUrl, param);
|
|
|
|
|
- console.log('restApiFetch --- result:', result)
|
|
|
|
|
- const body = await result.json();
|
|
|
|
|
-
|
|
|
|
|
- if (body.errors) throw body.errors[0];
|
|
|
|
|
|
|
+ const result = await fetch(url, param);
|
|
|
|
|
|
|
|
|
|
+ const body = await result.json();
|
|
|
|
|
+ console.log('restApiFetch --- body:', body)
|
|
|
return { status: result.status, body };
|
|
return { status: result.status, body };
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
throw e;
|
|
throw e;
|
|
@@ -176,7 +185,7 @@ export async function serverGraphqlFetch<
|
|
|
tags,
|
|
tags,
|
|
|
variables,
|
|
variables,
|
|
|
revalidate = 0,
|
|
revalidate = 0,
|
|
|
- operationName = ''
|
|
|
|
|
|
|
+ // operationName = ''
|
|
|
}: {
|
|
}: {
|
|
|
cache?: RequestCache;
|
|
cache?: RequestCache;
|
|
|
headers?: HeadersInit | Record<string, string>;
|
|
headers?: HeadersInit | Record<string, string>;
|
|
@@ -184,7 +193,7 @@ export async function serverGraphqlFetch<
|
|
|
tags?: string[];
|
|
tags?: string[];
|
|
|
variables?: TVariables;
|
|
variables?: TVariables;
|
|
|
revalidate?: number;
|
|
revalidate?: number;
|
|
|
- operationName?: string;
|
|
|
|
|
|
|
+ // operationName?: string;
|
|
|
}): Promise<FetchGraphqlResult<TData>> {
|
|
}): Promise<FetchGraphqlResult<TData>> {
|
|
|
try {
|
|
try {
|
|
|
const queryString = typeof query === "string" ? query : print(query);
|
|
const queryString = typeof query === "string" ? query : print(query);
|
|
@@ -245,15 +254,21 @@ export async function serverGraphqlFetch<
|
|
|
|
|
|
|
|
const body = await result.json();
|
|
const body = await result.json();
|
|
|
console.log('serverGraphqlFetch --- body:',body);
|
|
console.log('serverGraphqlFetch --- body:',body);
|
|
|
- if (body.errors) {
|
|
|
|
|
- if(operationName === 'GetCartItem' && body.errors[0].message === 'Cart not found') {
|
|
|
|
|
- return { status: result.status, data: body.data, error: null };
|
|
|
|
|
- } else {
|
|
|
|
|
- return { status: result.status, data: body.data, error: body.errors[0] };
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return {
|
|
|
|
|
+ status:result.status,
|
|
|
|
|
+ data:body.data ?? null,
|
|
|
|
|
+ error:body.errors?.[0] ?? null
|
|
|
}
|
|
}
|
|
|
- return { status: result.status, data: body.data,error: null };
|
|
|
|
|
|
|
+ // if (body.errors) {
|
|
|
|
|
+ // if(operationName === 'GetCartItem' && body.errors[0].message === 'Cart not found') {
|
|
|
|
|
+ // return { status: result.status, data: body.data, error: null };
|
|
|
|
|
+ // } else {
|
|
|
|
|
+ // return { status: result.status, data: body.data, error: body.errors[0] };
|
|
|
|
|
+ // }
|
|
|
|
|
+ // }
|
|
|
|
|
+ // return { status: result.status, data: body.data,error: null };
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
|
|
+ console.error( "GraphQL request failed", e );
|
|
|
throw e;
|
|
throw e;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -335,70 +350,12 @@ export async function bagistoFetch<T>({
|
|
|
|
|
|
|
|
const body = await result.json();
|
|
const body = await result.json();
|
|
|
console.log('bagistoFetch --- body:',body);
|
|
console.log('bagistoFetch --- body:',body);
|
|
|
- if (body.errors) {
|
|
|
|
|
- if(operationName === 'GetCartItem' && body.errors[0].message === 'Cart not found') {
|
|
|
|
|
- return { status: result.status, body };
|
|
|
|
|
- }
|
|
|
|
|
- throw body.errors[0]
|
|
|
|
|
- }
|
|
|
|
|
return { status: result.status, body };
|
|
return { status: result.status, body };
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
throw e;
|
|
throw e;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-/*
|
|
|
|
|
-export async function bagistoFetchNoSession<T>({
|
|
|
|
|
- query,
|
|
|
|
|
- tags,
|
|
|
|
|
- variables,
|
|
|
|
|
- headers,
|
|
|
|
|
- cache = "force-cache",
|
|
|
|
|
- revalidate = 60,
|
|
|
|
|
-}: {
|
|
|
|
|
- query: string;
|
|
|
|
|
- tags?: string[];
|
|
|
|
|
- variables?: ExtractVariables<T>;
|
|
|
|
|
- headers?: HeadersInit | Record<string, string>;
|
|
|
|
|
- cache?: RequestCache;
|
|
|
|
|
- isCookies?: boolean;
|
|
|
|
|
- revalidate?: number;
|
|
|
|
|
-}): Promise<{ status: number; body: T } | never> {
|
|
|
|
|
- try {
|
|
|
|
|
- const result = await fetch(GRAPHQL_URL, {
|
|
|
|
|
- method: "POST",
|
|
|
|
|
- headers: {
|
|
|
|
|
- "Content-Type": "application/json",
|
|
|
|
|
- "X-STOREFRONT-KEY": STOREFRONT_KEY,
|
|
|
|
|
- "x-locale": "en",
|
|
|
|
|
- "x-currency": "USD",
|
|
|
|
|
- ...headers,
|
|
|
|
|
- },
|
|
|
|
|
- body: JSON.stringify({
|
|
|
|
|
- ...(query && { query }),
|
|
|
|
|
- ...(variables && { variables }),
|
|
|
|
|
- }),
|
|
|
|
|
- cache,
|
|
|
|
|
- next: {
|
|
|
|
|
- revalidate: cache === "no-store" ? 0 : revalidate || 60,
|
|
|
|
|
- ...(tags && { tags }),
|
|
|
|
|
- },
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const body = await result.json();
|
|
|
|
|
-
|
|
|
|
|
- if (body.errors) {
|
|
|
|
|
- throw body.errors[0];
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
- return {
|
|
|
|
|
- status: result.status,
|
|
|
|
|
- body,
|
|
|
|
|
- };
|
|
|
|
|
- } catch (e) {
|
|
|
|
|
- throw { error: e, query };
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-*/
|
|
|
|
|
export const removeEdgesAndNodes = <T>(array: Array<T>) => {
|
|
export const removeEdgesAndNodes = <T>(array: Array<T>) => {
|
|
|
return array?.map((edge) => edge);
|
|
return array?.map((edge) => edge);
|
|
|
};
|
|
};
|