@octostar/platform-api 0.5.10 → 0.5.12

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.
package/dist/index.d.ts CHANGED
@@ -8,4 +8,6 @@ export * from './lib/JWTProvider';
8
8
  export * from './lib/AIClientImpl';
9
9
  export * from './lib/Proxy';
10
10
  export * from './lib/userWorkspace';
11
+ export * from './lib/ContextAPI';
12
+ export * from './lib/classInstanceToObject';
11
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -8,4 +8,6 @@ export * from './lib/JWTProvider';
8
8
  export * from './lib/AIClientImpl';
9
9
  export * from './lib/Proxy';
10
10
  export * from './lib/userWorkspace';
11
+ export * from './lib/ContextAPI';
12
+ export * from './lib/classInstanceToObject';
11
13
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,aAAa,CAAC;AAC5B,cAAc,qBAAqB,CAAC;AACpC,cAAc,kBAAkB,CAAC;AACjC,cAAc,6BAA6B,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { Context, ContextAPISetParams, ContextAPIStaticData, ContextListener } from '@octostar/platform-types';
2
+ type ContextAPIContructorParams<ContextData extends Record<string, any>> = ContextAPIStaticData<ContextData> & {
3
+ id?: string;
4
+ };
5
+ export declare class ContextAPI<ContextData extends Record<string, any>> implements Context<ContextData> {
6
+ readonly static_data: ContextAPIStaticData<ContextData>;
7
+ private channel;
8
+ private listeners;
9
+ private id;
10
+ constructor({ id, ...static_data }: ContextAPIContructorParams<ContextData>);
11
+ get context(): ContextData;
12
+ set context(context: ContextData);
13
+ get channelId(): string | undefined;
14
+ set channelId(channelId: string | undefined);
15
+ getContext(): ContextData;
16
+ setContext(context: ContextAPISetParams<ContextData>): void;
17
+ notifyListeners(): void;
18
+ syncContextToChannel(): void;
19
+ subscribeToChanges(contextListener: ContextListener<ContextData>): string;
20
+ subscribeToChanges(id: string, contextListener: ContextListener<ContextData>): string;
21
+ unsubscribeFromChanges(id: string): void;
22
+ }
23
+ export {};
24
+ //# sourceMappingURL=ContextAPI.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextAPI.d.ts","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,mBAAmB,EAAqB,oBAAoB,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAIlI,KAAK,0BAA0B,CAAC,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,oBAAoB,CAAC,WAAW,CAAC,GAAG;IAC7G,EAAE,CAAC,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,qBAAa,UAAU,CAAC,WAAW,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAE,YAAW,OAAO,CAAC,WAAW,CAAC;IAC9F,QAAQ,CAAC,WAAW,EAAE,oBAAoB,CAAC,WAAW,CAAC,CAAA;IAEvD,OAAO,CAAC,OAAO,CAA+B;IAE9C,OAAO,CAAC,SAAS,CAA+C;IAEhE,OAAO,CAAC,EAAE,CAAS;gBAEP,EAAE,EAAE,EAAE,GAAG,WAAW,EAAE,EAAE,0BAA0B,CAAC,WAAW,CAAC;IAO3E,IAAI,OAAO,IAIU,WAAW,CAF/B;IAED,IAAI,OAAO,CAAC,OAAO,EAAE,WAAW,EAK/B;IAED,IAAI,SAAS,IAIc,MAAM,GAAG,SAAS,CAF5C;IAEC,IAAI,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,EAe1C;IAEH,UAAU;IAIV,UAAU,CAAC,OAAO,EAAE,mBAAmB,CAAC,WAAW,CAAC;IAKpD,eAAe;IAIf,oBAAoB;IAapB,kBAAkB,CAAC,eAAe,EAAE,eAAe,CAAC,WAAW,CAAC,GAAG,MAAM;IAEzE,kBAAkB,CAChB,EAAE,EAAE,MAAM,EACV,eAAe,EAAE,eAAe,CAAC,WAAW,CAAC,GAC5C,MAAM;IAqBT,sBAAsB,CAAC,EAAE,EAAE,MAAM;CAGlC"}
@@ -0,0 +1,69 @@
1
+ import { v4 as uuidv4 } from 'uuid';
2
+ import { classInstanceToObject } from './classInstanceToObject';
3
+ export class ContextAPI {
4
+ constructor({ id, ...static_data }) {
5
+ this.static_data = static_data;
6
+ this.listeners = {};
7
+ this.id = id ?? uuidv4();
8
+ this.channelId = static_data.channelId;
9
+ }
10
+ get context() {
11
+ return this.static_data.context;
12
+ }
13
+ set context(context) {
14
+ this.static_data.context = context;
15
+ this.notifyListeners();
16
+ this.syncContextToChannel();
17
+ }
18
+ get channelId() {
19
+ return this.static_data.channelId;
20
+ }
21
+ set channelId(channelId) {
22
+ this.static_data.channelId = channelId;
23
+ const contextChannelId = this.static_data.channelId && `ContextAPI-${this.static_data.channelId}`;
24
+ if (!contextChannelId) {
25
+ return;
26
+ }
27
+ this.channel = new BroadcastChannel(contextChannelId);
28
+ this.channel.onmessage = (event) => {
29
+ if (event.data.from !== this.id) {
30
+ this.static_data.context = event.data.context;
31
+ }
32
+ };
33
+ }
34
+ getContext() {
35
+ return this.context;
36
+ }
37
+ setContext(context) {
38
+ this.context =
39
+ typeof context === 'function' ? context(this.context) : context;
40
+ }
41
+ notifyListeners() {
42
+ Object.values(this.listeners).forEach(listener => listener(this.context));
43
+ }
44
+ syncContextToChannel() {
45
+ if (!this.channel) {
46
+ return;
47
+ }
48
+ const context = classInstanceToObject(this.context);
49
+ this.channel.postMessage({
50
+ from: this.id,
51
+ context,
52
+ });
53
+ }
54
+ subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
55
+ const id = typeof idOrContextListener === 'string' ? idOrContextListener : uuidv4();
56
+ const contextListener = typeof idOrContextListener === 'function'
57
+ ? idOrContextListener
58
+ : contextListenerOrNothing;
59
+ if (!contextListener) {
60
+ throw new Error('Context listener is required');
61
+ }
62
+ this.listeners[id] = contextListener;
63
+ return id;
64
+ }
65
+ unsubscribeFromChanges(id) {
66
+ delete this.listeners[id];
67
+ }
68
+ }
69
+ //# sourceMappingURL=ContextAPI.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ContextAPI.js","sourceRoot":"","sources":["../../src/lib/ContextAPI.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AACpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAMhE,MAAM,OAAO,UAAU;IASrB,YAAY,EAAE,EAAE,EAAE,GAAG,WAAW,EAA2C;QACzE,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAC9B,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAI,CAAC,EAAE,GAAG,EAAE,IAAI,MAAM,EAAE,CAAC;QACzB,IAAI,CAAC,SAAS,GAAG,WAAW,CAAC,SAAS,CAAC;IACzC,CAAC;IAED,IAAI,OAAO;QACT,OAAO,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC;IAClC,CAAC;IAED,IAAI,OAAO,CAAC,OAAoB;QAC9B,IAAI,CAAC,WAAW,CAAC,OAAO,GAAG,OAAO,CAAC;QAEnC,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,IAAI,CAAC,oBAAoB,EAAE,CAAC;IAC9B,CAAC;IAED,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC;IACpC,CAAC;IAEC,IAAI,SAAS,CAAC,SAA6B;QACzC,IAAI,CAAC,WAAW,CAAC,SAAS,GAAG,SAAS,CAAC;QACvC,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,CAAC,SAAS,IAAI,cAAc,IAAI,CAAC,WAAW,CAAC,SAAS,EAAE,CAAC;QAClG,IAAI,CAAC,gBAAgB,EAAE,CAAC;YACtB,OAAO;QACT,CAAC;QACD,IAAI,CAAC,OAAO,GAAG,IAAI,gBAAgB,CAAC,gBA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AI,CAAC,eAAe,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;QAClD,CAAC;QAED,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,GAAG,eAAe,CAAC;QACrC,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,sBAAsB,CAAC,EAAU;QAC/B,OAAO,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,CAAC;IAC5B,CAAC;CACF"}
@@ -0,0 +1,12 @@
1
+ export declare const classInstanceToObject: <T extends {
2
+ [key: string]: any;
3
+ }>(instance: T, options?: {
4
+ globals?: {
5
+ canCallAction?: (prams: {
6
+ method: string;
7
+ class: string;
8
+ }) => boolean;
9
+ };
10
+ methodWrappers?: { [K in keyof T]?: (fn: T[K], ...args: T[K] extends (...args: infer P) => any ? P : never) => T[K] extends (...args: any) => infer R ? R : never; };
11
+ }) => T;
12
+ //# sourceMappingURL=classInstanceToObject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classInstanceToObject.d.ts","sourceRoot":"","sources":["../../src/lib/classInstanceToObject.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,EACpE,UAAU,CAAC,EACX,UAAU;IACR,OAAO,CAAC,EAAE;QACR,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE;YAAE,MAAM,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,KAAK,OAAO,CAAC;KACvE,CAAC;IACF,cAAc,CAAC,EAAE,GACd,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CACf,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,EACR,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,MAAM,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,KAAK,KACxD,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,KAAK,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,GACxD,CAAC;CACH,KACA,CAoDF,CAAC"}
@@ -0,0 +1,45 @@
1
+ export const classInstanceToObject = (instance, options) => {
2
+ const obj = {};
3
+ // const prototype = Object.getPrototypeOf(instance);
4
+ const properties = [
5
+ // ...Object.getOwnPropertyNames(instance),
6
+ // ...Object.getOwnPropertyNames(prototype),
7
+ ];
8
+ let currentClass = instance;
9
+ while (currentClass && currentClass !== Object) {
10
+ // console.log('current class', currentClass); // TODO: test if it omits the Object methods
11
+ properties.push(...Object.getOwnPropertyNames(currentClass));
12
+ currentClass = Object.getPrototypeOf(currentClass);
13
+ }
14
+ properties.forEach(property => {
15
+ if (['constructor'].includes(property)) {
16
+ return;
17
+ }
18
+ let method = instance[property];
19
+ method =
20
+ typeof method === 'function' && typeof method.bind === 'function'
21
+ ? method.bind(instance)
22
+ : method;
23
+ if (options?.globals?.canCallAction) {
24
+ const originalMethod = method;
25
+ method = (...args) => {
26
+ const canCall = options.globals.canCallAction({
27
+ method: method.name,
28
+ class: instance.constructor.name,
29
+ });
30
+ if (!canCall) {
31
+ throw new Error(`No permissions for call: ${method.name}`); // TODO: consider usage of a specific permissions error
32
+ }
33
+ return originalMethod(...args);
34
+ };
35
+ }
36
+ if (typeof options?.methodWrappers?.[property] === 'function') {
37
+ obj[property] = (...args) => options.methodWrappers[property](method, ...args);
38
+ }
39
+ else {
40
+ obj[property] = (...args) => method(...args);
41
+ }
42
+ });
43
+ return obj;
44
+ };
45
+ //# sourceMappingURL=classInstanceToObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"classInstanceToObject.js","sourceRoot":"","sources":["../../src/lib/classInstanceToObject.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,qBAAqB,GAAG,CACnC,QAAW,EACX,OAUC,EACE,EAAE;IACL,MAAM,GAAG,GAA4B,EAAE,CAAC;I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CAAC,CAAC;QACJ,CAAC;QAED,IAAI,OAAO,OAAO,EAAE,cAAc,EAAE,CAAC,QAAQ,CAAC,KAAK,UAAU,EAAE,CAAC;YAC9D,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAS,EAAE,EAAE,CAC/B,OAAQ,CAAC,cAAe,CAAC,QAAQ,CAAE,CAAC,MAAa,EAAE,GAAG,IAAI,CAAC,CAAC;QAChE,CAAC;aAAM,CAAC;YACN,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,IAAS,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC;QACpD,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,GAAQ,CAAC;AAClB,CAAC,CAAC"}
@@ -6684,7 +6684,7 @@ var forwardCallsToFutureObject = exports.forwardCallsToFutureObject = function f
6684
6684
  });
6685
6685
  };
6686
6686
 
6687
- },{"./constants":33,"./utils":43,"post-robot":5}],35:[function(require,module,exports){
6687
+ },{"./constants":33,"./utils":45,"post-robot":5}],35:[function(require,module,exports){
6688
6688
  "use strict";
6689
6689
 
6690
6690
  Object.defineProperty(exports, "__esModule", {
@@ -6800,8 +6800,30 @@ Object.keys(_userWorkspace).forEach(function (key) {
6800
6800
  }
6801
6801
  });
6802
6802
  });
6803
+ var _ContextAPI = require("./lib/ContextAPI");
6804
+ Object.keys(_ContextAPI).forEach(function (key) {
6805
+ if (key === "default" || key === "__esModule") return;
6806
+ if (key in exports && exports[key] === _ContextAPI[key]) return;
6807
+ Object.defineProperty(exports, key, {
6808
+ enumerable: true,
6809
+ get: function get() {
6810
+ return _ContextAPI[key];
6811
+ }
6812
+ });
6813
+ });
6814
+ var _classInstanceToObject = require("./lib/classInstanceToObject");
6815
+ Object.keys(_classInstanceToObject).forEach(function (key) {
6816
+ if (key === "default" || key === "__esModule") return;
6817
+ if (key in exports && exports[key] === _classInstanceToObject[key]) return;
6818
+ Object.defineProperty(exports, key, {
6819
+ enumerable: true,
6820
+ get: function get() {
6821
+ return _classInstanceToObject[key];
6822
+ }
6823
+ });
6824
+ });
6803
6825
 
6804
- },{"./apis":22,"./components":32,"./constants":33,"./lib/AIClientImpl":36,"./lib/JWTProvider":38,"./lib/Proxy":39,"./lib/userWorkspace":40,"./permissions":41,"./types":42,"./utils":43}],36:[function(require,module,exports){
6826
+ },{"./apis":22,"./components":32,"./constants":33,"./lib/AIClientImpl":36,"./lib/ContextAPI":38,"./lib/JWTProvider":39,"./lib/Proxy":40,"./lib/classInstanceToObject":41,"./lib/userWorkspace":42,"./permissions":43,"./types":44,"./utils":45}],36:[function(require,module,exports){
6805
6827
  "use strict";
6806
6828
 
6807
6829
  Object.defineProperty(exports, "__esModule", {
@@ -7051,6 +7073,112 @@ AsyncServiceLocator.instances = {};
7051
7073
  },{}],38:[function(require,module,exports){
7052
7074
  "use strict";
7053
7075
 
7076
+ Object.defineProperty(exports, "__esModule", {
7077
+ value: true
7078
+ });
7079
+ exports.ContextAPI = void 0;
7080
+ var _uuid = require("uuid");
7081
+ var _classInstanceToObject = require("./classInstanceToObject");
7082
+ var _excluded = ["id"];
7083
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
7084
+ function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
7085
+ function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
7086
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
7087
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
7088
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
7089
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
7090
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7091
+ var ContextAPI = exports.ContextAPI = /*#__PURE__*/function () {
7092
+ function ContextAPI(_ref) {
7093
+ var id = _ref.id,
7094
+ static_data = _objectWithoutProperties(_ref, _excluded);
7095
+ _classCallCheck(this, ContextAPI);
7096
+ this.static_data = static_data;
7097
+ this.listeners = {};
7098
+ this.id = id !== null && id !== void 0 ? id : (0, _uuid.v4)();
7099
+ this.channelId = static_data.channelId;
7100
+ }
7101
+ return _createClass(ContextAPI, [{
7102
+ key: "context",
7103
+ get: function get() {
7104
+ return this.static_data.context;
7105
+ },
7106
+ set: function set(context) {
7107
+ this.static_data.context = context;
7108
+ this.notifyListeners();
7109
+ this.syncContextToChannel();
7110
+ }
7111
+ }, {
7112
+ key: "channelId",
7113
+ get: function get() {
7114
+ return this.static_data.channelId;
7115
+ },
7116
+ set: function set(channelId) {
7117
+ var _this = this;
7118
+ this.static_data.channelId = channelId;
7119
+ var contextChannelId = this.static_data.channelId && "ContextAPI-".concat(this.static_data.channelId);
7120
+ if (!contextChannelId) {
7121
+ return;
7122
+ }
7123
+ this.channel = new BroadcastChannel(contextChannelId);
7124
+ this.channel.onmessage = function (event) {
7125
+ if (event.data.from !== _this.id) {
7126
+ _this.static_data.context = event.data.context;
7127
+ }
7128
+ };
7129
+ }
7130
+ }, {
7131
+ key: "getContext",
7132
+ value: function getContext() {
7133
+ return this.context;
7134
+ }
7135
+ }, {
7136
+ key: "setContext",
7137
+ value: function setContext(context) {
7138
+ this.context = typeof context === 'function' ? context(this.context) : context;
7139
+ }
7140
+ }, {
7141
+ key: "notifyListeners",
7142
+ value: function notifyListeners() {
7143
+ var _this2 = this;
7144
+ Object.values(this.listeners).forEach(function (listener) {
7145
+ return listener(_this2.context);
7146
+ });
7147
+ }
7148
+ }, {
7149
+ key: "syncContextToChannel",
7150
+ value: function syncContextToChannel() {
7151
+ if (!this.channel) {
7152
+ return;
7153
+ }
7154
+ var context = (0, _classInstanceToObject.classInstanceToObject)(this.context);
7155
+ this.channel.postMessage({
7156
+ from: this.id,
7157
+ context: context
7158
+ });
7159
+ }
7160
+ }, {
7161
+ key: "subscribeToChanges",
7162
+ value: function subscribeToChanges(idOrContextListener, contextListenerOrNothing) {
7163
+ var id = typeof idOrContextListener === 'string' ? idOrContextListener : (0, _uuid.v4)();
7164
+ var contextListener = typeof idOrContextListener === 'function' ? idOrContextListener : contextListenerOrNothing;
7165
+ if (!contextListener) {
7166
+ throw new Error('Context listener is required');
7167
+ }
7168
+ this.listeners[id] = contextListener;
7169
+ return id;
7170
+ }
7171
+ }, {
7172
+ key: "unsubscribeFromChanges",
7173
+ value: function unsubscribeFromChanges(id) {
7174
+ delete this.listeners[id];
7175
+ }
7176
+ }]);
7177
+ }();
7178
+
7179
+ },{"./classInstanceToObject":41,"uuid":6}],39:[function(require,module,exports){
7180
+ "use strict";
7181
+
7054
7182
  Object.defineProperty(exports, "__esModule", {
7055
7183
  value: true
7056
7184
  });
@@ -7134,7 +7262,7 @@ var JWTProvider = exports.JWTProvider = /*#__PURE__*/function () {
7134
7262
  }]);
7135
7263
  }();
7136
7264
 
7137
- },{}],39:[function(require,module,exports){
7265
+ },{}],40:[function(require,module,exports){
7138
7266
  "use strict";
7139
7267
 
7140
7268
  Object.defineProperty(exports, "__esModule", {
@@ -7169,7 +7297,70 @@ var makeProxy = exports.makeProxy = function makeProxy(obj) {
7169
7297
  return proxyObj;
7170
7298
  };
7171
7299
 
7172
- },{}],40:[function(require,module,exports){
7300
+ },{}],41:[function(require,module,exports){
7301
+ "use strict";
7302
+
7303
+ Object.defineProperty(exports, "__esModule", {
7304
+ value: true
7305
+ });
7306
+ exports.classInstanceToObject = void 0;
7307
+ function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
7308
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7309
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
7310
+ function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
7311
+ function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
7312
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
7313
+ var classInstanceToObject = exports.classInstanceToObject = function classInstanceToObject(instance, options) {
7314
+ var obj = {};
7315
+ // const prototype = Object.getPrototypeOf(instance);
7316
+ var properties = [
7317
+ // ...Object.getOwnPropertyNames(instance),
7318
+ // ...Object.getOwnPropertyNames(prototype),
7319
+ ];
7320
+ var currentClass = instance;
7321
+ while (currentClass && currentClass !== Object) {
7322
+ // console.log('current class', currentClass); // TODO: test if it omits the Object methods
7323
+ properties.push.apply(properties, _toConsumableArray(Object.getOwnPropertyNames(currentClass)));
7324
+ currentClass = Object.getPrototypeOf(currentClass);
7325
+ }
7326
+ properties.forEach(function (property) {
7327
+ var _options$globals, _options$methodWrappe;
7328
+ if (['constructor'].includes(property)) {
7329
+ return;
7330
+ }
7331
+ var _method = instance[property];
7332
+ _method = typeof _method === 'function' && typeof _method.bind === 'function' ? _method.bind(instance) : _method;
7333
+ if (options !== null && options !== void 0 && (_options$globals = options.globals) !== null && _options$globals !== void 0 && _options$globals.canCallAction) {
7334
+ var originalMethod = _method;
7335
+ _method = function method() {
7336
+ var canCall = options.globals.canCallAction({
7337
+ method: _method.name,
7338
+ "class": instance.constructor.name
7339
+ });
7340
+ if (!canCall) {
7341
+ throw new Error("No permissions for call: ".concat(_method.name)); // TODO: consider usage of a specific permissions error
7342
+ }
7343
+ return originalMethod.apply(void 0, arguments);
7344
+ };
7345
+ }
7346
+ if (typeof (options === null || options === void 0 || (_options$methodWrappe = options.methodWrappers) === null || _options$methodWrappe === void 0 ? void 0 : _options$methodWrappe[property]) === 'function') {
7347
+ obj[property] = function () {
7348
+ var _options$methodWrappe2;
7349
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
7350
+ args[_key] = arguments[_key];
7351
+ }
7352
+ return (_options$methodWrappe2 = options.methodWrappers)[property].apply(_options$methodWrappe2, [_method].concat(args));
7353
+ };
7354
+ } else {
7355
+ obj[property] = function () {
7356
+ return _method.apply(void 0, arguments);
7357
+ };
7358
+ }
7359
+ });
7360
+ return obj;
7361
+ };
7362
+
7363
+ },{}],42:[function(require,module,exports){
7173
7364
  "use strict";
7174
7365
 
7175
7366
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
@@ -7222,7 +7413,7 @@ var getOrCreateUsersPrivateWorkspaceRecord = exports.getOrCreateUsersPrivateWork
7222
7413
  };
7223
7414
  }();
7224
7415
 
7225
- },{"uuid":6}],41:[function(require,module,exports){
7416
+ },{"uuid":6}],43:[function(require,module,exports){
7226
7417
  "use strict";
7227
7418
 
7228
7419
  Object.defineProperty(exports, "__esModule", {
@@ -7247,14 +7438,14 @@ var ONTOLOGY_PERMISSION_GROUP = exports.ONTOLOGY_PERMISSION_GROUP = 'octostar:on
7247
7438
  var ALL_PERMISSIONS = exports.ALL_PERMISSIONS = 'all';
7248
7439
  var ALL_PERMISSIONS_ARRAY = exports.ALL_PERMISSIONS_ARRAY = [ALL_PERMISSIONS];
7249
7440
 
7250
- },{}],42:[function(require,module,exports){
7441
+ },{}],44:[function(require,module,exports){
7251
7442
  "use strict";
7252
7443
 
7253
7444
  Object.defineProperty(exports, "__esModule", {
7254
7445
  value: true
7255
7446
  });
7256
7447
 
7257
- },{}],43:[function(require,module,exports){
7448
+ },{}],45:[function(require,module,exports){
7258
7449
  "use strict";
7259
7450
 
7260
7451
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@octostar/platform-api",
3
- "version": "0.5.10",
3
+ "version": "0.5.12",
4
4
  "description": "Provides access to the API exposed by Octostar platform to an application run in an iframe",
5
5
  "keywords": [
6
6
  "octostar",
@@ -38,10 +38,10 @@
38
38
  "rimraf": "^5.0.5",
39
39
  "tsup": "^8.4.0",
40
40
  "typescript": "^5.8.2",
41
- "@octostar/platform-types": "0.5.13"
41
+ "@octostar/platform-types": "0.5.15"
42
42
  },
43
43
  "peerDependencies": {
44
- "@octostar/platform-types": "^0.5.13"
44
+ "@octostar/platform-types": "^0.5.15"
45
45
  },
46
46
  "publishConfig": {
47
47
  "access": "public"