| 
1 |  | -import { setContext } from "@apollo/client/link/context";  | 
2 |  | -import { onError } from "@apollo/client/link/error";  | 
 | 1 | +import { assert } from "@ember/debug";  | 
 | 2 | +import {  | 
 | 3 | +  dependencySatisfies,  | 
 | 4 | +  macroCondition,  | 
 | 5 | +  importSync,  | 
 | 6 | +} from "@embroider/macros";  | 
3 | 7 | import { handleUnauthorized } from "ember-simple-auth-oidc";  | 
4 | 8 | 
 
  | 
5 |  | -export default function apolloMiddleware(httpLink, session) {  | 
6 |  | -  const authMiddleware = setContext(async (_, context) => {  | 
7 |  | -    await session.refreshAuthentication.perform();  | 
 | 9 | +let apolloMiddleware;  | 
8 | 10 | 
 
  | 
9 |  | -    return {  | 
10 |  | -      ...context,  | 
11 |  | -      headers: {  | 
12 |  | -        ...context.headers,  | 
13 |  | -        ...session.headers,  | 
14 |  | -      },  | 
15 |  | -    };  | 
16 |  | -  });  | 
 | 11 | +if (macroCondition(dependencySatisfies("@apollo/client", "^3.13.0"))) {  | 
 | 12 | +  const { setContext } = importSync("@apollo/client/link/context");  | 
 | 13 | +  const { onError } = importSync("@apollo/client/link/error");  | 
17 | 14 | 
 
  | 
18 |  | -  const authAfterware = onError((error) => {  | 
19 |  | -    if (error.networkError && error.networkError.statusCode === 401) {  | 
20 |  | -      handleUnauthorized(session);  | 
21 |  | -    }  | 
22 |  | -  });  | 
 | 15 | +  apolloMiddleware = function apolloMiddleware(httpLink, session) {  | 
 | 16 | +    const authMiddleware = setContext(async (_, context) => {  | 
 | 17 | +      await session.refreshAuthentication.perform();  | 
23 | 18 | 
 
  | 
24 |  | -  return authMiddleware.concat(authAfterware).concat(httpLink);  | 
 | 19 | +      return {  | 
 | 20 | +        ...context,  | 
 | 21 | +        headers: {  | 
 | 22 | +          ...context.headers,  | 
 | 23 | +          ...session.headers,  | 
 | 24 | +        },  | 
 | 25 | +      };  | 
 | 26 | +    });  | 
 | 27 | + | 
 | 28 | +    const authAfterware = onError((error) => {  | 
 | 29 | +      if (error.networkError && error.networkError.statusCode === 401) {  | 
 | 30 | +        handleUnauthorized(session);  | 
 | 31 | +      }  | 
 | 32 | +    });  | 
 | 33 | + | 
 | 34 | +    return authMiddleware.concat(authAfterware).concat(httpLink);  | 
 | 35 | +  };  | 
 | 36 | +} else {  | 
 | 37 | +  apolloMiddleware = () =>  | 
 | 38 | +    assert(  | 
 | 39 | +      "@apollo/client ^3.13.0 must be installed in order to use the apollo middleware",  | 
 | 40 | +    );  | 
25 | 41 | }  | 
 | 42 | + | 
 | 43 | +export default apolloMiddleware;  | 
0 commit comments