@arkeytyp/valu-api 1.0.1 → 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/package.json +1 -1
- package/src/APIPointer.js +2 -4
- package/src/ValuApi.js +43 -16
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkeytyp/valu-api",
|
|
3
|
-
"version": "1.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
5
|
"main": "src/ValuApi.js",
|
|
6
6
|
"scripts": {
|
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 =
|
|
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
|
@@ -15,9 +15,11 @@ export class ValuApi {
|
|
|
15
15
|
|
|
16
16
|
#eventEmitter;
|
|
17
17
|
#valuApplication = {};
|
|
18
|
-
#
|
|
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
|
|
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
|
-
|
|
57
|
+
|
|
49
58
|
return apiPointer;
|
|
50
59
|
}
|
|
51
60
|
|
|
52
|
-
#registerApiPointer(
|
|
61
|
+
async #registerApiPointer(apiName, version, guid) {
|
|
62
|
+
let deferredPromise = this.#createDeferred();
|
|
63
|
+
|
|
53
64
|
this.#postToValuApp('api:create-pointer', {
|
|
54
|
-
guid:
|
|
55
|
-
api:
|
|
56
|
-
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.#
|
|
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.#
|
|
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.#
|
|
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.#
|
|
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.#
|
|
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.#
|
|
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
|
}
|