import { Client, InitConfig } from '@jovotech/client-web';
import { PluginObject } from 'vue';

declare global {
  interface Window {
    JovoWebClientVue?: typeof import('.');
  }
}

declare module 'vue/types/vue' {
  interface Vue {
    $client: Client;
  }
}

export interface JovoWebClientVueConfig {
  endpointUrl: string;
  config?: InitConfig;
}

export type PluginConfig = JovoWebClientVueConfig | Client;

export * from '@jovotech/client-web';

const plugin: PluginObject<PluginConfig> = {
  install: (vue, configOrClient) => {
    if (!(configOrClient instanceof Client)) {
      if (!configOrClient?.endpointUrl) {
        throw new Error(
          `At least the 'endpointUrl' option has to be set in order to use the JovoWebClientPlugin. `,
        );
      }
      configOrClient = new Client(configOrClient.endpointUrl, configOrClient.config);
    }
    // make the client reactive
    vue.prototype.$client = vue.observable(configOrClient);
  },
};

export default plugin;

import { Client, InitConfig } from '@jovotech/client-web';
import { PluginObject } from 'vue';

declare global {
  interface Window {
    JovoWebClientVue?: typeof import('.');
  }
}

declare module 'vue/types/vue' {
  interface Vue {
    $client: Client;
  }
}

export interface JovoWebClientVueConfig {
  endpointUrl: string;
  config?: InitConfig;
}

export * from '@jovotech/client-web';

const plugin: PluginObject<JovoWebClientVueConfig> = {
  install: (vue, config) => {
    if (!config?.endpointUrl) {
      throw new Error(
        `At least the 'endpointUrl' option has to be set in order to use the JovoWebClientPlugin. `,
      );
    }
    const client = new Client(config.endpointUrl, config.config);
    // make the client reactive
    vue.prototype.$client = vue.observable(client);
  },
};

export default plugin;

--- 
+++ 
@@ -18,18 +18,22 @@
   config?: InitConfig;
 }
 
+export type PluginConfig = JovoWebClientVueConfig | Client;
+
 export * from '@jovotech/client-web';
 
-const plugin: PluginObject<JovoWebClientVueConfig> = {
-  install: (vue, config) => {
-    if (!config?.endpointUrl) {
-      throw new Error(
-        `At least the 'endpointUrl' option has to be set in order to use the JovoWebClientPlugin. `,
-      );
+const plugin: PluginObject<PluginConfig> = {
+  install: (vue, configOrClient) => {
+    if (!(configOrClient instanceof Client)) {
+      if (!configOrClient?.endpointUrl) {
+        throw new Error(
+          `At least the 'endpointUrl' option has to be set in order to use the JovoWebClientPlugin. `,
+        );
+      }
+      configOrClient = new Client(configOrClient.endpointUrl, configOrClient.config);
     }
-    const client = new Client(config.endpointUrl, config.config);
     // make the client reactive
-    vue.prototype.$client = vue.observable(client);
+    vue.prototype.$client = vue.observable(configOrClient);
   },
 };
 
:sparkles: Allow directly passing a Client-instance to the vue2 plugin
