From: Eduardo San Martin Morote Date: Mon, 15 Apr 2019 14:58:24 +0000 (+0200) Subject: refactor: rename href property X-Git-Tag: v4.0.0-alpha.0~452 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=23ffe56db5e41d694066bf5b8a15e7355c4134dc;p=thirdparty%2Fvuejs%2Frouter.git refactor: rename href property --- diff --git a/__tests__/url.spec.js b/__tests__/url.spec.js index cbfc6047..6d4fe768 100644 --- a/__tests__/url.spec.js +++ b/__tests__/url.spec.js @@ -8,6 +8,7 @@ const parseURL = BaseHistory.prototype.parseURL describe('URL parsing', () => { it('works with no query no hash', () => { expect(parseURL('/foo')).toEqual({ + fullPath: '/foo', path: '/foo', hash: '', query: {}, @@ -16,6 +17,7 @@ describe('URL parsing', () => { it('extracts the query', () => { expect(parseURL('/foo?a=one&b=two')).toEqual({ + fullPath: '/foo?a=one&b=two', path: '/foo', hash: '', query: { @@ -27,6 +29,7 @@ describe('URL parsing', () => { it('extracts the hash', () => { expect(parseURL('/foo#bar')).toEqual({ + fullPath: '/foo#bar', path: '/foo', hash: '#bar', query: {}, @@ -35,6 +38,7 @@ describe('URL parsing', () => { it('extracts query and hash', () => { expect(parseURL('/foo?a=one#bar')).toEqual({ + fullPath: '/foo?a=one#bar', path: '/foo', hash: '#bar', query: { a: 'one' }, diff --git a/src/history/base.ts b/src/history/base.ts index eb0a4e3a..7ddb09b3 100644 --- a/src/history/base.ts +++ b/src/history/base.ts @@ -1,7 +1,12 @@ export type HistoryLocation = string export interface HistoryURL { + // full path (like href) + fullPath: string + // pathname section path: string + // search string parsed query: Record // TODO: handle arrays + // hash with the # hash: string } @@ -107,6 +112,7 @@ export abstract class BaseHistory { path = path || location return { + fullPath: location, path, // TODO: transform searchString query, diff --git a/src/matcher.ts b/src/matcher.ts index 012e3b5e..3c53280d 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -2,7 +2,7 @@ import pathToRegexp from 'path-to-regexp' import { RouteRecord, RouteParams, - RouterLocation, + MatcherLocation, RouterLocationNormalized, } from './types/index' import { stringifyQuery } from './utils' @@ -35,12 +35,11 @@ export class RouterMatcher { } /** - * Transforms a RouterLocation object into a URL string. If a string is - * passed, it returns the string itself - * @param location RouterLocation to resolve to a url + * Transforms a MatcherLocation object into a normalized location + * @param location MatcherLocation to resolve to a url */ resolve( - location: Readonly, + location: Readonly, currentLocation: Readonly ): RouterLocationNormalized { if (typeof location === 'string') diff --git a/src/router.ts b/src/router.ts index 50c78bf5..9afbd670 100644 --- a/src/router.ts +++ b/src/router.ts @@ -25,7 +25,8 @@ export class Router { this.history.listen((to, from, info) => { // TODO: check navigation guards - this.currentRoute = this.matcher.resolve(to, this.currentRoute) + const url = this.history.parseURL(to) + this.currentRoute = this.matcher.resolve(url, this.currentRoute) }) } @@ -35,7 +36,8 @@ export class Router { */ push(to: RouterLocation) { // TODO: resolve URL - const location = this.matcher.resolve(to, this.currentRoute) + const url = typeof to === 'string' ? this.history.parseURL(to) : to + const location = this.matcher.resolve(url, this.currentRoute) // TODO: call hooks, guards this.history.push(location.fullPath) this.currentRoute = location diff --git a/src/types/index.ts b/src/types/index.ts index e29cac3a..1f3b17f6 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,3 +1,5 @@ +import { HistoryURL } from '../history/base' + type TODO = any export type RouteParams = Record @@ -24,9 +26,7 @@ export interface RouteRecord { // props: PT } -// TODO: location should be an object -export type RouterLocation = - | string +type RouteObjectLocation = | { path: string query?: RouteQuery @@ -44,12 +44,17 @@ export type RouterLocation = hash?: string } +// TODO: location should be an object +export type MatcherLocation = HistoryURL | RouteObjectLocation + +export type RouterLocation = string | RouteObjectLocation + export interface RouterLocationNormalized { path: string fullPath: string name?: string params: RouteParams - query: TODO + query: RouteQuery hash: string }