UNPKG

3.86 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Service = void 0;
4/* eslint-disable @typescript-eslint/ban-ts-comment */
5const errors_1 = require("@feathersjs/errors");
6const commons_1 = require("@feathersjs/commons");
7const debug = (0, commons_1.createDebug)('@feathersjs/transport-commons/client');
8const namespacedEmitterMethods = [
9 'addListener',
10 'addEventListener',
11 'emit',
12 'listenerCount',
13 'listeners',
14 'on',
15 'once',
16 'prependListener',
17 'prependOnceListener',
18 'removeAllListeners',
19 'removeEventListener',
20 'removeListener'
21];
22const otherEmitterMethods = ['eventNames', 'getMaxListeners', 'setMaxListeners'];
23const addEmitterMethods = (service) => {
24 otherEmitterMethods.forEach((method) => {
25 service[method] = function (...args) {
26 if (typeof this.connection[method] !== 'function') {
27 throw new Error(`Can not call '${method}' on the client service connection`);
28 }
29 return this.connection[method](...args);
30 };
31 });
32 // Methods that should add the namespace (service path)
33 namespacedEmitterMethods.forEach((method) => {
34 service[method] = function (name, ...args) {
35 if (typeof this.connection[method] !== 'function') {
36 throw new Error(`Can not call '${method}' on the client service connection`);
37 }
38 const eventName = `${this.path} ${name}`;
39 debug(`Calling emitter method ${method} with ` + `namespaced event '${eventName}'`);
40 const result = this.connection[method](eventName, ...args);
41 return result === this.connection ? this : result;
42 };
43 });
44};
45class Service {
46 constructor(options) {
47 this.events = options.events;
48 this.path = options.name;
49 this.connection = options.connection;
50 this.method = options.method;
51 addEmitterMethods(this);
52 }
53 send(method, ...args) {
54 return new Promise((resolve, reject) => {
55 args.unshift(method, this.path);
56 args.push(function (error, data) {
57 return error ? reject((0, errors_1.convert)(error)) : resolve(data);
58 });
59 debug(`Sending socket.${this.method}`, args);
60 this.connection[this.method](...args);
61 });
62 }
63 methods(...names) {
64 names.forEach((name) => {
65 this[name] = function (data, params = {}) {
66 return this.send(name, data, params.query || {});
67 };
68 });
69 return this;
70 }
71 find(params = {}) {
72 return this.send('find', params.query || {});
73 }
74 get(id, params = {}) {
75 return this.send('get', id, params.query || {});
76 }
77 create(data, params = {}) {
78 return this.send('create', data, params.query || {});
79 }
80 update(id, data, params = {}) {
81 return this.send('update', id, data, params.query || {});
82 }
83 patch(id, data, params = {}) {
84 return this.send('patch', id, data, params.query || {});
85 }
86 remove(id, params = {}) {
87 return this.send('remove', id, params.query || {});
88 }
89 // `off` is actually not part of the Node event emitter spec
90 // but we are adding it since everybody is expecting it because
91 // of the emitter-component Socket.io is using
92 off(name, ...args) {
93 if (typeof this.connection.off === 'function') {
94 const result = this.connection.off(`${this.path} ${name}`, ...args);
95 return result === this.connection ? this : result;
96 }
97 else if (args.length === 0) {
98 // @ts-ignore
99 return this.removeAllListeners(name);
100 }
101 // @ts-ignore
102 return this.removeListener(name, ...args);
103 }
104}
105exports.Service = Service;
106//# sourceMappingURL=client.js.map
\No newline at end of file