mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-14 17:26:22 +01:00
Use '@babel/plugin-proposal-class-properties' to avoid problems with undefined 'this'
This commit is contained in:
@@ -158,31 +158,32 @@ export function createUrlWithIdentifiers(url: string): string {
|
|||||||
type ErrorListener = (error: Error) => void;
|
type ErrorListener = (error: Error) => void;
|
||||||
|
|
||||||
class ApiClient {
|
class ApiClient {
|
||||||
constructor() {
|
|
||||||
this.notifyAndRethrow = this.notifyAndRethrow.bind(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
errorListeners: ErrorListener[] = [];
|
errorListeners: ErrorListener[] = [];
|
||||||
|
|
||||||
get(url: string): Promise<Response> {
|
get = (url: string): Promise<Response> => {
|
||||||
return fetch(createUrl(url), applyFetchOptions({}))
|
return fetch(createUrl(url), applyFetchOptions({}))
|
||||||
.then(handleFailure)
|
.then(handleFailure)
|
||||||
.catch(this.notifyAndRethrow);
|
.catch(this.notifyAndRethrow);
|
||||||
}
|
};
|
||||||
|
|
||||||
post(url: string, payload?: any, contentType = "application/json", additionalHeaders: Record<string, string> = {}) {
|
post = (
|
||||||
|
url: string,
|
||||||
|
payload?: any,
|
||||||
|
contentType = "application/json",
|
||||||
|
additionalHeaders: Record<string, string> = {}
|
||||||
|
) => {
|
||||||
return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload);
|
return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload);
|
||||||
}
|
};
|
||||||
|
|
||||||
postText(url: string, payload: string, additionalHeaders: Record<string, string> = {}) {
|
postText = (url: string, payload: string, additionalHeaders: Record<string, string> = {}) => {
|
||||||
return this.httpRequestWithTextBody("POST", url, additionalHeaders, payload);
|
return this.httpRequestWithTextBody("POST", url, additionalHeaders, payload);
|
||||||
}
|
};
|
||||||
|
|
||||||
putText(url: string, payload: string, additionalHeaders: Record<string, string> = {}) {
|
putText = (url: string, payload: string, additionalHeaders: Record<string, string> = {}) => {
|
||||||
return this.httpRequestWithTextBody("PUT", url, additionalHeaders, payload);
|
return this.httpRequestWithTextBody("PUT", url, additionalHeaders, payload);
|
||||||
}
|
};
|
||||||
|
|
||||||
postBinary(url: string, fileAppender: (p: FormData) => void, additionalHeaders: Record<string, string> = {}) {
|
postBinary = (url: string, fileAppender: (p: FormData) => void, additionalHeaders: Record<string, string> = {}) => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
fileAppender(formData);
|
fileAppender(formData);
|
||||||
|
|
||||||
@@ -192,13 +193,13 @@ class ApiClient {
|
|||||||
headers: additionalHeaders
|
headers: additionalHeaders
|
||||||
};
|
};
|
||||||
return this.httpRequestWithBinaryBody(options, url);
|
return this.httpRequestWithBinaryBody(options, url);
|
||||||
}
|
};
|
||||||
|
|
||||||
put(url: string, payload: any, contentType = "application/json", additionalHeaders: Record<string, string> = {}) {
|
put(url: string, payload: any, contentType = "application/json", additionalHeaders: Record<string, string> = {}) {
|
||||||
return this.httpRequestWithJSONBody("PUT", url, contentType, additionalHeaders, payload);
|
return this.httpRequestWithJSONBody("PUT", url, contentType, additionalHeaders, payload);
|
||||||
}
|
}
|
||||||
|
|
||||||
head(url: string) {
|
head = (url: string) => {
|
||||||
let options: RequestInit = {
|
let options: RequestInit = {
|
||||||
method: "HEAD"
|
method: "HEAD"
|
||||||
};
|
};
|
||||||
@@ -206,9 +207,9 @@ class ApiClient {
|
|||||||
return fetch(createUrl(url), options)
|
return fetch(createUrl(url), options)
|
||||||
.then(handleFailure)
|
.then(handleFailure)
|
||||||
.catch(this.notifyAndRethrow);
|
.catch(this.notifyAndRethrow);
|
||||||
}
|
};
|
||||||
|
|
||||||
delete(url: string): Promise<Response> {
|
delete = (url: string): Promise<Response> => {
|
||||||
let options: RequestInit = {
|
let options: RequestInit = {
|
||||||
method: "DELETE"
|
method: "DELETE"
|
||||||
};
|
};
|
||||||
@@ -216,15 +217,15 @@ class ApiClient {
|
|||||||
return fetch(createUrl(url), options)
|
return fetch(createUrl(url), options)
|
||||||
.then(handleFailure)
|
.then(handleFailure)
|
||||||
.catch(this.notifyAndRethrow);
|
.catch(this.notifyAndRethrow);
|
||||||
}
|
};
|
||||||
|
|
||||||
httpRequestWithJSONBody(
|
httpRequestWithJSONBody = (
|
||||||
method: string,
|
method: string,
|
||||||
url: string,
|
url: string,
|
||||||
contentType: string,
|
contentType: string,
|
||||||
additionalHeaders: Record<string, string>,
|
additionalHeaders: Record<string, string>,
|
||||||
payload?: any
|
payload?: any
|
||||||
): Promise<Response> {
|
): Promise<Response> => {
|
||||||
const options: RequestInit = {
|
const options: RequestInit = {
|
||||||
method: method,
|
method: method,
|
||||||
headers: additionalHeaders
|
headers: additionalHeaders
|
||||||
@@ -233,23 +234,23 @@ class ApiClient {
|
|||||||
options.body = JSON.stringify(payload);
|
options.body = JSON.stringify(payload);
|
||||||
}
|
}
|
||||||
return this.httpRequestWithBinaryBody(options, url, contentType);
|
return this.httpRequestWithBinaryBody(options, url, contentType);
|
||||||
}
|
};
|
||||||
|
|
||||||
httpRequestWithTextBody(
|
httpRequestWithTextBody = (
|
||||||
method: string,
|
method: string,
|
||||||
url: string,
|
url: string,
|
||||||
additionalHeaders: Record<string, string> = {},
|
additionalHeaders: Record<string, string> = {},
|
||||||
payload: string
|
payload: string
|
||||||
) {
|
) => {
|
||||||
const options: RequestInit = {
|
const options: RequestInit = {
|
||||||
method: method,
|
method: method,
|
||||||
headers: additionalHeaders
|
headers: additionalHeaders
|
||||||
};
|
};
|
||||||
options.body = payload;
|
options.body = payload;
|
||||||
return this.httpRequestWithBinaryBody(options, url, "text/plain");
|
return this.httpRequestWithBinaryBody(options, url, "text/plain");
|
||||||
}
|
};
|
||||||
|
|
||||||
httpRequestWithBinaryBody(options: RequestInit, url: string, contentType?: string) {
|
httpRequestWithBinaryBody = (options: RequestInit, url: string, contentType?: string) => {
|
||||||
options = applyFetchOptions(options);
|
options = applyFetchOptions(options);
|
||||||
if (contentType) {
|
if (contentType) {
|
||||||
if (!options.headers) {
|
if (!options.headers) {
|
||||||
@@ -262,7 +263,7 @@ class ApiClient {
|
|||||||
return fetch(createUrl(url), options)
|
return fetch(createUrl(url), options)
|
||||||
.then(handleFailure)
|
.then(handleFailure)
|
||||||
.catch(this.notifyAndRethrow);
|
.catch(this.notifyAndRethrow);
|
||||||
}
|
};
|
||||||
|
|
||||||
subscribe(url: string, argument: SubscriptionArgument): Cancel {
|
subscribe(url: string, argument: SubscriptionArgument): Cancel {
|
||||||
const es = new EventSource(createUrlWithIdentifiers(url), {
|
const es = new EventSource(createUrlWithIdentifiers(url), {
|
||||||
@@ -293,14 +294,14 @@ class ApiClient {
|
|||||||
return () => es.close();
|
return () => es.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
onError(errorListener: ErrorListener) {
|
onError = (errorListener: ErrorListener) => {
|
||||||
this.errorListeners.push(errorListener);
|
this.errorListeners.push(errorListener);
|
||||||
}
|
};
|
||||||
|
|
||||||
private notifyAndRethrow(error: Error): never {
|
private notifyAndRethrow = (error: Error): never => {
|
||||||
this.errorListeners.forEach(errorListener => errorListener(error));
|
this.errorListeners.forEach(errorListener => errorListener(error));
|
||||||
throw error;
|
throw error;
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const apiClient = new ApiClient();
|
export const apiClient = new ApiClient();
|
||||||
|
|||||||
Reference in New Issue
Block a user