1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.normalizeServiceOptions = exports.protectedMethods = exports.defaultServiceEvents = exports.defaultEventMap = exports.defaultServiceMethods = exports.defaultServiceArguments = exports.SERVICE = void 0;
|
4 | exports.getHookMethods = getHookMethods;
|
5 | exports.getServiceOptions = getServiceOptions;
|
6 | exports.wrapService = wrapService;
|
7 | const events_1 = require("events");
|
8 | const commons_1 = require("@feathersjs/commons");
|
9 | exports.SERVICE = (0, commons_1.createSymbol)('@feathersjs/service');
|
10 | exports.defaultServiceArguments = {
|
11 | find: ['params'],
|
12 | get: ['id', 'params'],
|
13 | create: ['data', 'params'],
|
14 | update: ['id', 'data', 'params'],
|
15 | patch: ['id', 'data', 'params'],
|
16 | remove: ['id', 'params']
|
17 | };
|
18 | exports.defaultServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'];
|
19 | exports.defaultEventMap = {
|
20 | create: 'created',
|
21 | update: 'updated',
|
22 | patch: 'patched',
|
23 | remove: 'removed'
|
24 | };
|
25 | exports.defaultServiceEvents = Object.values(exports.defaultEventMap);
|
26 | exports.protectedMethods = Object.keys(Object.prototype)
|
27 | .concat(Object.keys(events_1.EventEmitter.prototype))
|
28 | .concat(['all', 'around', 'before', 'after', 'error', 'hooks', 'setup', 'teardown', 'publish']);
|
29 | function getHookMethods(service, options) {
|
30 | const { methods } = options;
|
31 | return exports.defaultServiceMethods
|
32 | .filter((m) => typeof service[m] === 'function' && !methods.includes(m))
|
33 | .concat(methods);
|
34 | }
|
35 | function getServiceOptions(service) {
|
36 | return service[exports.SERVICE];
|
37 | }
|
38 | const normalizeServiceOptions = (service, options = {}) => {
|
39 | const { methods = exports.defaultServiceMethods.filter((method) => typeof service[method] === 'function'), events = service.events || [] } = options;
|
40 | const serviceEvents = options.serviceEvents || exports.defaultServiceEvents.concat(events);
|
41 | return {
|
42 | ...options,
|
43 | events,
|
44 | methods,
|
45 | serviceEvents
|
46 | };
|
47 | };
|
48 | exports.normalizeServiceOptions = normalizeServiceOptions;
|
49 | function wrapService(location, service, options) {
|
50 |
|
51 | if (service[exports.SERVICE]) {
|
52 | return service;
|
53 | }
|
54 | const protoService = Object.create(service);
|
55 | const serviceOptions = (0, exports.normalizeServiceOptions)(service, options);
|
56 | if (Object.keys(serviceOptions.methods).length === 0 &&
|
57 | ![...exports.defaultServiceMethods, 'setup', 'teardown'].some((method) => typeof service[method] === 'function')) {
|
58 | throw new Error(`Invalid service object passed for path \`${location}\``);
|
59 | }
|
60 | Object.defineProperty(protoService, exports.SERVICE, {
|
61 | value: serviceOptions
|
62 | });
|
63 | return protoService;
|
64 | }
|
65 |
|
\ | No newline at end of file |