@arkeytyp/valu-api 1.0.0 → 1.0.2

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/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  The `valu-api` package enables developers to create custom iframe applications for Valu Social. It provides tools to invoke functions of registered Valu applications and subscribe to their events. With features like API versioning, event handling, and console command testing, developers can seamlessly integrate and extend functionality within the Valu Social ecosystem.
2
2
 
3
+ # install
4
+ ```
5
+ npm install @arkeytyp/valu-api
6
+ ```
7
+
3
8
  # Usage
4
9
 
5
10
  ## 1. Initialize ValuApi
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@arkeytyp/valu-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "A package for developing iframe applications for Valu Social. Allows invoking functions of registered Valu applications and subscribing to events, as well as other features that enable developers creating own ifram apps for the value social",
5
- "main": "ValuApi.js",
5
+ "main": "src/ValuApi.js",
6
6
  "scripts": {
7
7
  "test": "echo \"No test specified\" && exit 1"
8
8
  },
@@ -13,4 +13,4 @@
13
13
  "type": "git",
14
14
  "url": "https://github.com/arkeytyp/valu-api.git"
15
15
  }
16
- }
16
+ }
package/src/APIPointer.js CHANGED
@@ -34,7 +34,6 @@ export class APIPointer {
34
34
  get apiName () {
35
35
  return this.#apiName;
36
36
  }
37
-
38
37
  /**
39
38
  * @private
40
39
  * @description Stores the version of the API.
@@ -44,11 +43,11 @@ export class APIPointer {
44
43
  return this.#version;
45
44
  }
46
45
 
47
- constructor(apiName, version, runRequest) {
46
+ constructor(apiName, version, guid, runRequest) {
48
47
  this.#apiName = apiName
49
48
  this.#version = version;
50
49
  this.#eventEmitter = new EventEmitter();
51
- this.#guid = guid4();
50
+ this.#guid = guid;
52
51
  this.#runRequest = runRequest;
53
52
  }
54
53
 
@@ -77,7 +76,6 @@ export class APIPointer {
77
76
  return deferredPromise.promise;
78
77
  }
79
78
 
80
-
81
79
  postRunResult(requestId, result) {
82
80
  let deferred = this.#apiCalls[requestId];
83
81
  if(!deferred) {
package/src/ValuApi.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {EventEmitter} from "./EventEmitter.js";
2
2
  import {APIPointer} from "./APIPointer.js";
3
- import {guid4, nextId} from "@/valu-api/Utils.js";
3
+ import {guid4, nextId} from "./Utils.js";
4
4
 
5
5
 
6
6
  /**
@@ -15,9 +15,11 @@ export class ValuApi {
15
15
 
16
16
  #eventEmitter;
17
17
  #valuApplication = {};
18
- #apiRequests = {};
19
- #consoleCommands = new Map();
18
+ #requests = new Map();
20
19
 
20
+ get connected() {
21
+ return this.#valuApplication.origin !== undefined;
22
+ }
21
23
 
22
24
  constructor() {
23
25
  globalThis.addEventListener('message', (event) => {
@@ -41,20 +43,33 @@ export class ValuApi {
41
43
  *
42
44
  * This method enables interaction with a specific version of an API module, allowing users to access its functionality and listen to events.
43
45
  */
44
- getApi(apiName, version) {
45
- const apiPointer = new APIPointer(apiName, version, (functionName, params, requestId, apiPointer) => {
46
+ async getApi(apiName, version) {
47
+ const guid = guid4();
48
+ const result = await this.#registerApiPointer(apiName, version, guid);
49
+
50
+ if(result.error) {
51
+ throw new Error(result.error);
52
+ }
53
+
54
+ const apiPointer = new APIPointer(apiName, result.version, guid,(functionName, params, requestId, apiPointer) => {
46
55
  this.#onApiRunRequest(functionName, params, requestId, apiPointer);
47
56
  });
48
- this.#registerApiPointer(apiPointer);
57
+
49
58
  return apiPointer;
50
59
  }
51
60
 
52
- #registerApiPointer(apiPointer) {
61
+ async #registerApiPointer(apiName, version, guid) {
62
+ let deferredPromise = this.#createDeferred();
63
+
53
64
  this.#postToValuApp('api:create-pointer', {
54
- guid: apiPointer.guid,
55
- api: apiPointer.apiName,
56
- version: apiPointer.version,
65
+ guid: guid,
66
+ api: apiName,
67
+ version: version,
68
+ requestId: deferredPromise.id,
57
69
  });
70
+
71
+ this.#requests[deferredPromise.id] = deferredPromise;
72
+ return deferredPromise.promise;
58
73
  }
59
74
 
60
75
  #postToValuApp(name, message) {
@@ -65,9 +80,8 @@ export class ValuApi {
65
80
  }
66
81
 
67
82
  async #onApiRunRequest(functionName, params, requestId, apiPointer) {
68
- console.log('onApiRunRequest', this);
69
83
 
70
- this.#apiRequests[requestId] = apiPointer;
84
+ this.#requests[requestId] = apiPointer;
71
85
 
72
86
  this.#postToValuApp('api:run', {
73
87
  apiPointerId: apiPointer.guid,
@@ -92,7 +106,7 @@ export class ValuApi {
92
106
  */
93
107
  async runConsoleCommand(command) {
94
108
  let deferredPromise = this.#createDeferred();
95
- this.#consoleCommands[deferredPromise.id] = deferredPromise;
109
+ this.#requests[deferredPromise.id] = deferredPromise;
96
110
 
97
111
  this.#postToValuApp('api:run-console', {
98
112
  requestId: deferredPromise.id,
@@ -135,27 +149,40 @@ export class ValuApi {
135
149
 
136
150
  case 'api:run-console-completed': {
137
151
  const requestId = event.data.requestId;
138
- const deferred = this.#consoleCommands[requestId];
152
+ const deferred = this.#requests[requestId];
139
153
  if(deferred) {
140
154
  deferred.resolve(message);
141
155
  } else {
142
156
  console.log('Failed to locate console request with Id: ', requestId);
143
157
  }
144
158
 
145
- delete this.#consoleCommands[requestId];
159
+ delete this.#requests[requestId];
146
160
  break;
147
161
  }
148
162
 
149
163
  case 'api:run-completed': {
150
164
  const requestId = event.data.requestId;
151
- const apiPointer = this.#apiRequests[requestId];
165
+ const apiPointer = this.#requests[requestId];
152
166
  if(!apiPointer) {
153
167
  console.error(`Failed to find Api Pointer for requestId: ${requestId}`);
154
168
  break
155
169
  }
156
170
 
157
171
  apiPointer.postRunResult(requestId, message);
158
- delete this.#apiRequests[requestId];
172
+ delete this.#requests[requestId];
173
+ break;
174
+ }
175
+
176
+ case 'api:pointer-created': {
177
+ const requestId = event.data.requestId;
178
+ const deferred = this.#requests[requestId];
179
+ if(deferred) {
180
+ deferred.resolve(message);
181
+ delete this.#requests[requestId];
182
+ } else {
183
+ console.log('Failed to locate pointer create request with Id: ', requestId);
184
+ }
185
+
159
186
  break;
160
187
  }
161
188
  }