@neuraiproject/neurai-rpc 0.2.7 → 0.4.0

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/index.ts CHANGED
@@ -1,11 +1,10 @@
1
- import axios from "axios";
2
1
  import { methods } from "./docs";
3
2
 
4
3
  export { methods };
5
4
  function throwSyntaxError() {
6
5
  throw new Error("Syntax error, call getRPC with (username, password, URL)");
7
6
  }
8
-
7
+
9
8
  export function getRPC(username: string, password: string, URL: string) {
10
9
  if (!username) {
11
10
  throwSyntaxError();
@@ -19,12 +18,6 @@ export function getRPC(username: string, password: string, URL: string) {
19
18
 
20
19
  return async function rpc(method: string, params: any[]) {
21
20
  const promise = new Promise((resolutionFunc, rejectionFunc) => {
22
- const options = {
23
- auth: {
24
- username,
25
- password,
26
- },
27
- };
28
21
  const data = {
29
22
  jsonrpc: "2.0",
30
23
  id: Math.random(),
@@ -33,36 +26,47 @@ export function getRPC(username: string, password: string, URL: string) {
33
26
  };
34
27
 
35
28
  try {
36
- const rpcResponse = axios.post(URL, data, options);
29
+ const rpcResponse = postData(URL, data, username, password);
37
30
 
38
31
  rpcResponse
39
- .then((re) => {
40
- const result = re.data.result;
41
- resolutionFunc(result);
42
- })
43
- .catch((e) => {
44
- if (e.response) {
45
- //We were able to connect to the wallet but something was wrong with our request
32
+ .then(async (response) => {
33
+ /*
34
+ This means that we got a response.
35
+ The response can still be an error/problem but the server responded
46
36
 
47
- const { response } = e;
48
- const { request, ...errorObject } = response;
37
+ */
49
38
 
50
- rejectionFunc({
51
- status: e.response.status,
52
- statusText: e.response.statusText,
53
- error: errorObject.data.error,
54
- description: `Could connect to Raven core node but got an error. Method "${method}"`,
55
- });
56
- } else if (e.request) {
57
- //Could NOT connect to wallet
39
+ if (response.ok) {
40
+ //Happy flow
41
+ const obj = await response.json(); //Convert to JSON
42
+ resolutionFunc(obj.result);
43
+ } else if (response.status !== 200) {
44
+ //OK something is wrong
45
+ let obj = {
46
+ error: null,
47
+ description: null,
48
+ };
49
+ try {
50
+ obj = await response.json();
51
+ } catch (e) {}
52
+ const myError = {
53
+ statusText: response.statusText,
54
+ status: response.status,
55
+ description: obj.description,
56
+ error: obj.error,
57
+ };
58
58
 
59
- rejectionFunc({
60
- type:"ServerUnreachable",
61
- error: "Could not communicate with Raven core node",
62
- description:
63
- "Are you sure that the URL is correct? The URL is usually something like http://localhost:8766",
64
- });
59
+ rejectionFunc(myError);
65
60
  }
61
+ })
62
+ .catch((e) => {
63
+ rejectionFunc({
64
+ originalError: e,
65
+ type: "ServerUnreachable",
66
+ error: "Could not communicate with Raven core node",
67
+ description:
68
+ "Are you sure that the URL is correct? The URL is usually mainnet = http://127.0.0.1:19001 and testnet = http://127.0.0.1:119001",
69
+ });
66
70
  });
67
71
  } catch (e) {
68
72
  rejectionFunc(e.response);
@@ -75,6 +79,41 @@ function getValidMethods() {
75
79
  const keys = Object.keys(methods).sort();
76
80
  return keys.join(" ");
77
81
  }
82
+ // Example POST method implementation:
83
+ async function postData(
84
+ url = "",
85
+ data = {},
86
+ username: string,
87
+ password: string
88
+ ) {
89
+ let base64Credentials = "";
90
+
91
+ //btoa or Buffer, depends if we are in Node.js or in the browser
92
+ if (typeof btoa === "function") {
93
+ base64Credentials = btoa(`${username}:${password}`);
94
+ } else if (Buffer) {
95
+ base64Credentials = Buffer.from(`${username}:${password}`).toString(
96
+ "base64"
97
+ );
98
+ }
99
+
100
+ // Default options are marked with *
101
+ const response = await fetch(url, {
102
+ method: "POST", // *GET, POST, PUT, DELETE, etc.
103
+ mode: "cors", // no-cors, *cors, same-origin
104
+ headers: {
105
+ Authorization: "Basic " + base64Credentials,
106
+ "Content-Type": "application/json",
107
+
108
+ // 'Content-Type': 'application/x-www-form-urlencoded',
109
+ },
110
+ redirect: "follow", // manual, *follow, error
111
+ referrerPolicy: "no-referrer", // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
112
+ body: JSON.stringify(data), // body data type must match "Content-Type" header
113
+ });
114
+ return response;
115
+ }
116
+
78
117
  /*
79
118
  export const methods = {
80
119
  "getaddressbalance": "getaddressbalance",