learn-request-queue/src/twitch.ts

26 lines
882 B
TypeScript
Raw Normal View History

2020-07-05 18:46:41 +00:00
import * as config from "./config";
import fetch, { Response as FetchResponse } from "node-fetch";
export interface TokenResponse {
access_token: string;
refresh_token: string;
token_type: string;
expires_in: number;
}
export interface TokenPair {
access_token: string;
refresh_token: string;
}
export async function apiRequest(tokens: TokenPair, method: string, endpoint: string): Promise<any>;
export async function apiRequest(tokens: TokenPair, method: string, endpoint: string, query: string): Promise<any>;
export async function apiRequest(tokens: TokenPair, method: string, endpoint: string, query?: string,) {
var headers = {
"Authorization": "Bearer " + tokens.access_token,
"Client-ID": config.twitchClientId
};
return fetch("https://api.twitch.tv/helix" + endpoint, { method: method, headers: headers})
.then(async (res: FetchResponse) => res.json());
}