Skip to content

Commit

Permalink
getAdditionalState only takes the state object
Browse files Browse the repository at this point in the history
  • Loading branch information
sometimeskind committed Jun 27, 2018
1 parent 2aa21ec commit 4d4b7a8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
11 changes: 7 additions & 4 deletions lib/am-path-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ export default function getPathHelpers(routesMap: { [routeName: string]: { path?
return Object.keys(pathCheckers).find((routeName) => pathCheckers[routeName].find((checker) => checker.exec(path)));
},

getAdditionalState(routeName: string, parentState: { resource: string }): { params: ParamsType, query: ParamsType } {
if (typeof parentState.resource !== 'string') {
getAdditionalState(parentState: { resource: string, route: ?{ name: string } }): { params: ParamsType, query: ParamsType } {
const { resource, route } = parentState;

if (typeof resource !== 'string') {
throw new Error('Resource not provided.');
}

const [path, search] = parentState.resource.split('?');
const [path, search] = resource.split('?');

return {
params: (routeName && evalPathParams(routeName, path)) || {},
params: (route && evalPathParams(route.name, path)) || {},
query: qs.parse(search),
};
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "am-path-helpers",
"version": "0.1.0",
"version": "0.2.0",
"description": "Helpers for App Manager that provide enhanced tools for path matching",
"main": "index.js",
"scripts": {
Expand Down
10 changes: 5 additions & 5 deletions test/path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Path Helpers', () => {
const { getAdditionalState } = getPathHelpers(routes);

it('finds params from route', () => {
const { params } = getAdditionalState('APP', { resource: '/path/1058690/valid/optional' });
const { params } = getAdditionalState({ resource: '/path/1058690/valid/optional', route: { name: 'APP' } });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand All @@ -60,7 +60,7 @@ describe('Path Helpers', () => {
});

it('handles optional params from route', () => {
const { params, query } = getAdditionalState('APP', { resource: '/path/1058690/valid' });
const { params, query } = getAdditionalState({ resource: '/path/1058690/valid', route: { name: 'APP' } });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand All @@ -70,17 +70,17 @@ describe('Path Helpers', () => {
});

it('handles invalid routes', () => {
const { params } = getAdditionalState('APP', { resource: '/invalid/1058690/valid' });
const { params } = getAdditionalState({ resource: '/invalid/1058690/valid', route: { name: 'APP' } });
expect(params).to.be.an('object').that.is.empty;
});

it('handles invalid params', () => {
const { params } = getAdditionalState('APP', { resource: '/path/1058690/invalid/abcde' });
const { params } = getAdditionalState({ resource: '/path/1058690/invalid/abcde', route: { name: 'APP' } });
expect(params).to.be.an('object').that.is.empty;
});

it('parses queries', () => {
const { params, query } = getAdditionalState('APP', { resource: '/path/1058690/valid?test=true' });
const { params, query } = getAdditionalState({ resource: '/path/1058690/valid?test=true', route: { name: 'APP' } });
expect(params).to.deep.equals({
id: '1058690',
otherParam: 'valid',
Expand Down

0 comments on commit 4d4b7a8

Please sign in to comment.