-
-
Notifications
You must be signed in to change notification settings - Fork 797
Expand file tree
/
Copy pathindex.ts
More file actions
156 lines (134 loc) · 4.52 KB
/
index.ts
File metadata and controls
156 lines (134 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import express, { Express } from 'express'
import { Application as FeathersApplication, defaultServiceMethods } from '@feathersjs/feathers'
import { routing } from '@feathersjs/transport-commons'
import { createDebug } from '@feathersjs/commons'
import cors from 'cors'
import compression from 'compression'
import { rest, RestOptions, formatter } from './rest'
import { errorHandler, notFound, ErrorHandlerOptions } from './handlers'
import { Application, ExpressOverrides } from './declarations'
import { AuthenticationSettings, authenticate, parseAuthentication } from './authentication'
import { default as original, static as serveStatic, json, raw, text, urlencoded, query } from 'express'
export {
original,
serveStatic,
serveStatic as static,
json,
raw,
text,
urlencoded,
query,
rest,
RestOptions,
formatter,
errorHandler,
notFound,
Application,
ErrorHandlerOptions,
ExpressOverrides,
AuthenticationSettings,
parseAuthentication,
authenticate,
cors,
compression
}
const debug = createDebug('@feathersjs/express')
export default function feathersExpress<S = any, C = any>(
feathersApp?: FeathersApplication<S, C>,
expressApp: Express = express()
): Application<S, C> {
if (!feathersApp) {
return expressApp as any
}
if (typeof feathersApp.setup !== 'function') {
throw new Error('@feathersjs/express requires a valid Feathers application instance')
}
const app = expressApp as any as Application<S, C>
const { use: expressUse, listen: expressListen } = expressApp as any
const { use: feathersUse, teardown: feathersTeardown } = feathersApp
Object.assign(app, {
use(location: string & keyof S, ...rest: any[]) {
let service: any
let options = {}
const middleware = rest.reduce(
function (middleware, arg) {
if (typeof arg === 'function' || Array.isArray(arg)) {
middleware[service ? 'after' : 'before'].push(arg)
} else if (!service) {
service = arg
} else if (arg.methods || arg.events || arg.express || arg.koa) {
options = arg
} else {
throw new Error('Invalid options passed to app.use')
}
return middleware
},
{
before: [],
after: []
}
)
const hasMethod = (methods: string[]) =>
methods.some((name) => service && typeof service[name] === 'function')
// Check for service (any object with at least one service method)
if (hasMethod(['handle', 'set']) || !hasMethod(defaultServiceMethods)) {
debug('Passing app.use call to Express app')
return expressUse.call(this, location, ...rest)
}
debug('Registering service with middleware', middleware)
// Since this is a service, call Feathers `.use`
feathersUse.call(this, location, service, {
express: middleware,
...options
})
return this
},
async listen(...args: any[]) {
const server = expressListen.call(this, ...args)
this.server = server
await this.setup(server)
debug('Feathers application listening')
return server
}
} as Application<S, C>)
const appDescriptors = {
...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(app)),
...Object.getOwnPropertyDescriptors(app)
}
const newDescriptors = {
...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(feathersApp)),
...Object.getOwnPropertyDescriptors(feathersApp)
}
// Copy all non-existing properties (including non-enumerables)
// that don't already exist on the Express app
Object.keys(newDescriptors).forEach((prop) => {
const appProp = appDescriptors[prop]
const newProp = newDescriptors[prop]
if (appProp === undefined && newProp !== undefined) {
Object.defineProperty(expressApp, prop, newProp)
}
})
// Assign teardown and setup which will also make sure that hooks are initialized
app.setup = feathersApp.setup as any
app.teardown = async function teardown(server?: any) {
return feathersTeardown.call(this, server).then(
() =>
new Promise((resolve, reject) => {
if (this.server) {
this.server.close((e) => (e ? reject(e) : resolve(this)))
} else {
resolve(this)
}
})
)
}
app.configure(routing() as any)
app.use((req, _res, next) => {
req.feathers = { ...req.feathers, provider: 'rest' }
return next()
})
return app
}
if (typeof module !== 'undefined') {
module.exports = Object.assign(feathersExpress, module.exports)
}