Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 4x 4x 4x 4x 13x 13x 4x 9x 4x 5x 4x 14x 2x 12x 6x 6x 1x 5x 5x 1x 4x | import unfetch from 'isomorphic-unfetch';
import { FetchConnector, FetchError, NetworkError } from './utils';
// this is related to a bug
// https://stackoverflow.com/questions/44720448/fetch-typeerror-failed-to-execute-fetch-on-window-illegal-invocation
const fetch = unfetch;
export interface HttpConnectorOptions {
refreshAccessTokenUri: string;
logoutUri: string;
}
export default class HttpConnector implements FetchConnector {
private refreshAccessTokenUri: string;
private logoutUri: string;
constructor(options: HttpConnectorOptions) {
this.refreshAccessTokenUri = options.refreshAccessTokenUri;
this.logoutUri = options.logoutUri;
}
public createAccessToken(
fetchOptions: RequestInit
): Promise<{ accessToken: string }> {
return this.fetch(this.refreshAccessTokenUri, fetchOptions);
}
public logout(fetchOptions: RequestInit): Promise<{ done: boolean }> {
return this.fetch(this.logoutUri, fetchOptions);
}
private async fetch(url: string, fetchOptions: RequestInit) {
let res: Response;
try {
res = await fetch(url, fetchOptions);
} catch (err) {
throw new NetworkError();
}
if (res.status >= 200 && res.status < 300) {
return res.json();
}
const contentType = res.headers.get('Content-Type');
if (contentType && contentType.includes('application/json')) {
const errorData = await res.json();
throw new FetchError(res, errorData);
}
throw new FetchError(res);
}
}
|