All files plugin.ts

100% Statements 27/27
80% Branches 8/10
100% Functions 3/3
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x 1x                                                                                                                               1x         1x 19x 19x   19x 1x 1x 15x 15x 15x 1x 1x 15x 15x 15x 1x 1x 15x 15x     15x 15x 19x 1x  
import { RouterLink } from './router-link';
import { RouterView } from './router-view';
import { getRoute, getRouter } from './use';
import { isVue3 } from './util';
 
interface VueApp {
    config?: {
        globalProperties: Record<string, unknown>;
    };
    prototype?: Record<string, unknown>;
    component(name: string, component: unknown): void;
}
 
/**
 * Vue plugin for \@esmx/router integration.
 * Registers RouterLink and RouterView components globally.
 * Compatible with both Vue 2.7+ and Vue 3.
 *
 * @example
 *
 * Vue 3 installation
 *
 * ```typescript
 * import { createApp } from 'vue';
 * import { Router } from '@esmx/router';
 * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
 *
 * const routes = [
 *   { path: '/', component: Home },
 *   { path: '/about', component: About }
 * ];
 *
 * const router = new Router({ routes });
 * const app = createApp({
 *   setup() {
 *     useProvideRouter(router);
 *   }
 * });
 *
 * app.use(RouterPlugin);
 * app.mount('#app');
 * ```
 *
 * @example
 *
 * Vue 2 installation
 *
 * ```typescript
 * import Vue from 'vue';
 * import { Router } from '@esmx/router';
 * import { RouterPlugin, useProvideRouter } from '@esmx/router-vue';
 *
 * const routes = [
 *   { path: '/', component: Home },
 *   { path: '/about', component: About }
 * ];
 *
 * const router = new Router({ routes });
 * Vue.use(RouterPlugin);
 *
 * new Vue({
 *   setup() {
 *     useProvideRouter(router);
 *   }
 * }).$mount('#app');
 * ```
 */
export const RouterPlugin = {
    /**
     * Install the router plugin.
     * @param app Vue application instance (Vue 3) or Vue constructor (Vue 2)
     */
    install(app: unknown): void {
        const vueApp = app as VueApp;
        const target = vueApp.config?.globalProperties || vueApp.prototype;
 
        if (!target) {
            throw new Error('[@esmx/router-vue] Invalid Vue app instance');
        }
        Object.defineProperties(target, {
            $router: {
                get() {
                    return getRouter(isVue3 ? null : this);
                }
            },
            $route: {
                get() {
                    return getRoute(isVue3 ? null : this);
                }
            }
        });
 
        // Register global components
        vueApp.component('RouterLink', RouterLink);
        vueApp.component('RouterView', RouterView);
    }
};