Skip to content

Commit

Permalink
feat: Allow registering a service at the root level (#1115)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Dec 16, 2018
1 parent c9f4b42 commit c73d322
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
2 changes: 2 additions & 0 deletions packages/express/test/rest/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ describe('@feathersjs/express/rest provider', () => {
return Promise.resolve(data);
}
})
.use('/', Service)
.use('todo', Service);

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

testCrud('Services', 'todo');
testCrud('Root Service', '/');
testCrud('Dynamic Services', 'tasks');

describe('res.hook', () => {
Expand Down
6 changes: 3 additions & 3 deletions packages/feathers/lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const application = {
throw new Error('Registering a new service with `app.service(path, service)` is no longer supported. Use `app.use(path, service)` instead.');
}

const location = stripSlashes(path);
const location = stripSlashes(path) || '/';
const current = this.services[location];

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

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

const location = stripSlashes(path);
const location = stripSlashes(path) || '/';
const isSubApp = typeof service.service === 'function' && service.services;
const isService = this.methods.concat('setup').some(name =>
(service && typeof service[name] === 'function')
Expand Down
21 changes: 13 additions & 8 deletions packages/feathers/test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,9 @@ describe('Feathers application', () => {
const app = feathers();

try {
app.use('/', {});
app.use(null, {});
} catch (e) {
assert.strictEqual(e.message, `'/' is not a valid service path.`);
}

try {
app.use('', {});
} catch (e) {
assert.strictEqual(e.message, `'' is not a valid service path.`);
assert.strictEqual(e.message, `'null' is not a valid service path.`);
}

try {
Expand Down Expand Up @@ -135,6 +129,17 @@ describe('Feathers application', () => {
}).then(data => assert.strictEqual(data.message, 'Test message'));
});

it('can use a root level service', () => {
const app = feathers().use('/', {
get (id) {
return Promise.resolve({ id });
}
});

return app.service('/').get('test')
.then(result => assert.deepStrictEqual(result, { id: 'test' }));
});

it('services can be re-used (#566)', done => {
const app1 = feathers();
const app2 = feathers();
Expand Down
1 change: 1 addition & 0 deletions packages/socketio-client/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ describe('@feathersjs/socketio-client', () => {
});

baseTests(app, 'todos');
baseTests(app, '/');
});
16 changes: 11 additions & 5 deletions packages/socketio-client/test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,25 @@ class TodoService extends Service {
module.exports = function () {
const app = feathers()
.configure(socketio())
.use('/', new TodoService())
.use('/todos', new TodoService());
const service = app.service('todos');
const rootService = app.service('/');
const publisher = () => app.channel('general');
const data = {
text: 'some todo',
complete: false
};

app.on('connection', connection =>
app.channel('general').join(connection)
);

service.create({
text: 'some todo',
complete: false
});
rootService.create(data);
rootService.publish(publisher);

service.publish(() => app.channel('general'));
service.create(data);
service.publish(publisher);

return app;
};
2 changes: 1 addition & 1 deletion packages/transport-commons/lib/routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function () {
return null;
}

return this[ROUTER].lookup(stripSlashes('' + path));
return this[ROUTER].lookup(stripSlashes('' + path) || '/');
}
});

Expand Down

0 comments on commit c73d322

Please sign in to comment.