@feathersjs/feathers 5.0.0-pre.2 → 5.0.0-pre.20

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.
@@ -0,0 +1,169 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.enableRegularHooks = exports.convertHookData = exports.collectRegularHooks = exports.fromErrorHooks = exports.fromAfterHooks = exports.fromBeforeHooks = exports.fromErrorHook = exports.fromAfterHook = exports.fromBeforeHook = void 0;
4
+ const service_1 = require("../service");
5
+ const runHook = (hook, context, type) => {
6
+ if (type)
7
+ context.type = type;
8
+ return Promise.resolve(hook.call(context.self, context))
9
+ .then((res) => {
10
+ if (type)
11
+ context.type = null;
12
+ if (res && res !== context) {
13
+ Object.assign(context, res);
14
+ }
15
+ });
16
+ };
17
+ function fromBeforeHook(hook) {
18
+ return (context, next) => {
19
+ return runHook(hook, context, 'before').then(next);
20
+ };
21
+ }
22
+ exports.fromBeforeHook = fromBeforeHook;
23
+ function fromAfterHook(hook) {
24
+ return (context, next) => {
25
+ return next().then(() => runHook(hook, context, 'after'));
26
+ };
27
+ }
28
+ exports.fromAfterHook = fromAfterHook;
29
+ function fromErrorHook(hook) {
30
+ return (context, next) => {
31
+ return next().catch((error) => {
32
+ if (context.error !== error || context.result !== undefined) {
33
+ context.original = { ...context };
34
+ context.error = error;
35
+ delete context.result;
36
+ }
37
+ return runHook(hook, context, 'error').then(() => {
38
+ if (context.result === undefined && context.error !== undefined) {
39
+ throw context.error;
40
+ }
41
+ });
42
+ });
43
+ };
44
+ }
45
+ exports.fromErrorHook = fromErrorHook;
46
+ const RunHooks = (hooks) => (context) => {
47
+ return hooks.reduce((promise, hook) => {
48
+ return promise.then(() => runHook(hook, context));
49
+ }, Promise.resolve(undefined));
50
+ };
51
+ function fromBeforeHooks(hooks) {
52
+ return fromBeforeHook(RunHooks(hooks));
53
+ }
54
+ exports.fromBeforeHooks = fromBeforeHooks;
55
+ function fromAfterHooks(hooks) {
56
+ return fromAfterHook(RunHooks(hooks));
57
+ }
58
+ exports.fromAfterHooks = fromAfterHooks;
59
+ function fromErrorHooks(hooks) {
60
+ return fromErrorHook(RunHooks(hooks));
61
+ }
62
+ exports.fromErrorHooks = fromErrorHooks;
63
+ function collectRegularHooks(target, method) {
64
+ return target.__hooks.hooks[method] || [];
65
+ }
66
+ exports.collectRegularHooks = collectRegularHooks;
67
+ // Converts different hook registration formats into the
68
+ // same internal format
69
+ function convertHookData(input) {
70
+ const result = {};
71
+ if (Array.isArray(input)) {
72
+ result.all = input;
73
+ }
74
+ else if (typeof input !== 'object') {
75
+ result.all = [input];
76
+ }
77
+ else {
78
+ for (const key of Object.keys(input)) {
79
+ const value = input[key];
80
+ result[key] = Array.isArray(value) ? value : [value];
81
+ }
82
+ }
83
+ return result;
84
+ }
85
+ exports.convertHookData = convertHookData;
86
+ const types = ['before', 'after', 'error'];
87
+ const isType = (value) => types.includes(value);
88
+ const wrappers = {
89
+ before: fromBeforeHooks,
90
+ after: fromAfterHooks,
91
+ error: fromErrorHooks
92
+ };
93
+ const createStore = (methods) => {
94
+ const store = {
95
+ before: {},
96
+ after: {},
97
+ error: {},
98
+ hooks: {}
99
+ };
100
+ for (const method of methods) {
101
+ store.hooks[method] = [];
102
+ }
103
+ return store;
104
+ };
105
+ const setStore = (object, store) => {
106
+ Object.defineProperty(object, '__hooks', {
107
+ configurable: true,
108
+ value: store,
109
+ writable: true
110
+ });
111
+ };
112
+ const getStore = (object) => object.__hooks;
113
+ const createMap = (input, methods) => {
114
+ const map = {};
115
+ Object.keys(input).forEach((type) => {
116
+ if (!isType(type)) {
117
+ throw new Error(`'${type}' is not a valid hook type`);
118
+ }
119
+ const data = convertHookData(input[type]);
120
+ Object.keys(data).forEach((method) => {
121
+ if (method !== 'all' && !methods.includes(method)) {
122
+ throw new Error(`'${method}' is not a valid hook method`);
123
+ }
124
+ });
125
+ map[type] = data;
126
+ });
127
+ return map;
128
+ };
129
+ const createAdapter = (type) => {
130
+ const hooks = [];
131
+ const hook = wrappers[type](hooks);
132
+ const adapter = Object.assign(hook, { hooks });
133
+ return adapter;
134
+ };
135
+ const updateStore = (store, map) => {
136
+ Object.keys(store.hooks).forEach((method) => {
137
+ let adapted = false;
138
+ Object.keys(map).forEach((key) => {
139
+ var _a;
140
+ const type = key;
141
+ const allHooks = map[type].all || [];
142
+ const methodHooks = map[type][method] || [];
143
+ if (allHooks.length || methodHooks.length) {
144
+ const adapter = (_a = store[type])[method] || (_a[method] = (adapted = true, createAdapter(type)));
145
+ adapter.hooks.push(...allHooks, ...methodHooks);
146
+ }
147
+ });
148
+ if (adapted) {
149
+ store.hooks[method] = [
150
+ store.error[method],
151
+ store.before[method],
152
+ store.after[method]
153
+ ].filter(hook => hook);
154
+ }
155
+ });
156
+ };
157
+ // Add `.hooks` functionality to an object
158
+ function enableRegularHooks(object, methods = service_1.defaultServiceMethods) {
159
+ const store = createStore(methods);
160
+ setStore(object, store);
161
+ return function regularHooks(input) {
162
+ const store = getStore(this);
163
+ const map = createMap(input, methods);
164
+ updateStore(store, map);
165
+ return this;
166
+ };
167
+ }
168
+ exports.enableRegularHooks = enableRegularHooks;
169
+ //# sourceMappingURL=regular.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regular.js","sourceRoot":"","sources":["../../src/hooks/regular.ts"],"names":[],"mappings":";;;AACA,wCAAmD;AAEnD,MAAM,OAAO,GAAG,CAAQ,IAA+B,EAAE,OAAY,EAAE,IAAa,EAAE,EAAE;IACtF,IAAI,IAAI;QAAE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;IAC9B,OAAO,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;SACrD,IAAI,CAAC,CAAC,GAAQ,EAAE,EAAE;QACjB,IAAI,IAAI;YAAE,OAAO,CAAC,IAAI,GAAG,IAAI,CAAC;QAC9B,IAAI,GAAG,IAAI,GA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AK,EAAE,OAAO,CAAC,CAAC;QAEtC,WAAW,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAExB,OAAO,IAAI,CAAC;IACd,CAAC,CAAA;AACH,CAAC;AAhBD,gDAgBC"}
package/lib/index.d.ts CHANGED
@@ -2,7 +2,10 @@ import version from './version';
2
2
  import { Feathers } from './application';
3
3
  import { Application } from './declarations';
4
4
  export declare function feathers<T = any, S = any>(): Application<T, S>;
5
+ export declare namespace feathers {
6
+ var setDebug: typeof import("@feathersjs/commons/lib/debug").setDebug;
7
+ }
5
8
  export { version, Feathers };
9
+ export * from './hooks/index';
6
10
  export * from './declarations';
7
11
  export * from './service';
8
- export * from './hooks';
package/lib/index.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -14,6 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
18
  };
15
19
  Object.defineProperty(exports, "__esModule", { value: true });
16
20
  exports.Feathers = exports.version = exports.feathers = void 0;
21
+ const dependencies_1 = require("./dependencies");
17
22
  const version_1 = __importDefault(require("./version"));
18
23
  exports.version = version_1.default;
19
24
  const application_1 = require("./application");
@@ -22,9 +27,10 @@ function feathers() {
22
27
  return new application_1.Feathers();
23
28
  }
24
29
  exports.feathers = feathers;
30
+ feathers.setDebug = dependencies_1.setDebug;
31
+ __exportStar(require("./hooks/index"), exports);
25
32
  __exportStar(require("./declarations"), exports);
26
33
  __exportStar(require("./service"), exports);
27
- __exportStar(require("./hooks"), exports);
28
34
  if (typeof module !== 'undefined') {
29
35
  module.exports = Object.assign(feathers, module.exports);
30
36
  }
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAEA,wDAAgC;AAQvB,kBARF,iBAAO,CAQE;AAPhB,+CAAyC;AAOvB,yFAPT,sBAAQ,OAOS;AAJ1B,SAAgB,QAAQ;IACtB,OAAO,IAAI,sBAAQ,EAA6B,CAAC;AACnD,CAAC;AAFD,4BAEC;AAGD,iDAA+B;AAC/B,4CAA0B;AAC1B,0CAAwB;AAExB,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CAC1D"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,iDAA0C;AAC1C,wDAAgC;AAUvB,kBAVF,iBAAO,CAUE;AAThB,+CAAyC;AASvB,yFATT,sBAAQ,OASS;AAN1B,SAAgB,QAAQ;IACtB,OAAO,IAAI,sBAAQ,EAA6B,CAAC;AACnD,CAAC;AAFD,4BAEC;AAED,QAAQ,CAAC,QAAQ,GAAG,uBAAQ,CAAC;AAG7B,gDAA8B;AAC9B,iDAA+B;AAC/B,4CAA0B;AAE1B,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;IACjC,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;CAC1D"}
package/lib/service.d.ts CHANGED
@@ -15,6 +15,7 @@ export declare const defaultEventMap: {
15
15
  patch: string;
16
16
  remove: string;
17
17
  };
18
+ export declare const protectedMethods: string[];
18
19
  export declare function getHookMethods(service: any, options: ServiceOptions): string[];
19
20
  export declare function getServiceOptions(service: any, options?: ServiceOptions): ServiceOptions;
20
21
  export declare function wrapService(location: string, service: any, options: ServiceOptions): any;
package/lib/service.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wrapService = exports.getServiceOptions = exports.getHookMethods = exports.defaultEventMap = exports.defaultServiceMethods = exports.defaultServiceArguments = exports.SERVICE = void 0;
4
- const commons_1 = require("@feathersjs/commons");
5
- exports.SERVICE = commons_1.createSymbol('@feathersjs/service');
3
+ exports.wrapService = exports.getServiceOptions = exports.getHookMethods = exports.protectedMethods = exports.defaultEventMap = exports.defaultServiceMethods = exports.defaultServiceArguments = exports.SERVICE = void 0;
4
+ const dependencies_1 = require("./dependencies");
5
+ exports.SERVICE = (0, dependencies_1.createSymbol)('@feathersjs/service');
6
6
  exports.defaultServiceArguments = {
7
7
  find: ['params'],
8
8
  get: ['id', 'params'],
@@ -18,6 +18,18 @@ exports.defaultEventMap = {
18
18
  patch: 'patched',
19
19
  remove: 'removed'
20
20
  };
21
+ exports.protectedMethods = Object.keys(Object.prototype)
22
+ .concat(Object.keys(dependencies_1.EventEmitter.prototype))
23
+ .concat([
24
+ 'all',
25
+ 'before',
26
+ 'after',
27
+ 'error',
28
+ 'hooks',
29
+ 'setup',
30
+ 'teardown',
31
+ 'publish'
32
+ ]);
21
33
  function getHookMethods(service, options) {
22
34
  const { methods } = options;
23
35
  return exports.defaultServiceMethods.filter(m => typeof service[m] === 'function' && !methods.includes(m)).concat(methods);
@@ -30,9 +42,12 @@ function getServiceOptions(service, options = {}) {
30
42
  }
31
43
  const { methods = exports.defaultServiceMethods.filter(method => typeof service[method] === 'function'), events = service.events || [] } = options;
32
44
  const { serviceEvents = Object.values(exports.defaultEventMap).concat(events) } = options;
33
- return Object.assign(Object.assign({}, options), { events,
45
+ return {
46
+ ...options,
47
+ events,
34
48
  methods,
35
- serviceEvents });
49
+ serviceEvents
50
+ };
36
51
  }
37
52
  exports.getServiceOptions = getServiceOptions;
38
53
  function wrapService(location, service, options) {
@@ -1 +1 @@
1
- {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":";;;AAAA,iDAAmD;AAItC,QAAA,OAAO,GAAG,sBAAY,CAAC,qBAAqB,CAAC,CAAC;AAE9C,QAAA,uBAAuB,GAAG;IACrC,IAAI,EAAE,CAAE,QAAQ,CAAE;IAClB,GAAG,EAAE,CAAE,IAAI,EAAE,QAAQ,CAAE;IACvB,MAAM,EAAE,CAAE,MAAM,EAAE,QAAQ,CAAE;IAC5B,MAAM,EAAE,CAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAE;IAClC,KAAK,EAAE,CAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAE;IACjC,MAAM,EAAE,CAAE,IAAI,EAAE,QAAQ,CAAE;CAC3B,CAAA;AAEY,QAAA,qBAAqB,GAAG,MAAM,CAAC,IAAI,CAAC,+BAAuB,CAAC,CAAC;AAE7D,QAAA,eAAe,GAAG;IAC7B,MAAM,EAAE,SAAS;IACjB,MAAM,EAAE,SAAS;IACjB,KAAK,EAAE,SAAS;IAChB,MAAM,EAAE,SAAS;CAClB,CAAA;AAED,SAAgB,cAAc,CAAE,OAAY,EAAE,OAAuB;IACnE,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;IAE5B,OAAO,6BAAqB,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CACtC,OAAO,OAAO,CAAC,CAAC,CAAC,KAAK,UAAU,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,CACzD,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACpB,CAAC;AAND,wCAMC;AAED,SAAgB,iBAAiB,CAC/B,OAAY,EAAE,UAA0B,EAAE;IAE1C,MAAM,eAAe,GAAG,OAAO,CAAC,eAAO,CAAC,CAAC;IAEzC,IAAI,eAAe,EAAE;QACnB,O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zC,IAAI,eAAe,EAAE;QACnB,OAAO,eAAe,CAAC;KACxB;IAED,MAAM,EACJ,OAAO,GAAG,6BAAqB,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAC9C,OAAO,OAAO,CAAC,MAAM,CAAC,KAAK,UAAU,CACtC,EACD,MAAM,GAAG,OAAO,CAAC,MAAM,IAAI,EAAE,EAC9B,GAAG,OAAO,CAAC;IACZ,MAAM,EACJ,aAAa,GAAG,MAAM,CAAC,MAAM,CAAC,uBAAe,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,EAC9D,GAAG,OAAO,CAAC;IAEZ,OAAO;QACL,GAAG,OAAO;QACV,MAAM;QACN,OAAO;QACP,aAAa;KACd,CAAC;AACJ,CAAC;AAzBD,8CAyBC;AAED,SAAgB,WAAW,CACzB,QAAgB,EAAE,OAAY,EAAE,OAAuB;IAEvD,+CAA+C;IAC/C,IAAI,OAAO,CAAC,eAAO,CAAC,EAAE;QACpB,OAAO,OAAO,CAAC;KAChB;IAED,MAAM,YAAY,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC5C,MAAM,cAAc,GAAG,iBAAiB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAE3D,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC,MAAM,KAAK,CAAC,IAAI,OAAO,OAAO,CAAC,KAAK,KAAK,UAAU,EAAE;QAC3F,MAAM,IAAI,KAAK,CAAC,4CAA4C,QAAQ,IAAI,CAAC,CAAC;KAC3E;IAED,MAAM,CAAC,cAAc,CAAC,YAAY,EAAE,eAAO,EAAE;QAC3C,KAAK,EAAE,cAAc;KACtB,CAAC,CAAC;IAEH,OAAO,YAAY,CAAC;AACtB,CAAC;AApBD,kCAoBC"}
package/lib/version.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const _default: "5.0.0-pre.2";
1
+ declare const _default: "5.0.0-pre.20";
2
2
  export default _default;
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.default = '5.0.0-pre.2';
3
+ exports.default = '5.0.0-pre.20';
4
4
  //# sourceMappingURL=version.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;AAAA,kBAAe,aAAa,CAAC"}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;AAAA,kBAAe,cAAc,CAAC"}
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@feathersjs/feathers",
3
3
  "description": "A framework for real-time applications and REST API with JavaScript and TypeScript",
4
- "version": "5.0.0-pre.2",
4
+ "version": "5.0.0-pre.20",
5
5
  "homepage": "http://feathersjs.com",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git://github.com/feathersjs/feathers.git"
8
+ "url": "git://github.com/feathersjs/feathers.git",
9
+ "directory": "packages/feathers"
9
10
  },
10
11
  "keywords": [
11
12
  "feathers",
@@ -47,8 +48,7 @@
47
48
  "version": "npm run write-version",
48
49
  "publish": "npm run reset-version",
49
50
  "compile": "shx rm -rf lib/ && tsc",
50
- "test": "npm run compile && npm run mocha",
51
- "mocha": "mocha --config ../../.mocharc.json --recursive test/"
51
+ "test": "mocha --config ../../.mocharc.json --recursive test/"
52
52
  },
53
53
  "engines": {
54
54
  "node": ">= 12"
@@ -57,18 +57,17 @@
57
57
  "access": "public"
58
58
  },
59
59
  "dependencies": {
60
- "@feathersjs/commons": "^5.0.0-pre.2",
61
- "@feathersjs/hooks": "^0.6.3",
62
- "debug": "^4.3.1",
60
+ "@feathersjs/commons": "^5.0.0-pre.20",
61
+ "@feathersjs/hooks": "^0.7.4",
63
62
  "events": "^3.3.0"
64
63
  },
65
64
  "devDependencies": {
66
- "@types/mocha": "^8.2.2",
67
- "@types/node": "^14.14.37",
68
- "mocha": "^8.3.2",
69
- "shx": "^0.3.3",
70
- "ts-node": "^9.1.1",
71
- "typescript": "^4.2.3"
65
+ "@types/mocha": "^9.1.1",
66
+ "@types/node": "^17.0.31",
67
+ "mocha": "^10.0.0",
68
+ "shx": "^0.3.4",
69
+ "ts-node": "^10.7.0",
70
+ "typescript": "^4.6.4"
72
71
  },
73
- "gitHead": "6e1f888dc5b612d2d77653177622e3f66245a0fb"
72
+ "gitHead": "54de749a0b392c7da726c668002b50cafaca530c"
74
73
  }
@@ -1,12 +1,10 @@
1
- import Debug from 'debug';
2
- import { EventEmitter } from 'events';
3
- import { stripSlashes } from '@feathersjs/commons';
4
- import { HOOKS } from '@feathersjs/hooks';
5
-
6
1
  import version from './version';
2
+ import {
3
+ EventEmitter, stripSlashes, createDebug, HOOKS, hooks, middleware
4
+ } from './dependencies';
7
5
  import { eventHook, eventMixin } from './events';
8
- import { hookMixin } from './hooks';
9
- import { wrapService, getServiceOptions } from './service';
6
+ import { hookMixin } from './hooks/index';
7
+ import { wrapService, getServiceOptions, protectedMethods } from './service';
10
8
  import {
11
9
  FeathersApplication,
12
10
  ServiceMixin,
@@ -14,37 +12,44 @@ import {
14
12
  ServiceOptions,
15
13
  ServiceInterface,
16
14
  Application,
17
- HookOptions,
18
15
  FeathersService,
19
16
  HookMap,
20
- LegacyHookMap
17
+ ApplicationHookOptions
21
18
  } from './declarations';
22
- import { enableLegacyHooks } from './hooks/legacy';
19
+ import { enableRegularHooks } from './hooks/regular';
23
20
 
24
- const debug = Debug('@feathersjs/feathers');
21
+ const debug = createDebug('@feathersjs/feathers');
25
22
 
26
- export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements FeathersApplication<ServiceTypes, AppSettings> {
27
- services: ServiceTypes = ({} as ServiceTypes);
28
- settings: AppSettings = ({} as AppSettings);
29
- mixins: ServiceMixin<Application<ServiceTypes, AppSettings>>[] = [ hookMixin, eventMixin ];
23
+ export class Feathers<Services, Settings> extends EventEmitter implements FeathersApplication<Services, Settings> {
24
+ services: Services = ({} as Services);
25
+ settings: Settings = ({} as Settings);
26
+ mixins: ServiceMixin<Application<Services, Settings>>[] = [ hookMixin, eventMixin ];
30
27
  version: string = version;
31
28
  _isSetup = false;
32
- appHooks: HookMap<Application<ServiceTypes, AppSettings>, any> = {
29
+ appHooks: HookMap<Application<Services, Settings>, any> = {
33
30
  [HOOKS]: [ (eventHook as any) ]
34
31
  };
35
32
 
36
- private legacyHooks: (this: any, allHooks: any) => any;
33
+ private regularHooks: (this: any, allHooks: any) => any;
37
34
 
38
35
  constructor () {
39
36
  super();
40
- this.legacyHooks = enableLegacyHooks(this);
37
+ this.regularHooks = enableRegularHooks(this);
38
+ hooks(this, {
39
+ setup: middleware().params('server').props({
40
+ app: this
41
+ }),
42
+ teardown: middleware().params('server').props({
43
+ app: this
44
+ })
45
+ });
41
46
  }
42
47
 
43
- get<L extends keyof AppSettings & string> (name: L): AppSettings[L] {
48
+ get<L extends keyof Settings & string> (name: L): Settings[L] {
44
49
  return this.settings[name];
45
50
  }
46
51
 
47
- set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]) {
52
+ set<L extends keyof Settings & string> (name: L, value: Settings[L]) {
48
53
  this.settings[name] = value;
49
54
  return this;
50
55
  }
@@ -55,13 +60,13 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
55
60
  return this;
56
61
  }
57
62
 
58
- defaultService (location: string): ServiceInterface<any> {
63
+ defaultService (location: string): ServiceInterface {
59
64
  throw new Error(`Can not find service '${location}'`);
60
65
  }
61
66
 
62
- service<L extends keyof ServiceTypes & string> (
67
+ service<L extends keyof Services & string> (
63
68
  location: L
64
- ): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : ServiceTypes[L]> {
69
+ ): FeathersService<this, keyof any extends keyof Services ? Service : Services[L]> {
65
70
  const path = (stripSlashes(location) || '/') as L;
66
71
  const current = this.services[path];
67
72
 
@@ -73,9 +78,9 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
73
78
  return current as any;
74
79
  }
75
80
 
76
- use<L extends keyof ServiceTypes & string> (
81
+ use<L extends keyof Services & string> (
77
82
  path: L,
78
- service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L],
83
+ service: keyof any extends keyof Services ? ServiceInterface | Application : Services[L],
79
84
  options?: ServiceOptions
80
85
  ): this {
81
86
  if (typeof path !== 'string') {
@@ -95,35 +100,41 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
95
100
  }
96
101
 
97
102
  const protoService = wrapService(location, service, options);
98
- const serviceOptions = getServiceOptions(service, options);
103
+ const serviceOptions = getServiceOptions(protoService);
104
+
105
+ for (const name of protectedMethods) {
106
+ if (serviceOptions.methods.includes(name)) {
107
+ throw new Error(`'${name}' on service '${location}' is not allowed as a custom method name`);
108
+ }
109
+ }
99
110
 
100
111
  debug(`Registering new service at \`${location}\``);
101
112
 
102
113
  // Add all the mixins
103
114
  this.mixins.forEach(fn => fn.call(this, protoService, location, serviceOptions));
104
115
 
116
+ this.services[location] = protoService;
117
+
105
118
  // If we ran setup already, set this service up explicitly, this will not `await`
106
119
  if (this._isSetup && typeof protoService.setup === 'function') {
107
120
  debug(`Setting up service for \`${location}\``);
108
121
  protoService.setup(this, location);
109
122
  }
110
123
 
111
- this.services[location] = protoService;
112
-
113
124
  return this;
114
125
  }
115
126
 
116
- hooks (hookMap: HookOptions<this, any>) {
117
- const legacyMap = hookMap as LegacyHookMap<this, any>;
127
+ hooks (hookMap: ApplicationHookOptions<this>) {
128
+ const untypedMap = hookMap as any;
118
129
 
119
- if (legacyMap.before || legacyMap.after || legacyMap.error) {
120
- return this.legacyHooks(legacyMap);
121
- }
122
-
123
- if (Array.isArray(hookMap)) {
130
+ if (untypedMap.before || untypedMap.after || untypedMap.error) {
131
+ this.regularHooks(untypedMap);
132
+ } else if (untypedMap.setup || untypedMap.teardown) {
133
+ hooks(this, untypedMap);
134
+ } else if (Array.isArray(hookMap)) {
124
135
  this.appHooks[HOOKS].push(...hookMap as any);
125
136
  } else {
126
- const methodHookMap = hookMap as HookMap<Application<ServiceTypes, AppSettings>, any>;
137
+ const methodHookMap = hookMap as HookMap<Application<Services, Settings>, any>;
127
138
 
128
139
  Object.keys(methodHookMap).forEach(key => {
129
140
  const methodHooks = this.appHooks[key] || [];
@@ -136,24 +147,32 @@ export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements
136
147
  }
137
148
 
138
149
  setup () {
139
- let promise = Promise.resolve();
150
+ this._isSetup = true;
140
151
 
141
- // Setup each service (pass the app so that they can look up other services etc.)
142
- for (const path of Object.keys(this.services)) {
143
- promise = promise.then(() => {
152
+ return Object.keys(this.services).reduce((current, path) => current
153
+ .then(() => {
144
154
  const service: any = this.service(path as any);
145
-
155
+
146
156
  if (typeof service.setup === 'function') {
147
157
  debug(`Setting up service for \`${path}\``);
148
-
158
+
149
159
  return service.setup(this, path);
150
160
  }
151
- });
152
- }
161
+ }), Promise.resolve()).then(() => this);
162
+ }
153
163
 
154
- return promise.then(() => {
155
- this._isSetup = true;
156
- return this;
157
- });
164
+ teardown () {
165
+ this._isSetup = false;
166
+
167
+ return Object.keys(this.services).reduce((current, path) => current
168
+ .then(() => {
169
+ const service: any = this.service(path as any);
170
+
171
+ if (typeof service.teardown === 'function') {
172
+ debug(`Tearing down service for \`${path}\``);
173
+
174
+ return service.teardown(this, path);
175
+ }
176
+ }), Promise.resolve()).then(() => this)
158
177
  }
159
178
  }