1- import express , {
2- Express , static as _static , json , raw , text , urlencoded , query
3- } from 'express' ;
4- import {
5- Application as FeathersApplication , defaultServiceMethods
6- } from '@feathersjs/feathers' ;
1+ import express , { Express } from 'express' ;
2+ import { Application as FeathersApplication , defaultServiceMethods } from '@feathersjs/feathers' ;
3+ import { routing } from '@feathersjs/transport-commons' ;
74import { createDebug } from '@feathersjs/commons' ;
85
96import { Application } from './declarations' ;
10- import { errorHandler , notFound } from './handlers' ;
11- import { parseAuthentication , authenticate } from './authentication' ;
127
13- export {
14- _static as serveStatic , _static as static , json , raw , text ,
15- urlencoded , query , errorHandler , notFound , express as original ,
16- authenticate , parseAuthentication
17- } ;
8+ export { default as original , static , static as serveStatic , json , raw , text , urlencoded , query } from 'express' ;
189
19- export * from './rest ' ;
10+ export * from './authentication ' ;
2011export * from './declarations' ;
12+ export * from './handlers' ;
13+ export * from './rest' ;
2114
2215const debug = createDebug ( '@feathersjs/express' ) ;
2316
@@ -30,10 +23,12 @@ export default function feathersExpress<S = any, C = any> (feathersApp?: Feather
3023 throw new Error ( '@feathersjs/express requires a valid Feathers application instance' ) ;
3124 }
3225
33- const { use, listen } = expressApp as any ;
34- // A mixin that provides the extended functionality
35- const mixin : any = {
36- use ( location : string , ...rest : any [ ] ) {
26+ const app = expressApp as any as Application < S , C > ;
27+ const { use : expressUse , listen : expressListen } = expressApp as any ;
28+ const feathersUse = feathersApp . use ;
29+
30+ Object . assign ( app , {
31+ use ( location : string & keyof S , ...rest : any [ ] ) {
3732 let service : any ;
3833 let options = { } ;
3934
@@ -60,46 +55,56 @@ export default function feathersExpress<S = any, C = any> (feathersApp?: Feather
6055 // Check for service (any object with at least one service method)
6156 if ( hasMethod ( [ 'handle' , 'set' ] ) || ! hasMethod ( defaultServiceMethods ) ) {
6257 debug ( 'Passing app.use call to Express app' ) ;
63- return use . call ( this , location , ...rest ) ;
58+ return expressUse . call ( this , location , ...rest ) ;
6459 }
6560
6661 debug ( 'Registering service with middleware' , middleware ) ;
6762 // Since this is a service, call Feathers `.use`
68- ( feathersApp as FeathersApplication ) . use . call ( this , location , service , {
63+ feathersUse . call ( this , location , service , {
6964 ...options ,
70- middleware
65+ express : middleware
7166 } ) ;
7267
7368 return this ;
7469 } ,
7570
7671 async listen ( ...args : any [ ] ) {
77- const server = listen . call ( this , ...args ) ;
72+ const server = expressListen . call ( this , ...args ) ;
7873
7974 await this . setup ( server ) ;
8075 debug ( 'Feathers application listening' ) ;
8176
8277 return server ;
8378 }
84- } ;
79+ } as Application < S , C > ) ;
8580
86- const feathersDescriptors = {
81+ const appDescriptors = {
82+ ...Object . getOwnPropertyDescriptors ( Object . getPrototypeOf ( app ) ) ,
83+ ...Object . getOwnPropertyDescriptors ( app )
84+ } ;
85+ const newDescriptors = {
8786 ...Object . getOwnPropertyDescriptors ( Object . getPrototypeOf ( feathersApp ) ) ,
8887 ...Object . getOwnPropertyDescriptors ( feathersApp )
8988 } ;
9089
9190 // Copy all non-existing properties (including non-enumerables)
9291 // that don't already exist on the Express app
93- Object . keys ( feathersDescriptors ) . forEach ( prop => {
94- const feathersProp = feathersDescriptors [ prop ] ;
95- const expressProp = Object . getOwnPropertyDescriptor ( expressApp , prop ) ;
92+ Object . keys ( newDescriptors ) . forEach ( prop => {
93+ const appProp = appDescriptors [ prop ] ;
94+ const newProp = newDescriptors [ prop ] ;
9695
97- if ( expressProp === undefined && feathersProp !== undefined ) {
98- Object . defineProperty ( expressApp , prop , feathersProp ) ;
96+ if ( appProp === undefined && newProp !== undefined ) {
97+ Object . defineProperty ( expressApp , prop , newProp ) ;
9998 }
10099 } ) ;
101100
102- return Object . assign ( expressApp , mixin ) ;
101+ app . configure ( routing ( ) as any ) ;
102+ app . use ( ( req , _res , next ) => {
103+ req . feathers = { ...req . feathers , provider : 'rest' } ;
104+ return next ( ) ;
105+ } ) ;
106+
107+ return app ;
103108}
104109
105110if ( typeof module !== 'undefined' ) {
0 commit comments