/
client.js
219 lines (177 loc) · 6.49 KB
/
client.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _request = require('./request');
var _errors = require('./errors');
var _utils = require('./utils');
var _url = require('url');
var _url2 = _interopRequireDefault(_url);
var _assign = require('lodash/assign');
var _assign2 = _interopRequireDefault(_assign);
var _isString = require('lodash/isString');
var _isString2 = _interopRequireDefault(_isString);
var _isNumber = require('lodash/isNumber');
var _isNumber2 = _interopRequireDefault(_isNumber);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var defaultTimeout = process && process.env && process.env.KINVEY_DEFAULT_TIMEOUT || '60000' || 60000;
var _sharedInstance = null;
var Client = function () {
function Client() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Client);
options = (0, _assign2.default)({
apiHostname: 'https://baas.kinvey.com',
micHostname: 'https://auth.kinvey.com',
liveServiceHostname: 'https://kls.kinvey.com'
}, options);
if (options.apiHostname && (0, _isString2.default)(options.apiHostname)) {
var apiHostnameParsed = _url2.default.parse(options.apiHostname);
options.apiProtocol = apiHostnameParsed.protocol || 'https:';
options.apiHost = apiHostnameParsed.host;
}
if (options.micHostname && (0, _isString2.default)(options.micHostname)) {
var micHostnameParsed = _url2.default.parse(options.micHostname);
options.micProtocol = micHostnameParsed.protocol || 'https:';
options.micHost = micHostnameParsed.host;
}
if (options.liveServiceHostname && (0, _isString2.default)(options.liveServiceHostname)) {
var liveServiceHostnameParsed = _url2.default.parse(options.liveServiceHostname);
options.liveServiceProtocol = liveServiceHostnameParsed.protocol || 'https:';
options.liveServiceHost = liveServiceHostnameParsed.host;
}
this.apiProtocol = options.apiProtocol;
this.apiHost = options.apiHost;
this.micProtocol = options.micProtocol;
this.micHost = options.micHost;
this.liveServiceProtocol = options.liveServiceProtocol;
this.liveServiceHost = options.liveServiceHost;
this.appKey = options.appKey;
this.appSecret = options.appSecret;
this.masterSecret = options.masterSecret;
this.encryptionKey = options.encryptionKey;
this.appVersion = options.appVersion;
this.defaultTimeout = (0, _utils.isDefined)(options.defaultTimeout) ? options.defaultTimeout : defaultTimeout;
}
_createClass(Client, [{
key: 'toPlainObject',
value: function toPlainObject() {
return {
apiHostname: this.apiHostname,
apiProtocol: this.apiProtocol,
apiHost: this.apiHost,
micHostname: this.micHostname,
micProtocol: this.micProtocol,
micHost: this.micHost,
liveServiceHostname: this.liveServiceHostname,
liveServiceHost: this.liveServiceHost,
liveServiceProtocol: this.liveServiceProtocol,
appKey: this.appKey,
appSecret: this.appSecret,
masterSecret: this.masterSecret,
encryptionKey: this.encryptionKey,
appVersion: this.appVersion
};
}
}, {
key: 'activeUser',
get: function get() {
return _request.CacheRequest.getActiveUser(this);
}
}, {
key: 'apiHostname',
get: function get() {
return _url2.default.format({
protocol: this.apiProtocol,
host: this.apiHost
});
}
}, {
key: 'baseUrl',
get: function get() {
return this.apiHostname;
}
}, {
key: 'protocol',
get: function get() {
return this.apiProtocol;
}
}, {
key: 'host',
get: function get() {
return this.apiHost;
}
}, {
key: 'micHostname',
get: function get() {
return _url2.default.format({
protocol: this.micProtocol,
host: this.micHost
});
}
}, {
key: 'liveServiceHostname',
get: function get() {
return _url2.default.format({
protocol: this.liveServiceProtocol,
host: this.liveServiceHost
});
}
}, {
key: 'appVersion',
get: function get() {
return this._appVersion;
},
set: function set(appVersion) {
if (appVersion && !(0, _isString2.default)(appVersion)) {
appVersion = String(appVersion);
}
this._appVersion = appVersion;
}
}, {
key: 'defaultTimeout',
get: function get() {
return this._defaultTimeout;
},
set: function set(timeout) {
timeout = parseInt(timeout, 10);
if ((0, _isNumber2.default)(timeout) === false || isNaN(timeout)) {
throw new _errors.KinveyError('Invalid timeout. Timeout must be a number.');
}
if (timeout < 0) {
_utils.Log.info('Default timeout is less than 0. Setting default timeout to ' + defaultTimeout + 'ms.');
timeout = defaultTimeout;
}
this._defaultTimeout = timeout;
}
}], [{
key: 'init',
value: function init(options) {
var client = new Client(options);
_sharedInstance = client;
_request.CacheRequest.loadActiveUserLegacy(client);
return client;
}
}, {
key: 'initialize',
value: function initialize(options) {
var client = new Client(options);
_sharedInstance = client;
return _request.CacheRequest.loadActiveUser(client).then(function () {
return client;
});
}
}, {
key: 'sharedInstance',
value: function sharedInstance() {
if (!_sharedInstance) {
throw new _errors.KinveyError('You have not initialized the library. ' + 'Please call Kinvey.init() to initialize the library.');
}
return _sharedInstance;
}
}]);
return Client;
}();
exports.default = Client;