2018-07-04 16:43:46 +02:00
|
|
|
// @flow
|
2018-08-27 15:47:02 +02:00
|
|
|
import { contextPath } from "./urls";
|
2019-03-04 11:49:12 +01:00
|
|
|
import { createBackendError, ForbiddenError, isBackendError, UnauthorizedError } from "./errors";
|
2018-11-15 21:39:08 +01:00
|
|
|
import type { BackendErrorContent } from "./errors";
|
2018-07-04 16:43:46 +02:00
|
|
|
|
|
|
|
|
const fetchOptions: RequestOptions = {
|
|
|
|
|
credentials: "same-origin",
|
|
|
|
|
headers: {
|
2018-07-10 15:18:37 +02:00
|
|
|
Cache: "no-cache"
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-02-25 17:40:53 +01:00
|
|
|
|
2018-11-15 21:39:08 +01:00
|
|
|
|
|
|
|
|
function handleFailure(response: Response) {
|
2018-07-04 16:43:46 +02:00
|
|
|
if (!response.ok) {
|
2018-11-15 21:39:08 +01:00
|
|
|
if (isBackendError(response)) {
|
|
|
|
|
return response.json()
|
|
|
|
|
.then((content: BackendErrorContent) => {
|
|
|
|
|
throw createBackendError(content, response.status);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2019-02-25 17:40:53 +01:00
|
|
|
if (response.status === 401) {
|
|
|
|
|
throw new UnauthorizedError("Unauthorized", 401);
|
2019-03-04 11:49:12 +01:00
|
|
|
} else if (response.status === 403) {
|
|
|
|
|
throw new ForbiddenError("Forbidden", 403);
|
2019-02-25 17:40:53 +01:00
|
|
|
}
|
2019-03-04 11:49:12 +01:00
|
|
|
|
2018-11-15 21:39:08 +01:00
|
|
|
throw new Error("server returned status code " + response.status);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 08:22:27 +02:00
|
|
|
export function createUrl(url: string) {
|
2018-08-03 08:36:16 +02:00
|
|
|
if (url.includes("://")) {
|
2018-07-11 12:02:53 +02:00
|
|
|
return url;
|
|
|
|
|
}
|
2018-07-12 08:22:27 +02:00
|
|
|
let urlWithStartingSlash = url;
|
2018-07-11 22:01:36 +02:00
|
|
|
if (url.indexOf("/") !== 0) {
|
2018-07-12 08:22:27 +02:00
|
|
|
urlWithStartingSlash = "/" + urlWithStartingSlash;
|
2018-07-11 22:01:36 +02:00
|
|
|
}
|
2018-10-01 17:22:03 +02:00
|
|
|
return `${contextPath}/api/v2${urlWithStartingSlash}`;
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class ApiClient {
|
2018-07-11 12:02:53 +02:00
|
|
|
get(url: string): Promise<Response> {
|
2018-11-15 21:39:08 +01:00
|
|
|
return fetch(createUrl(url), fetchOptions).then(handleFailure);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:36:16 +02:00
|
|
|
post(url: string, payload: any, contentType: string = "application/json") {
|
|
|
|
|
return this.httpRequestWithJSONBody("POST", url, contentType, payload);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-03 08:36:16 +02:00
|
|
|
put(url: string, payload: any, contentType: string = "application/json") {
|
|
|
|
|
return this.httpRequestWithJSONBody("PUT", url, contentType, payload);
|
2018-07-11 17:02:38 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 13:45:52 +02:00
|
|
|
head(url: string) {
|
|
|
|
|
let options: RequestOptions = {
|
|
|
|
|
method: "HEAD"
|
|
|
|
|
};
|
|
|
|
|
options = Object.assign(options, fetchOptions);
|
2018-11-15 21:39:08 +01:00
|
|
|
return fetch(createUrl(url), options).then(handleFailure);
|
2018-10-15 16:45:44 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 12:02:53 +02:00
|
|
|
delete(url: string): Promise<Response> {
|
2018-07-04 16:43:46 +02:00
|
|
|
let options: RequestOptions = {
|
|
|
|
|
method: "DELETE"
|
|
|
|
|
};
|
|
|
|
|
options = Object.assign(options, fetchOptions);
|
2018-11-15 21:39:08 +01:00
|
|
|
return fetch(createUrl(url), options).then(handleFailure);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-11 12:02:53 +02:00
|
|
|
httpRequestWithJSONBody(
|
2018-07-11 17:02:38 +02:00
|
|
|
method: string,
|
2018-08-03 08:36:16 +02:00
|
|
|
url: string,
|
|
|
|
|
contentType: string,
|
|
|
|
|
payload: any
|
2018-07-11 12:02:53 +02:00
|
|
|
): Promise<Response> {
|
2018-07-04 16:43:46 +02:00
|
|
|
let options: RequestOptions = {
|
|
|
|
|
method: method,
|
2018-08-03 08:36:16 +02:00
|
|
|
body: JSON.stringify(payload)
|
2018-07-04 16:43:46 +02:00
|
|
|
};
|
|
|
|
|
options = Object.assign(options, fetchOptions);
|
|
|
|
|
// $FlowFixMe
|
2018-07-11 17:02:38 +02:00
|
|
|
options.headers["Content-Type"] = contentType;
|
2018-07-04 16:43:46 +02:00
|
|
|
|
2018-11-15 21:39:08 +01:00
|
|
|
return fetch(createUrl(url), options).then(handleFailure);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export let apiClient = new ApiClient();
|