-
-
Notifications
You must be signed in to change notification settings - Fork 743
/
service.ts
81 lines (65 loc) · 1.97 KB
/
service.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
// DO NOT MODIFY - generated from packages/feathers/src/service.ts
import { createSymbol } from './dependencies.ts';
import { ServiceOptions } from './declarations.ts';
export const SERVICE = createSymbol('@feathersjs/service');
export const defaultServiceArguments = {
find: [ 'params' ],
get: [ 'id', 'params' ],
create: [ 'data', 'params' ],
update: [ 'id', 'data', 'params' ],
patch: [ 'id', 'data', 'params' ],
remove: [ 'id', 'params' ]
}
export const defaultServiceMethods = Object.keys(defaultServiceArguments);
export const defaultEventMap = {
create: 'created',
update: 'updated',
patch: 'patched',
remove: 'removed'
}
export function getHookMethods (service: any, options: ServiceOptions) {
const { methods } = options;
return defaultServiceMethods.filter(m =>
typeof service[m] === 'function' && !methods.includes(m)
).concat(methods);
}
export function getServiceOptions (
service: any, options: ServiceOptions = {}
): ServiceOptions {
const existingOptions = service[SERVICE];
if (existingOptions) {
return existingOptions;
}
const {
methods = defaultServiceMethods.filter(method =>
typeof service[method] === 'function'
),
events = service.events || []
} = options;
const {
serviceEvents = Object.values(defaultEventMap).concat(events)
} = options;
return {
...options,
events,
methods,
serviceEvents
};
}
export function wrapService (
location: string, service: any, options: ServiceOptions
) {
// Do nothing if this is already an initialized
if (service[SERVICE]) {
return service;
}
const protoService = Object.create(service);
const serviceOptions = getServiceOptions(service, options);
if (Object.keys(serviceOptions.methods).length === 0 && typeof service.setup !== 'function') {
throw new Error(`Invalid service object passed for path \`${location}\``);
}
Object.defineProperty(protoService, SERVICE, {
value: serviceOptions
});
return protoService;
}