1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Service = void 0;
|
4 |
|
5 | const errors_1 = require("@feathersjs/errors");
|
6 | const commons_1 = require("@feathersjs/commons");
|
7 | const debug = (0, commons_1.createDebug)('@feathersjs/transport-commons/client');
|
8 | const 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 | ];
|
22 | const otherEmitterMethods = ['eventNames', 'getMaxListeners', 'setMaxListeners'];
|
23 | const 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 |
|
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 | };
|
45 | class 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 |
|
90 |
|
91 |
|
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 |
|
99 | return this.removeAllListeners(name);
|
100 | }
|
101 |
|
102 | return this.removeListener(name, ...args);
|
103 | }
|
104 | }
|
105 | exports.Service = Service;
|
106 |
|
\ | No newline at end of file |