From 457e7d22759451f696bf0aaf52699cd380913e2d Mon Sep 17 00:00:00 2001 From: Jake Deery Date: Mon, 1 Dec 2025 14:37:22 +0000 Subject: [PATCH] Bug 14962: On Display code for API Clients This patch adds the required API clients for use with the On Display module. Please see the test files commit for instructions on how to test this bundle of patches. --- .../intranet-tmpl/prog/js/fetch/api-client.js | 6 + .../prog/js/fetch/biblio-api-client.js | 25 ++++ .../prog/js/fetch/display-api-client.js | 121 ++++++++++++++++++ .../prog/js/fetch/http-client.js | 6 + .../prog/js/fetch/item-api-client.js | 12 ++ .../prog/js/fetch/item-type-api-client.js | 21 +++ .../prog/js/fetch/library-api-client.js | 21 +++ 7 files changed, 212 insertions(+) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/biblio-api-client.js create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/display-api-client.js create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/item-type-api-client.js create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/library-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js index 918947db605..b9c71db3b53 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -187,10 +187,16 @@ export const APIClient = { () => import("./authorised-values-api-client.js") ), acquisition: createClientProxy(() => import("./acquisition-api-client.js")), + acquisition: createClientProxy(() => import("./acquisition-api-client.js")), + biblio: createClientProxy(() => import("./biblio-api-client.js")), cataloguing: createClientProxy(() => import("./cataloguing-api-client.js")), circulation: createClientProxy(() => import("./circulation-api-client.js")), club: createClientProxy(() => import("./club-api-client.js")), cover_image: createClientProxy(() => import("./cover-image-api-client.js")), + display: createClientProxy(() => import("./display-api-client.js")), + item: createClientProxy(() => import("./item-api-client.js")), + item_type: createClientProxy(() => import("./item-type-api-client.js")), + library: createClientProxy(() => import("./library-api-client.js")), localization: createClientProxy( () => import("./localization-api-client.js") ), diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/biblio-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/biblio-api-client.js new file mode 100644 index 00000000000..6a0ba72553d --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/biblio-api-client.js @@ -0,0 +1,25 @@ +export class BiblioAPIClient { + constructor(HttpClient) { + this.httpClient = new HttpClient({ + baseURL: "/api/v1/", + }); + } + + get biblios() { + return { + getAll: (query, params, headers) => + this.httpClient.getAll({ + endpoint: "biblios", + query, + params, + headers, + }), + get: id => + this.httpClient.get({ + endpoint: "items/" + id, + }), + }; + } +} + +export default BiblioAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/display-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/display-api-client.js new file mode 100644 index 00000000000..0edc4250ae7 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/display-api-client.js @@ -0,0 +1,121 @@ +export class DisplayAPIClient { + constructor(HttpClient) { + this.httpClientDisplaysConfig = new HttpClient({ + baseURL: "/api/v1/displays/config/", + }); + + this.httpClientDisplays = new HttpClient({ + baseURL: "/api/v1/displays/", + }); + + this.httpClientDisplayItems = new HttpClient({ + baseURL: "/api/v1/display/items/", + }); + } + + get config() { + return { + get: () => + this.httpClientDisplaysConfig.get({ + endpoint: "", + }), + }; + } + + get displays() { + return { + create: display => + this.httpClientDisplays.post({ + endpoint: "", + body: display, + }), + get: id => + this.httpClientDisplays.get({ + endpoint: "" + id, + headers: { + "x-koha-embed": "display_items,library,item_type,+strings", + }, + }), + getAll: (query, params) => + this.httpClientDisplays.getAll({ + endpoint: "", + headers: { + "x-koha-embed": "display_items,library,item_type,+strings", + }, + params, + query, + }), + update: (display, id) => + this.httpClientDisplays.put({ + endpoint: "" + id, + body: display, + }), + delete: id => + this.httpClientDisplays.delete({ + endpoint: "" + id, + }), + count: (query = {}) => + this.httpClientDisplays.count({ + endpoint: + "?" + + new URLSearchParams({ + _page: 1, + _per_page: 1, + ...(query && { q: JSON.stringify(query) }), + }), + }), + }; + } + + get displayItems() { + return { + create: displayItem => + this.httpClientDisplayItems.post({ + endpoint: "", + body: displayItem, + }), + get: (display_id, itemnumber) => + this.httpClientDisplayItems.get({ + endpoint: "" + display_id + "/" + itemnumber, + }), + getAll: (query, params) => + this.httpClientDisplayItems.getAll({ + endpoint: "", + query, + params, + }), + update: (displayItem, display_id, itemnumber) => + this.httpClientDisplayItems.put({ + endpoint: "" + display_id + "/" + itemnumber, + body: displayItem, + }), + delete: (display_id, itemnumber) => + this.httpClientDisplayItems.delete({ + endpoint: "" + display_id + "/" + itemnumber, + }), + batchAdd: displayItems => + this.httpClientDisplayItems.post({ + endpoint: "batch", + body: displayItems, + }), + batchDelete: displayItems => + this.httpClientDisplayItems.delete({ + endpoint: "batch", + body: displayItems, + parseResponse: true, + }), + count: (query = {}) => + this.httpClientDisplayItems.count({ + endpoint: + "?" + + new URLSearchParams({ + _page: 1, + _per_page: 1, + ...(query && { q: JSON.stringify(query) }), + }), + }), + }; + } +} + +export default DisplayAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js index ee2722faa64..7cfc3539eb3 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js @@ -184,6 +184,11 @@ class HttpClient { } delete(params = {}) { + const body = params.body + ? typeof params.body === "string" + ? params.body + : JSON.stringify(params.body) + : undefined; let csrf_token = { "CSRF-TOKEN": this.csrf_token }; let headers = { ...csrf_token, ...params.headers }; return this._fetchJSON( @@ -192,6 +197,7 @@ class HttpClient { { parseResponse: false, ...params.options, + body, method: "DELETE", }, params.return_response ?? true, diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/item-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/item-api-client.js index c4a31f0fed5..f94ff01e8ce 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/item-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/item-api-client.js @@ -14,6 +14,18 @@ export class ItemAPIClient { params, headers, }), + get: id => + this.httpClient.get({ + endpoint: "items/" + id, + }), + getByExternalId: external_id => + this.httpClient.get({ + endpoint: "items?" + + new URLSearchParams({ + _match: 'starts_with', + external_id: external_id, + }), + }), }; } diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/item-type-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/item-type-api-client.js new file mode 100644 index 00000000000..c4ac342f876 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/item-type-api-client.js @@ -0,0 +1,21 @@ +export class ItemTypeAPIClient { + constructor(HttpClient) { + this.httpClient = new HttpClient({ + baseURL: "/api/v1/", + }); + } + + get item_types() { + return { + getAll: (query, params, headers) => + this.httpClient.getAll({ + endpoint: "item_types", + query, + params, + headers, + }), + }; + } +} + +export default ItemTypeAPIClient; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/library-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/library-api-client.js new file mode 100644 index 00000000000..474a19d1d4e --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/library-api-client.js @@ -0,0 +1,21 @@ +export class LibraryAPIClient { + constructor(HttpClient) { + this.httpClient = new HttpClient({ + baseURL: "/api/v1/", + }); + } + + get libraries() { + return { + getAll: (query, params, headers) => + this.httpClient.getAll({ + endpoint: "libraries", + query, + params, + headers, + }), + }; + } +} + +export default LibraryAPIClient; -- 2.50.1 (Apple Git-155)