All files / src/helpers/commercetoolsLink index.ts

47.22% Statements 34/72
0% Branches 0/12
22.22% Functions 4/18
56.14% Lines 32/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 1191x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x   1x 1x                       1x 1x                         1x 1x                                           1x 1x 1x   1x 1x 1x 1x   1x                         1x                           1x         1x   1x               1x 1x 1x    
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { ApolloLink } from 'apollo-link';
import { RetryLink } from 'apollo-link-retry';
import fetch from 'isomorphic-fetch';
import SdkAuth, { TokenProvider } from '@commercetools/sdk-auth';
import { asyncMap } from '@apollo/client/utilities';
import { Logger } from '@vue-storefront/core';
import { onError } from 'apollo-link-error';
import { Config, ApiConfig } from '../../types/setup';
import { handleBeforeAuth, handleAfterAuth, handleRetry } from './linkHandlers';
import { isAnonymousSession, isUserSession, getAccessToken } from '../utils';
 
const createAuthClient = (config: ApiConfig): SdkAuth => {
  return new SdkAuth({
    host: config.authHost,
    projectKey: config.projectKey,
    disableRefreshToken: false,
    credentials: {
      clientId: config.clientId,
      clientSecret: config.clientSecret
    },
    scopes: config.scopes
  });
};
 
const createTokenProvider = (settings: Config, { sdkAuth, currentToken }) => {
  return new TokenProvider({
    sdkAuth,
    fetchTokenInfo: (sdkAuthInstance) => sdkAuthInstance.clientCredentialsFlow(),
    onTokenInfoChanged: (tokenInfo) => {
      Logger.debug('TokenProvider.onTokenInfoChanged', getAccessToken(tokenInfo));
      settings.auth.onTokenChange(tokenInfo);
    },
    onTokenInfoRefreshed: (tokenInfo) => {
      Logger.debug('TokenProvider.onTokenInfoRefreshed', getAccessToken(tokenInfo));
    }
  }, currentToken);
};
 
const createErrorHandler = () => {
  return onError(({ graphQLErrors, networkError }) => {
    if (graphQLErrors) {
      graphQLErrors.map(({ message, locations, path }) => {
        if (!message.includes('Resource Owner Password Credentials Grant')) {
          if (!locations) {
            Logger.error(`[GraphQL error]: Message: ${message}, Path: ${path}`);
            return;
          }
 
          const parsedLocations = locations.map(({ column, line }) => `[column: ${column}, line: ${line}]`);
 
          Logger.error(`[GraphQL error]: Message: ${message}, Location: ${parsedLocations.join(', ')}, Path: ${path}`);
        }
      });
    }
 
    if (networkError) {
      Logger.error(`[Network error]: ${networkError}`);
    }
  });
};
 
const createCommerceToolsConnection = (settings: Config): any => {
  let currentToken: any = settings.auth.onTokenRead();
  Logger.debug('createCommerceToolsConnection', getAccessToken(currentToken));
 
  const sdkAuth = createAuthClient(settings.api);
  const tokenProvider = createTokenProvider(settings, { sdkAuth, currentToken });
  const httpLink = createHttpLink({ uri: settings.api.uri, fetch });
  const onErrorLink = createErrorHandler();
 
  const authLinkBefore = setContext(async (apolloReq, { headers }) => {
    Logger.debug('Apollo authLinkBefore', apolloReq.operationName);
    currentToken = await handleBeforeAuth({ sdkAuth, tokenProvider, apolloReq, currentToken });
    Logger.debug('Apollo authLinkBefore, finished, generated token: ', getAccessToken(currentToken));
 
    return {
      headers: {
        ...headers,
        authorization: `Bearer ${currentToken.access_token}`
      }
    };
  });
 
  const authLinkAfter = new ApolloLink((apolloReq, forward): any => {
    return asyncMap(forward(apolloReq) as any, async (response: any) => {
      Logger.debug('Apollo authLinkAfter', apolloReq.operationName);
      currentToken = await handleAfterAuth({ sdkAuth, tokenProvider, apolloReq, currentToken });
 
      const errors = (response.errors || []).filter(({ message }) =>
        !message.includes('Resource Owner Password Credentials Grant') &&
        !message.includes('This endpoint requires an access token issued either')
      );
 
      return { ...response, errors };
    });
  });
 
  const errorRetry = new RetryLink({
    attempts: handleRetry({ tokenProvider }),
    delay: () => 0
  });
 
  const apolloLink = ApolloLink.from([onErrorLink, errorRetry, authLinkBefore, authLinkAfter.concat(httpLink)]);
 
  return {
    apolloLink,
    sdkAuth,
    tokenProvider
  };
};
 
export {
  isAnonymousSession,
  isUserSession,
  createCommerceToolsConnection
};