forked from adfinis/ember-simple-auth-oidc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(apollo): make apollo client a dependency
This is necessary since embroider doesn't handle optional dependencies properly as reported here: embroider-build/embroider#1266 This commit makes `@apollo/client` temporarly a non optional dependency to fix the current issue we have in apps without that dependency. However, we need to revert this as soon as the issue above is resolved since we don't want to enforce this dependency. Related to adfinis#597
- Loading branch information
Showing
2 changed files
with
20 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,25 @@ | ||
import { assert } from "@ember/debug"; | ||
import { | ||
dependencySatisfies, | ||
macroCondition, | ||
importSync, | ||
} from "@embroider/macros"; | ||
import { setContext } from "@apollo/client/link/context"; | ||
import { onError } from "@apollo/client/link/error"; | ||
import { handleUnauthorized } from "ember-simple-auth-oidc"; | ||
|
||
let apolloMiddleware; | ||
export default function apolloMiddleware(httpLink, session) { | ||
const authMiddleware = setContext(async (_, context) => { | ||
await session.refreshAuthentication.perform(); | ||
|
||
if (macroCondition(dependencySatisfies("@apollo/client", ">=3.0.0"))) { | ||
const { setContext } = importSync("@apollo/client/link/context"); | ||
const { onError } = importSync("@apollo/client/link/error"); | ||
return { | ||
...context, | ||
headers: { | ||
...context.headers, | ||
...session.headers, | ||
}, | ||
}; | ||
}); | ||
|
||
apolloMiddleware = (httpLink, session) => { | ||
const authMiddleware = setContext(async (_, context) => { | ||
await session.refreshAuthentication.perform(); | ||
const authAfterware = onError((error) => { | ||
if (error.networkError && error.networkError.statusCode === 401) { | ||
handleUnauthorized(session); | ||
} | ||
}); | ||
|
||
return { | ||
...context, | ||
headers: { | ||
...context.headers, | ||
...session.headers, | ||
}, | ||
}; | ||
}); | ||
|
||
const authAfterware = onError((error) => { | ||
if (error.networkError && error.networkError.statusCode === 401) { | ||
handleUnauthorized(session); | ||
} | ||
}); | ||
|
||
return authMiddleware.concat(authAfterware).concat(httpLink); | ||
}; | ||
} else { | ||
apolloMiddleware = () => | ||
assert( | ||
"@apollo/client >= 3.0.0 must be installed in order to use the apollo middleware" | ||
); | ||
return authMiddleware.concat(authAfterware).concat(httpLink); | ||
} | ||
|
||
export default apolloMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters