UNPKG

2.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.normalizeServiceOptions = exports.protectedMethods = exports.defaultServiceEvents = exports.defaultEventMap = exports.defaultServiceMethods = exports.defaultServiceArguments = exports.SERVICE = void 0;
4exports.getHookMethods = getHookMethods;
5exports.getServiceOptions = getServiceOptions;
6exports.wrapService = wrapService;
7const events_1 = require("events");
8const commons_1 = require("@feathersjs/commons");
9exports.SERVICE = (0, commons_1.createSymbol)('@feathersjs/service');
10exports.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};
18exports.defaultServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'];
19exports.defaultEventMap = {
20 create: 'created',
21 update: 'updated',
22 patch: 'patched',
23 remove: 'removed'
24};
25exports.defaultServiceEvents = Object.values(exports.defaultEventMap);
26exports.protectedMethods = Object.keys(Object.prototype)
27 .concat(Object.keys(events_1.EventEmitter.prototype))
28 .concat(['all', 'around', 'before', 'after', 'error', 'hooks', 'setup', 'teardown', 'publish']);
29function 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}
35function getServiceOptions(service) {
36 return service[exports.SERVICE];
37}
38const 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};
48exports.normalizeServiceOptions = normalizeServiceOptions;
49function wrapService(location, service, options) {
50 // Do nothing if this is already an initialized
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//# sourceMappingURL=service.js.map
\No newline at end of file