From 74aecf7085500cf875b7940d7a9de677a49d9040 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 1 Apr 2024 12:35:58 -0300 Subject: [PATCH] Bug 36369: Introduce an APIClientBase class --- .../components/Admin/RecordSources/List.vue | 4 +- .../prog/js/vue/fetch/admin-api-client.js | 10 +++ .../prog/js/vue/fetch/api-client-base.js | 74 +++++++++++++++++++ .../prog/js/vue/fetch/api-client.js | 4 +- .../js/vue/fetch/record-sources-api-client.js | 44 +---------- 5 files changed, 90 insertions(+), 46 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/fetch/admin-api-client.js create mode 100644 koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client-base.js diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Admin/RecordSources/List.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Admin/RecordSources/List.vue index a1595b9e64f..4e45c3352d1 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/Admin/RecordSources/List.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/Admin/RecordSources/List.vue @@ -84,7 +84,7 @@ export default { }, methods: { async getRecordSourcesCount() { - const client = APIClient.record_sources + const client = APIClient.admin await client.record_sources.count().then( count => { this.record_sources_count = count @@ -112,7 +112,7 @@ export default { cancel_label: this.$__("No, do not delete"), }, () => { - const client = APIClient.record_sources + const client = APIClient.admin client.record_sources .delete(record_source.record_source_id) .then( diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/admin-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/admin-api-client.js new file mode 100644 index 00000000000..54755641848 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/admin-api-client.js @@ -0,0 +1,10 @@ +import RecordSourcesAPIClient from './record-sources-api-client'; + +export class AdminAPIClient { + + get record_sources() { + return new RecordSourcesAPIClient(); // maybe with some default embeds for the 'admin' context usage, etc + } +} + +export default AdminAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client-base.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client-base.js new file mode 100644 index 00000000000..d1b1660f854 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client-base.js @@ -0,0 +1,74 @@ +import HttpClient from "./http-client"; + +export class APIClientBase extends HttpClient { + + + // FIXME: All methods should accept: + // * 'embeds' and do the right thing (i.e. merge them with other headers) + // * custom headers (thinking of x-koha-override, Accept, etc) Even delete() should + // accept headers + create(resource, ...params) { + let headers = params.headers + if ( params['embeds'] ) { + + } + return this.post({ + endpoint: "", + body: resource, + headers: headers + }); + } + + delete(id) { + return this.delete({ + endpoint: this._baseURL + "/" + id, + }); + } + + update( resource, id ) { + return this.put({ + endpoint: this._baseURL + "/" + id, + body: resource, + }); + } + + patch( partial_resource, id ) { + return this.patch({ + endpoint: this._baseURL + "/" + id, + body: partial_resource, + }); + } + + get(id) { + return this.get({ + endpoint: this._baseURL + "/" + id, + }); + } + + getAll(query, params) { + return this.getAll({ + endpoint: this._baseURL, + query, + params, + headers: {}, + }); + } + + count(query = {}) { + return this.count({ + endpoint: + "?" + + new URLSearchParams({ + _page: 1, + _per_page: 1, + ...(query && { q: JSON.stringify(query) }), + }), + }); + } + + build_x_koha_embed(embeds) { + return { "x-koha-embed": embeds.join(",") }; + } +} + +export default APIClientBase; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js index 4bfd3c8dd0c..0ef07a707d0 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/api-client.js @@ -3,9 +3,9 @@ import PatronAPIClient from "./patron-api-client"; import AcquisitionAPIClient from "./acquisition-api-client"; import AVAPIClient from "./authorised-values-api-client"; import ItemAPIClient from "./item-api-client"; -import RecordSourcesAPIClient from "./record-sources-api-client"; import SysprefAPIClient from "./system-preferences-api-client"; import PreservationAPIClient from "./preservation-api-client"; +import AdminAPIClient from "./admin-api-client"; export const APIClient = { erm: new ERMAPIClient(), @@ -15,5 +15,5 @@ export const APIClient = { item: new ItemAPIClient(), sysprefs: new SysprefAPIClient(), preservation: new PreservationAPIClient(), - record_sources: new RecordSourcesAPIClient(), + admin: new AdminAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/record-sources-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/record-sources-api-client.js index b12882d1dca..f310e9f4c57 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/record-sources-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/fetch/record-sources-api-client.js @@ -1,51 +1,11 @@ -import HttpClient from "./http-client"; +import APIClientBase from "./api-client-base"; -export class RecordSourcesAPIClient extends HttpClient { +export class RecordSourcesAPIClient extends APIClientBase { constructor() { super({ baseURL: "/api/v1/record_sources", }); } - - get record_sources() { - return { - create: record_source => - this.post({ - endpoint: "", - body: record_source, - }), - delete: id => - this.delete({ - endpoint: "/" + id, - }), - update: (record_source, id) => - this.put({ - endpoint: "/" + id, - body: record_source, - }), - get: id => - this.get({ - endpoint: "/" + id, - }), - getAll: (query, params) => - this.getAll({ - endpoint: "/", - query, - params, - headers: {}, - }), - count: (query = {}) => - this.count({ - endpoint: - "?" + - new URLSearchParams({ - _page: 1, - _per_page: 1, - ...(query && { q: JSON.stringify(query) }), - }), - }), - }; - } } export default RecordSourcesAPIClient; -- 2.44.0