schon/storefront/app/plugins/01.apollo.ts
Alexandr SaVBaD Waltz c36135d78d feat(storefront): add wishlist and guest cart support with cookie persistence
Enhancements:
- Introduced `wishlist.vue` for displaying and managing the wishlist.
- Added guest cart and wishlist handling via cookies for unauthenticated users.
- Implemented synchronization logic for wishlist and cart (`useOrderSync` and `useWishlistSync`) upon user login.
- Updated `cart.vue` layout with a bulk 'add all to cart' button for wishlist items.
- Enhanced `post.vue` prop handling for improved type safety.

Fixes:
- Fixed breadcrumbs console log removal in `useBreadcrumbs.ts`.
- Corrected and unified translations in `en-gb.json` for cart and wishlist descriptions.
- Fixed stale routes in footer (`terms-and-condition` -> `terms-and-conditions`, etc.).

Extras:
- Refactored composables `useWishlistOverwrite` and `useOrderOverwrite` for cookie-based fallback.
- Applied code styling improvements, organized imports, and optimized API requests in Apollo plugin.
2026-02-28 17:39:17 +03:00

77 lines
No EOL
2.1 KiB
TypeScript

import { ApolloLink, from } from '@apollo/client/core';
import { setContext } from '@apollo/client/link/context';
import { onError } from '@apollo/client/link/error';
import { provideApolloClient } from '@vue/apollo-composable';
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
export default defineNuxtPlugin((nuxtApp) => {
const runtime = useRuntimeConfig();
const { $appHelpers, $apollo } = useNuxtApp();
const errorLink = onError((err) => {
nuxtApp.callHook('apollo:error', err);
});
nuxtApp.hook('apollo:error', (error) => {
console.error('[Apollo Error]:', error);
});
const authLink = setContext((_, { headers }) => {
let accessToken = '';
let locale = 'en-gb';
if (import.meta.client) {
const clientCookies = document.cookie.split(';').reduce((acc, cookie) => {
const [key, value] = cookie.trim().split('=');
acc[key] = decodeURIComponent(value);
return acc;
}, {} as Record<string, string>);
accessToken = clientCookies[$appHelpers.COOKIES_ACCESS_TOKEN_KEY] || '';
locale = clientCookies[$appHelpers.COOKIES_LOCALE_KEY] || 'en-gb';
} else {
const cookieHeader = nuxtApp.ssrContext?.event?.node?.req?.headers?.cookie || '';
const serverCookies = cookieHeader.split(';').reduce((acc, cookie) => {
const [key, value] = cookie.trim().split('=');
if (key && value) {
acc[key] = decodeURIComponent(value);
}
return acc;
}, {} as Record<string, string>);
accessToken = serverCookies[$appHelpers.COOKIES_ACCESS_TOKEN_KEY] || '';
locale = serverCookies[$appHelpers.COOKIES_LOCALE_KEY] || 'en-gb';
}
const hdrs: Record<string, string> = {
...headers,
'Accept-Language': locale
};
if (accessToken) {
hdrs['X-SCHON-AUTH'] = `Bearer ${accessToken}`;
}
return { headers: hdrs };
});
const customLink = new ApolloLink((operation, forward) => {
return forward(operation).map((data) => {
return data;
});
});
const httpLink = createUploadLink({
uri: `https://api.${runtime.public.schonBaseDomain}/graphql/`
});
$apollo.defaultClient.setLink(from([
errorLink,
authLink,
customLink,
httpLink,
]));
provideApolloClient($apollo.defaultClient);
});