forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
44 lines (39 loc) · 1.18 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { ApolloError } = require('apollo-server');
const fetch = require('node-fetch');
const getData = async url => {
try {
const res = await fetch(url);
const json = await res.json();
if(isHTTPError(res.status)) {
throw new ApolloError(json, "http-status-error", {statusCode: res.status, error: json});
}
console.log(json);
return json;
} catch (error) {
console.log(JSON.stringify(error));
throw error;
}
};
const postData = async (url, body) => {
try {
const res = await fetch(url, {
method: 'post',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
});
const json = await res.json();
if(isHTTPError(res.status)) {
throw new ApolloError(json, "http-status-error", {statusCode: res.status, error: json});
}
console.log(json);
return json;
} catch (error) {
console.log(JSON.stringify(error));
throw error;
}
};
const isHTTPError = status => {
return !((status >= 200) && (status < 300));
};
exports.getData = getData;
exports.postData = postData;