Commit f879ec7e authored by Robert Knight's avatar Robert Knight

Introduce types for route name and route params

 - Introduce a union type for current route names

 - Use `Record<Key, Value>` rather than `Object.<Key, Value>` as it is
   stricter (eg. it is non-nullable)
parent 179271b7
import * as queryString from 'query-string'; import * as queryString from 'query-string';
/**
* @typedef {'annotation'|'notebook'|'stream'|'sidebar'} RouteName
* @typedef {Record<string,string>} RouteParams
*/
/** /**
* A service that manages the association between the route and route parameters * A service that manages the association between the route and route parameters
* implied by the URL and the corresponding route state in the store. * implied by the URL and the corresponding route state in the store.
...@@ -18,6 +23,8 @@ export class RouterService { ...@@ -18,6 +23,8 @@ export class RouterService {
/** /**
* Return the name and parameters of the current route. * Return the name and parameters of the current route.
*
* @return {{ route: RouteName, params: RouteParams }}
*/ */
currentRoute() { currentRoute() {
const path = this._window.location.pathname; const path = this._window.location.pathname;
...@@ -36,6 +43,7 @@ export class RouterService { ...@@ -36,6 +43,7 @@ export class RouterService {
// extension. // extension.
const mainSegment = pathSegments[0].replace(/\.html$/, ''); const mainSegment = pathSegments[0].replace(/\.html$/, '');
/** @type {RouteName} */
let route; let route;
switch (mainSegment) { switch (mainSegment) {
...@@ -60,8 +68,8 @@ export class RouterService { ...@@ -60,8 +68,8 @@ export class RouterService {
/** /**
* Generate a URL for a given route. * Generate a URL for a given route.
* *
* @param {string} name * @param {RouteName} name
* @param {Object.<string,string>} params * @param {RouteParams} params
*/ */
routeUrl(name, params = {}) { routeUrl(name, params = {}) {
let url; let url;
...@@ -122,8 +130,8 @@ export class RouterService { ...@@ -122,8 +130,8 @@ export class RouterService {
/** /**
* Navigate to a given route. * Navigate to a given route.
* *
* @param {string} name * @param {RouteName} name
* @param {Object.<string,string>} params * @param {RouteParams} params
*/ */
navigate(name, params) { navigate(name, params) {
this._window.history.pushState({}, '', this.routeUrl(name, params)); this._window.history.pushState({}, '', this.routeUrl(name, params));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment