Skip to content

Commit

Permalink
implement folio-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnC-80 committed Nov 15, 2023
1 parent a323456 commit bd9890c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@apollo/client": "^3.2.1",
"classnames": "^2.2.5",
"core-js": "^3.26.1",
"fake-xml-http-request": "^2.1.2",
"final-form": "^4.18.2",
"graphql": "^16.0.0",
"history": "^4.6.3",
Expand All @@ -80,6 +81,7 @@
"localforage": "^1.5.6",
"lodash": "^4.17.21",
"moment-timezone": "^0.5.14",
"pretender": "^3.4.7",
"prop-types": "^15.5.10",
"query-string": "^7.1.2",
"react-cookie": "^4.0.3",
Expand All @@ -96,7 +98,8 @@
"regenerator-runtime": "^0.13.10",
"rtl-detect": "^1.0.2",
"rxjs": "^6.6.3",
"use-deep-compare": "^1.1.0"
"use-deep-compare": "^1.1.0",
"whatwg-fetch": "^3.6.19"
},
"peerDependencies": {
"@folio/stripes-components": "^12.0.0",
Expand Down
6 changes: 4 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import gatherActions from './gatherActions';
import { destroyStore } from './mainActions';

import Root from './components/Root';
import { registerServiceWorker } from './serviceWorkerRegistration';
// import { registerServiceWorker } from './serviceWorkerRegistration';
import FolioFetch from './folio-fetch';

export default class StripesCore extends Component {
static propTypes = {
Expand All @@ -36,7 +37,8 @@ export default class StripesCore extends Component {
// the service worker functions as a proxy between between the browser
// and the network, intercepting ALL fetch requests to make sure they
// are accompanied by a valid access-token.
registerServiceWorker(okapiConfig, config, this.logger);
// registerServiceWorker(okapiConfig, config, this.logger);
this.ffetch = new FolioFetch();
}

componentWillUnmount() {
Expand Down
30 changes: 15 additions & 15 deletions src/loginServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,21 @@ export async function logout(okapiUrl, store) {
* @returns {Promise}
*/
export const postTokenExpiration = (tokenExpiration) => {
if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
return navigator.serviceWorker.ready
.then((reg) => {
const sw = reg.active;
if (sw) {
const message = { source: '@folio/stripes-core', type: 'TOKEN_EXPIRATION', value: { tokenExpiration } };
logger.log('rtr', '<= sending', message);
sw.postMessage(message);
} else {
logger.log('rtr', 'error, could not send TOKEN_EXPIRATION message; no ServiceWorker is active');
}
});
}

logger.log('rtr', 'error, could not send TOKEN_EXPIRATION message; navigator.serviceWorker is empty');
// if ('serviceWorker' in navigator && navigator.serviceWorker.controller) {
// return navigator.serviceWorker.ready
// .then((reg) => {
// const sw = reg.active;
// if (sw) {
// const message = { source: '@folio/stripes-core', type: 'TOKEN_EXPIRATION', value: { tokenExpiration } };
// logger.log('rtr', '<= sending', message);
// sw.postMessage(message);
// } else {
// logger.log('rtr', 'error, could not send TOKEN_EXPIRATION message; no ServiceWorker is active');
// }
// });
// }

// logger.log('rtr', 'error, could not send TOKEN_EXPIRATION message; navigator.serviceWorker is empty');
return Promise.resolve();
};

Expand Down
11 changes: 8 additions & 3 deletions src/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
*
*/

import { okapiUrl, okapiTenant } from 'micro-stripes-config';
import { okapi } from 'stripes-config';

const {
url: okapiUrl,
tenant: okapiTenant
} = okapi;

/** { atExpires, rtExpires } both are JS millisecond timestamps */
let tokenExpiration = null;
Expand Down Expand Up @@ -327,9 +332,9 @@ const passThroughWithRT = (event) => {
* @returns Promise
* @throws if any fetch fails
*/
const passThroughWithAT = (event) => {
export const passThroughWithAT = (event, handler) => {
if (shouldLog) console.log('-- (rtr-sw) (valid AT or authn request)');
return fetch(event.request, { credentials: 'include' })
return handler(event.request, { credentials: 'include' })
.then(response => {
// Handle three different situations:
// 1. 403: AT was expired (try RTR)
Expand Down

0 comments on commit bd9890c

Please sign in to comment.