@feathersjs/express 5.0.0-pre.1 → 5.0.0-pre.15
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 +167 -0
- package/LICENSE +1 -1
- package/README.md +1 -1
- package/lib/authentication.d.ts +1 -1
- package/lib/authentication.js +5 -8
- package/lib/authentication.js.map +1 -1
- package/lib/declarations.d.ts +39 -0
- package/lib/declarations.js +3 -0
- package/lib/declarations.js.map +1 -0
- package/lib/index.d.ts +6 -18
- package/lib/index.js +34 -26
- package/lib/index.js.map +1 -1
- package/lib/rest.d.ts +9 -0
- package/lib/rest.js +100 -0
- package/lib/rest.js.map +1 -0
- package/package.json +21 -20
- package/src/authentication.ts +3 -3
- package/src/declarations.ts +60 -0
- package/src/index.ts +37 -50
- package/src/rest.ts +119 -0
- package/lib/rest/getHandler.d.ts +0 -17
- package/lib/rest/getHandler.js +0 -105
- package/lib/rest/getHandler.js.map +0 -1
- package/lib/rest/index.d.ts +0 -20
- package/lib/rest/index.js +0 -129
- package/lib/rest/index.js.map +0 -1
- package/src/rest/getHandler.ts +0 -115
- package/src/rest/index.ts +0 -148
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/express",
|
|
3
3
|
"description": "Feathers Express framework bindings and REST provider",
|
|
4
|
-
"version": "5.0.0-pre.
|
|
4
|
+
"version": "5.0.0-pre.15",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
7
|
"keywords": [
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"url": "https://github.com/feathersjs/feathers/issues"
|
|
28
28
|
},
|
|
29
29
|
"engines": {
|
|
30
|
-
"node": ">=
|
|
30
|
+
"node": ">= 12"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"CHANGELOG.md",
|
|
@@ -49,27 +49,28 @@
|
|
|
49
49
|
"access": "public"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@feathersjs/commons": "^5.0.0-pre.
|
|
53
|
-
"@feathersjs/errors": "^5.0.0-pre.
|
|
54
|
-
"@
|
|
55
|
-
"
|
|
52
|
+
"@feathersjs/commons": "^5.0.0-pre.15",
|
|
53
|
+
"@feathersjs/errors": "^5.0.0-pre.15",
|
|
54
|
+
"@feathersjs/feathers": "^5.0.0-pre.15",
|
|
55
|
+
"@feathersjs/transport-commons": "^5.0.0-pre.15",
|
|
56
|
+
"@types/express": "^4.17.13",
|
|
57
|
+
"@types/express-serve-static-core": "^4.17.24",
|
|
56
58
|
"express": "^4.17.1",
|
|
57
|
-
"lodash": "^4.17.
|
|
58
|
-
"uberproto": "^2.0.6"
|
|
59
|
+
"lodash": "^4.17.21"
|
|
59
60
|
},
|
|
60
61
|
"devDependencies": {
|
|
61
|
-
"@feathersjs/authentication": "^5.0.0-pre.
|
|
62
|
-
"@feathersjs/authentication-local": "^5.0.0-pre.
|
|
63
|
-
"@feathersjs/
|
|
64
|
-
"@
|
|
65
|
-
"@types/mocha": "^
|
|
66
|
-
"@types/node": "^
|
|
67
|
-
"axios": "^0.
|
|
68
|
-
"lodash": "^4.17.
|
|
69
|
-
"mocha": "^
|
|
62
|
+
"@feathersjs/authentication": "^5.0.0-pre.15",
|
|
63
|
+
"@feathersjs/authentication-local": "^5.0.0-pre.15",
|
|
64
|
+
"@feathersjs/tests": "^5.0.0-pre.15",
|
|
65
|
+
"@types/lodash": "^4.14.176",
|
|
66
|
+
"@types/mocha": "^9.0.0",
|
|
67
|
+
"@types/node": "^16.11.6",
|
|
68
|
+
"axios": "^0.24.0",
|
|
69
|
+
"lodash": "^4.17.21",
|
|
70
|
+
"mocha": "^9.1.3",
|
|
70
71
|
"shx": "^0.3.3",
|
|
71
|
-
"ts-node": "^
|
|
72
|
-
"typescript": "^4.
|
|
72
|
+
"ts-node": "^10.4.0",
|
|
73
|
+
"typescript": "^4.4.4"
|
|
73
74
|
},
|
|
74
|
-
"gitHead": "
|
|
75
|
+
"gitHead": "8008bf4f8529a2a40b6a2f976c1f43ae13675693"
|
|
75
76
|
}
|
package/src/authentication.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createDebug } from '@feathersjs/commons';
|
|
2
2
|
import { merge, flatten } from 'lodash';
|
|
3
|
-
import { NextFunction, RequestHandler } from 'express';
|
|
3
|
+
import { NextFunction, RequestHandler, Request, Response } from 'express';
|
|
4
4
|
|
|
5
|
-
const debug =
|
|
5
|
+
const debug = createDebug('@feathersjs/express/authentication');
|
|
6
6
|
|
|
7
7
|
type StrategyOptions = {
|
|
8
8
|
service?: string;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import http from 'http';
|
|
2
|
+
import express, { Express } from 'express';
|
|
3
|
+
import {
|
|
4
|
+
Application as FeathersApplication, Params as FeathersParams,
|
|
5
|
+
HookContext, ServiceMethods, ServiceInterface
|
|
6
|
+
} from '@feathersjs/feathers';
|
|
7
|
+
|
|
8
|
+
interface ExpressUseHandler<T, Services> {
|
|
9
|
+
<L extends keyof Services & string> (
|
|
10
|
+
path: L,
|
|
11
|
+
...middlewareOrService: (
|
|
12
|
+
Express|express.RequestHandler|
|
|
13
|
+
(keyof any extends keyof Services ? ServiceInterface : Services[L])
|
|
14
|
+
)[]
|
|
15
|
+
): T;
|
|
16
|
+
(path: string|RegExp, ...expressHandlers: express.RequestHandler[]): T;
|
|
17
|
+
(...expressHandlers: express.RequestHandler[]): T;
|
|
18
|
+
(handler: Express|express.ErrorRequestHandler): T;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface ExpressOverrides<Services> {
|
|
22
|
+
listen(port: number, hostname: string, backlog: number, callback?: () => void): Promise<http.Server>;
|
|
23
|
+
listen(port: number, hostname: string, callback?: () => void): Promise<http.Server>;
|
|
24
|
+
listen(port: number|string|any, callback?: () => void): Promise<http.Server>;
|
|
25
|
+
listen(callback?: () => void): Promise<http.Server>;
|
|
26
|
+
use: ExpressUseHandler<this, Services>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export type Application<Services = any, Settings = any> =
|
|
30
|
+
Omit<Express, 'listen'|'use'> &
|
|
31
|
+
FeathersApplication<Services, Settings> &
|
|
32
|
+
ExpressOverrides<Services>;
|
|
33
|
+
|
|
34
|
+
declare module '@feathersjs/feathers/lib/declarations' {
|
|
35
|
+
interface ServiceOptions {
|
|
36
|
+
middleware?: {
|
|
37
|
+
before: express.RequestHandler[],
|
|
38
|
+
after: express.RequestHandler[]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare module 'express-serve-static-core' {
|
|
44
|
+
interface Request {
|
|
45
|
+
feathers?: Partial<FeathersParams>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface Response {
|
|
49
|
+
data?: any;
|
|
50
|
+
hook?: HookContext;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface IRouterMatcher<T> {
|
|
54
|
+
// eslint-disable-next-line
|
|
55
|
+
<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any>(
|
|
56
|
+
path: PathParams,
|
|
57
|
+
...handlers: (RequestHandler<P, ResBody, ReqBody> | Partial<ServiceMethods> | Application)[]
|
|
58
|
+
): T;
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,48 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import Debug from 'debug';
|
|
1
|
+
import express, {
|
|
2
|
+
Express, static as _static, json, raw, text, urlencoded, query
|
|
3
|
+
} from 'express';
|
|
5
4
|
import {
|
|
6
|
-
Application as FeathersApplication,
|
|
7
|
-
HookContext, ServiceMethods, SetupMethod
|
|
5
|
+
Application as FeathersApplication, defaultServiceMethods
|
|
8
6
|
} from '@feathersjs/feathers';
|
|
7
|
+
import { createDebug } from '@feathersjs/commons';
|
|
9
8
|
|
|
9
|
+
import { Application } from './declarations';
|
|
10
10
|
import { errorHandler, notFound } from './handlers';
|
|
11
|
-
import { rest } from './rest';
|
|
12
11
|
import { parseAuthentication, authenticate } from './authentication';
|
|
13
12
|
|
|
14
13
|
export {
|
|
15
|
-
_static as static, json, raw, text,
|
|
16
|
-
errorHandler, notFound,
|
|
14
|
+
_static as serveStatic, _static as static, json, raw, text,
|
|
15
|
+
urlencoded, query, errorHandler, notFound, express as original,
|
|
17
16
|
authenticate, parseAuthentication
|
|
18
17
|
};
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
export * from './rest';
|
|
20
|
+
export * from './declarations';
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
interface Request {
|
|
24
|
-
feathers?: Partial<FeathersParams>;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
interface Response {
|
|
28
|
-
data?: any;
|
|
29
|
-
hook?: HookContext;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
type FeathersService = Partial<ServiceMethods<any> & SetupMethod>;
|
|
33
|
-
|
|
34
|
-
interface IRouterMatcher<T> {
|
|
35
|
-
// eslint-disable-next-line
|
|
36
|
-
<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any>(
|
|
37
|
-
path: PathParams,
|
|
38
|
-
...handlers: (RequestHandler<P, ResBody, ReqBody> | FeathersService | Application)[]
|
|
39
|
-
): T;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
22
|
+
const debug = createDebug('@feathersjs/express');
|
|
42
23
|
|
|
43
|
-
export
|
|
44
|
-
|
|
45
|
-
export default function feathersExpress<T = any> (feathersApp?: FeathersApplication, expressApp: Express = express()): Application<T> {
|
|
24
|
+
export default function feathersExpress<S = any, C = any> (feathersApp?: FeathersApplication<S, C>, expressApp: Express = express()): Application<S, C> {
|
|
46
25
|
if (!feathersApp) {
|
|
47
26
|
return expressApp as any;
|
|
48
27
|
}
|
|
@@ -51,20 +30,20 @@ export default function feathersExpress<T = any> (feathersApp?: FeathersApplicat
|
|
|
51
30
|
throw new Error('@feathersjs/express requires a valid Feathers application instance');
|
|
52
31
|
}
|
|
53
32
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// An Uberproto mixin that provides the extended functionality
|
|
33
|
+
const { use, listen } = expressApp as any;
|
|
34
|
+
// A mixin that provides the extended functionality
|
|
59
35
|
const mixin: any = {
|
|
60
|
-
use (location: string) {
|
|
36
|
+
use (location: string, ...rest: any[]) {
|
|
61
37
|
let service: any;
|
|
62
|
-
|
|
63
|
-
|
|
38
|
+
let options = {};
|
|
39
|
+
|
|
40
|
+
const middleware = rest.reduce(function (middleware, arg) {
|
|
64
41
|
if (typeof arg === 'function' || Array.isArray(arg)) {
|
|
65
42
|
middleware[service ? 'after' : 'before'].push(arg);
|
|
66
43
|
} else if (!service) {
|
|
67
44
|
service = arg;
|
|
45
|
+
} else if (arg.methods || arg.events) {
|
|
46
|
+
options = arg;
|
|
68
47
|
} else {
|
|
69
48
|
throw new Error('Invalid options passed to app.use');
|
|
70
49
|
}
|
|
@@ -79,32 +58,40 @@ export default function feathersExpress<T = any> (feathersApp?: FeathersApplicat
|
|
|
79
58
|
);
|
|
80
59
|
|
|
81
60
|
// Check for service (any object with at least one service method)
|
|
82
|
-
if (hasMethod(['handle', 'set']) || !hasMethod(
|
|
61
|
+
if (hasMethod(['handle', 'set']) || !hasMethod(defaultServiceMethods)) {
|
|
83
62
|
debug('Passing app.use call to Express app');
|
|
84
|
-
return
|
|
63
|
+
return use.call(this, location, ...rest);
|
|
85
64
|
}
|
|
86
65
|
|
|
87
66
|
debug('Registering service with middleware', middleware);
|
|
88
67
|
// Since this is a service, call Feathers `.use`
|
|
89
|
-
feathersApp.use.call(this, location, service, {
|
|
68
|
+
(feathersApp as FeathersApplication).use.call(this, location, service, {
|
|
69
|
+
...options,
|
|
70
|
+
middleware
|
|
71
|
+
});
|
|
90
72
|
|
|
91
73
|
return this;
|
|
92
74
|
},
|
|
93
75
|
|
|
94
|
-
listen () {
|
|
95
|
-
const server =
|
|
76
|
+
async listen (...args: any[]) {
|
|
77
|
+
const server = listen.call(this, ...args);
|
|
96
78
|
|
|
97
|
-
this.setup(server);
|
|
79
|
+
await this.setup(server);
|
|
98
80
|
debug('Feathers application listening');
|
|
99
81
|
|
|
100
82
|
return server;
|
|
101
83
|
}
|
|
102
84
|
};
|
|
103
85
|
|
|
86
|
+
const feathersDescriptors = {
|
|
87
|
+
...Object.getOwnPropertyDescriptors(Object.getPrototypeOf(feathersApp)),
|
|
88
|
+
...Object.getOwnPropertyDescriptors(feathersApp)
|
|
89
|
+
};
|
|
90
|
+
|
|
104
91
|
// Copy all non-existing properties (including non-enumerables)
|
|
105
92
|
// that don't already exist on the Express app
|
|
106
|
-
Object.
|
|
107
|
-
const feathersProp =
|
|
93
|
+
Object.keys(feathersDescriptors).forEach(prop => {
|
|
94
|
+
const feathersProp = feathersDescriptors[prop];
|
|
108
95
|
const expressProp = Object.getOwnPropertyDescriptor(expressApp, prop);
|
|
109
96
|
|
|
110
97
|
if (expressProp === undefined && feathersProp !== undefined) {
|
|
@@ -112,7 +99,7 @@ export default function feathersExpress<T = any> (feathersApp?: FeathersApplicat
|
|
|
112
99
|
}
|
|
113
100
|
});
|
|
114
101
|
|
|
115
|
-
return
|
|
102
|
+
return Object.assign(expressApp, mixin);
|
|
116
103
|
}
|
|
117
104
|
|
|
118
105
|
if (typeof module !== 'undefined') {
|
package/src/rest.ts
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { MethodNotAllowed } from '@feathersjs/errors';
|
|
2
|
+
import { createDebug } from '@feathersjs/commons';
|
|
3
|
+
import { http } from '@feathersjs/transport-commons';
|
|
4
|
+
import { HookContext, createContext, defaultServiceMethods, getServiceOptions } from '@feathersjs/feathers';
|
|
5
|
+
import { Request, Response, NextFunction, RequestHandler, Router } from 'express';
|
|
6
|
+
|
|
7
|
+
import { parseAuthentication } from './authentication';
|
|
8
|
+
|
|
9
|
+
const debug = createDebug('@feathersjs/express/rest');
|
|
10
|
+
|
|
11
|
+
export type ServiceCallback = (req: Request, res: Response, options: http.ServiceParams) => Promise<HookContext>;
|
|
12
|
+
|
|
13
|
+
export const feathersParams = (req: Request, _res: Response, next: NextFunction) => {
|
|
14
|
+
req.feathers = {
|
|
15
|
+
...req.feathers,
|
|
16
|
+
provider: 'rest',
|
|
17
|
+
headers: req.headers
|
|
18
|
+
};
|
|
19
|
+
next();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const formatter = (_req: Request, res: Response, next: NextFunction) => {
|
|
23
|
+
if (res.data === undefined) {
|
|
24
|
+
return next();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
res.format({
|
|
28
|
+
'application/json' () {
|
|
29
|
+
res.json(res.data);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
export const serviceMiddleware = (callback: ServiceCallback) =>
|
|
36
|
+
async (req: Request, res: Response, next: NextFunction) => {
|
|
37
|
+
debug(`Running service middleware for '${req.url}'`);
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const { query, body: data } = req;
|
|
41
|
+
const { __feathersId: id = null, ...route } = req.params;
|
|
42
|
+
const params = { query, route, ...req.feathers };
|
|
43
|
+
const context = await callback(req, res, { id, data, params });
|
|
44
|
+
const result = http.getData(context);
|
|
45
|
+
|
|
46
|
+
res.data = result;
|
|
47
|
+
res.status(http.getStatusCode(context, result));
|
|
48
|
+
|
|
49
|
+
next();
|
|
50
|
+
} catch (error: any) {
|
|
51
|
+
next(error);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const serviceMethodHandler = (
|
|
56
|
+
service: any, methodName: string, getArgs: (opts: http.ServiceParams) => any[], headerOverride?: string
|
|
57
|
+
) => serviceMiddleware(async (req, res, options) => {
|
|
58
|
+
const methodOverride = typeof headerOverride === 'string' && (req.headers[headerOverride] as string);
|
|
59
|
+
const method = methodOverride ? methodOverride : methodName
|
|
60
|
+
const { methods } = getServiceOptions(service);
|
|
61
|
+
|
|
62
|
+
if (!methods.includes(method) || defaultServiceMethods.includes(methodOverride)) {
|
|
63
|
+
res.status(http.statusCodes.methodNotAllowed);
|
|
64
|
+
|
|
65
|
+
throw new MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const args = getArgs(options);
|
|
69
|
+
const context = createContext(service, method, { http: {} });
|
|
70
|
+
|
|
71
|
+
res.hook = context;
|
|
72
|
+
|
|
73
|
+
return service[method](...args, context);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
export function rest (handler: RequestHandler = formatter) {
|
|
77
|
+
return function (this: any, app: any) {
|
|
78
|
+
if (typeof app.route !== 'function') {
|
|
79
|
+
throw new Error('@feathersjs/express/rest needs an Express compatible app.');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
app.use(feathersParams);
|
|
83
|
+
app.use(parseAuthentication());
|
|
84
|
+
|
|
85
|
+
// Register the REST provider
|
|
86
|
+
app.mixins.push(function (service: any, path: string, options: any) {
|
|
87
|
+
const { middleware: { before = [] } } = options;
|
|
88
|
+
let { middleware: { after = [] } } = options;
|
|
89
|
+
|
|
90
|
+
if (typeof handler === 'function') {
|
|
91
|
+
after = after.concat(handler);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const baseUri = `/${path}`;
|
|
95
|
+
const find = serviceMethodHandler(service, 'find', http.argumentsFor.find);
|
|
96
|
+
const get = serviceMethodHandler(service, 'get', http.argumentsFor.get);
|
|
97
|
+
const create = serviceMethodHandler(service, 'create', http.argumentsFor.create, http.METHOD_HEADER);
|
|
98
|
+
const update = serviceMethodHandler(service, 'update', http.argumentsFor.update);
|
|
99
|
+
const patch = serviceMethodHandler(service, 'patch', http.argumentsFor.patch);
|
|
100
|
+
const remove = serviceMethodHandler(service, 'remove', http.argumentsFor.remove);
|
|
101
|
+
|
|
102
|
+
debug(`Adding REST provider for service \`${path}\` at base route \`${baseUri}\``);
|
|
103
|
+
|
|
104
|
+
const idRoute = '/:__feathersId';
|
|
105
|
+
const serviceRouter = Router({ mergeParams: true })
|
|
106
|
+
.get('/', find)
|
|
107
|
+
.post('/', create)
|
|
108
|
+
.get(idRoute, get)
|
|
109
|
+
.put('/', update)
|
|
110
|
+
.put(idRoute, update)
|
|
111
|
+
.patch('/', patch)
|
|
112
|
+
.patch(idRoute, patch)
|
|
113
|
+
.delete('/', remove)
|
|
114
|
+
.delete(idRoute, remove);
|
|
115
|
+
|
|
116
|
+
app.use(baseUri, ...before, serviceRouter, ...after);
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
}
|
package/lib/rest/getHandler.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Request, Response, NextFunction } from 'express';
|
|
2
|
-
export declare const statusCodes: {
|
|
3
|
-
created: number;
|
|
4
|
-
noContent: number;
|
|
5
|
-
methodNotAllowed: number;
|
|
6
|
-
};
|
|
7
|
-
export declare const methodMap: {
|
|
8
|
-
find: string;
|
|
9
|
-
get: string;
|
|
10
|
-
create: string;
|
|
11
|
-
update: string;
|
|
12
|
-
patch: string;
|
|
13
|
-
remove: string;
|
|
14
|
-
};
|
|
15
|
-
export declare function getAllowedMethods(service: any, routes: any): any;
|
|
16
|
-
export declare function makeArgsGetter(argsOrder: any): (req: Request, params: any) => any;
|
|
17
|
-
export declare function getHandler(method: string): (service: any, routes: any) => (req: Request, res: Response, next: NextFunction) => void;
|
package/lib/rest/getHandler.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.getHandler = exports.makeArgsGetter = exports.getAllowedMethods = exports.methodMap = exports.statusCodes = void 0;
|
|
7
|
-
const debug_1 = __importDefault(require("debug"));
|
|
8
|
-
const errors_1 = require("@feathersjs/errors");
|
|
9
|
-
const commons_1 = require("@feathersjs/commons");
|
|
10
|
-
const { omit } = commons_1._;
|
|
11
|
-
const debug = debug_1.default('@feathersjs/express/rest');
|
|
12
|
-
exports.statusCodes = {
|
|
13
|
-
created: 201,
|
|
14
|
-
noContent: 204,
|
|
15
|
-
methodNotAllowed: 405
|
|
16
|
-
};
|
|
17
|
-
exports.methodMap = {
|
|
18
|
-
find: 'GET',
|
|
19
|
-
get: 'GET',
|
|
20
|
-
create: 'POST',
|
|
21
|
-
update: 'PUT',
|
|
22
|
-
patch: 'PATCH',
|
|
23
|
-
remove: 'DELETE'
|
|
24
|
-
};
|
|
25
|
-
function getAllowedMethods(service, routes) {
|
|
26
|
-
if (routes) {
|
|
27
|
-
return routes
|
|
28
|
-
.filter(({ method }) => typeof service[method] === 'function')
|
|
29
|
-
.map((methodRoute) => methodRoute.verb.toUpperCase())
|
|
30
|
-
.filter((value, index, list) => list.indexOf(value) === index);
|
|
31
|
-
}
|
|
32
|
-
return Object.keys(exports.methodMap)
|
|
33
|
-
.filter((method) => typeof service[method] === 'function')
|
|
34
|
-
.map((method) => exports.methodMap[method])
|
|
35
|
-
// Filter out duplicates
|
|
36
|
-
.filter((value, index, list) => list.indexOf(value) === index);
|
|
37
|
-
}
|
|
38
|
-
exports.getAllowedMethods = getAllowedMethods;
|
|
39
|
-
function makeArgsGetter(argsOrder) {
|
|
40
|
-
return (req, params) => argsOrder.map((argName) => {
|
|
41
|
-
switch (argName) {
|
|
42
|
-
case 'id':
|
|
43
|
-
return req.params.__feathersId || null;
|
|
44
|
-
case 'data':
|
|
45
|
-
return req.body;
|
|
46
|
-
case 'params':
|
|
47
|
-
return params;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
exports.makeArgsGetter = makeArgsGetter;
|
|
52
|
-
// A function that returns the middleware for a given method and service
|
|
53
|
-
// `getArgs` is a function that should return additional leading service arguments
|
|
54
|
-
function getHandler(method) {
|
|
55
|
-
return (service, routes) => {
|
|
56
|
-
const getArgs = makeArgsGetter(service.methods[method]);
|
|
57
|
-
const allowedMethods = getAllowedMethods(service, routes);
|
|
58
|
-
return (req, res, next) => {
|
|
59
|
-
const { query } = req;
|
|
60
|
-
const route = omit(req.params, '__feathersId');
|
|
61
|
-
res.setHeader('Allow', allowedMethods.join(','));
|
|
62
|
-
// Check if the method exists on the service at all. Send 405 (Method not allowed) if not
|
|
63
|
-
if (typeof service[method] !== 'function') {
|
|
64
|
-
debug(`Method '${method}' not allowed on '${req.url}'`);
|
|
65
|
-
res.status(exports.statusCodes.methodNotAllowed);
|
|
66
|
-
return next(new errors_1.MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`));
|
|
67
|
-
}
|
|
68
|
-
// Grab the service parameters. Use req.feathers
|
|
69
|
-
// and set the query to req.query merged with req.params
|
|
70
|
-
const params = Object.assign({
|
|
71
|
-
query, route
|
|
72
|
-
}, req.feathers);
|
|
73
|
-
Object.defineProperty(params, '__returnHook', {
|
|
74
|
-
value: true
|
|
75
|
-
});
|
|
76
|
-
const args = getArgs(req, params);
|
|
77
|
-
debug(`REST handler calling \`${method}\` from \`${req.url}\``);
|
|
78
|
-
service[method](...args, true)
|
|
79
|
-
.then((hook) => {
|
|
80
|
-
const data = hook.dispatch !== undefined ? hook.dispatch : hook.result;
|
|
81
|
-
res.data = data;
|
|
82
|
-
res.hook = hook;
|
|
83
|
-
if (hook.statusCode) {
|
|
84
|
-
res.status(hook.statusCode);
|
|
85
|
-
}
|
|
86
|
-
else if (!data) {
|
|
87
|
-
debug(`No content returned for '${req.url}'`);
|
|
88
|
-
res.status(exports.statusCodes.noContent);
|
|
89
|
-
}
|
|
90
|
-
else if (method === 'create') {
|
|
91
|
-
res.status(exports.statusCodes.created);
|
|
92
|
-
}
|
|
93
|
-
return next();
|
|
94
|
-
})
|
|
95
|
-
.catch((hook) => {
|
|
96
|
-
const { error } = hook;
|
|
97
|
-
debug(`Error in handler: \`${error.message}\``);
|
|
98
|
-
res.hook = hook;
|
|
99
|
-
return next(hook.error);
|
|
100
|
-
});
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
exports.getHandler = getHandler;
|
|
105
|
-
//# sourceMappingURL=getHandler.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getHandler.js","sourceRoot":"","sources":["../../src/rest/getHandler.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA2B;AAE3B,+CAAsD;AACtD,iDAAyC;AAGzC,MAAM,EAAE,IAAI,EAAE,GAAG,WAAC,CAAC;AACnB,MAAM,KAAK,GAAG,eAAK,CAAC,0BAA0B,CAAC,CAAC;AAEnC,QAAA,WAAW,GAAG;IACzB,OAAO,EAAE,GAAG;IACZ,SAAS,EAAE,GAAG;IACd,gBAAgB,EAAE,GAAG;CACtB,CAAC;AACW,QAAA,SAAS,GAAG;IACvB,IAAI,EAAE,KAAK;IACX,GAAG,EAAE,KAAK;IACV,MAAM,EAAE,MAAM;IACd,MAAM,EAAE,KAAK;IACb,KAAK,EAAE,OAAO;IACd,MAAM,EAAE,QAAQ;CACjB,CAAC;AAEF,SAAgB,iBAAiB,CAAE,OAAY,EAAE,MAAW;IAC1D,IAAI,MAAM,EAAE;QACV,OAAO,MAAM;aACV,MAAM,CAAC,CAAC,EAAE,MAAM,EAAO,EAAE,EAAE,CAAC,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,UAAU,CAAC;aAClE,GAAG,CAAC,CAAC,WAAgB,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC;aACzD,MAAM,CAAC,CAAC,KAAU,EAAE,KAAa,EAAE,IAAS,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC,CAAC;KACpF;IAED,OAAO,MAAM,CAAC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IAAI,EAAE;oBAChB,KAAK,CAAC,4BAA4B,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC;oBAC9C,GAAG,CAAC,MAAM,CAAC,mBAAW,CAAC,SAAS,CAAC,CAAC;iBACnC;qBAAM,IAAI,MAAM,KAAK,QAAQ,EAAE;oBAC9B,GAAG,CAAC,MAAM,CAAC,mBAAW,CAAC,OAAO,CAAC,CAAC;iBACjC;gBAED,OAAO,IAAI,EAAE,CAAC;YAChB,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,IAAiB,EAAE,EAAE;gBAC3B,MAAM,EAAE,KAAK,EAAE,GAAG,IAAI,CAAC;gBAEvB,KAAK,CAAC,uBAAuB,KAAK,CAAC,OAAO,IAAI,CAAC,CAAC;gBAChD,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC;gBAEhB,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;YAC1B,CAAC,CAAC,CAAC;QACP,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AA7DD,gCA6DC"}
|
package/lib/rest/index.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { Request, Response, NextFunction, RequestHandler } from 'express';
|
|
2
|
-
export declare function httpMethod(verb: any, uris?: any): (method: any) => any;
|
|
3
|
-
export declare function getDefaultUri(path: string, methods: any, method: any): string;
|
|
4
|
-
export declare function parseRoute(path: any, methods: any, method: any, route: any): {
|
|
5
|
-
method: any;
|
|
6
|
-
verb: any;
|
|
7
|
-
uri: string;
|
|
8
|
-
};
|
|
9
|
-
export declare function getServiceRoutes(service: any, path: any, defaultRoutes: any): any;
|
|
10
|
-
export declare function getDefaultRoutes(uri: string): {
|
|
11
|
-
method: string;
|
|
12
|
-
verb: string;
|
|
13
|
-
uri: string;
|
|
14
|
-
}[];
|
|
15
|
-
export declare function formatter(_req: Request, res: Response, next: NextFunction): void;
|
|
16
|
-
export declare function rest(handler?: RequestHandler): (this: any) => void;
|
|
17
|
-
export declare namespace rest {
|
|
18
|
-
var formatter: typeof import(".").formatter;
|
|
19
|
-
var httpMethod: typeof import(".").httpMethod;
|
|
20
|
-
}
|