Skip to content

Commit

Permalink
backward compat with http-header token authz
Browse files Browse the repository at this point in the history
  • Loading branch information
zburke committed Jan 3, 2024
1 parent 8b06801 commit aaea28b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 4 additions & 3 deletions src/createApolloClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { InMemoryCache, ApolloClient } from '@apollo/client';

const createClient = ({ url, tenant }) => (new ApolloClient({
uri: `${url}/graphql`,
const createClient = ({ tenant, token, url }) => (new ApolloClient({
cache: new InMemoryCache(),
credentials: 'include',
headers: {
'X-Okapi-Tenant': tenant,
...(token && { 'X-Okapi-Token': token }),
},
cache: new InMemoryCache(),
uri: `${url}/graphql`,
}));

export default createClient;
5 changes: 4 additions & 1 deletion src/useOkapiKy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import ky from 'ky';
import { useStripes } from './StripesContext';

export default ({ tenant } = {}) => {
const { locale = 'en', timeout = 30000, tenant: currentTenant, url } = useStripes().okapi;
const { locale = 'en', timeout = 30000, tenant: currentTenant, token, url } = useStripes().okapi;

return ky.create({
credentials: 'include',
Expand All @@ -11,6 +11,9 @@ export default ({ tenant } = {}) => {
request => {
request.headers.set('Accept-Language', locale);
request.headers.set('X-Okapi-Tenant', tenant ?? currentTenant);
if (token) {
request.headers.set('X-Okapi-Token', token);
}
}
]
},
Expand Down
14 changes: 9 additions & 5 deletions src/withOkapiKy.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,33 @@ const withOkapiKy = (WrappedComponent) => {
static propTypes = {
stripes: PropTypes.shape({
okapi: PropTypes.shape({
locale: PropTypes.string,
tenant: PropTypes.string.isRequired,
url: PropTypes.string.isRequired,
timeout: PropTypes.number,
locale: PropTypes.string,
token: PropTypes.string,
url: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
};

constructor(props) {
super();
const { tenant, url, timeout = 30000, locale = 'en' } = props.stripes.okapi;
const { tenant, token, url, timeout = 30000, locale = 'en' } = props.stripes.okapi;
this.okapiKy = ky.create({
credentials: 'include',
prefixUrl: url,
hooks: {
beforeRequest: [
request => {
request.headers.set('X-Okapi-Tenant', tenant);
request.headers.set('Accept-Language', locale);
request.headers.set('X-Okapi-Tenant', tenant);
if (token) {
request.headers.set('X-Okapi-Token', token);
}
}
]
},
mode: 'cors',
prefixUrl: url,
retry: 0,
timeout,
});
Expand Down

0 comments on commit aaea28b

Please sign in to comment.