@aerogel/plugin-routing 0.0.0-next.1415dfbeeb962c022e1bb2abfd77d0237a5d23ad

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("virtual:aerogel"),t=require("@aerogel/core"),r=require("@babel/runtime/helpers/defineProperty"),u=require("vue"),n=require("@noeldemartin/utils"),o=require("vue-router");function s(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("core-js/modules/esnext.async-iterator.map.js"),require("core-js/modules/esnext.iterator.map.js");var a=s(e),i=s(r),c=t.defineServiceState({name:"router",initialState:{flashRoute:null},persist:["flashRoute"]});class l extends c{constructor(){super(),i.default(this,"currentRoute",void 0),i.default(this,"router",void 0),i.default(this,"bindings",void 0),i.default(this,"currentRouteParams",void 0),this.router=u.ref(null),this.bindings={},this.currentRouteParams=u.ref({}),this.currentRoute=u.computed((()=>this.router.value?{...u.unref(this.router.value.currentRoute),params:this.currentRouteParams.value}:null))}use(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.router.value=e,this.bindings=t.bindings??{}}updateRouteParams(e){this.currentRouteParams.value=e}async resolveBinding(e,t,r){if(Array.isArray(t))return t;return(this.bindings[e]??(()=>t))(t,r)}async boot(){t.Events.on("before-login",(()=>this.flashRoute=n.objectOnly(this.currentRoute.value,["path","query","hash"]))),t.Events.on("login",(async()=>{this.flashRoute&&(await this.replace(this.flashRoute),this.flashRoute=null)}))}__get(e){return this.router.value?super.__get(e)??Reflect.get(this.router.value,e):super.__get(e)}}var f=n.facade(new l);function h(e){const t=o.createRouter({history:o.createWebHistory(e.basePath),routes:e.routes});return t.beforeEach(n.once((()=>function(e){const t=n.Storage.pull("static-404-redirect");t&&e.replace(t)}(t)))),t}function d(e){const t=o.useRoute(),r=u.ref({});async function n(){r.value=await async function(){const e={...t.params};for(const[r,u]of Object.entries(t.params))e[r]=await f.resolveBinding(r,u,e);return e}(),f.updateRouteParams(r.value)}return o.onBeforeRouteUpdate((()=>n())),n(),()=>u.h(e,r.value)}function p(e){return u.defineComponent({setup:()=>d(e)})}function v(e){if("function"==typeof e.component){const t=e.component;e.component=async()=>{const{default:e}=await t();return p(e)}}else e.component&&(e.component=p(e.component));return e}const m={$router:f};exports.Router=f,exports.RouterService=l,exports.default=function(e){return{async install(r){const u=h({routes:e.routes,basePath:e.basePath??a.default.basePath});r.use(u),f.use(u,e),await t.bootServices(r,m)}}},exports.defineRoute=v,exports.defineRouteBindings=function(e){return e},exports.defineRoutes=function(e){return e.map(v)};
2
+ //# sourceMappingURL=aerogel-plugin-routing.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aerogel-plugin-routing.cjs.js","sources":["../src/services/Router.state.ts","../src/services/Router.ts","../src/router.ts","../src/utils.ts","../src/main.ts"],"sourcesContent":["import { defineServiceState } from '@aerogel/core';\nimport type { RouteLocationRaw } from 'vue-router';\n\nexport default defineServiceState({\n name: 'router',\n initialState: {\n flashRoute: null as RouteLocationRaw | null,\n },\n persist: ['flashRoute'],\n});\n","import { computed, ref, unref } from 'vue';\nimport { Events } from '@aerogel/core';\nimport { facade, objectOnly } from '@noeldemartin/utils';\nimport type { ComputedRef, Ref } from 'vue';\nimport type { RouteLocationNormalizedLoaded, RouteParamValue, Router } from 'vue-router';\n\nimport Service from './Router.state';\n\nexport type LoadedRoute = Omit<RouteLocationNormalizedLoaded, 'params'> & { params: Record<string, unknown> };\nexport type RouteBinding = (slug: string, params: Record<string, unknown>) => unknown | Promise<unknown>;\nexport type RouteBindings = Record<string, RouteBinding>;\n\nexport class RouterService extends Service {\n\n public readonly currentRoute: ComputedRef<LoadedRoute | null>;\n protected router: Ref<Router | null>;\n protected bindings: RouteBindings;\n protected currentRouteParams: Ref<Record<string, unknown>>;\n\n constructor() {\n super();\n\n this.router = ref(null);\n this.bindings = {};\n this.currentRouteParams = ref({});\n this.currentRoute = computed(() => {\n if (!this.router.value) {\n return null;\n }\n\n return {\n ...unref(this.router.value.currentRoute),\n params: this.currentRouteParams.value,\n };\n });\n }\n\n public use(router: Router, options: { bindings?: RouteBindings } = {}): void {\n this.router.value = router;\n this.bindings = options.bindings ?? {};\n }\n\n public updateRouteParams(params: Record<string, unknown>): void {\n this.currentRouteParams.value = params;\n }\n\n public async resolveBinding(\n name: string,\n value: RouteParamValue | RouteParamValue[],\n params: Record<string, unknown>,\n ): Promise<unknown> {\n if (Array.isArray(value)) {\n return value;\n }\n\n const binding = this.bindings[name] ?? (() => value);\n\n return binding(value, params);\n }\n\n protected async boot(): Promise<void> {\n Events.on(\n 'before-login',\n () =>\n (this.flashRoute = objectOnly(this.currentRoute.value as unknown as Record<string, unknown>, [\n 'path',\n 'query',\n 'hash',\n ])),\n );\n Events.on('login', async () => {\n if (!this.flashRoute) {\n return;\n }\n\n await this.replace(this.flashRoute);\n\n this.flashRoute = null;\n });\n }\n\n protected __get(property: string): unknown {\n return this.router.value\n ? super.__get(property) ?? Reflect.get(this.router.value, property)\n : super.__get(property);\n }\n\n}\n\nexport interface RouterService extends Omit<Router, 'currentRoute'> {}\n\nexport default facade(new RouterService());\n","import { Storage, once } from '@noeldemartin/utils';\nimport { createRouter, createWebHistory } from 'vue-router';\nimport type { RouteLocationRaw, RouteRecordRaw, Router } from 'vue-router';\n\nfunction handleStatic404Redirect(router: Router): void {\n const route = Storage.pull<RouteLocationRaw>('static-404-redirect');\n\n route && router.replace(route);\n}\n\nexport function createAppRouter(options: { routes: RouteRecordRaw[]; basePath?: string }): Router {\n const router = createRouter({\n history: createWebHistory(options.basePath),\n routes: options.routes,\n });\n\n router.beforeEach(once(() => handleStatic404Redirect(router)));\n\n return router;\n}\n","import { defineComponent, h, ref } from 'vue';\nimport { onBeforeRouteUpdate, useRoute } from 'vue-router';\nimport type { Component, ConcreteComponent } from 'vue';\nimport type { RouteRecordRaw } from 'vue-router';\n\nimport Router from './services/Router';\nimport type { RouteBindings } from './services/Router';\n\nexport type AerogelRoute = RouteRecordRaw;\n\nfunction setupPageComponent(pageComponent: ConcreteComponent) {\n const route = useRoute();\n const params = ref<Record<string, unknown>>({});\n\n async function updateResolvedRouteParams() {\n params.value = await resolveRouteParameters();\n\n Router.updateRouteParams(params.value);\n }\n\n async function resolveRouteParameters(): Promise<Record<string, unknown>> {\n const resolvedParams: Record<string, unknown> = { ...route.params };\n\n for (const [paramName, paramValue] of Object.entries(route.params)) {\n resolvedParams[paramName] = await Router.resolveBinding(paramName, paramValue, resolvedParams);\n }\n\n return resolvedParams;\n }\n\n onBeforeRouteUpdate(() => updateResolvedRouteParams());\n updateResolvedRouteParams();\n\n return () => h(pageComponent, params.value);\n}\n\nfunction enhanceRouteComponent(pageComponent: ConcreteComponent): Component {\n return defineComponent({\n setup: () => setupPageComponent(pageComponent),\n });\n}\n\nexport function defineRoute(route: AerogelRoute): RouteRecordRaw {\n if (typeof route.component === 'function') {\n const lazyComponent = route.component as () => Promise<{ default: ConcreteComponent }>;\n\n route.component = async () => {\n const { default: component } = await lazyComponent();\n\n return enhanceRouteComponent(component);\n };\n } else if (route.component) {\n route.component = enhanceRouteComponent(route.component as ConcreteComponent);\n }\n\n return route;\n}\n\nexport function defineRouteBindings(bindings: RouteBindings): RouteBindings {\n return bindings;\n}\n\nexport function defineRoutes(routes: AerogelRoute[]): RouteRecordRaw[] {\n return routes.map(defineRoute);\n}\n","import Aerogel from 'virtual:aerogel';\n\nimport { bootServices } from '@aerogel/core';\nimport type { Plugin } from '@aerogel/core';\nimport type { RouteRecordRaw } from 'vue-router';\n\nimport Router from './services/Router';\nimport { createAppRouter } from './router';\nimport type { RouteBindings } from './services/Router';\n\nconst services = { $router: Router };\n\nexport * from './services/Router';\nexport * from './utils';\nexport { default as Router } from './services/Router';\n\nexport interface Options {\n routes: RouteRecordRaw[];\n bindings?: RouteBindings;\n basePath?: string;\n}\n\nexport type RoutingServices = typeof services;\n\nexport default function routing(options: Options): Plugin {\n return {\n async install(app) {\n const router = createAppRouter({\n routes: options.routes,\n basePath: options.basePath ?? Aerogel.basePath,\n });\n\n app.use(router);\n Router.use(router, options);\n\n await bootServices(app, services);\n },\n };\n}\n\ndeclare module '@vue/runtime-core' {\n interface ComponentCustomProperties extends RoutingServices {}\n}\n"],"names":["Service","defineServiceState","name","initialState","flashRoute","persist","RouterService","constructor","super","_defineProperty","this","router","ref","bindings","currentRouteParams","currentRoute","computed","value","unref","params","use","options","arguments","length","undefined","updateRouteParams","resolveBinding","Array","isArray","boot","Events","on","objectOnly","async","replace","__get","property","Reflect","get","Router","facade","createAppRouter","createRouter","history","createWebHistory","basePath","routes","beforeEach","once","route","Storage","pull","handleStatic404Redirect","setupPageComponent","pageComponent","useRoute","updateResolvedRouteParams","resolvedParams","paramName","paramValue","Object","entries","resolveRouteParameters","onBeforeRouteUpdate","h","enhanceRouteComponent","defineComponent","setup","defineRoute","component","lazyComponent","default","services","$router","install","app","Aerogel","bootServices","map"],"mappings":"icAGAA,EAAeC,qBAAmB,CAC9BC,KAAM,SACNC,aAAc,CACVC,WAAY,MAEhBC,QAAS,CAAC,gBCIR,MAAOC,UAAsBN,EAO/BO,WAAAA,GACIC,QAAQC,EAAAA,QAAAC,KAAA,oBAAA,GAAAD,EAAAA,QAAAC,KAAA,cAAA,GAAAD,EAAAA,QAAAC,KAAA,gBAAA,GAAAD,EAAAA,QAAAC,KAAA,0BAAA,GAERA,KAAKC,OAASC,MAAI,MAClBF,KAAKG,SAAW,GAChBH,KAAKI,mBAAqBF,MAAI,CAAA,GAC9BF,KAAKK,aAAeC,EAAAA,UAAS,IACpBN,KAAKC,OAAOM,MAIV,IACAC,EAAKA,MAACR,KAAKC,OAAOM,MAAMF,cAC3BI,OAAQT,KAAKI,mBAAmBG,OALzB,MAQnB,CAEOG,GAAAA,CAAIT,GAA0D,IAA1CU,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAwC,CAAA,EAC/DZ,KAAKC,OAAOM,MAAQN,E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AAYF,GAGnF,OAAOA,CACX,CAbyBK,GAErBvB,EAAOd,kBAAkBN,EAAOF,MACpC,CAeA,OAHA8C,uBAAoB,IAAMP,MAC1BA,IAEO,IAAMQ,EAAAA,EAAEV,EAAenC,EAAOF,MACzC,CAEA,SAASgD,EAAsBX,GAC3B,OAAOY,kBAAgB,CACnBC,MAAOA,IAAMd,EAAmBC,IAExC,CAEM,SAAUc,EAAYnB,GACxB,GAA+B,mBAApBA,EAAMoB,UAA0B,CACvC,MAAMC,EAAgBrB,EAAMoB,UAE5BpB,EAAMoB,UAAYpC,UACd,MAAQsC,QAASF,SAAoBC,IAErC,OAAOL,EAAsBI,EAAU,CAE9C,MAAUpB,EAAMoB,YACbpB,EAAMoB,UAAYJ,EAAsBhB,EAAMoB,YAGlD,OAAOpB,CACX,CC9CA,MAAMuB,EAAW,CAAEC,QAASlC,4DAcJ,SAAQlB,GAC5B,MAAO,CACH,aAAMqD,CAAQC,GACV,MAAMhE,EAAS8B,EAAgB,CAC3BK,OAAQzB,EAAQyB,OAChBD,SAAUxB,EAAQwB,UAAY+B,EAAAA,QAAQ/B,WAG1C8B,EAAIvD,IAAIT,GACR4B,EAAOnB,IAAIT,EAAQU,SAEbwD,EAAYA,aAACF,EAAKH,EAC5B,EAER,oDDoBM,SAA8B3D,GAChC,OAAOA,CACX,uBAEM,SAAuBiC,GACzB,OAAOA,EAAOgC,IAAIV,EACtB"}
@@ -0,0 +1,76 @@
1
+ import type { ComputedRef } from 'vue';
2
+ import { Constructor } from '@noeldemartin/utils';
3
+ import { Facade } from '@noeldemartin/utils';
4
+ import type { Plugin as Plugin_2 } from '@aerogel/core';
5
+ import type { Ref } from 'vue';
6
+ import type { RouteLocationNormalizedLoaded } from 'vue-router';
7
+ import type { RouteLocationRaw } from 'vue-router';
8
+ import type { RouteParamValue } from 'vue-router';
9
+ import type { Router as Router_2 } from 'vue-router';
10
+ import type { RouteRecordRaw } from 'vue-router';
11
+ import { Service } from '@aerogel/core';
12
+
13
+ export declare type AerogelRoute = RouteRecordRaw;
14
+
15
+ declare const _default: Constructor< {
16
+ flashRoute: RouteLocationRaw | null;
17
+ }> & Constructor< {}> & Constructor<Service< {
18
+ flashRoute: RouteLocationRaw | null;
19
+ }, {}, Partial<{
20
+ flashRoute: RouteLocationRaw | null;
21
+ }>>>;
22
+
23
+ export declare function defineRoute(route: AerogelRoute): RouteRecordRaw;
24
+
25
+ export declare function defineRouteBindings(bindings: RouteBindings): RouteBindings;
26
+
27
+ export declare function defineRoutes(routes: AerogelRoute[]): RouteRecordRaw[];
28
+
29
+ export declare type LoadedRoute = Omit<RouteLocationNormalizedLoaded, 'params'> & {
30
+ params: Record<string, unknown>;
31
+ };
32
+
33
+ export declare interface Options {
34
+ routes: RouteRecordRaw[];
35
+ bindings?: RouteBindings;
36
+ basePath?: string;
37
+ }
38
+
39
+ export declare type RouteBinding = (slug: string, params: Record<string, unknown>) => unknown | Promise<unknown>;
40
+
41
+ export declare type RouteBindings = Record<string, RouteBinding>;
42
+
43
+ export declare const Router: Facade<RouterService, Constructor<RouterService>>;
44
+
45
+ export declare class RouterService extends _default {
46
+ readonly currentRoute: ComputedRef<LoadedRoute | null>;
47
+ protected router: Ref<Router_2 | null>;
48
+ protected bindings: RouteBindings;
49
+ protected currentRouteParams: Ref<Record<string, unknown>>;
50
+ constructor();
51
+ use(router: Router_2, options?: {
52
+ bindings?: RouteBindings;
53
+ }): void;
54
+ updateRouteParams(params: Record<string, unknown>): void;
55
+ resolveBinding(name: string, value: RouteParamValue | RouteParamValue[], params: Record<string, unknown>): Promise<unknown>;
56
+ protected boot(): Promise<void>;
57
+ protected __get(property: string): unknown;
58
+ }
59
+
60
+ export declare interface RouterService extends Omit<Router_2, 'currentRoute'> {
61
+ }
62
+
63
+ declare function routing(options: Options): Plugin_2;
64
+ export default routing;
65
+
66
+ export declare type RoutingServices = typeof services;
67
+
68
+ declare const services: {
69
+ $router: Facade<RouterService, Constructor<RouterService>>;
70
+ };
71
+
72
+ export { }
73
+
74
+ declare module '@vue/runtime-core' {
75
+ interface ComponentCustomProperties extends RoutingServices {}
76
+ }
@@ -0,0 +1,2 @@
1
+ import t from"virtual:aerogel";import{defineServiceState as e,Events as r,bootServices as o}from"@aerogel/core";import n from"@babel/runtime/helpers/esm/defineProperty";import{ref as s,computed as u,unref as a,defineComponent as i,h as c}from"vue";import{facade as l,objectOnly as h,once as m,Storage as p}from"@noeldemartin/utils";import{createRouter as f,createWebHistory as v,useRoute as d,onBeforeRouteUpdate as R}from"vue-router";import"core-js/modules/esnext.async-iterator.map.js";import"core-js/modules/esnext.iterator.map.js";var g=e({name:"router",initialState:{flashRoute:null},persist:["flashRoute"]});class b extends g{constructor(){super(),n(this,"currentRoute",void 0),n(this,"router",void 0),n(this,"bindings",void 0),n(this,"currentRouteParams",void 0),this.router=s(null),this.bindings={},this.currentRouteParams=s({}),this.currentRoute=u((()=>this.router.value?{...a(this.router.value.currentRoute),params:this.currentRouteParams.value}:null))}use(t){let e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};this.router.value=t,this.bindings=e.bindings??{}}updateRouteParams(t){this.currentRouteParams.value=t}async resolveBinding(t,e,r){if(Array.isArray(e))return e;return(this.bindings[t]??(()=>e))(e,r)}async boot(){r.on("before-login",(()=>this.flashRoute=h(this.currentRoute.value,["path","query","hash"]))),r.on("login",(async()=>{this.flashRoute&&(await this.replace(this.flashRoute),this.flashRoute=null)}))}__get(t){return this.router.value?super.__get(t)??Reflect.get(this.router.value,t):super.__get(t)}}var y=l(new b);function P(t){const e=f({history:v(t.basePath),routes:t.routes});return e.beforeEach(m((()=>function(t){const e=p.pull("static-404-redirect");e&&t.replace(e)}(e)))),e}function w(t){const e=d(),r=s({});async function o(){r.value=await async function(){const t={...e.params};for(const[r,o]of Object.entries(e.params))t[r]=await y.resolveBinding(r,o,t);return t}(),y.updateRouteParams(r.value)}return R((()=>o())),o(),()=>c(t,r.value)}function _(t){return i({setup:()=>w(t)})}function j(t){if("function"==typeof t.component){const e=t.component;t.component=async()=>{const{default:t}=await e();return _(t)}}else t.component&&(t.component=_(t.component));return t}function x(t){return t}function A(t){return t.map(j)}const B={$router:y};function q(e){return{async install(r){const n=P({routes:e.routes,basePath:e.basePath??t.basePath});r.use(n),y.use(n,e),await o(r,B)}}}export{y as Router,b as RouterService,q as default,j as defineRoute,x as defineRouteBindings,A as defineRoutes};
2
+ //# sourceMappingURL=aerogel-plugin-routing.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aerogel-plugin-routing.esm.js","sources":["../src/services/Router.state.ts","../src/services/Router.ts","../src/router.ts","../src/utils.ts","../src/main.ts"],"sourcesContent":["import { defineServiceState } from '@aerogel/core';\nimport type { RouteLocationRaw } from 'vue-router';\n\nexport default defineServiceState({\n name: 'router',\n initialState: {\n flashRoute: null as RouteLocationRaw | null,\n },\n persist: ['flashRoute'],\n});\n","import { computed, ref, unref } from 'vue';\nimport { Events } from '@aerogel/core';\nimport { facade, objectOnly } from '@noeldemartin/utils';\nimport type { ComputedRef, Ref } from 'vue';\nimport type { RouteLocationNormalizedLoaded, RouteParamValue, Router } from 'vue-router';\n\nimport Service from './Router.state';\n\nexport type LoadedRoute = Omit<RouteLocationNormalizedLoaded, 'params'> & { params: Record<string, unknown> };\nexport type RouteBinding = (slug: string, params: Record<string, unknown>) => unknown | Promise<unknown>;\nexport type RouteBindings = Record<string, RouteBinding>;\n\nexport class RouterService extends Service {\n\n public readonly currentRoute: ComputedRef<LoadedRoute | null>;\n protected router: Ref<Router | null>;\n protected bindings: RouteBindings;\n protected currentRouteParams: Ref<Record<string, unknown>>;\n\n constructor() {\n super();\n\n this.router = ref(null);\n this.bindings = {};\n this.currentRouteParams = ref({});\n this.currentRoute = computed(() => {\n if (!this.router.value) {\n return null;\n }\n\n return {\n ...unref(this.router.value.currentRoute),\n params: this.currentRouteParams.value,\n };\n });\n }\n\n public use(router: Router, options: { bindings?: RouteBindings } = {}): void {\n this.router.value = router;\n this.bindings = options.bindings ?? {};\n }\n\n public updateRouteParams(params: Record<string, unknown>): void {\n this.currentRouteParams.value = params;\n }\n\n public async resolveBinding(\n name: string,\n value: RouteParamValue | RouteParamValue[],\n params: Record<string, unknown>,\n ): Promise<unknown> {\n if (Array.isArray(value)) {\n return value;\n }\n\n const binding = this.bindings[name] ?? (() => value);\n\n return binding(value, params);\n }\n\n protected async boot(): Promise<void> {\n Events.on(\n 'before-login',\n () =>\n (this.flashRoute = objectOnly(this.currentRoute.value as unknown as Record<string, unknown>, [\n 'path',\n 'query',\n 'hash',\n ])),\n );\n Events.on('login', async () => {\n if (!this.flashRoute) {\n return;\n }\n\n await this.replace(this.flashRoute);\n\n this.flashRoute = null;\n });\n }\n\n protected __get(property: string): unknown {\n return this.router.value\n ? super.__get(property) ?? Reflect.get(this.router.value, property)\n : super.__get(property);\n }\n\n}\n\nexport interface RouterService extends Omit<Router, 'currentRoute'> {}\n\nexport default facade(new RouterService());\n","import { Storage, once } from '@noeldemartin/utils';\nimport { createRouter, createWebHistory } from 'vue-router';\nimport type { RouteLocationRaw, RouteRecordRaw, Router } from 'vue-router';\n\nfunction handleStatic404Redirect(router: Router): void {\n const route = Storage.pull<RouteLocationRaw>('static-404-redirect');\n\n route && router.replace(route);\n}\n\nexport function createAppRouter(options: { routes: RouteRecordRaw[]; basePath?: string }): Router {\n const router = createRouter({\n history: createWebHistory(options.basePath),\n routes: options.routes,\n });\n\n router.beforeEach(once(() => handleStatic404Redirect(router)));\n\n return router;\n}\n","import { defineComponent, h, ref } from 'vue';\nimport { onBeforeRouteUpdate, useRoute } from 'vue-router';\nimport type { Component, ConcreteComponent } from 'vue';\nimport type { RouteRecordRaw } from 'vue-router';\n\nimport Router from './services/Router';\nimport type { RouteBindings } from './services/Router';\n\nexport type AerogelRoute = RouteRecordRaw;\n\nfunction setupPageComponent(pageComponent: ConcreteComponent) {\n const route = useRoute();\n const params = ref<Record<string, unknown>>({});\n\n async function updateResolvedRouteParams() {\n params.value = await resolveRouteParameters();\n\n Router.updateRouteParams(params.value);\n }\n\n async function resolveRouteParameters(): Promise<Record<string, unknown>> {\n const resolvedParams: Record<string, unknown> = { ...route.params };\n\n for (const [paramName, paramValue] of Object.entries(route.params)) {\n resolvedParams[paramName] = await Router.resolveBinding(paramName, paramValue, resolvedParams);\n }\n\n return resolvedParams;\n }\n\n onBeforeRouteUpdate(() => updateResolvedRouteParams());\n updateResolvedRouteParams();\n\n return () => h(pageComponent, params.value);\n}\n\nfunction enhanceRouteComponent(pageComponent: ConcreteComponent): Component {\n return defineComponent({\n setup: () => setupPageComponent(pageComponent),\n });\n}\n\nexport function defineRoute(route: AerogelRoute): RouteRecordRaw {\n if (typeof route.component === 'function') {\n const lazyComponent = route.component as () => Promise<{ default: ConcreteComponent }>;\n\n route.component = async () => {\n const { default: component } = await lazyComponent();\n\n return enhanceRouteComponent(component);\n };\n } else if (route.component) {\n route.component = enhanceRouteComponent(route.component as ConcreteComponent);\n }\n\n return route;\n}\n\nexport function defineRouteBindings(bindings: RouteBindings): RouteBindings {\n return bindings;\n}\n\nexport function defineRoutes(routes: AerogelRoute[]): RouteRecordRaw[] {\n return routes.map(defineRoute);\n}\n","import Aerogel from 'virtual:aerogel';\n\nimport { bootServices } from '@aerogel/core';\nimport type { Plugin } from '@aerogel/core';\nimport type { RouteRecordRaw } from 'vue-router';\n\nimport Router from './services/Router';\nimport { createAppRouter } from './router';\nimport type { RouteBindings } from './services/Router';\n\nconst services = { $router: Router };\n\nexport * from './services/Router';\nexport * from './utils';\nexport { default as Router } from './services/Router';\n\nexport interface Options {\n routes: RouteRecordRaw[];\n bindings?: RouteBindings;\n basePath?: string;\n}\n\nexport type RoutingServices = typeof services;\n\nexport default function routing(options: Options): Plugin {\n return {\n async install(app) {\n const router = createAppRouter({\n routes: options.routes,\n basePath: options.basePath ?? Aerogel.basePath,\n });\n\n app.use(router);\n Router.use(router, options);\n\n await bootServices(app, services);\n },\n };\n}\n\ndeclare module '@vue/runtime-core' {\n interface ComponentCustomProperties extends RoutingServices {}\n}\n"],"names":["Service","defineServiceState","name","initialState","flashRoute","persist","RouterService","constructor","super","_defineProperty","this","router","ref","bindings","currentRouteParams","currentRoute","computed","value","unref","params","use","options","arguments","length","undefined","updateRouteParams","resolveBinding","Array","isArray","boot","Events","on","objectOnly","async","replace","__get","property","Reflect","get","Router","facade","createAppRouter","createRouter","history","createWebHistory","basePath","routes","beforeEach","once","route","Storage","pull","handleStatic404Redirect","setupPageComponent","pageComponent","useRoute","updateResolvedRouteParams","resolvedParams","paramName","paramValue","Object","entries","resolveRouteParameters","onBeforeRouteUpdate","h","enhanceRouteComponent","defineComponent","setup","defineRoute","component","lazyComponent","default","defineRouteBindings","defineRoutes","map","services","$router","routing","install","app","Aerogel","bootServices"],"mappings":"uhBAGA,IAAAA,EAAeC,EAAmB,CAC9BC,KAAM,SACNC,aAAc,CACVC,WAAY,MAEhBC,QAAS,CAAC,gBCIR,MAAOC,UAAsBN,EAO/BO,WAAAA,GACIC,QAAQC,EAAAC,KAAA,oBAAA,GAAAD,EAAAC,KAAA,cAAA,GAAAD,EAAAC,KAAA,gBAAA,GAAAD,EAAAC,KAAA,0BAAA,GAERA,KAAKC,OAASC,EAAI,MAClBF,KAAKG,SAAW,GAChBH,KAAKI,mBAAqBF,EAAI,CAAA,GAC9BF,KAAKK,aAAeC,GAAS,IACpBN,KAAKC,OAAOM,MAIV,IACAC,EAAMR,KAAKC,OAAOM,MAAMF,cAC3BI,OAAQT,KAAKI,mBAAmBG,OALzB,MAQnB,CAEOG,GAAAA,CAAIT,GAA0D,IAA1CU,EAAAC,UAAAC,OAAA,QAAAC,IAAAF,UAAA,GAAAA,UAAA,GAAwC,CAAA,EAC/DZ,KAAKC,OAAOM,MAAQN,EACpBD,KAAKG,SAAWQ,EAAQR,UAAY,CAAA,CACxC,CAEOY,iBAAAA,CAAkBN,GACrBT,KAAKI,mBAAmBG,MAAQE,CACpC,CAEO,oBAAMO,CACTxB,EACAe,EACAE,GAEA,GAAIQ,MAAMC,QAAQX,GACd,OAA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SAAUc,EAAYnB,GACxB,GAA+B,mBAApBA,EAAMoB,UAA0B,CACvC,MAAMC,EAAgBrB,EAAMoB,UAE5BpB,EAAMoB,UAAYpC,UACd,MAAQsC,QAASF,SAAoBC,IAErC,OAAOL,EAAsBI,EAAU,CAE9C,MAAUpB,EAAMoB,YACbpB,EAAMoB,UAAYJ,EAAsBhB,EAAMoB,YAGlD,OAAOpB,CACX,CAEM,SAAUuB,EAAoB3D,GAChC,OAAOA,CACX,CAEM,SAAU4D,EAAa3B,GACzB,OAAOA,EAAO4B,IAAIN,EACtB,CCtDA,MAAMO,EAAW,CAAEC,QAASrC,GAcJ,SAAAsC,EAAQxD,GAC5B,MAAO,CACH,aAAMyD,CAAQC,GACV,MAAMpE,EAAS8B,EAAgB,CAC3BK,OAAQzB,EAAQyB,OAChBD,SAAUxB,EAAQwB,UAAYmC,EAAQnC,WAG1CkC,EAAI3D,IAAIT,GACR4B,EAAOnB,IAAIT,EAAQU,SAEb4D,EAAaF,EAAKJ,EAC5B,EAER"}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@aerogel/plugin-routing",
3
+ "description": "Aerogel routing plugin",
4
+ "version": "0.0.0-next.1415dfbeeb962c022e1bb2abfd77d0237a5d23ad",
5
+ "main": "dist/aerogel-plugin-routing.cjs.js",
6
+ "module": "dist/aerogel-plugin-routing.esm.js",
7
+ "types": "dist/aerogel-plugin-routing.d.ts",
8
+ "sideEffects": false,
9
+ "scripts": {
10
+ "build": "rm dist -rf && npm run build:js && npm run build:types",
11
+ "build:js": "noeldemartin-build-javascript",
12
+ "build:types": "noeldemartin-build-types",
13
+ "publish-next": "noeldemartin-publish-next",
14
+ "lint": "noeldemartin-lint src"
15
+ },
16
+ "engines": {
17
+ "node": ">=18.x"
18
+ },
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/NoelDeMartin/aerogeljs.git"
22
+ },
23
+ "keywords": [
24
+ "javascript",
25
+ "aerogel"
26
+ ],
27
+ "author": "Noel De Martin",
28
+ "license": "MIT",
29
+ "bugs": {
30
+ "url": "https://github.com/NoelDeMartin/aerogeljs/issues"
31
+ },
32
+ "peerDependencies": {
33
+ "@aerogel/core": "*",
34
+ "@aerogel/vite": "*"
35
+ },
36
+ "dependencies": {
37
+ "@noeldemartin/utils": "0.5.0-next.dce7f1ca99862fde01601bb39d71d0eaaed3abe5",
38
+ "vue": "^3.4.15",
39
+ "vue-router": "^4.2.1"
40
+ },
41
+ "devDependencies": {
42
+ "@aerogel/core": "*"
43
+ },
44
+ "eslintConfig": {
45
+ "extends": [
46
+ "@noeldemartin/eslint-config-typescript"
47
+ ]
48
+ }
49
+ }
package/src/main.ts ADDED
@@ -0,0 +1,43 @@
1
+ import Aerogel from 'virtual:aerogel';
2
+
3
+ import { bootServices } from '@aerogel/core';
4
+ import type { Plugin } from '@aerogel/core';
5
+ import type { RouteRecordRaw } from 'vue-router';
6
+
7
+ import Router from './services/Router';
8
+ import { createAppRouter } from './router';
9
+ import type { RouteBindings } from './services/Router';
10
+
11
+ const services = { $router: Router };
12
+
13
+ export * from './services/Router';
14
+ export * from './utils';
15
+ export { default as Router } from './services/Router';
16
+
17
+ export interface Options {
18
+ routes: RouteRecordRaw[];
19
+ bindings?: RouteBindings;
20
+ basePath?: string;
21
+ }
22
+
23
+ export type RoutingServices = typeof services;
24
+
25
+ export default function routing(options: Options): Plugin {
26
+ return {
27
+ async install(app) {
28
+ const router = createAppRouter({
29
+ routes: options.routes,
30
+ basePath: options.basePath ?? Aerogel.basePath,
31
+ });
32
+
33
+ app.use(router);
34
+ Router.use(router, options);
35
+
36
+ await bootServices(app, services);
37
+ },
38
+ };
39
+ }
40
+
41
+ declare module '@vue/runtime-core' {
42
+ interface ComponentCustomProperties extends RoutingServices {}
43
+ }
package/src/router.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { Storage, once } from '@noeldemartin/utils';
2
+ import { createRouter, createWebHistory } from 'vue-router';
3
+ import type { RouteLocationRaw, RouteRecordRaw, Router } from 'vue-router';
4
+
5
+ function handleStatic404Redirect(router: Router): void {
6
+ const route = Storage.pull<RouteLocationRaw>('static-404-redirect');
7
+
8
+ route && router.replace(route);
9
+ }
10
+
11
+ export function createAppRouter(options: { routes: RouteRecordRaw[]; basePath?: string }): Router {
12
+ const router = createRouter({
13
+ history: createWebHistory(options.basePath),
14
+ routes: options.routes,
15
+ });
16
+
17
+ router.beforeEach(once(() => handleStatic404Redirect(router)));
18
+
19
+ return router;
20
+ }
@@ -0,0 +1,10 @@
1
+ import { defineServiceState } from '@aerogel/core';
2
+ import type { RouteLocationRaw } from 'vue-router';
3
+
4
+ export default defineServiceState({
5
+ name: 'router',
6
+ initialState: {
7
+ flashRoute: null as RouteLocationRaw | null,
8
+ },
9
+ persist: ['flashRoute'],
10
+ });
@@ -0,0 +1,92 @@
1
+ import { computed, ref, unref } from 'vue';
2
+ import { Events } from '@aerogel/core';
3
+ import { facade, objectOnly } from '@noeldemartin/utils';
4
+ import type { ComputedRef, Ref } from 'vue';
5
+ import type { RouteLocationNormalizedLoaded, RouteParamValue, Router } from 'vue-router';
6
+
7
+ import Service from './Router.state';
8
+
9
+ export type LoadedRoute = Omit<RouteLocationNormalizedLoaded, 'params'> & { params: Record<string, unknown> };
10
+ export type RouteBinding = (slug: string, params: Record<string, unknown>) => unknown | Promise<unknown>;
11
+ export type RouteBindings = Record<string, RouteBinding>;
12
+
13
+ export class RouterService extends Service {
14
+
15
+ public readonly currentRoute: ComputedRef<LoadedRoute | null>;
16
+ protected router: Ref<Router | null>;
17
+ protected bindings: RouteBindings;
18
+ protected currentRouteParams: Ref<Record<string, unknown>>;
19
+
20
+ constructor() {
21
+ super();
22
+
23
+ this.router = ref(null);
24
+ this.bindings = {};
25
+ this.currentRouteParams = ref({});
26
+ this.currentRoute = computed(() => {
27
+ if (!this.router.value) {
28
+ return null;
29
+ }
30
+
31
+ return {
32
+ ...unref(this.router.value.currentRoute),
33
+ params: this.currentRouteParams.value,
34
+ };
35
+ });
36
+ }
37
+
38
+ public use(router: Router, options: { bindings?: RouteBindings } = {}): void {
39
+ this.router.value = router;
40
+ this.bindings = options.bindings ?? {};
41
+ }
42
+
43
+ public updateRouteParams(params: Record<string, unknown>): void {
44
+ this.currentRouteParams.value = params;
45
+ }
46
+
47
+ public async resolveBinding(
48
+ name: string,
49
+ value: RouteParamValue | RouteParamValue[],
50
+ params: Record<string, unknown>,
51
+ ): Promise<unknown> {
52
+ if (Array.isArray(value)) {
53
+ return value;
54
+ }
55
+
56
+ const binding = this.bindings[name] ?? (() => value);
57
+
58
+ return binding(value, params);
59
+ }
60
+
61
+ protected async boot(): Promise<void> {
62
+ Events.on(
63
+ 'before-login',
64
+ () =>
65
+ (this.flashRoute = objectOnly(this.currentRoute.value as unknown as Record<string, unknown>, [
66
+ 'path',
67
+ 'query',
68
+ 'hash',
69
+ ])),
70
+ );
71
+ Events.on('login', async () => {
72
+ if (!this.flashRoute) {
73
+ return;
74
+ }
75
+
76
+ await this.replace(this.flashRoute);
77
+
78
+ this.flashRoute = null;
79
+ });
80
+ }
81
+
82
+ protected __get(property: string): unknown {
83
+ return this.router.value
84
+ ? super.__get(property) ?? Reflect.get(this.router.value, property)
85
+ : super.__get(property);
86
+ }
87
+
88
+ }
89
+
90
+ export interface RouterService extends Omit<Router, 'currentRoute'> {}
91
+
92
+ export default facade(new RouterService());
package/src/utils.ts ADDED
@@ -0,0 +1,65 @@
1
+ import { defineComponent, h, ref } from 'vue';
2
+ import { onBeforeRouteUpdate, useRoute } from 'vue-router';
3
+ import type { Component, ConcreteComponent } from 'vue';
4
+ import type { RouteRecordRaw } from 'vue-router';
5
+
6
+ import Router from './services/Router';
7
+ import type { RouteBindings } from './services/Router';
8
+
9
+ export type AerogelRoute = RouteRecordRaw;
10
+
11
+ function setupPageComponent(pageComponent: ConcreteComponent) {
12
+ const route = useRoute();
13
+ const params = ref<Record<string, unknown>>({});
14
+
15
+ async function updateResolvedRouteParams() {
16
+ params.value = await resolveRouteParameters();
17
+
18
+ Router.updateRouteParams(params.value);
19
+ }
20
+
21
+ async function resolveRouteParameters(): Promise<Record<string, unknown>> {
22
+ const resolvedParams: Record<string, unknown> = { ...route.params };
23
+
24
+ for (const [paramName, paramValue] of Object.entries(route.params)) {
25
+ resolvedParams[paramName] = await Router.resolveBinding(paramName, paramValue, resolvedParams);
26
+ }
27
+
28
+ return resolvedParams;
29
+ }
30
+
31
+ onBeforeRouteUpdate(() => updateResolvedRouteParams());
32
+ updateResolvedRouteParams();
33
+
34
+ return () => h(pageComponent, params.value);
35
+ }
36
+
37
+ function enhanceRouteComponent(pageComponent: ConcreteComponent): Component {
38
+ return defineComponent({
39
+ setup: () => setupPageComponent(pageComponent),
40
+ });
41
+ }
42
+
43
+ export function defineRoute(route: AerogelRoute): RouteRecordRaw {
44
+ if (typeof route.component === 'function') {
45
+ const lazyComponent = route.component as () => Promise<{ default: ConcreteComponent }>;
46
+
47
+ route.component = async () => {
48
+ const { default: component } = await lazyComponent();
49
+
50
+ return enhanceRouteComponent(component);
51
+ };
52
+ } else if (route.component) {
53
+ route.component = enhanceRouteComponent(route.component as ConcreteComponent);
54
+ }
55
+
56
+ return route;
57
+ }
58
+
59
+ export function defineRouteBindings(bindings: RouteBindings): RouteBindings {
60
+ return bindings;
61
+ }
62
+
63
+ export function defineRoutes(routes: AerogelRoute[]): RouteRecordRaw[] {
64
+ return routes.map(defineRoute);
65
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,7 @@
1
+ {
2
+ "extends": "../../tsconfig.json",
3
+ "compilerOptions": {
4
+ "types": ["@aerogel/vite/dist/virtual"]
5
+ },
6
+ "include": ["src/**/*.ts"]
7
+ }