Files
SCM-Manager/scm-ui/ui-components/src/apiclient.ts

119 lines
3.0 KiB
TypeScript
Raw Normal View History

import { contextPath } from './urls';
import {
createBackendError,
ForbiddenError,
isBackendError,
UnauthorizedError,
} from './errors';
import { BackendErrorContent } from './errors';
const applyFetchOptions: (p: RequestOptions) => RequestOptions = o => {
o.credentials = 'same-origin';
o.headers = {
Cache: 'no-cache',
2019-03-12 13:55:34 +01:00
// identify the request as ajax request
'X-Requested-With': 'XMLHttpRequest',
};
return o;
};
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);
});
2018-11-15 21:39:08 +01:00
} 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
throw new Error('server returned status code ' + response.status);
}
}
return response;
}
export function createUrl(url: string) {
if (url.includes('://')) {
return url;
}
let urlWithStartingSlash = url;
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> {
return fetch(createUrl(url), applyFetchOptions({})).then(handleFailure);
}
post(url: string, payload: any, contentType: string = 'application/json') {
return this.httpRequestWithJSONBody('POST', url, contentType, payload);
}
postBinary(url: string, fileAppender: (p: FormData) => void) {
let formData = new FormData();
2019-08-30 09:11:14 +02:00
fileAppender(formData);
let options: RequestOptions = {
method: 'POST',
body: formData,
};
2019-08-30 09:11:14 +02:00
return this.httpRequestWithBinaryBody(options, url);
}
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',
2018-10-25 13:45:52 +02:00
};
options = applyFetchOptions(options);
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 = applyFetchOptions(options);
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),
};
2019-08-30 09:11:14 +02:00
return this.httpRequestWithBinaryBody(options, url, contentType);
}
httpRequestWithBinaryBody(
options: RequestOptions,
url: string,
contentType?: string,
) {
options = applyFetchOptions(options);
2019-08-30 09:11:14 +02:00
if (contentType) {
// $FlowFixMe
options.headers['Content-Type'] = contentType;
2019-08-30 09:11:14 +02:00
}
2018-11-15 21:39:08 +01:00
return fetch(createUrl(url), options).then(handleFailure);
}
}
export let apiClient = new ApiClient();