Skip to content

Commit

Permalink
fix: dynamic segments are not parsed on model-less resources (#510)
Browse files Browse the repository at this point in the history
* fix: dynamic segments are not parsed on model-less resources

* style: remove unecessary interpolation
  • Loading branch information
zacharygolba authored Nov 10, 2016
1 parent 56321bb commit 53378f6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/packages/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Namespace from './namespace';
import { build, define } from './definitions';
import createReplacer from './utils/create-replacer';
import type { Router$opts } from './interfaces';
import type Route from './route'; // eslint-disable-line no-duplicate-imports
import type Route from './route';

/**
* @private
Expand Down
46 changes: 46 additions & 0 deletions src/packages/router/test/create-replacer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @flow
import { expect } from 'chai';
import { it, describe, before } from 'mocha';

import { getTestApp } from '../../../../test/utils/get-test-app';
import createReplacer from '../utils/create-replacer';

describe('module "router"', () => {
describe('util createReplacer()', () => {
let subject;

before(async () => {
const app = await getTestApp();
// $FlowIgnore
const healthController = app.controllers.get('health');
const { constructor: HealthController } = healthController;

class AdminHealthController extends HealthController {}

subject = createReplacer(new Map([
// $FlowIgnore
['posts', app.controllers.get('posts')],
['health', healthController],
// $FlowIgnore
['admin/posts', app.controllers.get('admin/posts')],
['admin/health', new AdminHealthController({
namespace: 'admin'
})]
]));
});

it('returns an instance of RegExp', () => {
expect(subject).to.be.an.instanceOf(RegExp);
});

it('correctly replaces dynamic parts', () => {
expect(
'posts/1'.replace(subject, '$1/:dynamic')
).to.equal('posts/:dynamic');

expect(
'health/1'.replace(subject, '$1/:dynamic')
).to.equal('health/:dynamic');
});
});
});
22 changes: 19 additions & 3 deletions src/packages/router/utils/create-replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,25 @@ export default function createReplacer(
): RegExp {
const names = Array
.from(controllers)
.map(([, { model }]) => model)
.filter(Boolean)
.map(({ resourceName }) => resourceName)
.map(([, controller]) => {
const { model, namespace } = controller;

if (model) {
return model.resourceName;
}

let { constructor: { name } } = controller;

name = name
.replace(/controller/ig, '')
.toLowerCase();

return namespace
.split('/')
.reduce((str, part) => (
str.replace(new RegExp(part, 'ig'), '')
), name);
})
.filter((str, idx, arr) => idx === arr.lastIndexOf(str))
.join('|');

Expand Down

0 comments on commit 53378f6

Please sign in to comment.