@prisme.ai/sdk 0.0.2 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var _tslib = require('../../_virtual/_tslib.js');
5
6
  var io = require('socket.io-client');
6
7
 
7
8
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
@@ -9,14 +10,50 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
9
10
  var io__default = /*#__PURE__*/_interopDefaultLegacy(io);
10
11
 
11
12
  var Events = /** @class */ (function () {
12
- function Events(workspaceId, token, apiHost) {
13
- if (apiHost === void 0) { apiHost = 'https://api.eda.prisme.ai'; }
13
+ function Events(_a) {
14
+ var workspaceId = _a.workspaceId, token = _a.token, apiKey = _a.apiKey, _b = _a.apiHost, apiHost = _b === void 0 ? 'https://api.eda.prisme.ai' : _b, filters = _a.filters, api = _a.api;
15
+ var _this = this;
16
+ this.listeners = new Map();
14
17
  this.workspaceId = workspaceId;
15
- this.client = io__default["default"]("".concat(apiHost, "/workspaces/").concat(workspaceId, "/events"), {
16
- extraHeaders: {
17
- 'x-prismeai-session-token': token,
18
- },
18
+ var queryString = new URLSearchParams(filters || {}).toString();
19
+ var fullQueryString = queryString ? "?".concat(queryString) : '';
20
+ var extraHeaders = {
21
+ 'x-prismeai-token': token,
22
+ };
23
+ if (apiKey) {
24
+ extraHeaders['x-prismeai-api-key'] = apiKey;
25
+ }
26
+ this.client = io__default["default"]("".concat(apiHost, "/workspaces/").concat(workspaceId, "/events").concat(fullQueryString), {
27
+ extraHeaders: extraHeaders,
28
+ withCredentials: true,
19
29
  });
30
+ var lastConnectionTime = new Date();
31
+ var onConnect = function () {
32
+ setTimeout(function () { return _tslib.__awaiter(_this, void 0, void 0, function () {
33
+ var events;
34
+ var _this = this;
35
+ return _tslib.__generator(this, function (_a) {
36
+ switch (_a.label) {
37
+ case 0: return [4 /*yield*/, api.getEvents(workspaceId, _tslib.__assign(_tslib.__assign({}, this.filters[this.filters.length - 1]), { afterDate: lastConnectionTime.toISOString() }))];
38
+ case 1:
39
+ events = _a.sent();
40
+ events.reverse().forEach(function (event) {
41
+ (_this.listeners.get(event.type) || []).forEach(function (listener) {
42
+ return listener(event);
43
+ });
44
+ });
45
+ return [2 /*return*/];
46
+ }
47
+ });
48
+ }); }, 2000);
49
+ };
50
+ this.client.on('disconnect', function () {
51
+ lastConnectionTime = new Date();
52
+ _this.client.off('connect', onConnect);
53
+ _this.client.on('connect', onConnect);
54
+ });
55
+ this.filters = [filters || {}];
56
+ this.listenedUserTopics = new Map();
20
57
  }
21
58
  Object.defineProperty(Events.prototype, "socket", {
22
59
  get: function () {
@@ -43,17 +80,46 @@ var Events = /** @class */ (function () {
43
80
  };
44
81
  Events.prototype.on = function (ev, listener) {
45
82
  var _this = this;
83
+ this.listeners.set(ev, _tslib.__spreadArray(_tslib.__spreadArray([], (this.listeners.get(ev) || []), true), [listener]));
46
84
  this.client.on(ev, listener);
47
- return function () { return _this.client.off(ev, listener); };
85
+ return function () {
86
+ _this.listeners.set(ev, (_this.listeners.get(ev) || []).filter(function (l) { return l !== listener; }));
87
+ _this.client.off(ev, listener);
88
+ };
48
89
  };
49
- Events.prototype.emit = function (event, payload) {
90
+ Events.prototype.emit = function (event, payload, options) {
50
91
  this.client.emit('event', {
51
92
  type: event,
52
93
  payload: payload,
94
+ options: options,
53
95
  });
54
96
  };
97
+ Events.prototype.listenTopics = function (_a) {
98
+ var event = _a.event, topics = _a.topics;
99
+ topics = Array.isArray(topics) ? topics : [topics];
100
+ this.listenedUserTopics.set(event, topics);
101
+ this.filters = [
102
+ _tslib.__assign({}, this.filters[0]),
103
+ {
104
+ 'target.userTopic': Array.from(this.listenedUserTopics).flatMap(function (_a) {
105
+ _a[0]; var topics = _a[1];
106
+ return topics;
107
+ }),
108
+ },
109
+ ];
110
+ this.updateFilters({
111
+ payloadQuery: this.filters,
112
+ });
113
+ };
114
+ Events.prototype.updateFilters = function (filters) {
115
+ this.client.emit('filters', filters);
116
+ };
55
117
  Events.prototype.once = function (ev, listener) {
118
+ var _this = this;
56
119
  this.client.once(ev, listener);
120
+ return function () {
121
+ _this.client.off(ev, listener);
122
+ };
57
123
  };
58
124
  Events.prototype.close = function () {
59
125
  this.client.close();
@@ -16,25 +16,42 @@ var headersAsObject = function (headers) {
16
16
  var Fetcher = /** @class */ (function () {
17
17
  function Fetcher(host) {
18
18
  this.token = null;
19
+ this._apiKey = null;
19
20
  this.host = host;
20
21
  }
22
+ Object.defineProperty(Fetcher.prototype, "apiKey", {
23
+ set: function (apiKey) {
24
+ this._apiKey = apiKey;
25
+ },
26
+ enumerable: false,
27
+ configurable: true
28
+ });
29
+ Fetcher.prototype.prepareRequest = function (url, options) {
30
+ if (options === void 0) { options = {}; }
31
+ var headers = new Headers(options.headers || {});
32
+ if (this.token && !headers.has('x-prismeai-token')) {
33
+ headers.append('x-prismeai-token', this.token);
34
+ }
35
+ if (this._apiKey && !headers.has('x-prismeai-apikey')) {
36
+ headers.append('x-prismeai-api-key', this._apiKey);
37
+ }
38
+ if (this.language) {
39
+ headers.append('accept-language', this.language);
40
+ }
41
+ if ((!options.body || !(options.body instanceof FormData)) &&
42
+ !headers.has('Content-Type')) {
43
+ headers.append('Content-Type', 'application/json');
44
+ }
45
+ headers.append('Access-Control-Allow-Origin', '*');
46
+ return global.fetch("".concat(this.host).concat(url), _tslib.__assign(_tslib.__assign({}, options), { headers: headers }));
47
+ };
21
48
  Fetcher.prototype._fetch = function (url, options) {
22
49
  if (options === void 0) { options = {}; }
23
50
  return _tslib.__awaiter(this, void 0, void 0, function () {
24
- var headers, res, error, _a, clone, response;
51
+ var res, error, _a, contentType, response, text;
25
52
  return _tslib.__generator(this, function (_b) {
26
53
  switch (_b.label) {
27
- case 0:
28
- headers = new Headers(options.headers || {});
29
- if (this.token && !headers.has('x-prismeai-session-token')) {
30
- headers.append('x-prismeai-session-token', this.token);
31
- }
32
- if ((!options.body || !(options.body instanceof FormData)) &&
33
- !headers.has('Content-Type')) {
34
- headers.append('Content-Type', 'application/json');
35
- }
36
- headers.append('Access-Control-Allow-Origin', '*');
37
- return [4 /*yield*/, global.fetch("".concat(this.host).concat(url), _tslib.__assign(_tslib.__assign({}, options), { headers: headers }))];
54
+ case 0: return [4 /*yield*/, this.prepareRequest(url, options)];
38
55
  case 1:
39
56
  res = _b.sent();
40
57
  if (!!res.ok) return [3 /*break*/, 6];
@@ -53,10 +70,11 @@ var Fetcher = /** @class */ (function () {
53
70
  return [3 /*break*/, 5];
54
71
  case 5: throw error;
55
72
  case 6:
56
- clone = res.clone();
73
+ contentType = res.headers.get('content-type');
74
+ if (!(contentType && contentType.includes('application/json'))) return [3 /*break*/, 10];
57
75
  _b.label = 7;
58
76
  case 7:
59
- _b.trys.push([7, 9, , 11]);
77
+ _b.trys.push([7, 9, , 10]);
60
78
  return [4 /*yield*/, res.json()];
61
79
  case 8:
62
80
  response = (_b.sent()) || {};
@@ -69,9 +87,17 @@ var Fetcher = /** @class */ (function () {
69
87
  return [2 /*return*/, response];
70
88
  case 9:
71
89
  _b.sent();
72
- return [4 /*yield*/, clone.text()];
73
- case 10: return [2 /*return*/, (_b.sent())];
74
- case 11: return [2 /*return*/];
90
+ return [2 /*return*/, {}];
91
+ case 10: return [4 /*yield*/, res.text()];
92
+ case 11:
93
+ text = _b.sent();
94
+ try {
95
+ return [2 /*return*/, JSON.parse(text)];
96
+ }
97
+ catch (_c) {
98
+ return [2 /*return*/, text];
99
+ }
100
+ return [2 /*return*/];
75
101
  }
76
102
  });
77
103
  });
@@ -95,6 +121,16 @@ var Fetcher = /** @class */ (function () {
95
121
  });
96
122
  });
97
123
  };
124
+ Fetcher.prototype.put = function (url, body) {
125
+ return _tslib.__awaiter(this, void 0, void 0, function () {
126
+ return _tslib.__generator(this, function (_a) {
127
+ return [2 /*return*/, this._fetch(url, {
128
+ method: 'PUT',
129
+ body: JSON.stringify(body),
130
+ })];
131
+ });
132
+ });
133
+ };
98
134
  Fetcher.prototype.patch = function (url, body) {
99
135
  return _tslib.__awaiter(this, void 0, void 0, function () {
100
136
  return _tslib.__generator(this, function (_a) {