Skip to content

Commit

Permalink
feat: support http redirect code in transition hook plugin
Browse files Browse the repository at this point in the history
rename guard to transtionhookexecutor
  • Loading branch information
stas-nc committed Dec 20, 2024
1 parent 94da1da commit d787c16
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 112 deletions.
8 changes: 4 additions & 4 deletions ilc/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Router from './ClientRouter';
import initIlcState from './initIlcState';
import I18n from './i18n';

import GuardManager from './GuardManager';
import TransitionHooksExecutor from './TransitionHooksExecutor';
import ParcelApi from './ParcelApi';
import { BundleLoader } from './BundleLoader';
import { registerApplications } from './registerSpaApps';
Expand Down Expand Up @@ -62,7 +62,7 @@ export class Client {

#router;

#guardManager;
#transitionHooksExecutor;

#urlProcessor;

Expand Down Expand Up @@ -114,7 +114,7 @@ export class Client {
singleSpa,
this.#transitionManager.handlePageTransition.bind(this.#transitionManager),
);
this.#guardManager = new GuardManager(
this.#transitionHooksExecutor = new TransitionHooksExecutor(
this.#router,
this.#pluginManager,
this.#onCriticalInternalError.bind(this),
Expand Down Expand Up @@ -268,7 +268,7 @@ export class Client {
}

#configure() {
addNavigationHook((url) => (this.#guardManager.hasAccessTo(url) ? url : null));
addNavigationHook((url) => (this.#transitionHooksExecutor.hasAccessTo(url) ? url : null));
addNavigationHook((url) => this.#urlProcessor.process(url));

// TODO: window.ILC.importLibrary - calls bootstrap function with props (if supported), and returns exposed API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import errors from '../common/guard/errors';
import { TransitionHookError } from '../common/guard/errors';
import actionTypes from '../common/guard/actionTypes';

export default class GuardManager {
/**
* Executes ILC Transition plugin's hooks
*/
export default class TransitionHooksExecutor {
#router;
#transitionHooksPlugin;
#errorHandler;
Expand Down Expand Up @@ -58,7 +61,7 @@ export default class GuardManager {
this.#logger.info(
`ILC: Redirect from "${route.reqUrl}" to "${
action.newLocation
}" due to the Route Guard with index #${hooks.indexOf(hook)}`,
}" due to the Transition Hook with index #${hooks.indexOf(hook)}`,
);
this.#router.navigateToUrl(action.newLocation);
});
Expand All @@ -68,7 +71,7 @@ export default class GuardManager {
const hookIndex = hooks.indexOf(hook);

this.#errorHandler(
new errors.GuardTransitionHookError({
new TransitionHookError({
message: `An error has occurred while executing "${hookIndex}" transition hook for the following URL: "${url}".`,
data: {
hookIndex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import chai from 'chai';
import sinon from 'sinon';

import errors from '../common/guard/errors';
import { TransitionHookError } from '../common/guard/errors';
import actionTypes from '../common/guard/actionTypes';
import GuardManager from './GuardManager';
import TransitionHooksExecutor from './TransitionHooksExecutor';

describe('GuardManager', () => {
describe('TransitionHooksExecutor', () => {
let clock;

const route = Object.freeze({
Expand Down Expand Up @@ -68,9 +68,9 @@ describe('GuardManager', () => {
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns({ specialRole: 404 });

const guardManager = new GuardManager(router, pluginManager, errorHandler, logger);
const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(guardManager.hasAccessTo('/router/does/not/have/route')).to.be.true;
chai.expect(transitionHooksExecutor.hasAccessTo('/router/does/not/have/route')).to.be.true;
});

it(`if none of hooks returns "${actionTypes.stopNavigation}" or "${actionTypes.redirect}" action types`, () => {
Expand All @@ -85,9 +85,9 @@ describe('GuardManager', () => {
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns(route);

const guardManager = new GuardManager(router, pluginManager, errorHandler, logger);
const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(guardManager.hasAccessTo(url)).to.be.true;
chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.true;

for (const hook of hooks) {
sinon.assert.calledOnceWithExactly(hook, {
Expand All @@ -113,12 +113,12 @@ describe('GuardManager', () => {
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns(route);

const guardManager = new GuardManager(router, pluginManager, errorHandler, logger);
const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(guardManager.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
sinon.assert.calledOnce(errorHandler);
chai.expect(errorHandler.getCall(0).args[0]).to.have.property('cause', error);
chai.expect(errorHandler.getCall(0).args[0]).to.be.instanceOf(errors.GuardTransitionHookError);
chai.expect(errorHandler.getCall(0).args[0]).to.be.instanceOf(TransitionHookError);
chai.expect(errorHandler.getCall(0).args[0].data).to.be.eql({
hookIndex: 1,
url,
Expand Down Expand Up @@ -150,9 +150,9 @@ describe('GuardManager', () => {
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns(route);

const guardManager = new GuardManager(router, pluginManager, errorHandler, logger);
const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(guardManager.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
sinon.assert.calledOnceWithExactly(
logger.info,
`ILC: Stopped navigation due to the Route Guard with index #${1}`,
Expand Down Expand Up @@ -183,9 +183,9 @@ describe('GuardManager', () => {
transitionHooksPlugin.getTransitionHooks.returns(hooks);
router.match.returns(route);

const guardManager = new GuardManager(router, pluginManager, errorHandler, logger);
const transitionHooksExecutor = new TransitionHooksExecutor(router, pluginManager, errorHandler, logger);

chai.expect(guardManager.hasAccessTo(url)).to.be.false;
chai.expect(transitionHooksExecutor.hasAccessTo(url)).to.be.false;
sinon.assert.notCalled(router.navigateToUrl);

for (const hook of [hooks[0], hooks[1]]) {
Expand All @@ -204,7 +204,7 @@ describe('GuardManager', () => {

sinon.assert.calledWithExactly(
logger.info,
`ILC: Redirect from "${route.reqUrl}" to "${url}" due to the Route Guard with index #${1}`,
`ILC: Redirect from "${route.reqUrl}" to "${url}" due to the Transition Hook with index #${1}`,
);
sinon.assert.calledWithExactly(router.navigateToUrl, url);
});
Expand Down
11 changes: 0 additions & 11 deletions ilc/common/guard/errors.js

This file was deleted.

8 changes: 8 additions & 0 deletions ilc/common/guard/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { extendError } from '../utils';

export const TransitionError = extendError('TransitionError', { defaultData: {} });

export const TransitionHookError = extendError('TransitionHookError', {
parent: TransitionError,
defaultData: {},
});
8 changes: 4 additions & 4 deletions ilc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ilc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"fast-glob": "^3.3.2",
"fastify": "^2.15.3",
"http-status-codes": "^2.3.0",
"ilc-plugins-sdk": "^2.1.0",
"ilc-plugins-sdk": "^2.2.0",
"ilc-sdk": "^5.2.4",
"is-url": "^1.2.4",
"js-cookie": "^2.2.1",
Expand Down
59 changes: 0 additions & 59 deletions ilc/server/GuardManager.js

This file was deleted.

Loading

0 comments on commit d787c16

Please sign in to comment.