Files
SCM-Manager/scm-ui-components/packages/ui-components/src/apiclient.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

// @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";
const fetchOptions: RequestOptions = {
credentials: "same-origin",
headers: {
2018-07-10 15:18:37 +02:00
Cache: "no-cache"
}
};
2019-02-25 17:40:53 +01:00
2018-11-15 21:39:08 +01:00
function handleFailure(response: Response) {
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);
}
}
return response;
}
export function createUrl(url: string) {
if (url.includes("://")) {
return url;
}
let urlWithStartingSlash = url;
2018-07-11 22:01:36 +02:00
if (url.indexOf("/") !== 0) {
urlWithStartingSlash = "/" + urlWithStartingSlash;
2018-07-11 22:01:36 +02:00
}
2018-10-01 17:22:03 +02:00
return `${contextPath}/api/v2${urlWithStartingSlash}`;
}
class ApiClient {
get(url: string): Promise<Response> {
2018-11-15 21:39:08 +01:00
return fetch(createUrl(url), fetchOptions).then(handleFailure);
}
post(url: string, payload: any, contentType: string = "application/json") {
return this.httpRequestWithJSONBody("POST", url, contentType, payload);
}
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
}
delete(url: string): Promise<Response> {
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);
}
httpRequestWithJSONBody(
2018-07-11 17:02:38 +02:00
method: string,
url: string,
contentType: string,
payload: any
): Promise<Response> {
let options: RequestOptions = {
method: method,
body: JSON.stringify(payload)
};
options = Object.assign(options, fetchOptions);
// $FlowFixMe
2018-07-11 17:02:38 +02:00
options.headers["Content-Type"] = contentType;
2018-11-15 21:39:08 +01:00
return fetch(createUrl(url), options).then(handleFailure);
}
}
export let apiClient = new ApiClient();