|
|
@@ -24,7 +24,6 @@ import {
|
|
|
} from "@/utils/constants/server";
|
|
|
import {
|
|
|
GUEST_CART_TOKEN,
|
|
|
- IS_GUEST,
|
|
|
CURRENT_CURRENCY,
|
|
|
CURRENT_LOCAL,
|
|
|
CURRENT_CHANNEL,
|
|
|
@@ -44,14 +43,14 @@ import {
|
|
|
ThemeCustomizationResponse,
|
|
|
PageData,
|
|
|
} from "@/types/theme/theme-customization";
|
|
|
-import { decodeJWT } from "@/utils/jwt-cookie";
|
|
|
import {FetchGraphqlResult} from "@/types/graphqlFetch/type";
|
|
|
|
|
|
|
|
|
type ExtractVariables<T> = T extends { variables: object }
|
|
|
? T["variables"]
|
|
|
: any;
|
|
|
-type ExtractData<T> = T extends { data: infer D } ? D : any;
|
|
|
+type ExtractRestFulData<T> = T extends { data: infer D } ? D : any;
|
|
|
+type ExtractGraphqlData<T> = T extends { data: infer D } ? { data: D } : any;
|
|
|
|
|
|
interface PageByUrlKeyResponse {
|
|
|
pageByUrlKeypages?: PageData[];
|
|
|
@@ -88,7 +87,29 @@ async function getBaseHeader() {
|
|
|
}
|
|
|
return baseHeaders;
|
|
|
}
|
|
|
+export async function getAuthorizationToken(): Promise<{token: string | null; isGuest: boolean;}> {
|
|
|
+
|
|
|
+ // 登录用户从nextAuth里获取
|
|
|
+ const authSession = (await getServerSession(
|
|
|
+ authOptions,
|
|
|
+ )) as BagistoSession | null;
|
|
|
+ const accessToken = authSession?.user?.accessToken;
|
|
|
+ if (accessToken) {
|
|
|
+ return {
|
|
|
+ token: accessToken,
|
|
|
+ isGuest: false,
|
|
|
+ };
|
|
|
+ }
|
|
|
|
|
|
+ // 游客从cookie里获取
|
|
|
+ const cookieStore = await cookies();
|
|
|
+ const guestToken = cookieStore.get(GUEST_CART_TOKEN)?.value;
|
|
|
+
|
|
|
+ return {
|
|
|
+ token: guestToken || null,
|
|
|
+ isGuest: true
|
|
|
+ };
|
|
|
+}
|
|
|
|
|
|
// rest api fetch
|
|
|
export async function restApiFetch<T>({
|
|
|
@@ -98,8 +119,6 @@ export async function restApiFetch<T>({
|
|
|
headers,
|
|
|
tags,
|
|
|
variables,
|
|
|
- isCookies = true,
|
|
|
- guestToken,
|
|
|
revalidate = 60,
|
|
|
}: {
|
|
|
api: string;
|
|
|
@@ -111,11 +130,16 @@ export async function restApiFetch<T>({
|
|
|
isCookies?: boolean;
|
|
|
guestToken?: string;
|
|
|
revalidate?: number;
|
|
|
-}): Promise<{ status: number; body: ExtractData<T> } | never> {
|
|
|
+}): Promise<{ status: number; body: ExtractRestFulData<T> } | never> {
|
|
|
try {
|
|
|
const apiUrl = api.startsWith("http") ? api : `${REST_API_URL}${api}`;
|
|
|
const url = new URL(apiUrl);
|
|
|
|
|
|
+ const tokenRes = await getAuthorizationToken();
|
|
|
+
|
|
|
+ const headerRes = await getBaseHeader();
|
|
|
+ const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
+ /*
|
|
|
let accessToken: string | undefined = undefined;
|
|
|
|
|
|
if (isCookies) {
|
|
|
@@ -125,17 +149,19 @@ export async function restApiFetch<T>({
|
|
|
accessToken = sessions?.user?.accessToken;
|
|
|
}
|
|
|
|
|
|
- const headerRes = await getBaseHeader();
|
|
|
- const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
|
|
|
if (accessToken) {
|
|
|
baseHeaders.Authorization = `Bearer ${accessToken}`;
|
|
|
} else if (guestToken) {
|
|
|
baseHeaders.Authorization = `Bearer ${guestToken}`;
|
|
|
}
|
|
|
+ */
|
|
|
+ if (tokenRes.token) {
|
|
|
+ baseHeaders.Authorization = `Bearer ${tokenRes.token}`;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- if (isCookies && headers) {
|
|
|
+ if (headers) {
|
|
|
if (headers instanceof Headers) {
|
|
|
headers.forEach((value, key) => (baseHeaders[key] = value));
|
|
|
} else {
|
|
|
@@ -196,36 +222,13 @@ export async function serverGraphqlFetch<
|
|
|
try {
|
|
|
const queryString = typeof query === "string" ? query : print(query);
|
|
|
|
|
|
- let accessToken: string | undefined = undefined;
|
|
|
- let guestToken: string | undefined = undefined;
|
|
|
-
|
|
|
- const cookieStore = await cookies();
|
|
|
-
|
|
|
- const isGuest = cookieStore.get(IS_GUEST)?.value;
|
|
|
- const gt = cookieStore.get(GUEST_CART_TOKEN)?.value;
|
|
|
- if(isGuest === 'false') {
|
|
|
- // 登录用户
|
|
|
- const sessions = (await getServerSession(
|
|
|
- authOptions,
|
|
|
- )) as BagistoSession | null;
|
|
|
- accessToken = sessions?.user?.accessToken;
|
|
|
- } else if(gt) {
|
|
|
- // 游客
|
|
|
- const jwtRes = decodeJWT<{
|
|
|
- sessionToken: string;
|
|
|
- cartId: number;
|
|
|
- isGuest: boolean;
|
|
|
- }>(gt, true);
|
|
|
- guestToken = jwtRes?.sessionToken || '';
|
|
|
- }
|
|
|
-
|
|
|
+ const tokenRes = await getAuthorizationToken();
|
|
|
const headerRes = await getBaseHeader();
|
|
|
const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
|
|
|
- if (accessToken) {
|
|
|
- baseHeaders['Authorization'] = `Bearer ${accessToken}`;
|
|
|
- } else if (guestToken) {
|
|
|
- baseHeaders['Authorization'] = `Bearer ${guestToken}`;
|
|
|
+
|
|
|
+ if (tokenRes.token) {
|
|
|
+ baseHeaders['Authorization'] = `Bearer ${tokenRes.token}`;
|
|
|
}
|
|
|
|
|
|
if (headers) {
|
|
|
@@ -271,8 +274,8 @@ export async function bagistoFetch<T>({
|
|
|
query,
|
|
|
tags,
|
|
|
variables,
|
|
|
- isCookies = true,
|
|
|
- guestToken,
|
|
|
+ // isCookies = true,
|
|
|
+ // guestToken,
|
|
|
revalidate = 60,
|
|
|
}: {
|
|
|
cache?: RequestCache;
|
|
|
@@ -280,15 +283,18 @@ export async function bagistoFetch<T>({
|
|
|
query: string | DocumentNode;
|
|
|
tags?: string[];
|
|
|
variables?: ExtractVariables<T>;
|
|
|
- isCookies?: boolean;
|
|
|
- guestToken?: string;
|
|
|
+ // isCookies?: boolean;
|
|
|
+ // guestToken?: string;
|
|
|
revalidate?: number;
|
|
|
-}): Promise<{ status: number; body: T } | never> {
|
|
|
+}): Promise<{ status: number; body: ExtractGraphqlData<T> } | never> {
|
|
|
try {
|
|
|
const queryString =
|
|
|
typeof query === "string" ? query : print(query);
|
|
|
|
|
|
-
|
|
|
+ const tokenRes = await getAuthorizationToken();
|
|
|
+ const headerRes = await getBaseHeader();
|
|
|
+ const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
+ /*
|
|
|
let accessToken: string | undefined = undefined;
|
|
|
|
|
|
if (isCookies) {
|
|
|
@@ -298,19 +304,20 @@ export async function bagistoFetch<T>({
|
|
|
)) as BagistoSession | null;
|
|
|
accessToken = sessions?.user?.accessToken;
|
|
|
}
|
|
|
-
|
|
|
- const headerRes = await getBaseHeader();
|
|
|
- const baseHeaders: Record<string, string> = {...headerRes};
|
|
|
-
|
|
|
+
|
|
|
if (accessToken) {
|
|
|
baseHeaders.Authorization = `Bearer ${accessToken}`;
|
|
|
} else if (guestToken) {
|
|
|
baseHeaders.Authorization = `Bearer ${guestToken}`;
|
|
|
}
|
|
|
+ */
|
|
|
+ if (tokenRes.token) {
|
|
|
+ baseHeaders.Authorization = `Bearer ${tokenRes.token}`;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
- if (isCookies && headers) {
|
|
|
+ if (headers) {
|
|
|
if (headers instanceof Headers) {
|
|
|
headers.forEach((value, key) => (baseHeaders[key] = value));
|
|
|
} else {
|
|
|
@@ -444,7 +451,7 @@ export async function logoutUser() {
|
|
|
variables: { input: { token: string } };
|
|
|
}>({
|
|
|
query: CUSTOMER_LOGOUT,
|
|
|
- isCookies: true,
|
|
|
+ // isCookies: true,
|
|
|
revalidate: 3600,
|
|
|
});
|
|
|
|
|
|
@@ -591,7 +598,7 @@ export async function getPage(input: { urlKey: string }): Promise<PageData[]> {
|
|
|
}>({
|
|
|
query: PAGE_BY_URL_KEY,
|
|
|
cache: "no-store",
|
|
|
- isCookies: false,
|
|
|
+ // isCookies: false,
|
|
|
variables: { pageByUrlKey: input.urlKey },
|
|
|
});
|
|
|
|