@arkeytyp/valu-api 1.0.1 → 1.0.3
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 +2 -1
- package/src/APIPointer.js +3 -5
- package/src/ValuApi.js +46 -19
- package/types/valu-api.d.ts +22 -0
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arkeytyp/valu-api",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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": {
|
|
7
7
|
"test": "echo \"No test specified\" && exit 1"
|
|
8
8
|
},
|
|
9
|
+
"types": "./types/valu-api.d.ts",
|
|
9
10
|
"keywords": ["valu", "api", "iframe", "valu social", "multiverse"],
|
|
10
11
|
"author": "Stanislav Osipov",
|
|
11
12
|
"license": "MIT",
|
package/src/APIPointer.js
CHANGED
|
@@ -34,21 +34,20 @@ 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.
|
|
41
|
-
* @type {
|
|
40
|
+
* @type {number}
|
|
42
41
|
*/
|
|
43
42
|
get version () {
|
|
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) => {
|
|
@@ -32,7 +34,7 @@ export class ValuApi {
|
|
|
32
34
|
/**
|
|
33
35
|
* Retrieves an APIPointer object for a specific API module.
|
|
34
36
|
* @param {string} apiName The name of the API module to retrieve.
|
|
35
|
-
* @param {
|
|
37
|
+
* @param {number} [version] The optional version of the API module. If not provided, the APIPointer will be bound to the latest available version.
|
|
36
38
|
* @returns {APIPointer} An APIPointer object bound to the specified API version (or the latest version if no version is specified).
|
|
37
39
|
*
|
|
38
40
|
* The APIPointer object provides the ability to:
|
|
@@ -41,33 +43,45 @@ 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) {
|
|
61
76
|
const data = { name: name, message: message};
|
|
62
77
|
|
|
63
|
-
|
|
78
|
+
// console.log('Posting to Valu: ', name, ' ', message, ' source: ', this.#valuApplication.source);
|
|
64
79
|
this.#valuApplication.source.postMessage(data, this.#valuApplication.origin);
|
|
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,
|
|
@@ -119,7 +133,7 @@ export class ValuApi {
|
|
|
119
133
|
}
|
|
120
134
|
|
|
121
135
|
const message = event.data.message;
|
|
122
|
-
|
|
136
|
+
// console.log('Message From Valu: ', event.data.name, ' ', message);
|
|
123
137
|
|
|
124
138
|
switch (event.data.name) {
|
|
125
139
|
case 'api:ready': {
|
|
@@ -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
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
declare module '@arkeytyp/valu-api' {
|
|
2
|
+
export class ValuApi {
|
|
3
|
+
static API_READY: string;
|
|
4
|
+
|
|
5
|
+
get connected(): boolean;
|
|
6
|
+
|
|
7
|
+
addEventListener(event: string, callback: (data: any) => void): void;
|
|
8
|
+
removeEventListener(event: string, callback: (data: any) => void): void;
|
|
9
|
+
getApi(apiName: string, version?: number): Promise<APIPointer>;
|
|
10
|
+
runConsoleCommand(command: string): Promise<any | string>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class APIPointer {
|
|
14
|
+
get guid(): string;
|
|
15
|
+
get apiName(): string;
|
|
16
|
+
get version(): number;
|
|
17
|
+
|
|
18
|
+
addEventListener(event: string, callback: (data: any) => void): void;
|
|
19
|
+
removeEventListener(event: string, callback: (data: any) => void): void;
|
|
20
|
+
run(functionName: string, params?: any): Promise<any>;
|
|
21
|
+
}
|
|
22
|
+
}
|