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.Feathers = void 0;
|
7 | const version_1 = __importDefault(require("./version"));
|
8 | const events_1 = require("events");
|
9 | const commons_1 = require("@feathersjs/commons");
|
10 | const hooks_1 = require("@feathersjs/hooks");
|
11 | const events_2 = require("./events");
|
12 | const hooks_2 = require("./hooks");
|
13 | const service_1 = require("./service");
|
14 | const hooks_3 = require("./hooks");
|
15 | const debug = (0, commons_1.createDebug)('@feathersjs/feathers');
|
16 | class Feathers extends events_1.EventEmitter {
|
17 | constructor() {
|
18 | super();
|
19 | this.services = {};
|
20 | this.settings = {};
|
21 | this.mixins = [hooks_2.hookMixin, events_2.eventMixin];
|
22 | this.version = version_1.default;
|
23 | this._isSetup = false;
|
24 | this.registerHooks = (0, hooks_3.enableHooks)(this);
|
25 | this.registerHooks({
|
26 | around: [events_2.eventHook]
|
27 | });
|
28 | }
|
29 | get(name) {
|
30 | return this.settings[name];
|
31 | }
|
32 | set(name, value) {
|
33 | this.settings[name] = value;
|
34 | return this;
|
35 | }
|
36 | configure(callback) {
|
37 | callback.call(this, this);
|
38 | return this;
|
39 | }
|
40 | defaultService(location) {
|
41 | throw new Error(`Can not find service '${location}'`);
|
42 | }
|
43 | service(location) {
|
44 | const path = ((0, commons_1.stripSlashes)(location) || '/');
|
45 | const current = this.services.hasOwnProperty(path) ? this.services[path] : undefined;
|
46 | if (typeof current === 'undefined') {
|
47 | this.use(path, this.defaultService(path));
|
48 | return this.service(path);
|
49 | }
|
50 | return current;
|
51 | }
|
52 | _setup() {
|
53 | this._isSetup = true;
|
54 | return Object.keys(this.services)
|
55 | .reduce((current, path) => current.then(() => {
|
56 | const service = this.service(path);
|
57 | if (typeof service.setup === 'function') {
|
58 | debug(`Setting up service for \`${path}\``);
|
59 | return service.setup(this, path);
|
60 | }
|
61 | }), Promise.resolve())
|
62 | .then(() => this);
|
63 | }
|
64 | get setup() {
|
65 | return this._setup;
|
66 | }
|
67 | set setup(value) {
|
68 | this._setup = value[hooks_1.HOOKS]
|
69 | ? value
|
70 | : (0, hooks_1.hooks)(value, (0, hooks_1.middleware)().params('server').props({
|
71 | app: this
|
72 | }));
|
73 | }
|
74 | _teardown() {
|
75 | this._isSetup = false;
|
76 | return Object.keys(this.services)
|
77 | .reduce((current, path) => current.then(() => {
|
78 | const service = this.service(path);
|
79 | if (typeof service.teardown === 'function') {
|
80 | debug(`Tearing down service for \`${path}\``);
|
81 | return service.teardown(this, path);
|
82 | }
|
83 | }), Promise.resolve())
|
84 | .then(() => this);
|
85 | }
|
86 | get teardown() {
|
87 | return this._teardown;
|
88 | }
|
89 | set teardown(value) {
|
90 | this._teardown = value[hooks_1.HOOKS]
|
91 | ? value
|
92 | : (0, hooks_1.hooks)(value, (0, hooks_1.middleware)().params('server').props({
|
93 | app: this
|
94 | }));
|
95 | }
|
96 | use(path, service, options) {
|
97 | if (typeof path !== 'string') {
|
98 | throw new Error(`'${path}' is not a valid service path.`);
|
99 | }
|
100 | const location = ((0, commons_1.stripSlashes)(path) || '/');
|
101 | const subApp = service;
|
102 | const isSubApp = typeof subApp.service === 'function' && subApp.services;
|
103 | if (isSubApp) {
|
104 | Object.keys(subApp.services).forEach((subPath) => this.use(`${location}/${subPath}`, subApp.service(subPath)));
|
105 | return this;
|
106 | }
|
107 | const protoService = (0, service_1.wrapService)(location, service, options);
|
108 | const serviceOptions = (0, service_1.getServiceOptions)(protoService);
|
109 | for (const name of service_1.protectedMethods) {
|
110 | if (serviceOptions.methods.includes(name)) {
|
111 | throw new Error(`'${name}' on service '${location}' is not allowed as a custom method name`);
|
112 | }
|
113 | }
|
114 | debug(`Registering new service at \`${location}\``);
|
115 |
|
116 | this.mixins.forEach((fn) => fn.call(this, protoService, location, serviceOptions));
|
117 | this.services[location] = protoService;
|
118 |
|
119 | if (this._isSetup && typeof protoService.setup === 'function') {
|
120 | debug(`Setting up service for \`${location}\``);
|
121 | protoService.setup(this, location);
|
122 | }
|
123 | return this;
|
124 | }
|
125 | async unuse(location) {
|
126 | const path = ((0, commons_1.stripSlashes)(location) || '/');
|
127 | const service = this.services[path];
|
128 | if (service && typeof service.teardown === 'function') {
|
129 | await service.teardown(this, path);
|
130 | }
|
131 | delete this.services[path];
|
132 | return service;
|
133 | }
|
134 | hooks(hookMap) {
|
135 | const untypedMap = hookMap;
|
136 | if (untypedMap.before || untypedMap.after || untypedMap.error || untypedMap.around) {
|
137 |
|
138 | this.registerHooks(untypedMap);
|
139 | }
|
140 | else if (untypedMap.setup || untypedMap.teardown) {
|
141 |
|
142 | (0, hooks_1.hooks)(this, untypedMap);
|
143 | }
|
144 | else {
|
145 |
|
146 | this.registerHooks({
|
147 | around: untypedMap
|
148 | });
|
149 | }
|
150 | return this;
|
151 | }
|
152 | }
|
153 | exports.Feathers = Feathers;
|
154 |
|
\ | No newline at end of file |