@feathersjs/express 5.0.0-pre.6 → 5.0.0
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 +197 -219
- 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/lib/rest.js
CHANGED
|
@@ -1,38 +1,57 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
12
|
-
var t = {};
|
|
13
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
14
|
-
t[p] = s[p];
|
|
15
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
16
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
17
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
18
|
-
t[p[i]] = s[p[i]];
|
|
19
|
-
}
|
|
20
|
-
return t;
|
|
21
|
-
};
|
|
22
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.rest = exports.
|
|
3
|
+
exports.rest = exports.formatter = void 0;
|
|
4
|
+
const express_1 = require("express");
|
|
24
5
|
const errors_1 = require("@feathersjs/errors");
|
|
25
6
|
const commons_1 = require("@feathersjs/commons");
|
|
26
7
|
const transport_commons_1 = require("@feathersjs/transport-commons");
|
|
27
8
|
const feathers_1 = require("@feathersjs/feathers");
|
|
28
|
-
const express_1 = require("express");
|
|
29
9
|
const authentication_1 = require("./authentication");
|
|
30
|
-
const debug = commons_1.createDebug('@feathersjs/express/rest');
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
10
|
+
const debug = (0, commons_1.createDebug)('@feathersjs/express/rest');
|
|
11
|
+
const toHandler = (func) => {
|
|
12
|
+
return (req, res, next) => func(req, res, next).catch((error) => next(error));
|
|
13
|
+
};
|
|
14
|
+
const serviceMiddleware = () => {
|
|
15
|
+
return toHandler(async (req, res, next) => {
|
|
16
|
+
const { query, headers, path, body: data, method: httpMethod } = req;
|
|
17
|
+
const methodOverride = req.headers[transport_commons_1.http.METHOD_HEADER];
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
19
|
+
const { service, params: { __id: id = null, ...route } = {} } = req.lookup;
|
|
20
|
+
const method = transport_commons_1.http.getServiceMethod(httpMethod, id, methodOverride);
|
|
21
|
+
const { methods } = (0, feathers_1.getServiceOptions)(service);
|
|
22
|
+
debug(`Found service for path ${path}, attempting to run '${method}' service method`);
|
|
23
|
+
if (!methods.includes(method) || feathers_1.defaultServiceMethods.includes(methodOverride)) {
|
|
24
|
+
const error = new errors_1.MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`);
|
|
25
|
+
res.statusCode = error.code;
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
const createArguments = transport_commons_1.http.argumentsFor[method] || transport_commons_1.http.argumentsFor.default;
|
|
29
|
+
const params = { query, headers, route, ...req.feathers };
|
|
30
|
+
const args = createArguments({ id, data, params });
|
|
31
|
+
const contextBase = (0, feathers_1.createContext)(service, method, { http: {} });
|
|
32
|
+
res.hook = contextBase;
|
|
33
|
+
const context = await service[method](...args, contextBase);
|
|
34
|
+
res.hook = context;
|
|
35
|
+
const response = transport_commons_1.http.getResponse(context);
|
|
36
|
+
res.statusCode = response.status;
|
|
37
|
+
res.set(response.headers);
|
|
38
|
+
res.data = response.body;
|
|
39
|
+
return next();
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
const servicesMiddleware = () => {
|
|
43
|
+
return toHandler(async (req, res, next) => {
|
|
44
|
+
const app = req.app;
|
|
45
|
+
const lookup = app.lookup(req.path);
|
|
46
|
+
if (!lookup) {
|
|
47
|
+
return next();
|
|
48
|
+
}
|
|
49
|
+
req.lookup = lookup;
|
|
50
|
+
const options = (0, feathers_1.getServiceOptions)(lookup.service);
|
|
51
|
+
const middleware = options.express.composed;
|
|
52
|
+
return middleware(req, res, next);
|
|
53
|
+
});
|
|
34
54
|
};
|
|
35
|
-
exports.feathersParams = feathersParams;
|
|
36
55
|
const formatter = (_req, res, next) => {
|
|
37
56
|
if (res.data === undefined) {
|
|
38
57
|
return next();
|
|
@@ -44,73 +63,24 @@ const formatter = (_req, res, next) => {
|
|
|
44
63
|
});
|
|
45
64
|
};
|
|
46
65
|
exports.formatter = formatter;
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const params = Object.assign({ query, route }, req.feathers);
|
|
53
|
-
const context = yield callback(req, res, { id, data, params });
|
|
54
|
-
const result = transport_commons_1.http.getData(context);
|
|
55
|
-
res.data = result;
|
|
56
|
-
res.status(transport_commons_1.http.getStatusCode(context, result));
|
|
57
|
-
next();
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
next(error);
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
exports.serviceMiddleware = serviceMiddleware;
|
|
64
|
-
const serviceMethodHandler = (service, methodName, getArgs, headerOverride) => exports.serviceMiddleware((req, res, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
65
|
-
const methodOverride = typeof headerOverride === 'string' && req.headers[headerOverride];
|
|
66
|
-
const method = methodOverride ? methodOverride : methodName;
|
|
67
|
-
const { methods } = feathers_1.getServiceOptions(service);
|
|
68
|
-
if (!methods.includes(method) || feathers_1.defaultServiceMethods.includes(methodOverride)) {
|
|
69
|
-
res.status(transport_commons_1.http.statusCodes.methodNotAllowed);
|
|
70
|
-
throw new errors_1.MethodNotAllowed(`Method \`${method}\` is not supported by this endpoint.`);
|
|
71
|
-
}
|
|
72
|
-
const args = getArgs(options);
|
|
73
|
-
const context = feathers_1.createContext(service, method);
|
|
74
|
-
res.hook = context;
|
|
75
|
-
return service[method](...args, context);
|
|
76
|
-
}));
|
|
77
|
-
exports.serviceMethodHandler = serviceMethodHandler;
|
|
78
|
-
function rest(handler = exports.formatter) {
|
|
79
|
-
return function (app) {
|
|
66
|
+
const rest = (options) => {
|
|
67
|
+
options = typeof options === 'function' ? { formatter: options } : options || {};
|
|
68
|
+
const formatterMiddleware = options.formatter || exports.formatter;
|
|
69
|
+
const authenticationOptions = options.authentication;
|
|
70
|
+
return (app) => {
|
|
80
71
|
if (typeof app.route !== 'function') {
|
|
81
72
|
throw new Error('@feathersjs/express/rest needs an Express compatible app.');
|
|
82
73
|
}
|
|
83
|
-
app.use(
|
|
84
|
-
app.use(
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
const baseUri = `/${path}`;
|
|
93
|
-
const find = exports.serviceMethodHandler(service, 'find', transport_commons_1.http.argumentsFor.find);
|
|
94
|
-
const get = exports.serviceMethodHandler(service, 'get', transport_commons_1.http.argumentsFor.get);
|
|
95
|
-
const create = exports.serviceMethodHandler(service, 'create', transport_commons_1.http.argumentsFor.create, transport_commons_1.http.METHOD_HEADER);
|
|
96
|
-
const update = exports.serviceMethodHandler(service, 'update', transport_commons_1.http.argumentsFor.update);
|
|
97
|
-
const patch = exports.serviceMethodHandler(service, 'patch', transport_commons_1.http.argumentsFor.patch);
|
|
98
|
-
const remove = exports.serviceMethodHandler(service, 'remove', transport_commons_1.http.argumentsFor.remove);
|
|
99
|
-
debug(`Adding REST provider for service \`${path}\` at base route \`${baseUri}\``);
|
|
100
|
-
const idRoute = '/:__feathersId';
|
|
101
|
-
const serviceRouter = express_1.Router({ mergeParams: true })
|
|
102
|
-
.get('/', find)
|
|
103
|
-
.post('/', create)
|
|
104
|
-
.get(idRoute, get)
|
|
105
|
-
.put('/', update)
|
|
106
|
-
.put(idRoute, update)
|
|
107
|
-
.patch('/', patch)
|
|
108
|
-
.patch(idRoute, patch)
|
|
109
|
-
.delete('/', remove)
|
|
110
|
-
.delete(idRoute, remove);
|
|
111
|
-
app.use(baseUri, ...before, serviceRouter, ...after);
|
|
74
|
+
app.use((0, authentication_1.parseAuthentication)(authenticationOptions));
|
|
75
|
+
app.use(servicesMiddleware());
|
|
76
|
+
app.mixins.push((_service, _path, options) => {
|
|
77
|
+
const { express: { before = [], after = [] } = {} } = options;
|
|
78
|
+
const middlewares = [].concat(before, serviceMiddleware(), after, formatterMiddleware);
|
|
79
|
+
const middleware = (0, express_1.Router)().use(middlewares);
|
|
80
|
+
options.express || (options.express = {});
|
|
81
|
+
options.express.composed = middleware;
|
|
112
82
|
});
|
|
113
83
|
};
|
|
114
|
-
}
|
|
84
|
+
};
|
|
115
85
|
exports.rest = rest;
|
|
116
86
|
//# sourceMappingURL=rest.js.map
|
package/lib/rest.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"rest.js","sourceRoot":"","sources":["../src/rest.ts"],"names":[],"mappings":";;;AAAA,qCAAmE;AACnE,+CAAqD;AACrD,iDAAiD;AACjD,qEAAoD;AACpD,mDAA8F;AAE9F,qDAA8E;AAG9E,MAAM,KAAK,GAAG,IAAA,qBAAW,EAAC,0BAA0B,CAAC,CAAA;AAErD,MAAM,SAAS,GAAG,CAChB,IAAsE,EACtD,EAAE;IAClB,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAA;AAC/E,CAAC,CAAA;AAED,MAAM,iBAAiB,GAAG,GAAmB,EAAE;IAC7C,OAAO,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QACxC,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,GAAG,CAAA;QACpE,MAAM,cAAc,GAAG,GAAG,CAAC,OAAO,CAAC,wBAAI,CAAC,aAAa,CAAuB,CAAA;QAE5E,oEAAoE;QACpE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,IAAI,EAAE,GAAG,KAAK,EAAE,GAAG,EAAE,EAAE,GAAG,GAAG,CAAC,MAAO,CAAA;QAC3E,MAAM,MAAM,GAAG,wBAAI,CAAC,gBAAgB,CAAC,UAAU,EAAE,EAAE,EAAE,cAAc,CAAC,CAAA;QACpE,MAAM,EAAE,OAAO,EAAE,GAAG,IAAA,4BAAiB,EAAC,OAAO,CAAC,CA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cAAc,CAAA;IAEpD,OAAO,CAAC,GAAgB,EAAE,EAAE;QAC1B,IAAI,OAAO,GAAG,CAAC,KAAK,KAAK,UAAU,EAAE;YACnC,MAAM,IAAI,KAAK,CAAC,2DAA2D,CAAC,CAAA;SAC7E;QAED,GAAG,CAAC,GAAG,CAAC,IAAA,oCAAmB,EAAC,qBAAqB,CAAC,CAAC,CAAA;QACnD,GAAG,CAAC,GAAG,CAAC,kBAAkB,EAAE,CAAC,CAAA;QAE7B,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;YAC3C,MAAM,EAAE,OAAO,EAAE,EAAE,MAAM,GAAG,EAAE,EAAE,KAAK,GAAG,EAAE,EAAE,GAAG,EAAE,EAAE,GAAG,OAAO,CAAA;YAE7D,MAAM,WAAW,GAAG,EAAE,CAAC,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE,EAAE,KAAK,EAAE,mBAAmB,CAAC,CAAA;YACtF,MAAM,UAAU,GAAG,IAAA,gBAAM,GAAE,CAAC,GAAG,CAAC,WAAW,CAAC,CAAA;YAE5C,OAAO,CAAC,OAAO,KAAf,OAAO,CAAC,OAAO,GAAK,EAAE,EAAA;YACtB,OAAO,CAAC,OAAO,CAAC,QAAQ,GAAG,UAAU,CAAA;QACvC,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC,CAAA;AAxBY,QAAA,IAAI,QAwBhB"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feathersjs/express",
|
|
3
3
|
"description": "Feathers Express framework bindings and REST provider",
|
|
4
|
-
"version": "5.0.0
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"homepage": "https://feathersjs.com",
|
|
6
6
|
"main": "lib/",
|
|
7
|
+
"types": "lib/",
|
|
7
8
|
"keywords": [
|
|
8
9
|
"feathers",
|
|
9
10
|
"feathers-plugin"
|
|
@@ -15,7 +16,8 @@
|
|
|
15
16
|
},
|
|
16
17
|
"repository": {
|
|
17
18
|
"type": "git",
|
|
18
|
-
"url": "git://github.com/feathersjs/feathers.git"
|
|
19
|
+
"url": "git://github.com/feathersjs/feathers.git",
|
|
20
|
+
"directory": "packages/express"
|
|
19
21
|
},
|
|
20
22
|
"author": {
|
|
21
23
|
"name": "Feathers contributors",
|
|
@@ -39,7 +41,8 @@
|
|
|
39
41
|
],
|
|
40
42
|
"scripts": {
|
|
41
43
|
"prepublish": "npm run compile",
|
|
42
|
-
"
|
|
44
|
+
"pack": "npm pack --pack-destination ../generators/test/build",
|
|
45
|
+
"compile": "shx rm -rf lib/ && tsc && npm run pack",
|
|
43
46
|
"test": "mocha --config ../../.mocharc.json --recursive test/**.test.ts test/**/*.test.ts"
|
|
44
47
|
},
|
|
45
48
|
"directories": {
|
|
@@ -49,26 +52,30 @@
|
|
|
49
52
|
"access": "public"
|
|
50
53
|
},
|
|
51
54
|
"dependencies": {
|
|
52
|
-
"@feathersjs/
|
|
53
|
-
"@feathersjs/
|
|
54
|
-
"@feathersjs/
|
|
55
|
-
"@
|
|
56
|
-
"
|
|
57
|
-
"
|
|
55
|
+
"@feathersjs/authentication": "^5.0.0",
|
|
56
|
+
"@feathersjs/commons": "^5.0.0",
|
|
57
|
+
"@feathersjs/errors": "^5.0.0",
|
|
58
|
+
"@feathersjs/feathers": "^5.0.0",
|
|
59
|
+
"@feathersjs/transport-commons": "^5.0.0",
|
|
60
|
+
"@types/compression": "^1.7.2",
|
|
61
|
+
"@types/express": "^4.17.17",
|
|
62
|
+
"@types/express-serve-static-core": "^4.17.33",
|
|
63
|
+
"compression": "^1.7.4",
|
|
64
|
+
"cors": "^2.8.5",
|
|
65
|
+
"express": "^4.18.2"
|
|
58
66
|
},
|
|
59
67
|
"devDependencies": {
|
|
60
|
-
"@feathersjs/authentication": "^5.0.0
|
|
61
|
-
"@feathersjs/
|
|
62
|
-
"@
|
|
63
|
-
"@
|
|
64
|
-
"@types/
|
|
65
|
-
"
|
|
66
|
-
"axios": "^0.21.1",
|
|
68
|
+
"@feathersjs/authentication-local": "^5.0.0",
|
|
69
|
+
"@feathersjs/tests": "^5.0.0",
|
|
70
|
+
"@types/lodash": "^4.14.191",
|
|
71
|
+
"@types/mocha": "^10.0.1",
|
|
72
|
+
"@types/node": "^18.14.1",
|
|
73
|
+
"axios": "^1.3.4",
|
|
67
74
|
"lodash": "^4.17.21",
|
|
68
|
-
"mocha": "^
|
|
69
|
-
"shx": "^0.3.
|
|
70
|
-
"ts-node": "^10.1
|
|
71
|
-
"typescript": "^4.
|
|
75
|
+
"mocha": "^10.2.0",
|
|
76
|
+
"shx": "^0.3.4",
|
|
77
|
+
"ts-node": "^10.9.1",
|
|
78
|
+
"typescript": "^4.9.5"
|
|
72
79
|
},
|
|
73
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "90caf635aec850550b9d37bea2762af959d9e8d5"
|
|
74
81
|
}
|
package/src/authentication.ts
CHANGED
|
@@ -1,71 +1,66 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { RequestHandler, Request, Response } from 'express'
|
|
2
|
+
import { HookContext } from '@feathersjs/feathers'
|
|
3
|
+
import { createDebug } from '@feathersjs/commons'
|
|
4
|
+
import { authenticate as AuthenticateHook } from '@feathersjs/authentication'
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
import { Application } from './declarations'
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
service?: string;
|
|
9
|
-
strategies: string[]
|
|
10
|
-
};
|
|
8
|
+
const debug = createDebug('@feathersjs/express/authentication')
|
|
11
9
|
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
const toHandler = (
|
|
11
|
+
func: (req: Request, res: Response, next: () => void) => Promise<void>
|
|
12
|
+
): RequestHandler => {
|
|
13
|
+
return (req, res, next) => func(req, res, next).catch((error) => next(error))
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type AuthenticationSettings = {
|
|
17
|
+
service?: string
|
|
18
|
+
strategies?: string[]
|
|
19
|
+
}
|
|
16
20
|
|
|
17
|
-
export function parseAuthentication
|
|
18
|
-
return
|
|
19
|
-
const app = req.app as any
|
|
20
|
-
const service = app.defaultAuthentication
|
|
21
|
+
export function parseAuthentication(settings: AuthenticationSettings = {}): RequestHandler {
|
|
22
|
+
return toHandler(async (req, res, next) => {
|
|
23
|
+
const app = req.app as any as Application
|
|
24
|
+
const service = app.defaultAuthentication?.(settings.service)
|
|
21
25
|
|
|
22
|
-
if (service
|
|
23
|
-
return next()
|
|
26
|
+
if (!service) {
|
|
27
|
+
return next()
|
|
24
28
|
}
|
|
25
29
|
|
|
26
|
-
const config = service.configuration
|
|
27
|
-
const authStrategies = config.parseStrategies || config.authStrategies || []
|
|
30
|
+
const config = service.configuration
|
|
31
|
+
const authStrategies = settings.strategies || config.parseStrategies || config.authStrategies || []
|
|
28
32
|
|
|
29
33
|
if (authStrategies.length === 0) {
|
|
30
|
-
debug('No `authStrategies` or `parseStrategies` found in authentication configuration')
|
|
31
|
-
return next()
|
|
34
|
+
debug('No `authStrategies` or `parseStrategies` found in authentication configuration')
|
|
35
|
+
return next()
|
|
32
36
|
}
|
|
33
37
|
|
|
34
|
-
service.parse(req, res, ...authStrategies)
|
|
35
|
-
.then((authentication: any) => {
|
|
36
|
-
if (authentication) {
|
|
37
|
-
debug('Parsed authentication from HTTP header', authentication);
|
|
38
|
-
merge(req, {
|
|
39
|
-
authentication,
|
|
40
|
-
feathers: { authentication }
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
next();
|
|
45
|
-
}).catch(next);
|
|
46
|
-
};
|
|
47
|
-
}
|
|
38
|
+
const authentication = await service.parse(req, res, ...authStrategies)
|
|
48
39
|
|
|
49
|
-
|
|
50
|
-
|
|
40
|
+
if (authentication) {
|
|
41
|
+
debug('Parsed authentication from HTTP header', authentication)
|
|
42
|
+
req.feathers = { ...req.feathers, authentication }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return next()
|
|
46
|
+
})
|
|
47
|
+
}
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
export function authenticate(
|
|
50
|
+
settings: string | AuthenticationSettings,
|
|
51
|
+
...strategies: string[]
|
|
52
|
+
): RequestHandler {
|
|
53
|
+
const hook = AuthenticateHook(settings, ...strategies)
|
|
55
54
|
|
|
56
|
-
return (
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const
|
|
55
|
+
return toHandler(async (req, _res, next) => {
|
|
56
|
+
const app = req.app as any as Application
|
|
57
|
+
const params = req.feathers
|
|
58
|
+
const context = { app, params } as any as HookContext
|
|
60
59
|
|
|
61
|
-
|
|
60
|
+
await hook(context)
|
|
62
61
|
|
|
63
|
-
|
|
64
|
-
.then((authResult: any) => {
|
|
65
|
-
debug('Merging request with', authResult);
|
|
66
|
-
merge(req, authResult);
|
|
62
|
+
req.feathers = context.params
|
|
67
63
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
};
|
|
64
|
+
return next()
|
|
65
|
+
})
|
|
71
66
|
}
|
package/src/declarations.ts
CHANGED
|
@@ -1,60 +1,68 @@
|
|
|
1
|
-
import http from 'http'
|
|
2
|
-
import express, { Express } from 'express'
|
|
1
|
+
import http from 'http'
|
|
2
|
+
import express, { Express } from 'express'
|
|
3
3
|
import {
|
|
4
|
-
Application as FeathersApplication,
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
Application as FeathersApplication,
|
|
5
|
+
Params as FeathersParams,
|
|
6
|
+
HookContext,
|
|
7
|
+
ServiceMethods,
|
|
8
|
+
ServiceInterface,
|
|
9
|
+
RouteLookup
|
|
10
|
+
} from '@feathersjs/feathers'
|
|
7
11
|
|
|
8
|
-
interface ExpressUseHandler<T,
|
|
9
|
-
<L extends keyof
|
|
12
|
+
interface ExpressUseHandler<T, Services> {
|
|
13
|
+
<L extends keyof Services & string>(
|
|
10
14
|
path: L,
|
|
11
15
|
...middlewareOrService: (
|
|
12
|
-
Express
|
|
13
|
-
|
|
16
|
+
| Express
|
|
17
|
+
| express.RequestHandler
|
|
18
|
+
| express.RequestHandler[]
|
|
19
|
+
| (keyof any extends keyof Services ? ServiceInterface : Services[L])
|
|
14
20
|
)[]
|
|
15
|
-
): T
|
|
16
|
-
(path: string|RegExp, ...expressHandlers: express.RequestHandler[]): T
|
|
17
|
-
(...expressHandlers: express.RequestHandler[]): T
|
|
18
|
-
(handler: Express|express.ErrorRequestHandler): T
|
|
21
|
+
): T
|
|
22
|
+
(path: string | RegExp, ...expressHandlers: express.RequestHandler[]): T
|
|
23
|
+
(...expressHandlers: express.RequestHandler[]): T
|
|
24
|
+
(handler: Express | express.ErrorRequestHandler): T
|
|
19
25
|
}
|
|
20
26
|
|
|
21
|
-
export interface ExpressOverrides<
|
|
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,
|
|
27
|
+
export interface ExpressOverrides<Services> {
|
|
28
|
+
listen(port: number, hostname: string, backlog: number, callback?: () => void): Promise<http.Server>
|
|
29
|
+
listen(port: number, hostname: string, callback?: () => void): Promise<http.Server>
|
|
30
|
+
listen(port: number | string | any, callback?: () => void): Promise<http.Server>
|
|
31
|
+
listen(callback?: () => void): Promise<http.Server>
|
|
32
|
+
use: ExpressUseHandler<this, Services>
|
|
33
|
+
server?: http.Server
|
|
27
34
|
}
|
|
28
35
|
|
|
29
|
-
export type Application<
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
ExpressOverrides<ServiceTypes>;
|
|
36
|
+
export type Application<Services = any, Settings = any> = Omit<Express, 'listen' | 'use' | 'get' | 'set'> &
|
|
37
|
+
FeathersApplication<Services, Settings> &
|
|
38
|
+
ExpressOverrides<Services>
|
|
33
39
|
|
|
34
40
|
declare module '@feathersjs/feathers/lib/declarations' {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
before
|
|
38
|
-
after
|
|
41
|
+
interface ServiceOptions {
|
|
42
|
+
express?: {
|
|
43
|
+
before?: express.RequestHandler[]
|
|
44
|
+
after?: express.RequestHandler[]
|
|
45
|
+
composed?: express.RequestHandler
|
|
39
46
|
}
|
|
40
47
|
}
|
|
41
48
|
}
|
|
42
49
|
|
|
43
50
|
declare module 'express-serve-static-core' {
|
|
44
51
|
interface Request {
|
|
45
|
-
|
|
52
|
+
feathers: Partial<FeathersParams> & { [key: string]: any }
|
|
53
|
+
lookup?: RouteLookup
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
interface Response {
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
data?: any
|
|
58
|
+
hook?: HookContext
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
interface IRouterMatcher<T> {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
62
|
+
// eslint-disable-next-line
|
|
63
|
+
<P extends Params = ParamsDictionary, ResBody = any, ReqBody = any>(
|
|
64
|
+
path: PathParams,
|
|
65
|
+
...handlers: (RequestHandler<P, ResBody, ReqBody> | Partial<ServiceMethods> | Application)[]
|
|
66
|
+
): T
|
|
59
67
|
}
|
|
60
68
|
}
|