From 46e6eca20f550ff19a988f5afd604c2eba41de46 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 15 Feb 2023 10:43:46 +0100 Subject: [PATCH] Bug 32939: Introduce count Not used yet, will replace myFetchTotal Signed-off-by: Matt Blenkinsop Signed-off-by: Nick Clemens --- .../prog/js/vue/fetch/http-client.js | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js index d1b5a917b9..476440dadd 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/http-client.js @@ -8,13 +8,18 @@ class HttpClient { }; } - async _fetchJSON(endpoint, headers = {}, options = {}) { + async _fetchJSON( + endpoint, + headers = {}, + options = {}, + return_response = false + ) { let res; await fetch(this._baseURL + endpoint, { ...options, headers: { ...this._headers, ...headers }, }) - .then(this.checkError) + .then((response) => this.checkError(response, return_response)) .then( (result) => { res = result; @@ -60,6 +65,21 @@ class HttpClient { }); } + count(params = {}) { + let res; + this._fetchJSON(params.endpoint, params.headers, 1).then( + (response) => { + if (response) { + res = response.headers.get("X-Total-Count"); + } + }, + (error) => { + setError(error.toString()); + } + ); + return res; + } + checkError(response, return_response = 0) { if (response.status >= 200 && response.status <= 299) { return return_response ? response : response.json(); @@ -69,8 +89,6 @@ class HttpClient { throw Error("%s (%s)".format(response.statusText, response.status)); } } - - //TODO: Implement count method } export default HttpClient; -- 2.30.2