Skip to content

Commit

Permalink
feat: add beforeEach & afterEach hooks into router saga
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruwlalan committed Mar 5, 2022
1 parent 6e4c2b2 commit 67ccc52
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
"semantic-release": "semantic-release"
},
"dependencies": {
"qs": "^6.10.3",
"ramda": "^0.27.2",
"redux-saga": "^1.1.3",
"reselect": "^4.1.5",
"route-parser": "^0.0.5",
"seamless-immutable": "^7.1.4"
},
"devDependencies": {
Expand Down
24 changes: 17 additions & 7 deletions pnpm-lock.yaml

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

8 changes: 6 additions & 2 deletions src/__test__/createRouterSaga.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ describe('createActions', () => {
});
describe('when passed valid arguments', () => {
it('should not throw an error', () => {
expect(() => createRouterSaga({ '/dashboard': () => null })).not.toThrow();
expect(() => createRouterSaga({ '/dashboard': () => null }, true)).not.toThrow();
expect(() =>
createRouterSaga({ matchRoutes: { '/dashboard': () => null } }),
).not.toThrow();
expect(() =>
createRouterSaga({ matchRoutes: { '/dashboard': () => null } }, true),
).not.toThrow();
});
});
});
Expand Down
32 changes: 19 additions & 13 deletions src/createRouterSaga.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import { takeLatest, fork } from 'redux-saga/effects';
import { R, matchRoute } from './utils';

export const createRouterSaga = (routerConfig, logAction) => {
if (R.isNil(routerConfig)) throw new Error('router sagas cannot be empty');
if (R.isEmpty(routerConfig)) throw new Error('router sagas cannot be empty');
if (!R.is(Object, routerConfig)) throw new Error('router sagas must be a valid object');
if (logAction && !R.is(Boolean, logAction)) {
throw new Error('logAction should be a valid boolean');
}
import { R, matchRoute, getParams } from './utils';

const routes = R.keys(routerConfig);
export const createRouterSaga = ({ beforeEach, matchRoutes, afterEach }) => {
if (R.isNil(matchRoutes)) throw new Error('router sagas cannot be empty');
if (R.isEmpty(matchRoutes)) throw new Error('router sagas cannot be empty');
if (!R.is(Object, matchRoutes)) throw new Error('router sagas must be a valid object');

function* navigationLoader(action) {
if (logAction) console.dir(action);
const routes = R.keys(matchRoutes);

function* navigationLoader(action) {
const currentRoute = routes.find((route) => {
return matchRoute(route, action.payload.location.pathname);
});
if (currentRoute) yield* routerConfig[currentRoute](action);

if (currentRoute) {
const location = action.payload.location;
const { queryParams, routeParams } = getParams(location);
try {
yield beforeEach({ action, queryParams, routeParams });
yield* matchRoutes[currentRoute]({ action, queryParams, routeParams });
yield afterEach({ action, queryParams, routeParams });
} catch (error) {
yield afterEach({ action, queryParams, routeParams, error });
}
}

return Promise.resolve();
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import qs from 'qs';
import Route from 'route-parser';
import {
isNil,
isEmpty,
Expand Down Expand Up @@ -44,6 +46,14 @@ export function matchRoute(expectedPattern, actualPath) {
return match;
}

export function getParams(location) {
const options = { ignoreQueryPrefix: true };
const queryParams = qs.parse(location.search, options);
const route = new Route(location.pathname);
const routeParams = route.match(location.pathname);
return { queryParams, routeParams };
}

export const R = {
isNil,
isEmpty,
Expand Down

0 comments on commit 67ccc52

Please sign in to comment.