45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import fetch from 'node-fetch';
|
|
|
|
export const graphRequest = async (query, option): Promise<any> => {
|
|
console.log('[TESTS] Query', query);
|
|
|
|
let params;
|
|
if (option.authHeader)
|
|
params = {
|
|
body: JSON.stringify({ query }),
|
|
method: 'POST',
|
|
headers: {
|
|
authorization: option.authHeader,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
};
|
|
else
|
|
params = {
|
|
body: JSON.stringify({ query }),
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
};
|
|
|
|
const response = await fetch(option.url, params);
|
|
if (!response.ok) {
|
|
return new Promise((resolve, reject) => {
|
|
response
|
|
.text()
|
|
.then((text) => {
|
|
try {
|
|
console.log('[TESTS] Error', JSON.parse(text));
|
|
reject(JSON.parse(text));
|
|
} catch (err) {
|
|
console.log('[TESTS] Error', text);
|
|
reject(text);
|
|
}
|
|
})
|
|
.catch(reject);
|
|
});
|
|
}
|
|
const json = await response.json();
|
|
return json.data;
|
|
};
|