From 9f4f57cdc1258b47c217af2bd3651791fa99b41a Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Fri, 3 Aug 2018 08:36:16 +0200 Subject: [PATCH] simplify the post and put api of the apiClient --- scm-ui/src/apiclient.js | 55 +++++-------------------------- scm-ui/src/users/modules/users.js | 4 +-- 2 files changed, 11 insertions(+), 48 deletions(-) diff --git a/scm-ui/src/apiclient.js b/scm-ui/src/apiclient.js index bcddf040aa..f8cfc4db70 100644 --- a/scm-ui/src/apiclient.js +++ b/scm-ui/src/apiclient.js @@ -27,7 +27,7 @@ function handleStatusCode(response: Response) { } export function createUrl(url: string) { - if (url.indexOf("://") > 0) { + if (url.includes("://")) { return url; } let urlWithStartingSlash = url; @@ -42,26 +42,12 @@ class ApiClient { return fetch(createUrl(url), fetchOptions).then(handleStatusCode); } - post(url: string, payload: any) { - return this.httpRequestWithJSONBody(url, payload, "POST"); + post(url: string, payload: any, contentType: string = "application/json") { + return this.httpRequestWithJSONBody("POST", url, contentType, payload); } - postWithContentType(url: string, payload: any, contentType: string) { - return this.httpRequestWithContentType( - url, - "POST", - JSON.stringify(payload), - contentType - ); - } - - putWithContentType(url: string, payload: any, contentType: string) { - return this.httpRequestWithContentType( - url, - "PUT", - JSON.stringify(payload), - contentType - ); + put(url: string, payload: any, contentType: string = "application/json") { + return this.httpRequestWithJSONBody("PUT", url, contentType, payload); } delete(url: string): Promise { @@ -73,37 +59,14 @@ class ApiClient { } httpRequestWithJSONBody( - url: string, - payload: any, - method: string - ): Promise { - // let options: RequestOptions = { - // method: method, - // body: JSON.stringify(payload) - // }; - // options = Object.assign(options, fetchOptions); - // // $FlowFixMe - // options.headers["Content-Type"] = "application/json"; - - // return fetch(createUrl(url), options).then(handleStatusCode); - - return this.httpRequestWithContentType( - url, - method, - JSON.stringify(payload), - "application/json" - ).then(handleStatusCode); - } - - httpRequestWithContentType( - url: string, method: string, - payload: any, - contentType: string + url: string, + contentType: string, + payload: any ): Promise { let options: RequestOptions = { method: method, - body: payload + body: JSON.stringify(payload) }; options = Object.assign(options, fetchOptions); // $FlowFixMe diff --git a/scm-ui/src/users/modules/users.js b/scm-ui/src/users/modules/users.js index afd77021af..767c27119b 100644 --- a/scm-ui/src/users/modules/users.js +++ b/scm-ui/src/users/modules/users.js @@ -143,7 +143,7 @@ export function createUser(user: User, callback?: () => void) { return function(dispatch: Dispatch) { dispatch(createUserPending(user)); return apiClient - .postWithContentType(USERS_URL, user, CONTENT_TYPE_USER) + .post(USERS_URL, user, CONTENT_TYPE_USER) .then(() => { dispatch(createUserSuccess()); if (callback) { @@ -192,7 +192,7 @@ export function modifyUser(user: User, callback?: () => void) { return function(dispatch: Dispatch) { dispatch(modifyUserPending(user)); return apiClient - .putWithContentType(user._links.update.href, user, CONTENT_TYPE_USER) + .put(user._links.update.href, user, CONTENT_TYPE_USER) .then(() => { dispatch(modifyUserSuccess(user)); if (callback) {