schon/storefront/plugins/apollo.ts
Alexandr SaVBaD Waltz 4957039fc5 Features: 1) Integrate advanced Apollo link setup including error handling, authentication, and custom link chaining; 2) Replace apollo-upload-link.ts with revised client link configuration in apollo.ts; 3) Add @types/apollo-upload-client and @types/extract-files for enhanced TypeScript support;
Fixes: 1) Remove deprecated and redundant logic from `useAvatarUpload`; 2) Correct non-functional avatar upload and improve template handling in `settings.vue`;

Extra: 1) Cleanup unused imports, comments, and SCSS styles across files; 2) Simplify plugin configuration and migration to consolidated link logic; 3) Update package dependencies with precise resolution in `package-lock.json`.
2025-07-11 19:25:03 +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).value || '';
const { $apollo } = nuxtApp;
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) {
hdrs['X-EVIBES-AUTH'] = `Bearer ${token}`;
}
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);
});