Skip to content

Commit

Permalink
Add support for Feathers v3 sub-apps (#694)
Browse files Browse the repository at this point in the history
* Add support for Feathers v3 sub-apps

* Remove unused variable
  • Loading branch information
daffl committed Oct 29, 2017
1 parent dd0a360 commit 5a3df68
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## MIT License

Copyright (C) 2015 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)
Copyright (C) 2017 [Feathers contributors](https://github.com/feathersjs/feathers/graphs/contributors)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
1 change: 0 additions & 1 deletion client.js

This file was deleted.

18 changes: 15 additions & 3 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,27 @@ const application = {

use (path, service, options = {}) {
const location = stripSlashes(path);
const hasMethod = methods => methods.some(name =>
const isSubApp = typeof service.service === 'function' && service.services;
const isService = this.methods.concat('setup').some(name =>
(service && typeof service[name] === 'function')
);

if (!hasMethod(this.methods.concat('setup'))) {
if (isSubApp) {
const subApp = service;

Object.keys(subApp.services).forEach(subPath =>
this.use(`${location}/${subPath}`, subApp.service(subPath))
);

return this;
}

if (!isService) {
throw new Error(`Invalid service object passed for path \`${location}\``);
}

const protoService = Proto.extend(service);
// If the service is already Uberproto'd use it directly
const protoService = Proto.isPrototypeOf(service) ? service : Proto.extend(service);

debug(`Registering new service at \`${location}\``);

Expand Down
57 changes: 57 additions & 0 deletions test/application.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,4 +317,61 @@ describe('Feathers application', () => {
app.setup();
});
});

describe('sub apps', () => {
it('re-registers sub-app services with prefix', done => {
const app = feathers();
const subApp = feathers();

subApp.use('/service1', {
get (id) {
return Promise.resolve({
id, name: 'service1'
});
}
}).use('/service2', {
get (id) {
return Promise.resolve({
id, name: 'service2'
});
},

create (data) {
return Promise.resolve(data);
}
});

app.use('/api/', subApp);

app.service('/api/service2').once('created', data => {
assert.deepEqual(data, {
message: 'This is a test'
});

subApp.service('service2').once('created', data => {
assert.deepEqual(data, {
message: 'This is another test'
});

done();
});

app.service('api/service2').create({
message: 'This is another test'
});
});

app.service('/api/service1').get(10).then(data => {
assert.equal(data.name, 'service1');

return app.service('/api/service2').get(1);
}).then(data => {
assert.equal(data.name, 'service2');

return subApp.service('service2').create({
message: 'This is a test'
});
});
});
});
});

0 comments on commit 5a3df68

Please sign in to comment.