Add http base authentication for gist

This commit is contained in:
DefectingCat
2022-06-20 16:12:43 +08:00
parent 2b5d71620c
commit becd9d988e
5 changed files with 89 additions and 11 deletions

32
lib/fetcher.ts Normal file
View File

@ -0,0 +1,32 @@
import { Gist, GithubUser } 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 () => {
return (await fetch(`${baseUrl}users/${username}/gists`, { 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;
};