Skip to content

Commit 5a3df68

Browse files
committed
Add support for Feathers v3 sub-apps (#694)
* Add support for Feathers v3 sub-apps * Remove unused variable
1 parent dd0a360 commit 5a3df68

4 files changed

Lines changed: 73 additions & 5 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## MIT License
22

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

55
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:
66

client.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/application.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,27 @@ const application = {
7979

8080
use (path, service, options = {}) {
8181
const location = stripSlashes(path);
82-
const hasMethod = methods => methods.some(name =>
82+
const isSubApp = typeof service.service === 'function' && service.services;
83+
const isService = this.methods.concat('setup').some(name =>
8384
(service && typeof service[name] === 'function')
8485
);
8586

86-
if (!hasMethod(this.methods.concat('setup'))) {
87+
if (isSubApp) {
88+
const subApp = service;
89+
90+
Object.keys(subApp.services).forEach(subPath =>
91+
this.use(`${location}/${subPath}`, subApp.service(subPath))
92+
);
93+
94+
return this;
95+
}
96+
97+
if (!isService) {
8798
throw new Error(`Invalid service object passed for path \`${location}\``);
8899
}
89100

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

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

test/application.test.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,4 +317,61 @@ describe('Feathers application', () => {
317317
app.setup();
318318
});
319319
});
320+
321+
describe('sub apps', () => {
322+
it('re-registers sub-app services with prefix', done => {
323+
const app = feathers();
324+
const subApp = feathers();
325+
326+
subApp.use('/service1', {
327+
get (id) {
328+
return Promise.resolve({
329+
id, name: 'service1'
330+
});
331+
}
332+
}).use('/service2', {
333+
get (id) {
334+
return Promise.resolve({
335+
id, name: 'service2'
336+
});
337+
},
338+
339+
create (data) {
340+
return Promise.resolve(data);
341+
}
342+
});
343+
344+
app.use('/api/', subApp);
345+
346+
app.service('/api/service2').once('created', data => {
347+
assert.deepEqual(data, {
348+
message: 'This is a test'
349+
});
350+
351+
subApp.service('service2').once('created', data => {
352+
assert.deepEqual(data, {
353+
message: 'This is another test'
354+
});
355+
356+
done();
357+
});
358+
359+
app.service('api/service2').create({
360+
message: 'This is another test'
361+
});
362+
});
363+
364+
app.service('/api/service1').get(10).then(data => {
365+
assert.equal(data.name, 'service1');
366+
367+
return app.service('/api/service2').get(1);
368+
}).then(data => {
369+
assert.equal(data.name, 'service2');
370+
371+
return subApp.service('service2').create({
372+
message: 'This is a test'
373+
});
374+
});
375+
});
376+
});
320377
});

0 commit comments

Comments
 (0)