diff options
| author | Adam Soltys <[email protected]> | 2024-09-08 20:57:06 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2024-09-08 20:57:06 -0700 |
| commit | 5c105c8f4738ab1ead3c5b92f465f899cf0a7446 (patch) | |
| tree | 9cea2d1b46cdab9869f742e3286c9fb0762f8948 /lib/rpc.ts | |
| parent | 92bd9085a576517e06dc000d1e1ff11d27bc34b4 (diff) | |
| download | coinos-server-5c105c8f4738ab1ead3c5b92f465f899cf0a7446.tar.gz coinos-server-5c105c8f4738ab1ead3c5b92f465f899cf0a7446.zip | |
refactor rpc helper, accounts endpoints
Diffstat (limited to 'lib/rpc.ts')
| -rw-r--r-- | lib/rpc.ts | 43 |
1 files changed, 17 insertions, 26 deletions
@@ -1,28 +1,19 @@ import got from "got"; +import { fail } from "$lib/utils"; +import { err } from "$lib/logging"; -export default ({ host, port, wallet, username, password }) => - new Proxy( - {}, - { - get: - (_, prop) => - (...params) => - ((method, ...params) => { - let url = `http://${host}:${port}/wallet/${wallet}`; - return got - .post(url, { - json: { - method, - params, - }, - headers: { - authorization: `Basic ${Buffer.from( - `${username}:${password}`, - ).toString("base64")}`, - }, - }) - .json() - .then(({ result }) => result); - })((prop as any).toLowerCase(), ...params), - }, - ); +export default ({ host, port, wallet, username, password }): any => { + let url = `http://${host}:${port}/wallet/${wallet}`; + let token = btoa(`${username}:${password}`); + let headers = { authorization: `Basic ${token}` }; + let p = (json) => got.post(url, { json, headers }).json(); + + let get = + (_, prop: any) => + (...params) => + p({ method: prop.toLowerCase(), params }) + .then((res: any) => res.result) + .catch((e) => (err(e.message), fail(`Failed to call ${prop}`))); + + return new Proxy({}, { get }); +}; |
