Skip to content

Commit c73d322

Browse files
authored
feat: Allow registering a service at the root level (#1115)
1 parent c9f4b42 commit c73d322

6 files changed

Lines changed: 31 additions & 17 deletions

File tree

packages/express/test/rest/index.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ describe('@feathersjs/express/rest provider', () => {
102102
return Promise.resolve(data);
103103
}
104104
})
105+
.use('/', Service)
105106
.use('todo', Service);
106107

107108
server = app.listen(4777, () => app.use('tasks', Service));
@@ -110,6 +111,7 @@ describe('@feathersjs/express/rest provider', () => {
110111
after(done => server.close(done));
111112

112113
testCrud('Services', 'todo');
114+
testCrud('Root Service', '/');
113115
testCrud('Dynamic Services', 'tasks');
114116

115117
describe('res.hook', () => {

packages/feathers/lib/application.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const application = {
6666
throw new Error('Registering a new service with `app.service(path, service)` is no longer supported. Use `app.use(path, service)` instead.');
6767
}
6868

69-
const location = stripSlashes(path);
69+
const location = stripSlashes(path) || '/';
7070
const current = this.services[location];
7171

7272
if (typeof current === 'undefined' && typeof this.defaultService === 'function') {
@@ -78,11 +78,11 @@ const application = {
7878
},
7979

8080
use (path, service, options = {}) {
81-
if (typeof path !== 'string' || stripSlashes(path) === '') {
81+
if (typeof path !== 'string') {
8282
throw new Error(`'${path}' is not a valid service path.`);
8383
}
8484

85-
const location = stripSlashes(path);
85+
const location = stripSlashes(path) || '/';
8686
const isSubApp = typeof service.service === 'function' && service.services;
8787
const isService = this.methods.concat('setup').some(name =>
8888
(service && typeof service[name] === 'function')

packages/feathers/test/application.test.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,9 @@ describe('Feathers application', () => {
8585
const app = feathers();
8686

8787
try {
88-
app.use('/', {});
88+
app.use(null, {});
8989
} catch (e) {
90-
assert.strictEqual(e.message, `'/' is not a valid service path.`);
91-
}
92-
93-
try {
94-
app.use('', {});
95-
} catch (e) {
96-
assert.strictEqual(e.message, `'' is not a valid service path.`);
90+
assert.strictEqual(e.message, `'null' is not a valid service path.`);
9791
}
9892

9993
try {
@@ -135,6 +129,17 @@ describe('Feathers application', () => {
135129
}).then(data => assert.strictEqual(data.message, 'Test message'));
136130
});
137131

132+
it('can use a root level service', () => {
133+
const app = feathers().use('/', {
134+
get (id) {
135+
return Promise.resolve({ id });
136+
}
137+
});
138+
139+
return app.service('/').get('test')
140+
.then(result => assert.deepStrictEqual(result, { id: 'test' }));
141+
});
142+
138143
it('services can be re-used (#566)', done => {
139144
const app1 = feathers();
140145
const app2 = feathers();

packages/socketio-client/test/index.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,5 @@ describe('@feathersjs/socketio-client', () => {
7272
});
7373

7474
baseTests(app, 'todos');
75+
baseTests(app, '/');
7576
});

packages/socketio-client/test/server.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,25 @@ class TodoService extends Service {
3232
module.exports = function () {
3333
const app = feathers()
3434
.configure(socketio())
35+
.use('/', new TodoService())
3536
.use('/todos', new TodoService());
3637
const service = app.service('todos');
38+
const rootService = app.service('/');
39+
const publisher = () => app.channel('general');
40+
const data = {
41+
text: 'some todo',
42+
complete: false
43+
};
3744

3845
app.on('connection', connection =>
3946
app.channel('general').join(connection)
4047
);
4148

42-
service.create({
43-
text: 'some todo',
44-
complete: false
45-
});
49+
rootService.create(data);
50+
rootService.publish(publisher);
4651

47-
service.publish(() => app.channel('general'));
52+
service.create(data);
53+
service.publish(publisher);
4854

4955
return app;
5056
};

packages/transport-commons/lib/routing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function () {
1717
return null;
1818
}
1919

20-
return this[ROUTER].lookup(stripSlashes('' + path));
20+
return this[ROUTER].lookup(stripSlashes('' + path) || '/');
2121
}
2222
});
2323

0 commit comments

Comments
 (0)