@marxlnfcs/wildduck-api
Version:
Provides a client to interact with the wildduck api
46 lines (45 loc) • 1.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SSEClient = exports.createSSEClient = void 0;
const create_http_client_1 = require("./create-http-client");
const lodash_1 = require("lodash");
function createSSEClient(options, httpClient) {
return new SSEClient(options, httpClient);
}
exports.createSSEClient = createSSEClient;
class SSEClient {
constructor(options, http = new create_http_client_1.HttpClient(options)) {
this.options = options;
this.http = http;
}
create(path, params) {
var _a, _b, _c, _d;
const url = this.http.createUrl(path, { params });
const options = {
headers: {
'X-Access-Token': ((_a = this.options) === null || _a === void 0 ? void 0 : _a.accessToken) || '',
},
proxy: this.getProxy(),
https: { rejectUnauthorized: (_b = this.options.rejectUnauthorized) !== null && _b !== void 0 ? _b : true },
rejectUnauthorized: (_c = this.options.rejectUnauthorized) !== null && _c !== void 0 ? _c : true
};
if ((0, lodash_1.isFunction)((_d = this.options) === null || _d === void 0 ? void 0 : _d.onSSECreate)) {
this.options.onSSECreate({
url: url,
sseOptions: options,
});
}
return new EventSource(url, options);
}
getProxy() {
var _a;
if ((0, lodash_1.isNil)(this.options.proxy) || (0, lodash_1.isArray)(this.options.proxy))
return null;
if ((0, lodash_1.isString)(this.options.proxy))
return this.options.proxy;
if ((_a = this.options.proxy) === null || _a === void 0 ? void 0 : _a.url)
return this.options.proxy.url;
return `${this.options.proxy.protocol || 'http'}://${this.options.proxy.host}:${this.options.proxy.port}`;
}
}
exports.SSEClient = SSEClient;