deno.land / x / feathers@v5.0.0-pre.29 / _feathers / application.ts

application.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// DO NOT MODIFY - generated from packages/feathers/src/application.ts
import version from './version.ts';import { EventEmitter, stripSlashes, createDebug, HOOKS} from './dependencies.ts';import { eventHook, eventMixin } from './events.ts';import { hookMixin } from './hooks/index.ts';import { wrapService, getServiceOptions } from './service.ts';import { FeathersApplication, ServiceMixin, Service, ServiceOptions, ServiceInterface, Application, HookOptions, FeathersService, HookMap, LegacyHookMap} from './declarations.ts';import { enableLegacyHooks } from './hooks/legacy.ts';
const debug = createDebug('@feathersjs/feathers');
export class Feathers<ServiceTypes, AppSettings> extends EventEmitter implements FeathersApplication<ServiceTypes, AppSettings> { services: ServiceTypes = ({} as ServiceTypes); settings: AppSettings = ({} as AppSettings); mixins: ServiceMixin<Application<ServiceTypes, AppSettings>>[] = [ hookMixin, eventMixin ]; version: string = version; _isSetup = false; appHooks: HookMap<Application<ServiceTypes, AppSettings>, any> = { [HOOKS]: [ (eventHook as any) ] };
private legacyHooks: (this: any, allHooks: any) => any;
constructor () { super(); this.legacyHooks = enableLegacyHooks(this); }
get<L extends keyof AppSettings & string> (name: L): AppSettings[L] { return this.settings[name]; }
set<L extends keyof AppSettings & string> (name: L, value: AppSettings[L]) { this.settings[name] = value; return this; }
configure (callback: (this: this, app: this) => void) { callback.call(this, this);
return this; }
defaultService (location: string): ServiceInterface<any> { throw new Error(`Can not find service '${location}'`); }
service<L extends keyof ServiceTypes & string> ( location: L ): FeathersService<this, keyof any extends keyof ServiceTypes ? Service<any> : ServiceTypes[L]> { const path = (stripSlashes(location) || '/') as L; const current = this.services[path];
if (typeof current === 'undefined') { this.use(path, this.defaultService(path) as any); return this.service(path); }
return current as any; }
use<L extends keyof ServiceTypes & string> ( path: L, service: keyof any extends keyof ServiceTypes ? ServiceInterface<any> | Application : ServiceTypes[L], options?: ServiceOptions ): this { if (typeof path !== 'string') { throw new Error(`'${path}' is not a valid service path.`); }
const location = (stripSlashes(path) || '/') as L; const subApp = service as Application; const isSubApp = typeof subApp.service === 'function' && subApp.services;
if (isSubApp) { Object.keys(subApp.services).forEach(subPath => this.use(`${location}/${subPath}` as any, subApp.service(subPath) as any) );
return this; }
const protoService = wrapService(location, service, options); const serviceOptions = getServiceOptions(service, options);
debug(`Registering new service at \`${location}\``);
// Add all the mixins this.mixins.forEach(fn => fn.call(this, protoService, location, serviceOptions));
// If we ran setup already, set this service up explicitly, this will not `await` if (this._isSetup && typeof protoService.setup === 'function') { debug(`Setting up service for \`${location}\``); protoService.setup(this, location); }
this.services[location] = protoService;
return this; }
hooks (hookMap: HookOptions<this, any>) { const legacyMap = hookMap as LegacyHookMap<this, any>;
if (legacyMap.before || legacyMap.after || legacyMap.error) { return this.legacyHooks(legacyMap); }
if (Array.isArray(hookMap)) { this.appHooks[HOOKS].push(...hookMap as any); } else { const methodHookMap = hookMap as HookMap<Application<ServiceTypes, AppSettings>, any>;
Object.keys(methodHookMap).forEach(key => { const methodHooks = this.appHooks[key] || [];
this.appHooks[key] = methodHooks.concat(methodHookMap[key]); }); }
return this; }
setup () { let promise = Promise.resolve();
// Setup each service (pass the app so that they can look up other services etc.) for (const path of Object.keys(this.services)) { promise = promise.then(() => { const service: any = this.service(path as any); if (typeof service.setup === 'function') { debug(`Setting up service for \`${path}\``); return service.setup(this, path); } }); }
return promise.then(() => { this._isSetup = true; return this; }); }}
feathers
The API and real-time application framework
DocumentationDocumentation
GitHub Repositoryfeathersjs/feathers
GitHub Stars
14347

Version Info

Tagged at
4 months ago

External Dependencies

1 external dependency