@certik/skynet 0.10.39 → 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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.10.40
4
+
5
+ - Added graphql (copied from frontend)
6
+
3
7
  ## 0.10.38 & 0.10.39
4
8
 
5
9
  - Added x-instance-id header for api service
package/graphql.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Response } from "node-fetch";
2
+
3
+ type GraphqlVariables = { [key: string]: any };
4
+
5
+ export function gql(query: string, variables?: GraphqlVariables): Promise<Response>;
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@certik/skynet",
3
- "version": "0.10.39",
3
+ "version": "0.10.40",
4
4
  "description": "Skynet Shared JS library",
5
5
  "main": "index.js",
6
6
  "author": "CertiK Engineering",