Files
DefectingCat.github.io/lib/fetcher.ts
DefectingCat badd9f2727 🐛 Fix gists styles
fix #48
fix #49
2022-06-20 17:22:31 +08:00

39 lines
930 B
TypeScript

import { Gist, GithubUser, SignalGist } from 'types';
import { Base64 } from 'js-base64';
const baseUrl = 'https://api.github.com';
const username = 'DefectingCat';
const password = process.env.NEXT_PUBLIC_GITHUB_API;
const headers = new Headers();
headers.set(
'Authorization',
'Basic ' + Base64.encode(username + ':' + password)
);
/**
* Get all gists
* @returns
*/
export const getGists = async (page = 1, perPage = 10) => {
return (await fetch(`${baseUrl}/gists?per_page=${perPage}&page=${page}`, {
headers,
}).then((res) => res.json())) as Gist[];
};
/**
* Get user information.
* @returns
*/
export const getUser = async () => {
return (await fetch(`${baseUrl}/user`, { headers }).then((res) =>
res.json()
)) as GithubUser;
};
export const getSignalGist = async (id: string) => {
return (await fetch(`${baseUrl}/gists/${id}`, { headers }).then((res) =>
res.json()
)) as SignalGist;
};