UNPKG

937 BJavaScriptView Raw
1const Service = require('@feathersjs/transport-commons/client');
2
3function socketioClient (connection, options) {
4 if (!connection) {
5 throw new Error('Socket.io connection needs to be provided');
6 }
7
8 const defaultService = function (name) {
9 const events = Object.keys(this.eventMappings || {})
10 .map(method => this.eventMappings[method]);
11
12 const settings = Object.assign({}, options, {
13 events,
14 name,
15 connection,
16 method: 'emit'
17 });
18
19 return new Service(settings);
20 };
21
22 const initialize = function (app) {
23 if (typeof app.defaultService === 'function') {
24 throw new Error('Only one default client provider can be configured');
25 }
26
27 app.io = connection;
28 app.defaultService = defaultService;
29 };
30
31 initialize.Service = Service;
32 initialize.service = defaultService;
33
34 return initialize;
35}
36
37module.exports = socketioClient;
38module.exports.default = socketioClient;