2018-07-04 16:43:46 +02:00
|
|
|
// @flow
|
2018-11-14 21:34:10 +01:00
|
|
|
import {contextPath} from "./urls";
|
2018-07-04 16:43:46 +02:00
|
|
|
|
2018-11-21 11:12:47 +01:00
|
|
|
export const NOT_FOUND_ERROR_MESSAGE = "not found";
|
|
|
|
|
export const UNAUTHORIZED_ERROR_MESSAGE = "unauthorized";
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function handleStatusCode(response: Response) {
|
|
|
|
|
if (!response.ok) {
|
2018-11-15 08:40:13 +01:00
|
|
|
switch (response.status) {
|
|
|
|
|
case 401:
|
2018-11-21 11:12:47 +01:00
|
|
|
return throwErrorWithMessage(response, UNAUTHORIZED_ERROR_MESSAGE);
|
2018-11-15 08:40:13 +01:00
|
|
|
case 404:
|
2018-11-21 11:12:47 +01:00
|
|
|
return throwErrorWithMessage(response, NOT_FOUND_ERROR_MESSAGE);
|
2018-11-15 08:40:13 +01:00
|
|
|
default:
|
|
|
|
|
return throwErrorWithMessage(response, "server returned status code " + response.status);
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
2018-11-15 08:40:13 +01:00
|
|
|
|
2018-07-04 16:43:46 +02:00
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 08:40:13 +01:00
|
|
|
function throwErrorWithMessage(response: Response, message: string) {
|
|
|
|
|
return response.json().then(
|
|
|
|
|
json => {
|
|
|
|
|
throw Error(json.message);
|
|
|
|
|
},
|
|
|
|
|
() => {
|
|
|
|
|
throw Error(message);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
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-07-04 16:43:46 +02:00
|
|
|
return fetch(createUrl(url), fetchOptions).then(handleStatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
|
|
return fetch(createUrl(url), options).then(handleStatusCode);
|
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);
|
|
|
|
|
return fetch(createUrl(url), options).then(handleStatusCode);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
return fetch(createUrl(url), options).then(handleStatusCode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export let apiClient = new ApiClient();
|