Skip to content

Commit 53378f6

Browse files
authored
fix: dynamic segments are not parsed on model-less resources (#510)
* fix: dynamic segments are not parsed on model-less resources * style: remove unecessary interpolation
1 parent 56321bb commit 53378f6

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

src/packages/router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Namespace from './namespace';
66
import { build, define } from './definitions';
77
import createReplacer from './utils/create-replacer';
88
import type { Router$opts } from './interfaces';
9-
import type Route from './route'; // eslint-disable-line no-duplicate-imports
9+
import type Route from './route';
1010

1111
/**
1212
* @private
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @flow
2+
import { expect } from 'chai';
3+
import { it, describe, before } from 'mocha';
4+
5+
import { getTestApp } from '../../../../test/utils/get-test-app';
6+
import createReplacer from '../utils/create-replacer';
7+
8+
describe('module "router"', () => {
9+
describe('util createReplacer()', () => {
10+
let subject;
11+
12+
before(async () => {
13+
const app = await getTestApp();
14+
// $FlowIgnore
15+
const healthController = app.controllers.get('health');
16+
const { constructor: HealthController } = healthController;
17+
18+
class AdminHealthController extends HealthController {}
19+
20+
subject = createReplacer(new Map([
21+
// $FlowIgnore
22+
['posts', app.controllers.get('posts')],
23+
['health', healthController],
24+
// $FlowIgnore
25+
['admin/posts', app.controllers.get('admin/posts')],
26+
['admin/health', new AdminHealthController({
27+
namespace: 'admin'
28+
})]
29+
]));
30+
});
31+
32+
it('returns an instance of RegExp', () => {
33+
expect(subject).to.be.an.instanceOf(RegExp);
34+
});
35+
36+
it('correctly replaces dynamic parts', () => {
37+
expect(
38+
'posts/1'.replace(subject, '$1/:dynamic')
39+
).to.equal('posts/:dynamic');
40+
41+
expect(
42+
'health/1'.replace(subject, '$1/:dynamic')
43+
).to.equal('health/:dynamic');
44+
});
45+
});
46+
});

src/packages/router/utils/create-replacer.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,25 @@ export default function createReplacer(
99
): RegExp {
1010
const names = Array
1111
.from(controllers)
12-
.map(([, { model }]) => model)
13-
.filter(Boolean)
14-
.map(({ resourceName }) => resourceName)
12+
.map(([, controller]) => {
13+
const { model, namespace } = controller;
14+
15+
if (model) {
16+
return model.resourceName;
17+
}
18+
19+
let { constructor: { name } } = controller;
20+
21+
name = name
22+
.replace(/controller/ig, '')
23+
.toLowerCase();
24+
25+
return namespace
26+
.split('/')
27+
.reduce((str, part) => (
28+
str.replace(new RegExp(part, 'ig'), '')
29+
), name);
30+
})
1531
.filter((str, idx, arr) => idx === arr.lastIndexOf(str))
1632
.join('|');
1733

0 commit comments

Comments
 (0)