@certik/skynet 0.10.38 → 0.10.40
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/CHANGELOG.md +5 -1
- package/api.js +0 -2
- package/graphql.d.ts +5 -0
- package/graphql.js +51 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/api.js
CHANGED
package/graphql.d.ts
ADDED
package/graphql.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const fetch = require("node-fetch");
|
|
2
|
+
|
|
3
|
+
const LOCAL_URL = "http://localhost:20000/gql";
|
|
4
|
+
const PRODUCTION_URL = "https://api.certik-skynet.com/graphql/gql";
|
|
5
|
+
const GRAPHQL_API_KEY = process.env.GRAPHQL_API_KEY;
|
|
6
|
+
|
|
7
|
+
async function get(url, query, variables = {}) {
|
|
8
|
+
const res = await fetch(url, {
|
|
9
|
+
method: "POST",
|
|
10
|
+
headers: {
|
|
11
|
+
"x-api-key": GRAPHQL_API_KEY,
|
|
12
|
+
"content-type": "application/json",
|
|
13
|
+
},
|
|
14
|
+
body: JSON.stringify({ query: query.trim(), variables }),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
if (res.ok) {
|
|
18
|
+
const { data, errors } = await res.json();
|
|
19
|
+
|
|
20
|
+
if (errors && errors.length > 0) {
|
|
21
|
+
throw new Error(JSON.stringify(errors, null, 2));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return data;
|
|
25
|
+
} else {
|
|
26
|
+
throw new Error(await res.text());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async function gql(query, variables = {}) {
|
|
31
|
+
if (process.env.NODE_ENV === "development") {
|
|
32
|
+
try {
|
|
33
|
+
// try local first
|
|
34
|
+
return await get(LOCAL_URL, query, variables);
|
|
35
|
+
} catch (localErr) {
|
|
36
|
+
if (localErr.message.indexOf("ECONNREFUSED") !== -1) {
|
|
37
|
+
// when local server isn't available
|
|
38
|
+
// fallback to production
|
|
39
|
+
return await get(PRODUCTION_URL, query, variables);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
throw localErr;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
return await get(PRODUCTION_URL, query, variables);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module.exports = {
|
|
50
|
+
gql,
|
|
51
|
+
};
|