From f0e97c915b4d095f5a3f5056b34e2c70f1e8507b Mon Sep 17 00:00:00 2001 From: Johann Hoffmann Date: Thu, 5 Dec 2024 13:53:08 +0100 Subject: [PATCH] rename bindKeys() to addEndpoints(); removed Vue.reactive on $fhcApi; check if Api has been instantiated in install, only addEndpoints if that is the case; --- public/js/plugin/FhcApi.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/public/js/plugin/FhcApi.js b/public/js/plugin/FhcApi.js index c25204d49..0845a1a5e 100644 --- a/public/js/plugin/FhcApi.js +++ b/public/js/plugin/FhcApi.js @@ -4,6 +4,12 @@ import FhcApiFactory from '../api/fhcapifactory.js'; export default { install: (app, options) => { + if (app.config.globalProperties.$fhcApi) { + if (options?.factory) { + app.config.globalProperties.$fhcApi.factory.addEndpoints(options.factory); + } + return; + } app.use(FhcAlert); function _get_config(form, uri, data, config) { @@ -136,7 +142,7 @@ export default { return Promise.reject(error); }); - app.config.globalProperties.$fhcApi = Vue.reactive({ + app.config.globalProperties.$fhcApi = { getUri(url) { return fhcApiAxios.getUri({url}); }, @@ -289,38 +295,43 @@ export default { $fhcAlert.alertDefault('error', error.message, message); } } - }); + }; class FhcApiFactoryWrapper { constructor(factorypart, root) { - if (root === undefined) + if (root === undefined) { this.$fhcApi = app.config.globalProperties.$fhcApi; - else + this.$fhcApi.factory = this; + } else { Object.defineProperty(this, '$fhcApi', { get() { return (root || this).$fhcApi; } }) + } - this.bindKeys(factorypart, root) + this.addEndpoints(factorypart) } - bindKeys(factorypart, root) { + addEndpoints(factorypart) { Object.keys(factorypart).forEach(key => { Object.defineProperty(this, key, { get() { if (typeof factorypart[key] == 'function') return factorypart[key].bind(this); - return new FhcApiFactoryWrapper(factorypart[key], root || this); + return new FhcApiFactoryWrapper(factorypart[key], this.$fhcApi.factory); } }); }); } } - const mergedFhcApiFactory = options?.factory ? {...FhcApiFactory, ...options.factory} : FhcApiFactory; + const factory = new FhcApiFactoryWrapper(FhcApiFactory); + if (options?.factory) + factory.addEndpoints(options.factory); + + app.config.globalProperties.$fhcApi.factory = factory; - app.config.globalProperties.$fhcApi.factory = new FhcApiFactoryWrapper(mergedFhcApiFactory); app.provide('$fhcApi', app.config.globalProperties.$fhcApi); } }; \ No newline at end of file