From 0763ae94406658aa96e01c884189cf0edf479557 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 20 Nov 2019 08:29:05 +0100 Subject: [PATCH 1/5] Add optional heades for api client push and put --- scm-ui/ui-components/src/apiclient.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/scm-ui/ui-components/src/apiclient.ts b/scm-ui/ui-components/src/apiclient.ts index babb0b64d7..3fb03b067f 100644 --- a/scm-ui/ui-components/src/apiclient.ts +++ b/scm-ui/ui-components/src/apiclient.ts @@ -80,23 +80,24 @@ class ApiClient { return fetch(createUrl(url), applyFetchOptions({})).then(handleFailure); } - post(url: string, payload?: any, contentType = "application/json") { - return this.httpRequestWithJSONBody("POST", url, contentType, payload); + post(url: string, payload?: any, contentType = "application/json", additionalHeaders = new Headers()) { + return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload); } - postBinary(url: string, fileAppender: (p: FormData) => void) { + postBinary(url: string, fileAppender: (p: FormData) => void, additionalHeaders = new Headers()) { const formData = new FormData(); fileAppender(formData); const options: RequestInit = { method: "POST", - body: formData + body: formData, + headers: additionalHeaders }; return this.httpRequestWithBinaryBody(options, url); } - put(url: string, payload: any, contentType = "application/json") { - return this.httpRequestWithJSONBody("PUT", url, contentType, payload); + put(url: string, payload: any, contentType = "application/json", additionalHeaders = new Headers()) { + return this.httpRequestWithJSONBody("PUT", url, contentType, additionalHeaders, payload); } head(url: string) { @@ -115,9 +116,16 @@ class ApiClient { return fetch(createUrl(url), options).then(handleFailure); } - httpRequestWithJSONBody(method: string, url: string, contentType: string, payload?: any): Promise { + httpRequestWithJSONBody( + method: string, + url: string, + contentType: string, + additionalHeaders: Headers, + payload?: any + ): Promise { const options: RequestInit = { - method: method + method: method, + headers: additionalHeaders }; if (payload) { options.body = JSON.stringify(payload); From 20659bc32e23950e74f2d7d3c1260d3710044cc3 Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 20 Nov 2019 08:59:57 +0100 Subject: [PATCH 2/5] Add post and put with text --- scm-ui/ui-components/src/apiclient.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/scm-ui/ui-components/src/apiclient.ts b/scm-ui/ui-components/src/apiclient.ts index 3fb03b067f..4f2a8a735e 100644 --- a/scm-ui/ui-components/src/apiclient.ts +++ b/scm-ui/ui-components/src/apiclient.ts @@ -84,6 +84,14 @@ class ApiClient { return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload); } + postText(url: string, payload: string, additionalHeaders = new Headers()) { + return this.httpRequestWithTextBody("POST", url, additionalHeaders, payload); + } + + putText(url: string, payload: string, additionalHeaders = new Headers()) { + return this.httpRequestWithTextBody("PUT", url, additionalHeaders, payload); + } + postBinary(url: string, fileAppender: (p: FormData) => void, additionalHeaders = new Headers()) { const formData = new FormData(); fileAppender(formData); @@ -133,6 +141,15 @@ class ApiClient { return this.httpRequestWithBinaryBody(options, url, contentType); } + httpRequestWithTextBody(method: string, url: string, additionalHeaders: Headers, payload: string) { + const options: RequestInit = { + method: method, + headers: additionalHeaders + }; + options.body = payload; + return this.httpRequestWithBinaryBody(options, url, "text/plain"); + } + httpRequestWithBinaryBody(options: RequestInit, url: string, contentType?: string) { options = applyFetchOptions(options); if (contentType) { From a2474896cc38c7858f9a4dcf036c7dba3a4ef8ed Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 20 Nov 2019 10:44:11 +0100 Subject: [PATCH 3/5] Fix types --- scm-ui/ui-components/src/apiclient.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/scm-ui/ui-components/src/apiclient.ts b/scm-ui/ui-components/src/apiclient.ts index 4f2a8a735e..4c1bad8ac1 100644 --- a/scm-ui/ui-components/src/apiclient.ts +++ b/scm-ui/ui-components/src/apiclient.ts @@ -80,19 +80,19 @@ class ApiClient { return fetch(createUrl(url), applyFetchOptions({})).then(handleFailure); } - post(url: string, payload?: any, contentType = "application/json", additionalHeaders = new Headers()) { + post(url: string, payload?: any, contentType = "application/json", additionalHeaders: Record = {}) { return this.httpRequestWithJSONBody("POST", url, contentType, additionalHeaders, payload); } - postText(url: string, payload: string, additionalHeaders = new Headers()) { + postText(url: string, payload: string, additionalHeaders: Record = {}) { return this.httpRequestWithTextBody("POST", url, additionalHeaders, payload); } - putText(url: string, payload: string, additionalHeaders = new Headers()) { + putText(url: string, payload: string, additionalHeaders: Record = {}) { return this.httpRequestWithTextBody("PUT", url, additionalHeaders, payload); } - postBinary(url: string, fileAppender: (p: FormData) => void, additionalHeaders = new Headers()) { + postBinary(url: string, fileAppender: (p: FormData) => void, additionalHeaders: Record = {}) { const formData = new FormData(); fileAppender(formData); @@ -104,7 +104,7 @@ class ApiClient { return this.httpRequestWithBinaryBody(options, url); } - put(url: string, payload: any, contentType = "application/json", additionalHeaders = new Headers()) { + put(url: string, payload: any, contentType = "application/json", additionalHeaders: Record = {}) { return this.httpRequestWithJSONBody("PUT", url, contentType, additionalHeaders, payload); } @@ -128,7 +128,7 @@ class ApiClient { method: string, url: string, contentType: string, - additionalHeaders: Headers, + additionalHeaders: Record, payload?: any ): Promise { const options: RequestInit = { @@ -141,7 +141,12 @@ class ApiClient { return this.httpRequestWithBinaryBody(options, url, contentType); } - httpRequestWithTextBody(method: string, url: string, additionalHeaders: Headers, payload: string) { + httpRequestWithTextBody( + method: string, + url: string, + additionalHeaders: Record = {}, + payload: string + ) { const options: RequestInit = { method: method, headers: additionalHeaders @@ -156,7 +161,7 @@ class ApiClient { if (!options.headers) { options.headers = new Headers(); } - // @ts-ignore + // @ts-ignore We are sure that here we only get headers of type Record options.headers["Content-Type"] = contentType; } From cf17e07cea26b852d2a09d0390e1f20a569729cc Mon Sep 17 00:00:00 2001 From: Rene Pfeuffer Date: Wed, 20 Nov 2019 11:12:22 +0100 Subject: [PATCH 4/5] Do not overwrite existing headers in applyFetchOptions --- scm-ui/ui-components/src/apiclient.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/scm-ui/ui-components/src/apiclient.ts b/scm-ui/ui-components/src/apiclient.ts index 4c1bad8ac1..473bf3b925 100644 --- a/scm-ui/ui-components/src/apiclient.ts +++ b/scm-ui/ui-components/src/apiclient.ts @@ -27,13 +27,17 @@ const extractXsrfToken = () => { }; const applyFetchOptions: (p: RequestInit) => RequestInit = o => { - const headers: { [key: string]: string } = { - Cache: "no-cache", - // identify the request as ajax request - "X-Requested-With": "XMLHttpRequest", - // identify the web interface - "X-SCM-Client": "WUI" - }; + if (!o.headers) { + o.headers = {}; + } + + // @ts-ignore We are sure that here we only get headers of type Record + const headers: Record = o.headers; + headers["Cache"] = "no-cache"; + // identify the request as ajax request + headers["X-Requested-With"] = "XMLHttpRequest"; + // identify the web interface + headers["X-SCM-Client"] = "WUI"; const xsrf = extractXsrfToken(); if (xsrf) { @@ -159,7 +163,7 @@ class ApiClient { options = applyFetchOptions(options); if (contentType) { if (!options.headers) { - options.headers = new Headers(); + options.headers = {}; } // @ts-ignore We are sure that here we only get headers of type Record options.headers["Content-Type"] = contentType; From ba8c2f4ce734122132ed581daa9412e3d40de012 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 20 Nov 2019 15:23:10 +0000 Subject: [PATCH 5/5] Close branch feature/add_optional_headers_in_apiclient