|
1 |
| -'use strict'; |
| 1 | +import { Metacom } from './metacom.js'; |
2 | 2 |
|
3 |
| -// API Builder |
4 |
| - |
5 |
| -const socket = new WebSocket('wss://' + location.host); |
6 |
| - |
7 |
| -const buildAPI = (methods, socket = null) => { |
8 |
| - const api = {}; |
9 |
| - for (const method of methods) { |
10 |
| - api[method] = (args = {}) => new Promise((resolve, reject) => { |
11 |
| - if (socket) { |
12 |
| - socket.send(JSON.stringify({ method, args })); |
13 |
| - socket.onmessage = event => { |
14 |
| - const obj = JSON.parse(event.data); |
15 |
| - if (obj.result !== 'error') resolve(obj); |
16 |
| - else reject(new Error(`Status Code: ${obj.reason}`)); |
17 |
| - }; |
18 |
| - } else { |
19 |
| - fetch(`/api/${method}`, { |
20 |
| - method: 'POST', |
21 |
| - headers: { 'Content-Type': 'application/json' }, |
22 |
| - body: JSON.stringify(args), |
23 |
| - }).then(res => { |
24 |
| - const { status } = res; |
25 |
| - if (status === 200) resolve(res.json()); |
26 |
| - else reject(new Error(`Status Code: ${status}`)); |
27 |
| - }); |
28 |
| - } |
29 |
| - }); |
30 |
| - } |
31 |
| - return api; |
32 |
| -}; |
33 |
| - |
34 |
| -let api = buildAPI(['status', 'signIn', 'introspection'], socket); |
35 |
| - |
36 |
| -// Console Emulation |
| 3 | +const metacom = new Metacom(location.host); |
| 4 | +const { api } = metacom; |
| 5 | +window.api = api; |
37 | 6 |
|
38 | 7 | const ALPHA_UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
39 | 8 | const ALPHA_LOWER = 'abcdefghijklmnopqrstuvwxyz';
|
@@ -276,12 +245,12 @@ function commandLoop() {
|
276 | 245 |
|
277 | 246 | const signIn = async () => {
|
278 | 247 | try {
|
| 248 | + await metacom.load('status', 'signIn', 'introspection'); |
279 | 249 | await api.status();
|
280 | 250 | } catch (err) {
|
281 | 251 | await api.signIn({ login: 'marcus', password: 'marcus' });
|
282 | 252 | }
|
283 |
| - const methods = await api.introspection(); |
284 |
| - api = buildAPI(methods, socket); |
| 253 | + await metacom.load('example'); |
285 | 254 | };
|
286 | 255 |
|
287 | 256 | window.addEventListener('load', () => {
|
|
0 commit comments