This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/lib/utils/fetch.ts

33 lines
872 B
TypeScript
Raw Normal View History

2022-08-29 18:09:28 +00:00
import { API_URL, CLIENT_ID, CLIENT_SECRET, OAUTH_URL } from '../constants';
import { OAuth2TokenDto } from '../types/token-response.interface';
export async function fetchBearer<T>(
accessToken: string,
url: string,
...options: any[]
): Promise<T> {
return fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': 'application/json',
},
...options,
}).then((res) => res.json());
}
export async function fetchToken(code: string): Promise<OAuth2TokenDto> {
return fetch(`${OAUTH_URL}/token`, {
headers: {
Authorization: `Basic ${Buffer.from(
`${CLIENT_ID}:${CLIENT_SECRET}`
).toString('base64')}`,
'Content-Type': 'application/json',
},
method: 'POST',
body: JSON.stringify({
grant_type: 'authorization_code',
code,
}),
}).then((res) => res.json());
}