From f645cf0fc6db5b0964bb40e86a60446c0b1c3f64 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 8 Aug 2019 09:49:03 +0200 Subject: [PATCH] refactor(types): reformat --- types/index.d.ts | 8 +- types/router.d.ts | 192 +++++++++++++++++++----------------- types/test/index.ts | 232 +++++++++++++++++++++++--------------------- types/vue.d.ts | 20 ++-- 4 files changed, 237 insertions(+), 215 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index 087275cd7..a66f5ba15 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,7 @@ -import "./vue"; -import { VueRouter } from "./router"; +import './vue' +import { VueRouter } from './router' -export default VueRouter; +export default VueRouter export { RouterMode, @@ -13,4 +13,4 @@ export { Location, Route, NavigationGuard -} from "./router"; +} from './router' diff --git a/types/router.d.ts b/types/router.d.ts index 080707d1b..137261ee3 100644 --- a/types/router.d.ts +++ b/types/router.d.ts @@ -1,129 +1,145 @@ -import Vue, { ComponentOptions, PluginFunction, AsyncComponent } from "vue"; +import Vue, { ComponentOptions, PluginFunction, AsyncComponent } from 'vue' -type Component = ComponentOptions | typeof Vue | AsyncComponent; -type Dictionary = { [key: string]: T }; -type ErrorHandler = (err: Error) => void; +type Component = ComponentOptions | typeof Vue | AsyncComponent +type Dictionary < T > = { [key: string]: T } +type ErrorHandler = (err: Error) => void -export type RouterMode = "hash" | "history" | "abstract"; -export type RawLocation = string | Location; -export type RedirectOption = RawLocation | ((to: Route) => RawLocation); -export type NavigationGuard = ( +export type RouterMode = 'hash' | 'history' | 'abstract' +export type RawLocation = string | Location +export type RedirectOption = RawLocation | ((to: Route) => RawLocation) +export type NavigationGuard < V extends Vue = Vue > = ( to: Route, from: Route, next: (to?: RawLocation | false | ((vm: V) => any) | void) => void ) => any export declare class VueRouter { - constructor (options?: RouterOptions); + constructor(options?: RouterOptions) - app: Vue; - mode: RouterMode; - currentRoute: Route; + app: Vue + mode: RouterMode + currentRoute: Route - beforeEach (guard: NavigationGuard): Function; - beforeResolve (guard: NavigationGuard): Function; - afterEach (hook: (to: Route, from: Route) => any): Function; - push (location: RawLocation): Promise; - replace (location: RawLocation): Promise; - push (location: RawLocation, onComplete?: Function, onAbort?: ErrorHandler): void; - replace (location: RawLocation, onComplete?: Function, onAbort?: ErrorHandler): void; - go (n: number): void; - back (): void; - forward (): void; - getMatchedComponents (to?: RawLocation | Route): Component[]; - onReady (cb: Function, errorCb?: ErrorHandler): void; - onError (cb: ErrorHandler): void; - addRoutes (routes: RouteConfig[]): void; - resolve (to: RawLocation, current?: Route, append?: boolean): { - location: Location; - route: Route; - href: string; + beforeEach(guard: NavigationGuard): Function + beforeResolve(guard: NavigationGuard): Function + afterEach(hook: (to: Route, from: Route) => any): Function + push(location: RawLocation): Promise + replace(location: RawLocation): Promise + push( + location: RawLocation, + onComplete?: Function, + onAbort?: ErrorHandler + ): void + replace( + location: RawLocation, + onComplete?: Function, + onAbort?: ErrorHandler + ): void + go(n: number): void + back(): void + forward(): void + getMatchedComponents(to?: RawLocation | Route): Component[] + onReady(cb: Function, errorCb?: ErrorHandler): void + onError(cb: ErrorHandler): void + addRoutes(routes: RouteConfig[]): void + resolve( + to: RawLocation, + current?: Route, + append?: boolean + ): { + location: Location + route: Route + href: string // backwards compat - normalizedTo: Location; - resolved: Route; - }; + normalizedTo: Location + resolved: Route + } - static install: PluginFunction; + static install: PluginFunction } -type Position = { x: number, y: number }; -type PositionResult = Position | { selector: string, offset?: Position } | void; +type Position = { x: number; y: number } +type PositionResult = Position | { selector: string; offset?: Position } | void export interface RouterOptions { - routes?: RouteConfig[]; - mode?: RouterMode; - fallback?: boolean; - base?: string; - linkActiveClass?: string; - linkExactActiveClass?: string; - parseQuery?: (query: string) => Object; - stringifyQuery?: (query: Object) => string; + routes?: RouteConfig[] + mode?: RouterMode + fallback?: boolean + base?: string + linkActiveClass?: string + linkExactActiveClass?: string + parseQuery?: (query: string) => Object + stringifyQuery?: (query: Object) => string scrollBehavior?: ( to: Route, from: Route, savedPosition: Position | void - ) => PositionResult | Promise; + ) => PositionResult | Promise } -type RoutePropsFunction = (route: Route) => Object; +type RoutePropsFunction = (route: Route) => Object export interface PathToRegexpOptions { - sensitive?: boolean; - strict?: boolean; - end?: boolean; + sensitive?: boolean + strict?: boolean + end?: boolean } export interface RouteConfig { - path: string; - name?: string; - component?: Component; - components?: Dictionary; - redirect?: RedirectOption; - alias?: string | string[]; - children?: RouteConfig[]; - meta?: any; - beforeEnter?: NavigationGuard; - props?: boolean | Object | RoutePropsFunction; - caseSensitive?: boolean; - pathToRegexpOptions?: PathToRegexpOptions; + path: string + name?: string + component?: Component + components?: Dictionary + redirect?: RedirectOption + alias?: string | string[] + children?: RouteConfig[] + meta?: any + beforeEnter?: NavigationGuard + props?: boolean | Object | RoutePropsFunction + caseSensitive?: boolean + pathToRegexpOptions?: PathToRegexpOptions } export interface RouteRecord { - path: string; - regex: RegExp; - components: Dictionary; - instances: Dictionary; - name?: string; - parent?: RouteRecord; - redirect?: RedirectOption; - matchAs?: string; - meta: any; + path: string + regex: RegExp + components: Dictionary + instances: Dictionary + name?: string + parent?: RouteRecord + redirect?: RedirectOption + matchAs?: string + meta: any beforeEnter?: ( route: Route, redirect: (location: RawLocation) => void, next: () => void - ) => any; - props: boolean | Object | RoutePropsFunction | Dictionary; + ) => any + props: + | boolean + | Object + | RoutePropsFunction + | Dictionary } export interface Location { - name?: string; - path?: string; - hash?: string; - query?: Dictionary; - params?: Dictionary; - append?: boolean; - replace?: boolean; + name?: string + path?: string + hash?: string + query?: Dictionary + params?: Dictionary + append?: boolean + replace?: boolean } export interface Route { - path: string; - name?: string; - hash: string; - query: Dictionary; - params: Dictionary; - fullPath: string; - matched: RouteRecord[]; - redirectedFrom?: string; - meta?: any; + path: string + name?: string + hash: string + query: Dictionary + params: Dictionary + fullPath: string + matched: RouteRecord[] + redirectedFrom?: string + meta?: any } diff --git a/types/test/index.ts b/types/test/index.ts index ba169f9b8..a0a1bda86 100644 --- a/types/test/index.ts +++ b/types/test/index.ts @@ -1,63 +1,63 @@ -import Vue, { ComponentOptions, AsyncComponent } from "vue"; +import Vue, { ComponentOptions, AsyncComponent } from 'vue' -import VueRouter from "../index"; -import { Route, RouteRecord, RedirectOption } from "../index"; +import VueRouter from '../index' +import { Route, RouteRecord, RedirectOption } from '../index' -Vue.use(VueRouter); +Vue.use(VueRouter) -const Home = { template: "
home
" }; -const Foo = { template: "
foo
" }; -const Bar = { template: "
bar
" }; -const Async = () => Promise.resolve({ template: "
async
" }) +const Home = { template: '
home
' } +const Foo = { template: '
foo
' } +const Bar = { template: '
bar
' } +const Async = () => Promise.resolve({ template: '
async
' }) const Hook: ComponentOptions = { - template: "
hook
", + template: '
hook
', beforeRouteEnter (to, from, next) { - route.params; - next("/"); - next({ path: "/" }); + route.params + next('/') + next({ path: '/' }) next(vm => { - vm.$router; - }); + vm.$router + }) }, beforeRouteLeave (to, from, next) { - route.params; - next("/"); - next({ path: "/" }); - next(); + route.params + next('/') + next({ path: '/' }) + next() }, beforeRouteUpdate (to, from, next) { - route.params; - next("/"); - next({ path: "/" }); - next(); + route.params + next('/') + next({ path: '/' }) + next() } -}; +} const router = new VueRouter({ - mode: "history", - base: "/", + mode: 'history', + base: '/', fallback: false, - linkActiveClass: "active", - linkExactActiveClass: "exact-active", + linkActiveClass: 'active', + linkExactActiveClass: 'exact-active', scrollBehavior: (to, from, savedPosition) => { - if (from.path === "/") { - return { selector: "#app" }; + if (from.path === '/') { + return { selector: '#app' } } - if (from.path === "/offset") { + if (from.path === '/offset') { return { selector: '#foo', offset: { x: 0, y: 100 }} } - if (to.path === "/child") { - return; + if (to.path === '/child') { + return } if (savedPosition) { - return savedPosition; + return savedPosition } return Promise.resolve({ @@ -66,97 +66,102 @@ const router = new VueRouter({ }) }, routes: [ - { path: "/", name: "home", component: Home, children: [ - { - path: "child", - components: { - default: Foo, - bar: Bar, - asyncComponent: Async, + { + path: '/', + name: 'home', + component: Home, + children: [ + { + path: 'child', + components: { + default: Foo, + bar: Bar, + asyncComponent: Async + }, + meta: { auth: true }, + beforeEnter (to, from, next) { + to.params + from.params + next({ name: 'home' }) + next() + } }, - meta: { auth: true }, - beforeEnter (to, from, next) { - to.params; - from.params; - next({ name: "home" }); - next(); - } - }, - { - path: "children", - redirect: to => { - to.params; - return "/child"; + { + path: 'children', + redirect: to => { + to.params + return '/child' + } } - } - ]}, - { path: "/home", alias: "/" }, - { path: "/foo", props: true }, - { path: "/bar", props: { id: 123 }}, - { path: "/baz", props: (route: Route) => route.params }, - { path: "*", redirect: "/" } + ] + }, + { path: '/home', alias: '/' }, + { path: '/foo', props: true }, + { path: '/bar', props: { id: 123 }}, + { path: '/baz', props: (route: Route) => route.params }, + { path: '*', redirect: '/' } ] -}); - -const App: Vue = router.app; -const mode: string = router.mode; - -const route: Route = router.currentRoute; -const path: string = route.path; -const name: string | undefined = route.name; -const hash: string = route.hash; -const query: string | (string | null)[] | null = route.query["foo"]; -const params: string = route.params["bar"]; -const fullPath: string = route.fullPath; -const redirectedFrom: string | undefined = route.redirectedFrom; -const meta: any = route.meta; -const matched: RouteRecord[] = route.matched; +}) + +const App: Vue = router.app +const mode: string = router.mode + +const route: Route = router.currentRoute +const path: string = route.path +const name: string | undefined = route.name +const hash: string = route.hash +const query: string | (string | null)[] | null = route.query['foo'] +const params: string = route.params['bar'] +const fullPath: string = route.fullPath +const redirectedFrom: string | undefined = route.redirectedFrom +const meta: any = route.meta +const matched: RouteRecord[] = route.matched matched.forEach(m => { - const path: string = m.path; + const path: string = m.path const components: { - [key: string]: ComponentOptions | typeof Vue | AsyncComponent - } = m.components; - const instances: { [key: string]: Vue } = m.instances; - const name: string | undefined = m.name; - const parant: RouteRecord | undefined = m.parent; - const redirect: RedirectOption | undefined = m.redirect; -}); + [key: string]: ComponentOptions | typeof Vue | AsyncComponent + } = m.components + const instances: { [key: string]: Vue } = m.instances + const name: string | undefined = m.name + const parant: RouteRecord | undefined = m.parent + const redirect: RedirectOption | undefined = m.redirect +}) const unregister = router.beforeEach((to, from, next) => { - to.params; - next("/"); - next(); -}); + to.params + next('/') + next() +}) -unregister(); +unregister() router.beforeResolve((to, from, next) => { - to.params; - from.params; + to.params + from.params next() -}); +}) router.afterEach((to, from) => { - to.params; - from.params; -}); + to.params + from.params +}) router.push({ - path: "/", + path: '/', params: { - foo: "foo" + foo: 'foo' }, query: { - bar: "bar", + bar: 'bar', empty: null, removed: undefined, - withEmpty: ["1", null], - foo: ["foo1", "foo2"] + withEmpty: ['1', null], + foo: ['foo1', 'foo2'] }, - hash: "hash" -}); -router.replace({ name: "home" }); + hash: 'hash' +}) +router.replace({ name: 'home' }) router.push('/', () => {}, () => {}) router.replace('/foo', () => {}, () => {}) @@ -170,17 +175,18 @@ router }) .catch(() => {}) -router.onReady(() => {}); +router.onReady(() => {}) -router.addRoutes([ - { path: '/more' } -]); +router.addRoutes([{ path: '/more' }]) -router.go(-1); -router.back(); -router.forward(); +router.go(-1) +router.back() +router.forward() -const Components: (ComponentOptions | typeof Vue | AsyncComponent)[] = router.getMatchedComponents(); +const Components: ( + | ComponentOptions + | typeof Vue + | AsyncComponent)[] = router.getMatchedComponents() const vm = new Vue({ router, @@ -195,7 +201,7 @@ const vm = new Vue({ ` -}).$mount("#app"); +}).$mount('#app') -vm.$router.push("/"); -vm.$route.params; +vm.$router.push('/') +vm.$route.params diff --git a/types/vue.d.ts b/types/vue.d.ts index 4e57eec79..307515cf9 100644 --- a/types/vue.d.ts +++ b/types/vue.d.ts @@ -2,21 +2,21 @@ * Augment the typings of Vue.js */ -import Vue from "vue"; -import VueRouter, { Route, RawLocation, NavigationGuard } from "./index"; +import Vue from 'vue' +import VueRouter, { Route, RawLocation, NavigationGuard } from './index' -declare module "vue/types/vue" { +declare module 'vue/types/vue' { interface Vue { - $router: VueRouter; - $route: Route; + $router: VueRouter + $route: Route } } -declare module "vue/types/options" { +declare module 'vue/types/options' { interface ComponentOptions { - router?: VueRouter; - beforeRouteEnter?: NavigationGuard; - beforeRouteLeave?: NavigationGuard; - beforeRouteUpdate?: NavigationGuard; + router?: VueRouter + beforeRouteEnter?: NavigationGuard + beforeRouteLeave?: NavigationGuard + beforeRouteUpdate?: NavigationGuard } }