@feathersjs/express 5.0.0-pre.9 → 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/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.serviceMethodHandler = exports.serviceMiddleware = exports.formatter = exports.feathersParams = void 0;
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 feathersParams = (req, _res, next) => {
32
- req.feathers = Object.assign(Object.assign({}, req.feathers), { provider: 'rest', headers: req.headers });
33
- next();
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 serviceMiddleware = (callback) => (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
48
- debug(`Running service middleware for '${req.url}'`);
49
- try {
50
- const { query, body: data } = req;
51
- const _a = req.params, { __feathersId: id = null } = _a, route = __rest(_a, ["__feathersId"]);
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(exports.feathersParams);
84
- app.use(authentication_1.parseAuthentication());
85
- // Register the REST provider
86
- app.mixins.push(function (service, path, options) {
87
- const { middleware: { before = [] } } = options;
88
- let { middleware: { after = [] } } = options;
89
- if (typeof handler === 'function') {
90
- after = after.concat(handler);
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":";;;;;;;;;;;;;;;;;;;;;;;AAAA,+CAAsD;AAEtD,iDAAkD;AAClD,qEAAqD;AACr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AAG,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-pre.9",
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
- "compile": "shx rm -rf lib/ && tsc",
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/commons": "^5.0.0-pre.9",
53
- "@feathersjs/errors": "^5.0.0-pre.9",
54
- "@feathersjs/transport-commons": "^5.0.0-pre.9",
55
- "@types/express": "^4.17.13",
56
- "express": "^4.17.1",
57
- "lodash": "^4.17.21"
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-pre.9",
61
- "@feathersjs/authentication-local": "^5.0.0-pre.9",
62
- "@feathersjs/feathers": "^5.0.0-pre.9",
63
- "@feathersjs/tests": "^5.0.0-pre.9",
64
- "@types/mocha": "^9.0.0",
65
- "@types/node": "^16.4.13",
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": "^9.0.3",
69
- "shx": "^0.3.3",
70
- "ts-node": "^10.1.0",
71
- "typescript": "^4.3.5"
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": "3d1721a7286e6a7f37bbc38695fe45084023f13b"
80
+ "gitHead": "90caf635aec850550b9d37bea2762af959d9e8d5"
74
81
  }
@@ -1,71 +1,66 @@
1
- import { createDebug } from '@feathersjs/commons';
2
- import { merge, flatten } from 'lodash';
3
- import { NextFunction, RequestHandler } from 'express';
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
- const debug = createDebug('@feathersjs/express/authentication');
6
+ import { Application } from './declarations'
6
7
 
7
- type StrategyOptions = {
8
- service?: string;
9
- strategies: string[]
10
- };
8
+ const debug = createDebug('@feathersjs/express/authentication')
11
9
 
12
- const normalizeStrategy = (_settings: string|StrategyOptions, ..._strategies: string[]) =>
13
- typeof _settings === 'string'
14
- ? { strategies: flatten([ _settings, ..._strategies ]) }
15
- : _settings;
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 (settings: any = {}): RequestHandler {
18
- return function (req, res, next) {
19
- const app = req.app as any;
20
- const service = app.defaultAuthentication ? app.defaultAuthentication(settings.service) : null;
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 === null) {
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
- export function authenticate (_settings: string|StrategyOptions, ..._strategies: string[]) {
50
- const settings = normalizeStrategy(_settings, ..._strategies);
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
- if (!Array.isArray(settings.strategies) || settings.strategies.length === 0) {
53
- throw new Error('\'authenticate\' middleware requires at least one strategy name');
54
- }
49
+ export function authenticate(
50
+ settings: string | AuthenticationSettings,
51
+ ...strategies: string[]
52
+ ): RequestHandler {
53
+ const hook = AuthenticateHook(settings, ...strategies)
55
54
 
56
- return (_req: Request, _res: Response, next: NextFunction) => {
57
- const req = _req as any;
58
- const { app, authentication } = req;
59
- const service = app.defaultAuthentication(settings.service);
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
- debug('Authenticating with Express middleware and strategies', settings.strategies);
60
+ await hook(context)
62
61
 
63
- service.authenticate(authentication, req.feathers, ...settings.strategies)
64
- .then((authResult: any) => {
65
- debug('Merging request with', authResult);
66
- merge(req, authResult);
62
+ req.feathers = context.params
67
63
 
68
- next();
69
- }).catch(next);
70
- };
64
+ return next()
65
+ })
71
66
  }
@@ -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, Params as FeathersParams,
5
- HookContext, ServiceMethods, ServiceInterface
6
- } from '@feathersjs/feathers';
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, ServiceTypes> {
9
- <L extends keyof ServiceTypes & string> (
12
+ interface ExpressUseHandler<T, Services> {
13
+ <L extends keyof Services & string>(
10
14
  path: L,
11
15
  ...middlewareOrService: (
12
- Express|express.RequestHandler|
13
- (keyof any extends keyof ServiceTypes ? ServiceInterface<any> : ServiceTypes[L])
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<ServiceTypes> {
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, ServiceTypes>;
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<ServiceTypes = any, AppSettings = any> =
30
- Omit<Express, 'listen'|'use'> &
31
- FeathersApplication<ServiceTypes, AppSettings> &
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
- export interface ServiceOptions {
36
- middleware?: {
37
- before: express.RequestHandler[],
38
- after: express.RequestHandler[]
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
- feathers?: Partial<FeathersParams>;
52
+ feathers: Partial<FeathersParams> & { [key: string]: any }
53
+ lookup?: RouteLookup
46
54
  }
47
55
 
48
56
  interface Response {
49
- data?: any;
50
- hook?: HookContext;
57
+ data?: any
58
+ hook?: HookContext
51
59
  }
52
60
 
53
61
  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<any, any>> | Application)[]
58
- ): T;
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
  }