-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.js
141 lines (119 loc) · 3.33 KB
/
bundle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports"], factory);
} else if (typeof exports !== "undefined") {
factory(exports);
} else {
var mod = {
exports: {}
};
factory(mod.exports);
global.index = mod.exports;
}
})(this, function (_exports) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports.Socket = _exports.REST = void 0;
const {
services,
path
} = require('./services.json');
const URL = require('url-parse');
const axios = require('axios');
const io = require('socket.io-client');
const EventEmitter = require('events').EventEmitter;
class CommonSDK extends EventEmitter {
constructor(ip, transport) {
super();
let url = new URL(ip);
this.ip = url.host; // Iterate over the services we have defined
Object.keys(services).forEach(serviceName => {
let Service = services[serviceName]; // Create an object on the SDK for every service
this[serviceName] = {}; // For every method on the Service, create a function
Object.keys(Service).forEach(methodName => {
let method = Service[methodName];
let {
route,
restMethod,
args
} = method;
this[serviceName][methodName] = async ({
id,
data
} = {}) => {
if (args.includes('id') && id === undefined) throw Error("This method requires an ID parameter");
if (args.includes('data') && data === undefined) throw Error("This method requires a data object");
this.emit('request', {
serviceName,
methodName,
id,
data,
eventName: method.eventName
});
let response = await transport(method, {
id,
data
});
if (response.code) {
this.emit('error', response);
console.error(response);
} else {
this.emit('response', response);
}
return response;
};
});
});
}
}
class REST extends CommonSDK {
constructor(ip) {
super(ip, async (method, {
id,
data
} = {}) => {
let {
restMethod
} = method;
let url = `http://${this.ip}${method.route.replace(':id', id)}`;
let result = await axios({
method: restMethod,
url,
data,
validateStatus: function (status) {
return status < 500; // Reject only if the status code is greater than or equal to 500
}
});
if (result.data.code) {
throw result.data;
}
return result.data;
});
}
}
_exports.REST = REST;
class Socket extends CommonSDK {
constructor(ip) {
super(ip, (method, {
id,
data
} = {}) => {
return new Promise((resolve, reject) => {
this.socket.emit(method.eventName, {
id,
data
}, response => {
resolve(response);
});
});
});
let io_client = io.connect(`http://${this.ip}`);
Object.assign(this, {
socket: io_client
});
}
}
_exports.Socket = Socket;
});