Refactored multiple files for code styling consistency, using proper indentation and spacing to align with team standards. Improved readability and maintainability across composables, Apollo plugin, and localization files. Enhancements: - Standardized import and function indentation across all composables. - Updated `biome.json` schema to the latest version (v2.4.4) for tool compatibility. - Organized code blocks in Apollo plugin for better understandability. No functional changes introduced—this is a non-breaking, code refinement commit.
86 lines
2.2 KiB
TypeScript
86 lines
2.2 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);
|
|
});
|