UNPKG

@codybrom/denim

Version:

A Deno/TypeScript library for the Threads API

34 lines (29 loc) 996 B
import { THREADS_OAUTH_BASE_URL } from "../constants.ts"; import type { TokenResponse } from "../types.ts"; import { getAPI } from "../utils/getAPI.ts"; /** * Refreshes a long-lived access token. * * @param accessToken - The long-lived access token to refresh * @returns A Promise that resolves to the TokenResponse with the refreshed token * @throws Will throw an error if the API request fails */ export async function refreshToken( accessToken: string, ): Promise<TokenResponse> { const api = getAPI(); if (api) { return api.refreshToken(accessToken); } const url = new URL(`${THREADS_OAUTH_BASE_URL}/refresh_access_token`); url.searchParams.append("grant_type", "th_refresh_token"); url.searchParams.append("access_token", accessToken); const response = await fetch(url.toString()); if (!response.ok) { const errorBody = await response.text(); throw new Error( `Failed to refresh token (${response.status}): ${errorBody}`, ); } return await response.json(); }