get token asynchronously if it applies

This commit is contained in:
Fernando Rojo 2020-10-05 17:59:18 +00:00 committed by GitHub
parent cfe242c985
commit ea29f25b19
2 changed files with 6 additions and 4 deletions

View File

@ -1,11 +1,13 @@
function getHeaders(options: ApiRequestOptions): Headers {
async function getHeaders(options: ApiRequestOptions): Headers {
const headers = new Headers({
Accept: 'application/json',
...options.headers,
});
const token = typeof OpenAPI.TOKEN === 'function' ? await OpenAPI.TOKEN() : OpenAPI.TOKEN;
if (isDefined(OpenAPI.TOKEN) && OpenAPI.TOKEN !== '') {
headers.append('Authorization', `Bearer ${OpenAPI.TOKEN}`);
if (isDefined(token) && token !== '') {
headers.append('Authorization', `Bearer ${token}`);
}
if (options.body) {

View File

@ -1,7 +1,7 @@
async function sendRequest(options: ApiRequestOptions, url: string): Promise<Response> {
const request: RequestInit = {
method: options.method,
headers: getHeaders(options),
headers: await getHeaders(options),
body: getRequestBody(options),
};
return await fetch(url, request);