@feathersjs/express 5.0.0-pre.9 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +192 -234
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/lib/authentication.d.ts +5 -6
- package/lib/authentication.js +27 -38
- package/lib/authentication.js.map +1 -1
- package/lib/declarations.d.ts +16 -11
- package/lib/handlers.d.ts +1 -1
- package/lib/handlers.js +5 -3
- package/lib/handlers.js.map +1 -1
- package/lib/index.d.ts +9 -7
- package/lib/index.js +77 -47
- package/lib/index.js.map +1 -1
- package/lib/rest.d.ts +9 -9
- package/lib/rest.js +60 -90
- package/lib/rest.js.map +1 -1
- package/package.json +28 -21
- package/src/authentication.ts +48 -53
- package/src/declarations.ts +43 -35
- package/src/handlers.ts +52 -50
- package/src/index.ts +113 -64
- package/src/rest.ts +91 -98
package/src/handlers.ts
CHANGED
|
@@ -1,130 +1,132 @@
|
|
|
1
|
-
import path from 'path'
|
|
2
|
-
import { NotFound, GeneralError } from '@feathersjs/errors'
|
|
3
|
-
import { Request, Response, NextFunction, ErrorRequestHandler, RequestHandler } from 'express'
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { NotFound, GeneralError } from '@feathersjs/errors'
|
|
3
|
+
import { Request, Response, NextFunction, ErrorRequestHandler, RequestHandler } from 'express'
|
|
4
4
|
|
|
5
5
|
const defaults = {
|
|
6
6
|
public: path.resolve(__dirname, '..', 'public'),
|
|
7
7
|
logger: console
|
|
8
|
-
}
|
|
9
|
-
const defaultHtmlError = path.resolve(defaults.public, 'default.html')
|
|
8
|
+
}
|
|
9
|
+
const defaultHtmlError = path.resolve(defaults.public, 'default.html')
|
|
10
10
|
|
|
11
|
-
export function notFound
|
|
11
|
+
export function notFound({ verbose = false } = {}): RequestHandler {
|
|
12
12
|
return function (req: Request, _res: Response, next: NextFunction) {
|
|
13
|
-
const url = `${req.url}
|
|
14
|
-
const message = `Page not found${verbose ? ': ' + url : ''}
|
|
13
|
+
const url = `${req.url}`
|
|
14
|
+
const message = `Page not found${verbose ? ': ' + url : ''}`
|
|
15
15
|
|
|
16
|
-
next(new NotFound(message, { url }))
|
|
17
|
-
}
|
|
16
|
+
next(new NotFound(message, { url }))
|
|
17
|
+
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
export type ErrorHandlerOptions = {
|
|
21
|
-
public?: string
|
|
22
|
-
logger?: boolean|{ error?: (msg: any) => void
|
|
23
|
-
html?: any
|
|
21
|
+
public?: string
|
|
22
|
+
logger?: boolean | { error?: (msg: any) => void; info?: (msg: any) => void }
|
|
23
|
+
html?: any
|
|
24
24
|
json?: any
|
|
25
|
-
}
|
|
25
|
+
}
|
|
26
26
|
|
|
27
|
-
export function errorHandler
|
|
28
|
-
const options = Object.assign({}, defaults, _options)
|
|
27
|
+
export function errorHandler(_options: ErrorHandlerOptions = {}): ErrorRequestHandler {
|
|
28
|
+
const options = Object.assign({}, defaults, _options)
|
|
29
29
|
|
|
30
30
|
if (typeof options.html === 'undefined') {
|
|
31
31
|
options.html = {
|
|
32
32
|
401: path.resolve(options.public, '401.html'),
|
|
33
33
|
404: path.resolve(options.public, '404.html'),
|
|
34
34
|
default: defaultHtmlError
|
|
35
|
-
}
|
|
35
|
+
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
if (typeof options.json === 'undefined') {
|
|
39
|
-
options.json = {}
|
|
39
|
+
options.json = {}
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
return function (error: any, req: Request, res: Response, next: NextFunction) {
|
|
43
43
|
// Set the error code for HTTP processing semantics
|
|
44
|
-
error.code = !isNaN(parseInt(error.code, 10)) ? parseInt(error.code, 10) : 500
|
|
44
|
+
error.code = !isNaN(parseInt(error.code, 10)) ? parseInt(error.code, 10) : 500
|
|
45
45
|
|
|
46
46
|
// Log the error if it didn't come from a service method call
|
|
47
47
|
if (options.logger && typeof options.logger.error === 'function' && !res.hook) {
|
|
48
48
|
if (error.code >= 500) {
|
|
49
|
-
options.logger.error(error)
|
|
49
|
+
options.logger.error(error)
|
|
50
50
|
} else {
|
|
51
|
-
options.logger.info(error)
|
|
51
|
+
options.logger.info(error)
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (error.type !== 'FeathersError') {
|
|
56
|
-
const oldError = error
|
|
56
|
+
const oldError = error
|
|
57
57
|
|
|
58
|
-
error = oldError.errors
|
|
59
|
-
|
|
60
|
-
|
|
58
|
+
error = oldError.errors
|
|
59
|
+
? new GeneralError(oldError.message, {
|
|
60
|
+
errors: oldError.errors
|
|
61
|
+
})
|
|
62
|
+
: new GeneralError(oldError.message)
|
|
61
63
|
|
|
62
64
|
if (oldError.stack) {
|
|
63
|
-
error.stack = oldError.stack
|
|
65
|
+
error.stack = oldError.stack
|
|
64
66
|
}
|
|
65
67
|
}
|
|
66
68
|
|
|
67
|
-
const formatter: { [key: string]: any } = {}
|
|
69
|
+
const formatter: { [key: string]: any } = {}
|
|
68
70
|
|
|
69
71
|
// If the developer passed a custom function for ALL html errors
|
|
70
72
|
if (typeof options.html === 'function') {
|
|
71
|
-
formatter['text/html'] = options.html
|
|
73
|
+
formatter['text/html'] = options.html
|
|
72
74
|
} else {
|
|
73
|
-
let file = options.html[error.code]
|
|
75
|
+
let file = options.html[error.code]
|
|
74
76
|
if (!file) {
|
|
75
|
-
file = options.html.default || defaultHtmlError
|
|
77
|
+
file = options.html.default || defaultHtmlError
|
|
76
78
|
}
|
|
77
79
|
// If the developer passed a custom function for individual html errors
|
|
78
80
|
if (typeof file === 'function') {
|
|
79
|
-
formatter['text/html'] = file
|
|
81
|
+
formatter['text/html'] = file
|
|
80
82
|
} else {
|
|
81
83
|
formatter['text/html'] = function () {
|
|
82
|
-
res.set('Content-Type', 'text/html')
|
|
83
|
-
res.sendFile(file)
|
|
84
|
-
}
|
|
84
|
+
res.set('Content-Type', 'text/html')
|
|
85
|
+
res.sendFile(file)
|
|
86
|
+
}
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
|
|
88
90
|
// If the developer passed a custom function for ALL json errors
|
|
89
91
|
if (typeof options.json === 'function') {
|
|
90
|
-
formatter['application/json'] = options.json
|
|
92
|
+
formatter['application/json'] = options.json
|
|
91
93
|
} else {
|
|
92
|
-
const handler = options.json[error.code] || options.json.default
|
|
94
|
+
const handler = options.json[error.code] || options.json.default
|
|
93
95
|
// If the developer passed a custom function for individual json errors
|
|
94
96
|
if (typeof handler === 'function') {
|
|
95
|
-
formatter['application/json'] = handler
|
|
97
|
+
formatter['application/json'] = handler
|
|
96
98
|
} else {
|
|
97
99
|
// Don't show stack trace if it is a 404 error
|
|
98
100
|
if (error.code === 404) {
|
|
99
|
-
error.stack = null
|
|
101
|
+
error.stack = null
|
|
100
102
|
}
|
|
101
103
|
|
|
102
104
|
formatter['application/json'] = function () {
|
|
103
|
-
const output = Object.assign({}, error.toJSON())
|
|
105
|
+
const output = Object.assign({}, error.toJSON())
|
|
104
106
|
|
|
105
107
|
if (process.env.NODE_ENV === 'production') {
|
|
106
|
-
delete output.stack
|
|
108
|
+
delete output.stack
|
|
107
109
|
}
|
|
108
110
|
|
|
109
|
-
res.set('Content-Type', 'application/json')
|
|
110
|
-
res.json(output)
|
|
111
|
-
}
|
|
111
|
+
res.set('Content-Type', 'application/json')
|
|
112
|
+
res.json(output)
|
|
113
|
+
}
|
|
112
114
|
}
|
|
113
115
|
}
|
|
114
116
|
|
|
115
|
-
res.status(error.code)
|
|
117
|
+
res.status(error.code)
|
|
116
118
|
|
|
117
|
-
const contentType = req.headers['content-type'] || ''
|
|
118
|
-
const accepts = req.headers.accept || ''
|
|
119
|
+
const contentType = req.headers['content-type'] || ''
|
|
120
|
+
const accepts = req.headers.accept || ''
|
|
119
121
|
|
|
120
122
|
// by default just send back json
|
|
121
123
|
if (contentType.indexOf('json') !== -1 || accepts.indexOf('json') !== -1) {
|
|
122
|
-
formatter['application/json'](error, req, res, next)
|
|
124
|
+
formatter['application/json'](error, req, res, next)
|
|
123
125
|
} else if (options.html && (contentType.indexOf('html') !== -1 || accepts.indexOf('html') !== -1)) {
|
|
124
|
-
formatter['text/html'](error, req, res, next)
|
|
126
|
+
formatter['text/html'](error, req, res, next)
|
|
125
127
|
} else {
|
|
126
128
|
// TODO (EK): Maybe just return plain text
|
|
127
|
-
formatter['application/json'](error, req, res, next)
|
|
129
|
+
formatter['application/json'](error, req, res, next)
|
|
128
130
|
}
|
|
129
|
-
}
|
|
131
|
+
}
|
|
130
132
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,107 +1,156 @@
|
|
|
1
|
-
import express, {
|
|
2
|
-
|
|
3
|
-
} from '
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
1
|
+
import express, { Express } from 'express'
|
|
2
|
+
import { Application as FeathersApplication, defaultServiceMethods } from '@feathersjs/feathers'
|
|
3
|
+
import { routing } from '@feathersjs/transport-commons'
|
|
4
|
+
import { createDebug } from '@feathersjs/commons'
|
|
5
|
+
import cors from 'cors'
|
|
6
|
+
import compression from 'compression'
|
|
7
|
+
|
|
8
|
+
import { rest, RestOptions, formatter } from './rest'
|
|
9
|
+
import { errorHandler, notFound, ErrorHandlerOptions } from './handlers'
|
|
10
|
+
import { Application, ExpressOverrides } from './declarations'
|
|
11
|
+
import { AuthenticationSettings, authenticate, parseAuthentication } from './authentication'
|
|
12
|
+
import { default as original, static as serveStatic, json, raw, text, urlencoded, query } from 'express'
|
|
12
13
|
|
|
13
14
|
export {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
15
|
+
original,
|
|
16
|
+
serveStatic,
|
|
17
|
+
serveStatic as static,
|
|
18
|
+
json,
|
|
19
|
+
raw,
|
|
20
|
+
text,
|
|
21
|
+
urlencoded,
|
|
22
|
+
query,
|
|
23
|
+
rest,
|
|
24
|
+
RestOptions,
|
|
25
|
+
formatter,
|
|
26
|
+
errorHandler,
|
|
27
|
+
notFound,
|
|
28
|
+
Application,
|
|
29
|
+
ErrorHandlerOptions,
|
|
30
|
+
ExpressOverrides,
|
|
31
|
+
AuthenticationSettings,
|
|
32
|
+
parseAuthentication,
|
|
33
|
+
authenticate,
|
|
34
|
+
cors,
|
|
35
|
+
compression
|
|
36
|
+
}
|
|
21
37
|
|
|
22
|
-
const debug = createDebug('@feathersjs/express')
|
|
38
|
+
const debug = createDebug('@feathersjs/express')
|
|
23
39
|
|
|
24
|
-
export default function feathersExpress<S = any, C = any>
|
|
40
|
+
export default function feathersExpress<S = any, C = any>(
|
|
41
|
+
feathersApp?: FeathersApplication<S, C>,
|
|
42
|
+
expressApp: Express = express()
|
|
43
|
+
): Application<S, C> {
|
|
25
44
|
if (!feathersApp) {
|
|
26
|
-
return expressApp as any
|
|
45
|
+
return expressApp as any
|
|
27
46
|
}
|
|
28
47
|
|
|
29
48
|
if (typeof feathersApp.setup !== 'function') {
|
|
30
|
-
throw new Error('@feathersjs/express requires a valid Feathers application instance')
|
|
49
|
+
throw new Error('@feathersjs/express requires a valid Feathers application instance')
|
|
31
50
|
}
|
|
32
51
|
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
52
|
+
const app = expressApp as any as Application<S, C>
|
|
53
|
+
const { use: expressUse, listen: expressListen } = expressApp as any
|
|
54
|
+
const { use: feathersUse, teardown: feathersTeardown } = feathersApp
|
|
55
|
+
|
|
56
|
+
Object.assign(app, {
|
|
57
|
+
use(location: string & keyof S, ...rest: any[]) {
|
|
58
|
+
let service: any
|
|
59
|
+
let options = {}
|
|
39
60
|
|
|
40
|
-
const middleware = rest.reduce(
|
|
61
|
+
const middleware = rest.reduce(
|
|
62
|
+
function (middleware, arg) {
|
|
41
63
|
if (typeof arg === 'function' || Array.isArray(arg)) {
|
|
42
|
-
middleware[service ? 'after' : 'before'].push(arg)
|
|
64
|
+
middleware[service ? 'after' : 'before'].push(arg)
|
|
43
65
|
} else if (!service) {
|
|
44
|
-
service = arg
|
|
45
|
-
} else if (arg.methods || arg.events) {
|
|
46
|
-
options = arg
|
|
66
|
+
service = arg
|
|
67
|
+
} else if (arg.methods || arg.events || arg.express || arg.koa) {
|
|
68
|
+
options = arg
|
|
47
69
|
} else {
|
|
48
|
-
throw new Error('Invalid options passed to app.use')
|
|
70
|
+
throw new Error('Invalid options passed to app.use')
|
|
49
71
|
}
|
|
50
|
-
return middleware
|
|
51
|
-
},
|
|
72
|
+
return middleware
|
|
73
|
+
},
|
|
74
|
+
{
|
|
52
75
|
before: [],
|
|
53
76
|
after: []
|
|
54
|
-
}
|
|
77
|
+
}
|
|
78
|
+
)
|
|
55
79
|
|
|
56
|
-
const hasMethod = (methods: string[]) =>
|
|
57
|
-
(service && typeof service[name] === 'function')
|
|
58
|
-
);
|
|
80
|
+
const hasMethod = (methods: string[]) =>
|
|
81
|
+
methods.some((name) => service && typeof service[name] === 'function')
|
|
59
82
|
|
|
60
83
|
// Check for service (any object with at least one service method)
|
|
61
84
|
if (hasMethod(['handle', 'set']) || !hasMethod(defaultServiceMethods)) {
|
|
62
|
-
debug('Passing app.use call to Express app')
|
|
63
|
-
return
|
|
85
|
+
debug('Passing app.use call to Express app')
|
|
86
|
+
return expressUse.call(this, location, ...rest)
|
|
64
87
|
}
|
|
65
88
|
|
|
66
|
-
debug('Registering service with middleware', middleware)
|
|
89
|
+
debug('Registering service with middleware', middleware)
|
|
67
90
|
// Since this is a service, call Feathers `.use`
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
})
|
|
91
|
+
feathersUse.call(this, location, service, {
|
|
92
|
+
express: middleware,
|
|
93
|
+
...options
|
|
94
|
+
})
|
|
72
95
|
|
|
73
|
-
return this
|
|
96
|
+
return this
|
|
74
97
|
},
|
|
75
98
|
|
|
76
|
-
async listen
|
|
77
|
-
const server =
|
|
99
|
+
async listen(...args: any[]) {
|
|
100
|
+
const server = expressListen.call(this, ...args)
|
|
78
101
|
|
|
79
|
-
|
|
80
|
-
|
|
102
|
+
this.server = server
|
|
103
|
+
await this.setup(server)
|
|
104
|
+
debug('Feathers application listening')
|
|
81
105
|
|
|
82
|
-
return server
|
|
106
|
+
return server
|
|
83
107
|
}
|
|
84
|
-
}
|
|
108
|
+
} as Application<S, C>)
|
|
85
109
|
|
|
86
|
-
const
|
|
110
|
+
const appDescriptors = {
|
|
111
|
+
...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(app)),
|
|
112
|
+
...Object.getOwnPropertyDescriptors(app)
|
|
113
|
+
}
|
|
114
|
+
const newDescriptors = {
|
|
87
115
|
...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(feathersApp)),
|
|
88
116
|
...Object.getOwnPropertyDescriptors(feathersApp)
|
|
89
|
-
}
|
|
117
|
+
}
|
|
90
118
|
|
|
91
119
|
// Copy all non-existing properties (including non-enumerables)
|
|
92
120
|
// that don't already exist on the Express app
|
|
93
|
-
Object.keys(
|
|
94
|
-
const
|
|
95
|
-
const
|
|
121
|
+
Object.keys(newDescriptors).forEach((prop) => {
|
|
122
|
+
const appProp = appDescriptors[prop]
|
|
123
|
+
const newProp = newDescriptors[prop]
|
|
96
124
|
|
|
97
|
-
if (
|
|
98
|
-
Object.defineProperty(expressApp, prop,
|
|
125
|
+
if (appProp === undefined && newProp !== undefined) {
|
|
126
|
+
Object.defineProperty(expressApp, prop, newProp)
|
|
99
127
|
}
|
|
100
|
-
})
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
// Assign teardown and setup which will also make sure that hooks are initialized
|
|
131
|
+
app.setup = feathersApp.setup as any
|
|
132
|
+
app.teardown = async function teardown(server?: any) {
|
|
133
|
+
return feathersTeardown.call(this, server).then(
|
|
134
|
+
() =>
|
|
135
|
+
new Promise((resolve, reject) => {
|
|
136
|
+
if (this.server) {
|
|
137
|
+
this.server.close((e) => (e ? reject(e) : resolve(this)))
|
|
138
|
+
} else {
|
|
139
|
+
resolve(this)
|
|
140
|
+
}
|
|
141
|
+
})
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
app.configure(routing() as any)
|
|
146
|
+
app.use((req, _res, next) => {
|
|
147
|
+
req.feathers = { ...req.feathers, provider: 'rest' }
|
|
148
|
+
return next()
|
|
149
|
+
})
|
|
101
150
|
|
|
102
|
-
return
|
|
151
|
+
return app
|
|
103
152
|
}
|
|
104
153
|
|
|
105
154
|
if (typeof module !== 'undefined') {
|
|
106
|
-
module.exports = Object.assign(feathersExpress, module.exports)
|
|
155
|
+
module.exports = Object.assign(feathersExpress, module.exports)
|
|
107
156
|
}
|
package/src/rest.ts
CHANGED
|
@@ -1,120 +1,113 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { createDebug } from '@feathersjs/commons'
|
|
4
|
-
import { http } from '@feathersjs/transport-commons'
|
|
5
|
-
import { createContext, defaultServiceMethods, getServiceOptions } from '@feathersjs/feathers'
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
const debug = createDebug('@feathersjs/express/rest')
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
req.
|
|
16
|
-
...req.feathers,
|
|
17
|
-
provider: 'rest',
|
|
18
|
-
headers: req.headers
|
|
19
|
-
};
|
|
20
|
-
next();
|
|
1
|
+
import { Request, Response, RequestHandler, Router } from 'express'
|
|
2
|
+
import { MethodNotAllowed } from '@feathersjs/errors'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import { http } from '@feathersjs/transport-commons'
|
|
5
|
+
import { createContext, defaultServiceMethods, getServiceOptions } from '@feathersjs/feathers'
|
|
6
|
+
|
|
7
|
+
import { AuthenticationSettings, parseAuthentication } from './authentication'
|
|
8
|
+
import { Application } from './declarations'
|
|
9
|
+
|
|
10
|
+
const debug = createDebug('@feathersjs/express/rest')
|
|
11
|
+
|
|
12
|
+
const toHandler = (
|
|
13
|
+
func: (req: Request, res: Response, next: () => void) => Promise<void>
|
|
14
|
+
): RequestHandler => {
|
|
15
|
+
return (req, res, next) => func(req, res, next).catch((error) => next(error))
|
|
21
16
|
}
|
|
22
17
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const serviceMiddleware = (): RequestHandler => {
|
|
19
|
+
return toHandler(async (req, res, next) => {
|
|
20
|
+
const { query, headers, path, body: data, method: httpMethod } = req
|
|
21
|
+
const methodOverride = req.headers[http.METHOD_HEADER] as string | undefined
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
24
|
+
const { service, params: { __id: id = null, ...route } = {} } = req.lookup!
|
|
25
|
+
const method = http.getServiceMethod(httpMethod, id, methodOverride)
|
|
26
|
+
const { methods } = getServiceOptions(service)
|
|
27
|
+
|
|
28
|
+
debug(`Found service for path ${path}, attempting to run '${method}' service method`)
|
|
29
|
+
|
|
30
|
+
if (!methods.includes(method) || defaultServiceMethods.includes(methodOverride)) {
|
|
31
|
+
const error = new MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`)
|
|
32
|
+
res.statusCode = error.code
|
|
33
|
+
throw error
|
|
31
34
|
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
35
|
|
|
36
|
+
const createArguments = http.argumentsFor[method as 'get'] || http.argumentsFor.default
|
|
37
|
+
const params = { query, headers, route, ...req.feathers }
|
|
38
|
+
const args = createArguments({ id, data, params })
|
|
39
|
+
const contextBase = createContext(service, method, { http: {} })
|
|
40
|
+
res.hook = contextBase
|
|
35
41
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
debug(`Running service middleware for '${req.url}'`);
|
|
42
|
+
const context = await (service as any)[method](...args, contextBase)
|
|
43
|
+
res.hook = context
|
|
39
44
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const context = await callback(req, res, { id, data, params });
|
|
45
|
-
const result = http.getData(context);
|
|
45
|
+
const response = http.getResponse(context)
|
|
46
|
+
res.statusCode = response.status
|
|
47
|
+
res.set(response.headers)
|
|
48
|
+
res.data = response.body
|
|
46
49
|
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
return next()
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const servicesMiddleware = (): RequestHandler => {
|
|
55
|
+
return toHandler(async (req, res, next) => {
|
|
56
|
+
const app = req.app as any as Application
|
|
57
|
+
const lookup = app.lookup(req.path)
|
|
49
58
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
next(error);
|
|
59
|
+
if (!lookup) {
|
|
60
|
+
return next()
|
|
53
61
|
}
|
|
54
|
-
}
|
|
55
62
|
|
|
56
|
-
|
|
57
|
-
service: any, methodName: string, getArgs: (opts: http.ServiceParams) => any[], headerOverride?: string
|
|
58
|
-
) => serviceMiddleware(async (req, res, options) => {
|
|
59
|
-
const methodOverride = typeof headerOverride === 'string' && (req.headers[headerOverride] as string);
|
|
60
|
-
const method = methodOverride ? methodOverride : methodName
|
|
61
|
-
const { methods } = getServiceOptions(service);
|
|
63
|
+
req.lookup = lookup
|
|
62
64
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
const options = getServiceOptions(lookup.service)
|
|
66
|
+
const middleware = options.express.composed
|
|
65
67
|
|
|
66
|
-
|
|
68
|
+
return middleware(req, res, next)
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export const formatter: RequestHandler = (_req, res, next) => {
|
|
73
|
+
if (res.data === undefined) {
|
|
74
|
+
return next()
|
|
67
75
|
}
|
|
68
76
|
|
|
69
|
-
|
|
70
|
-
|
|
77
|
+
res.format({
|
|
78
|
+
'application/json'() {
|
|
79
|
+
res.json(res.data)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type RestOptions = {
|
|
85
|
+
formatter?: RequestHandler
|
|
86
|
+
authentication?: AuthenticationSettings
|
|
87
|
+
}
|
|
71
88
|
|
|
72
|
-
|
|
89
|
+
export const rest = (options?: RestOptions | RequestHandler) => {
|
|
90
|
+
options = typeof options === 'function' ? { formatter: options } : options || {}
|
|
73
91
|
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
const formatterMiddleware = options.formatter || formatter
|
|
93
|
+
const authenticationOptions = options.authentication
|
|
76
94
|
|
|
77
|
-
|
|
78
|
-
return function (this: any, app: any) {
|
|
95
|
+
return (app: Application) => {
|
|
79
96
|
if (typeof app.route !== 'function') {
|
|
80
|
-
throw new Error('@feathersjs/express/rest needs an Express compatible app.')
|
|
97
|
+
throw new Error('@feathersjs/express/rest needs an Express compatible app.')
|
|
81
98
|
}
|
|
82
99
|
|
|
83
|
-
app.use(
|
|
84
|
-
app.use(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const find = serviceMethodHandler(service, 'find', http.argumentsFor.find);
|
|
97
|
-
const get = serviceMethodHandler(service, 'get', http.argumentsFor.get);
|
|
98
|
-
const create = serviceMethodHandler(service, 'create', http.argumentsFor.create, http.METHOD_HEADER);
|
|
99
|
-
const update = serviceMethodHandler(service, 'update', http.argumentsFor.update);
|
|
100
|
-
const patch = serviceMethodHandler(service, 'patch', http.argumentsFor.patch);
|
|
101
|
-
const remove = serviceMethodHandler(service, 'remove', http.argumentsFor.remove);
|
|
102
|
-
|
|
103
|
-
debug(`Adding REST provider for service \`${path}\` at base route \`${baseUri}\``);
|
|
104
|
-
|
|
105
|
-
const idRoute = '/:__feathersId';
|
|
106
|
-
const serviceRouter = Router({ mergeParams: true })
|
|
107
|
-
.get('/', find)
|
|
108
|
-
.post('/', create)
|
|
109
|
-
.get(idRoute, get)
|
|
110
|
-
.put('/', update)
|
|
111
|
-
.put(idRoute, update)
|
|
112
|
-
.patch('/', patch)
|
|
113
|
-
.patch(idRoute, patch)
|
|
114
|
-
.delete('/', remove)
|
|
115
|
-
.delete(idRoute, remove);
|
|
116
|
-
|
|
117
|
-
app.use(baseUri, ...before, serviceRouter, ...after);
|
|
118
|
-
});
|
|
119
|
-
};
|
|
100
|
+
app.use(parseAuthentication(authenticationOptions))
|
|
101
|
+
app.use(servicesMiddleware())
|
|
102
|
+
|
|
103
|
+
app.mixins.push((_service, _path, options) => {
|
|
104
|
+
const { express: { before = [], after = [] } = {} } = options
|
|
105
|
+
|
|
106
|
+
const middlewares = [].concat(before, serviceMiddleware(), after, formatterMiddleware)
|
|
107
|
+
const middleware = Router().use(middlewares)
|
|
108
|
+
|
|
109
|
+
options.express ||= {}
|
|
110
|
+
options.express.composed = middleware
|
|
111
|
+
})
|
|
112
|
+
}
|
|
120
113
|
}
|