34 lines
987 B
TypeScript
34 lines
987 B
TypeScript
import { clog } from '../helpers/Utils';
|
|
import fetch from 'node-fetch';
|
|
|
|
export function testRunQuery(url: string, token: string): (query: string, variables?: Record<string, any>) => Promise<any> {
|
|
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
|
|
return async (query, variables) => {
|
|
const headers: any = {
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
if (token && (token as any) != '') headers.authorization = token;
|
|
|
|
const r = JSON.stringify({ query, variables });
|
|
clog({ query, variables });
|
|
|
|
const response = await fetch(url, {
|
|
body: r,
|
|
method: 'POST',
|
|
headers: headers,
|
|
});
|
|
|
|
try {
|
|
const text = await response.text();
|
|
const res = JSON.parse(text);
|
|
if (res.errors) clog(res.errors[0]);
|
|
clog(res.data);
|
|
} catch (err) {
|
|
console.log(err);
|
|
}
|
|
|
|
return;
|
|
};
|
|
}
|