-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements client-side http transport
* Adds support of node 20 to package.json
- Loading branch information
Showing
3 changed files
with
99 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,82 @@ | ||
'use strict'; | ||
|
||
const socket = new WebSocket('ws://127.0.0.1:8001/'); | ||
const HTTP_API_URL = 'http://localhost:8001'; | ||
let socket; | ||
|
||
const scaffold = (structure) => { | ||
const transports = { | ||
ws: (name, method) => { | ||
if (!socket) socket = new WebSocket('ws://127.0.0.1:8001/'); | ||
return (...args) => { | ||
return new Promise((resolve) => { | ||
const packet = { name, method, args }; | ||
socket.send(JSON.stringify(packet)); | ||
socket.onmessage = (event) => { | ||
const data = JSON.parse(event.data); | ||
resolve(data); | ||
}; | ||
}); | ||
}; | ||
}, | ||
|
||
http: | ||
(name, method, methodArgs) => | ||
(...args) => { | ||
const urlParts = [HTTP_API_URL, name, method]; | ||
if (methodArgs[0] === 'id') urlParts.push(args.shift()); | ||
|
||
return fetch(urlParts.join('/'), { | ||
method: 'POST', | ||
body: JSON.stringify(args), | ||
}).then((response) => response.json()); | ||
}, | ||
}; | ||
|
||
const scaffold = (structure, transport) => { | ||
const api = {}; | ||
const services = Object.keys(structure); | ||
for (const serviceName of services) { | ||
api[serviceName] = {}; | ||
const service = structure[serviceName]; | ||
const methods = Object.keys(service); | ||
for (const methodName of methods) { | ||
api[serviceName][methodName] = (...args) => new Promise((resolve) => { | ||
const packet = { name: serviceName, method: methodName, args }; | ||
socket.send(JSON.stringify(packet)); | ||
socket.onmessage = (event) => { | ||
const data = JSON.parse(event.data); | ||
resolve(data); | ||
}; | ||
}); | ||
api[serviceName][methodName] = transport( | ||
serviceName, | ||
methodName, | ||
service[methodName], | ||
); | ||
} | ||
} | ||
return api; | ||
}; | ||
|
||
const api = scaffold({ | ||
user: { | ||
create: ['record'], | ||
read: ['id'], | ||
update: ['id', 'record'], | ||
delete: ['id'], | ||
find: ['mask'], | ||
const api = scaffold( | ||
{ | ||
user: { | ||
create: ['record'], | ||
read: ['id'], | ||
update: ['id', 'record'], | ||
delete: ['id'], | ||
find: ['mask'], | ||
}, | ||
city: { | ||
read: ['id'], | ||
create: ['record'], | ||
update: ['id', 'record'], | ||
delete: ['id'], | ||
}, | ||
country: { | ||
read: ['id'], | ||
create: ['record'], | ||
update: ['id', 'record'], | ||
delete: ['id'], | ||
}, | ||
}, | ||
country: { | ||
read: ['id'], | ||
delete: ['id'], | ||
find: ['mask'], | ||
}, | ||
}); | ||
transports.http, | ||
); | ||
|
||
// socket.addEventListener('open', async () => { | ||
// const data = await api.user.read(3); | ||
// console.dir({ data }); | ||
// }); | ||
|
||
socket.addEventListener('open', async () => { | ||
const data = await api.user.read(3); | ||
console.dir({ data }); | ||
}); | ||
(async () => console.log(await api.user.read(3)))(); |