11import makeDebug from 'debug' ;
22import { stripSlashes } from 'feathers-commons' ;
33import Uberproto from 'uberproto' ;
4- import mixins from './mixins/index' ;
4+
5+ import events from './events' ;
6+ import hooks from './hooks' ;
57
68const debug = makeDebug ( 'feathers:application' ) ;
7- const methods = [ 'find' , 'get' , 'create' , 'update' , 'patch' , 'remove' ] ;
89const Proto = Uberproto . extend ( {
910 create : null
1011} ) ;
1112
12- export default {
13+ const application = {
1314 init ( ) {
1415 Object . assign ( this , {
15- methods,
16- mixins : mixins ( ) ,
16+ methods : [ 'find' , 'get' , 'create' , 'update' , 'patch' , 'remove' ] ,
17+ mixins : [ ] ,
1718 services : { } ,
1819 providers : [ ] ,
19- _setup : false
20+ _setup : false ,
21+ settings : { }
2022 } ) ;
23+
24+ this . configure ( hooks ( ) ) ;
25+ this . configure ( events ( ) ) ;
2126 } ,
2227
23- service ( location , service , options = { } ) {
24- location = stripSlashes ( location ) ;
28+ get ( name ) {
29+ return this . settings [ name ] ;
30+ } ,
2531
26- if ( ! service ) {
27- const current = this . services [ location ] ;
32+ set ( name , value ) {
33+ this . settings [ name ] = value ;
34+ return this ;
35+ } ,
2836
29- if ( typeof current === 'undefined' && typeof this . defaultService === 'function' ) {
30- return this . service ( location , this . defaultService ( location ) , options ) ;
31- }
37+ disable ( name ) {
38+ this . settings [ name ] = false ;
39+ return this ;
40+ } ,
41+
42+ disabled ( name ) {
43+ return ! this . settings [ name ] ;
44+ } ,
3245
33- return current ;
46+ enable ( name ) {
47+ this . settings [ name ] = true ;
48+ return this ;
49+ } ,
50+
51+ enabled ( name ) {
52+ return ! ! this . settings [ name ] ;
53+ } ,
54+
55+ configure ( fn ) {
56+ fn . call ( this ) ;
57+
58+ return this ;
59+ } ,
60+
61+ service ( path , service ) {
62+ if ( typeof service !== 'undefined' ) {
63+ throw new Error ( 'Registering a new service with `app.service(path, service)` is no longer supported. Use `app.use(path, service)` instead.' ) ;
3464 }
3565
36- let protoService = Proto . extend ( service ) ;
66+ const location = stripSlashes ( path ) ;
67+ const current = this . services [ location ] ;
68+
69+ if ( typeof current === 'undefined' && typeof this . defaultService === 'function' ) {
70+ return this . use ( `/${ location } ` , this . defaultService ( location ) )
71+ . service ( location ) ;
72+ }
73+
74+ return current ;
75+ } ,
76+
77+ use ( path , service , options = { } ) {
78+ const location = stripSlashes ( path ) ;
79+ const hasMethod = methods => methods . some ( name =>
80+ ( service && typeof service [ name ] === 'function' )
81+ ) ;
82+
83+ if ( ! hasMethod ( this . methods . concat ( 'setup' ) ) ) {
84+ throw new Error ( `Invalid service object passed for path \`${ location } \`` ) ;
85+ }
86+
87+ const protoService = Proto . extend ( service ) ;
3788
3889 debug ( `Registering new service at \`${ location } \`` ) ;
3990
4091 // Add all the mixins
41- this . mixins . forEach ( fn => fn . call ( this , protoService ) ) ;
92+ this . mixins . forEach ( fn => fn . call ( this , protoService , location , options ) ) ;
4293
4394 if ( typeof protoService . _setup === 'function' ) {
4495 protoService . _setup ( this , location ) ;
4596 }
4697
4798 // Run the provider functions to register the service
4899 this . providers . forEach ( provider =>
49- provider . call ( this , location , protoService , options )
100+ provider . call ( this , protoService , location , options )
50101 ) ;
51102
52103 // If we ran setup already, set this service up explicitly
@@ -55,38 +106,7 @@ export default {
55106 protoService . setup ( this , location ) ;
56107 }
57108
58- return ( this . services [ location ] = protoService ) ;
59- } ,
60-
61- use ( location ) {
62- let service ;
63- let middleware = Array . from ( arguments )
64- . slice ( 1 )
65- . reduce ( function ( middleware , arg ) {
66- if ( typeof arg === 'function' ) {
67- middleware [ service ? 'after' : 'before' ] . push ( arg ) ;
68- } else if ( ! service ) {
69- service = arg ;
70- } else {
71- throw new Error ( 'invalid arg passed to app.use' ) ;
72- }
73- return middleware ;
74- } , {
75- before : [ ] ,
76- after : [ ]
77- } ) ;
78-
79- const hasMethod = methods => methods . some ( name =>
80- ( service && typeof service [ name ] === 'function' )
81- ) ;
82-
83- // Check for service (any object with at least one service method)
84- if ( hasMethod ( [ 'handle' , 'set' ] ) || ! hasMethod ( this . methods . concat ( 'setup' ) ) ) {
85- return this . _super . apply ( this , arguments ) ;
86- }
87-
88- // Any arguments left over are other middleware that we want to pass to the providers
89- this . service ( location , service , { middleware } ) ;
109+ this . services [ location ] = protoService ;
90110
91111 return this ;
92112 } ,
@@ -97,6 +117,7 @@ export default {
97117 const service = this . services [ path ] ;
98118
99119 debug ( `Setting up service for \`${ path } \`` ) ;
120+
100121 if ( typeof service . setup === 'function' ) {
101122 service . setup ( this , path ) ;
102123 }
@@ -105,24 +126,7 @@ export default {
105126 this . _isSetup = true ;
106127
107128 return this ;
108- } ,
109-
110- // Express 3.x configure is gone in 4.x but we'll keep a more basic version
111- // That just takes a function in order to keep Feathers plugin configuration easier.
112- // Environment specific configurations should be done as suggested in the 4.x migration guide:
113- // https://github.com/visionmedia/express/wiki/Migrating-from-3.x-to-4.x
114- configure ( fn ) {
115- fn . call ( this ) ;
116-
117- return this ;
118- } ,
119-
120- listen ( ) {
121- const server = this . _super . apply ( this , arguments ) ;
122-
123- this . setup ( server ) ;
124- debug ( 'Feathers application listening' ) ;
125-
126- return server ;
127129 }
128130} ;
131+
132+ export default application ;
0 commit comments