schon/storefront/plugins/apollo.ts
Alexandr SaVBaD Waltz cb8e4fb2ab Features: 1) Add default value and path options for cookieAccess initialization in useRefresh; 2) Implement nextTick usage in useLogin and useRefresh for improved reactivity; 3) Enhance apollo.ts with cookieAccess object to ensure token consistency;
Fixes: 1) Reorder `router.push` in `useLogout` to properly clear cookies before redirection; 2) Resolve issues with inconsistent access token handling during Apollo header configuration;

Extra: 1) Cleanup comments in `useRefresh
2025-08-19 17:48:41 +03:00

49 lines
No EOL
1.4 KiB
TypeScript

import { from, ApolloLink } from '@apollo/client/core';
import { onError } from '@apollo/client/link/error';
import { setContext } from '@apollo/client/link/context';
import { provideApolloClient } from '@vue/apollo-composable';
import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
import { useAppConfig } from '~/composables/config';
export default defineNuxtPlugin((nuxtApp) => {
const runtime = useRuntimeConfig();
const localeCookie = useCookie(useAppConfig().COOKIES_LOCALE_KEY);
const token = useCookie(useAppConfig().COOKIES_ACCESS_TOKEN_KEY);
const { $apollo } = nuxtApp as any;
const errorLink = onError((err) => {
nuxtApp.callHook('apollo:error', err);
});
const authLink = setContext(async (_, { headers }) => {
const hdrs: Record<string,string> = {
...headers,
'Accept-Language': localeCookie.value || 'en-gb'
};
if (token.value) {
hdrs['X-EVIBES-AUTH'] = `Bearer ${token.value}`;
}
return { headers: hdrs };
});
const customLink = new ApolloLink((operation, forward) => {
return forward(operation).map((data) => {
return data;
});
});
const httpLink = createUploadLink({
uri: `https://api.${runtime.public.evibesBaseDomain}/graphql/`
});
$apollo.defaultClient.setLink(from([
errorLink,
authLink,
customLink,
httpLink,
]));
provideApolloClient($apollo.defaultClient);
});