UNPKG

@prefecthq/prefect-ui-library

Version:

This library is the Vue and Typescript component library for [Prefect 2](https://github.com/PrefectHQ/prefect) and [Prefect Cloud 2](https://www.prefect.io/cloud/). _The components and utilities in this project are not meant to be used independently_.

44 lines (33 loc) 1.36 kB
import { RouteLocationNormalized, RouteLocationRaw } from 'vue-router' import { RouteGuard, RouteGuardReturn } from '@/types/RouteGuard' export class RouteGuardExecutioner { private static readonly global: RouteGuard[] = [] public static async before(to: RouteLocationNormalized, from: RouteLocationNormalized): Promise<Awaited<RouteGuardReturn>> { const guards = this.getRouteGuards(to) for (const guard of guards) { // this is intentional to allow each guard to cancel navigation // eslint-disable-next-line no-await-in-loop const result = await guard.before?.(to, from) if (this.isRouteLocation(result)) { return result } } } public static after(to: RouteLocationNormalized, from: RouteLocationNormalized): void { const guards = this.getRouteGuards(to) for (const guard of guards) { guard.after?.(to, from) } } public static register(guard: RouteGuard): void { this.global.push(guard) } private static getRouteGuards(route: RouteLocationNormalized): RouteGuard[] { const guards = route.matched.flatMap(route => route.meta.guards ?? []) return [...this.global, ...guards] } // test this private static isRouteLocation(result: RouteGuardReturn): result is RouteLocationRaw { return typeof result === 'string' || typeof result == 'object' } }