storybook/scripts/utils/githubClient.ts
2021-11-15 16:22:22 +08:00

26 lines
596 B
TypeScript

import fetch from 'node-fetch';
const GITHUB_API = 'https://api.github.com/graphql';
export const githubClient = (apiKey: string) => {
return async (query: string, variables?: { [key: string]: any }) => {
const res = await fetch(GITHUB_API, {
method: 'POST',
headers: {
authorization: `token ${apiKey}`,
},
body: JSON.stringify({
query,
variables,
}),
});
const result = await res.json();
const { data, errors } = result;
if (errors) {
throw new Error(JSON.stringify(errors[0]));
}
return data;
};
};