]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: group guards code
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 13:44:24 +0000 (15:44 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 8 Apr 2020 13:44:24 +0000 (15:44 +0200)
src/navigationGuards.ts
src/router.ts
src/utils/guardToPromiseFn.ts [deleted file]
src/utils/index.ts

index db7bd60a72d0d3885ee58f42c7f99e794c1c84f1..e647c79deef3a6fab016a691c3e6f77d3c2f4559 100644 (file)
@@ -1,6 +1,24 @@
-import { NavigationGuard } from './types'
+import {
+  NavigationGuard,
+  RouteLocationNormalized,
+  NavigationGuardCallback,
+  RouteLocationRaw,
+  RouteLocationNormalizedLoaded,
+  NavigationGuardNextCallback,
+  isRouteLocation,
+} from './types'
+
+import {
+  createRouterError,
+  ErrorTypes,
+  NavigationError,
+  NavigationRedirectError,
+} from './errors'
+import { ComponentPublicInstance } from 'vue'
 import { inject, getCurrentInstance, warn } from 'vue'
 import { matchedRouteKey } from './utils/injectionSymbols'
+import { RouteRecordNormalized } from './matcher/types'
+import { isESModule } from './utils'
 
 export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
   const instance = getCurrentInstance()
@@ -23,3 +41,97 @@ export function onBeforeRouteLeave(leaveGuard: NavigationGuard) {
     leaveGuard.bind(instance.proxy)
   )
 }
+
+export function guardToPromiseFn(
+  guard: NavigationGuard<undefined>,
+  to: RouteLocationNormalized,
+  from: RouteLocationNormalizedLoaded,
+  instance?: undefined
+): () => Promise<void>
+export function guardToPromiseFn<
+  ThisType extends ComponentPublicInstance | undefined
+>(
+  guard: NavigationGuard<ThisType>,
+  to: RouteLocationNormalized,
+  from: RouteLocationNormalizedLoaded,
+  instance: ThisType
+): () => Promise<void> {
+  return () =>
+    new Promise((resolve, reject) => {
+      const next: NavigationGuardCallback = (
+        valid?: boolean | RouteLocationRaw | NavigationGuardNextCallback
+      ) => {
+        if (valid === false)
+          reject(
+            createRouterError<NavigationError>(ErrorTypes.NAVIGATION_ABORTED, {
+              from,
+              to,
+            })
+          )
+        else if (isRouteLocation(valid)) {
+          reject(
+            createRouterError<NavigationRedirectError>(
+              ErrorTypes.NAVIGATION_GUARD_REDIRECT,
+              {
+                from: to,
+                to: valid,
+              }
+            )
+          )
+        } else if (valid instanceof Error) {
+          // TODO
+        } else {
+          // TODO: call the in component enter callbacks. Maybe somewhere else
+          // record && record.enterCallbacks.push(valid)
+          resolve()
+        }
+      }
+
+      // TODO: write tests
+      Promise.resolve(guard.call(instance, to, from, next)).catch(err =>
+        reject(err)
+      )
+    })
+}
+
+type GuardType = 'beforeRouteEnter' | 'beforeRouteUpdate' | 'beforeRouteLeave'
+
+export function extractComponentsGuards(
+  matched: RouteRecordNormalized[],
+  guardType: GuardType,
+  to: RouteLocationNormalized,
+  from: RouteLocationNormalizedLoaded
+) {
+  const guards: Array<() => Promise<void>> = []
+
+  for (const record of matched) {
+    for (const name in record.components) {
+      const rawComponent = record.components[name]
+      if (typeof rawComponent === 'function') {
+        // start requesting the chunk already
+        const componentPromise = rawComponent().catch(() => null)
+        guards.push(async () => {
+          const resolved = await componentPromise
+          if (!resolved) throw new Error('TODO: error while fetching')
+          const resolvedComponent = isESModule(resolved)
+            ? resolved.default
+            : resolved
+          // replace the function with the resolved component
+          record.components[name] = resolvedComponent
+          const guard = resolvedComponent[guardType]
+          return (
+            // @ts-ignore: the guards matched the instance type
+            guard && guardToPromiseFn(guard, to, from, record.instances[name])()
+          )
+        })
+      } else {
+        const guard = rawComponent[guardType]
+        guard &&
+          // @ts-ignore: the guards matched the instance type
+          guards.push(guardToPromiseFn(guard, to, from, record.instances[name]))
+      }
+    }
+  }
+
+  return guards
+}
index bd4a8202d90a1dd67fdcf86f020ea61d16fb1c47..9f32645fee0c3d0efc428ba0b6cc5a06a1c90a26 100644 (file)
@@ -21,13 +21,7 @@ import {
 } from './utils/scroll'
 import { createRouterMatcher } from './matcher'
 import { createRouterError, ErrorTypes, NavigationError } from './errors'
-import {
-  extractComponentsGuards,
-  guardToPromiseFn,
-  applyToParams,
-  isSameRouteRecord,
-  isSameLocationObject,
-} from './utils'
+import { applyToParams, isSameRouteRecord, isSameLocationObject } from './utils'
 import { useCallbacks } from './utils/callbacks'
 import { encodeParam, decode } from './utils/encoding'
 import {
@@ -51,6 +45,7 @@ import { Link } from './components/Link'
 import { View } from './components/View'
 import { routerKey, routeLocationKey } from './utils/injectionSymbols'
 import { parseURL, stringifyURL } from './utils/location'
+import { extractComponentsGuards, guardToPromiseFn } from './navigationGuards'
 
 /**
  * Internal type to define an ErrorHandler
diff --git a/src/utils/guardToPromiseFn.ts b/src/utils/guardToPromiseFn.ts
deleted file mode 100644 (file)
index 9e31269..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-import {
-  NavigationGuard,
-  RouteLocationNormalized,
-  NavigationGuardCallback,
-  RouteLocationRaw,
-  RouteLocationNormalizedLoaded,
-  NavigationGuardNextCallback,
-  isRouteLocation,
-} from '../types'
-
-import {
-  createRouterError,
-  ErrorTypes,
-  NavigationError,
-  NavigationRedirectError,
-} from '../errors'
-import { ComponentPublicInstance } from 'vue'
-
-export function guardToPromiseFn(
-  guard: NavigationGuard<undefined>,
-  to: RouteLocationNormalized,
-  from: RouteLocationNormalizedLoaded,
-  instance?: undefined
-): () => Promise<void>
-export function guardToPromiseFn<
-  ThisType extends ComponentPublicInstance | undefined
->(
-  guard: NavigationGuard<ThisType>,
-  to: RouteLocationNormalized,
-  from: RouteLocationNormalizedLoaded,
-  instance: ThisType
-): () => Promise<void> {
-  return () =>
-    new Promise((resolve, reject) => {
-      const next: NavigationGuardCallback = (
-        valid?: boolean | RouteLocationRaw | NavigationGuardNextCallback
-      ) => {
-        if (valid === false)
-          reject(
-            createRouterError<NavigationError>(ErrorTypes.NAVIGATION_ABORTED, {
-              from,
-              to,
-            })
-          )
-        else if (isRouteLocation(valid)) {
-          reject(
-            createRouterError<NavigationRedirectError>(
-              ErrorTypes.NAVIGATION_GUARD_REDIRECT,
-              {
-                from: to,
-                to: valid,
-              }
-            )
-          )
-        } else if (!valid || valid === true) {
-          resolve()
-        } else {
-          // TODO: call the in component enter callbacks. Maybe somewhere else
-          // record && record.enterCallbacks.push(valid)
-          resolve()
-        }
-      }
-
-      guard.call(instance, to, from, next)
-    })
-}
index 932bafd15904df76c2607594ff4b2c0290ae83a4..924cbb57587610eb4165c3ac787defc2686a15d9 100644 (file)
@@ -2,62 +2,16 @@ import {
   RouteLocationNormalized,
   RouteParams,
   RouteComponent,
-  RouteLocationNormalizedLoaded,
   RouteParamValue,
 } from '../types'
-import { guardToPromiseFn } from './guardToPromiseFn'
-import { RouteRecord, RouteRecordNormalized } from '../matcher/types'
+import { RouteRecord } from '../matcher/types'
 import { LocationQueryValue } from './query'
 import { hasSymbol } from './injectionSymbols'
 
-export * from './guardToPromiseFn'
-
-function isESModule(obj: any): obj is { default: RouteComponent } {
+export function isESModule(obj: any): obj is { default: RouteComponent } {
   return obj.__esModule || (hasSymbol && obj[Symbol.toStringTag] === 'Module')
 }
 
-type GuardType = 'beforeRouteEnter' | 'beforeRouteUpdate' | 'beforeRouteLeave'
-
-export function extractComponentsGuards(
-  matched: RouteRecordNormalized[],
-  guardType: GuardType,
-  to: RouteLocationNormalized,
-  from: RouteLocationNormalizedLoaded
-) {
-  const guards: Array<() => Promise<void>> = []
-
-  for (const record of matched) {
-    for (const name in record.components) {
-      const rawComponent = record.components[name]
-      if (typeof rawComponent === 'function') {
-        // start requesting the chunk already
-        const componentPromise = rawComponent().catch(() => null)
-        guards.push(async () => {
-          const resolved = await componentPromise
-          if (!resolved) throw new Error('TODO: error while fetching')
-          const resolvedComponent = isESModule(resolved)
-            ? resolved.default
-            : resolved
-          // replace the function with the resolved component
-          record.components[name] = resolvedComponent
-          const guard = resolvedComponent[guardType]
-          return (
-            // @ts-ignore: the guards matched the instance type
-            guard && guardToPromiseFn(guard, to, from, record.instances[name])()
-          )
-        })
-      } else {
-        const guard = rawComponent[guardType]
-        guard &&
-          // @ts-ignore: the guards matched the instance type
-          guards.push(guardToPromiseFn(guard, to, from, record.instances[name]))
-      }
-    }
-  }
-
-  return guards
-}
-
 export function applyToParams(
   fn: (v: string) => string,
   params: RouteParams | undefined