You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
7 years ago
|
import { TokenStore } from "@client/state/TokenStore";
|
||
7 years ago
|
import ApiError from "@common/ApiError";
|
||
|
import { ErrorCode } from "@common/ErrorCode";
|
||
7 years ago
|
|
||
7 years ago
|
export { ApiError };
|
||
7 years ago
|
|
||
|
export default class HttpApi {
|
||
|
baseUrl: string;
|
||
|
|
||
|
tokenStore: TokenStore;
|
||
|
|
||
|
private get authorizationHeader(): {} | { "Authorization": string } {
|
||
|
if (!this.tokenStore.accessToken) {
|
||
|
return {};
|
||
|
}
|
||
|
return { Authorization: `Bearer ${this.tokenStore.accessToken.token}` };
|
||
|
}
|
||
|
|
||
7 years ago
|
constructor(baseUrl: string = `${location.protocol}//${location.hostname}:${location.port}/api`) {
|
||
7 years ago
|
while (baseUrl.charAt(baseUrl.length - 1) === "/") {
|
||
|
baseUrl = baseUrl.substring(0, baseUrl.length - 1);
|
||
|
}
|
||
|
this.baseUrl = baseUrl;
|
||
|
|
||
|
this.tokenStore = new TokenStore(this);
|
||
|
}
|
||
|
|
||
|
async makeRequest(url: string, options?: RequestInit, body?: any): Promise<any> {
|
||
|
options = options || {};
|
||
|
options = {
|
||
|
headers: {
|
||
|
"Content-Type": "application/json",
|
||
|
...this.authorizationHeader,
|
||
|
...options.headers || {},
|
||
|
},
|
||
|
body: JSON.stringify(body),
|
||
|
...options,
|
||
|
};
|
||
|
const response = await fetch(this.baseUrl + url, options);
|
||
7 years ago
|
let responseBody: any;
|
||
|
try {
|
||
|
responseBody = await response.json() || {};
|
||
|
} catch (e) {
|
||
|
throw new ApiError("Invalid JSON response", ErrorCode.Internal, e);
|
||
|
}
|
||
7 years ago
|
if (!response.ok) {
|
||
7 years ago
|
throw new ApiError(responseBody.message || response.statusText, responseBody.code, responseBody.data);
|
||
7 years ago
|
}
|
||
|
return responseBody;
|
||
|
}
|
||
|
|
||
|
}
|