Skip to content

Commit 2e8c96d

Browse files
authored
Fix typings: router.resolve() and context.next() always return a promise now (#187)
1 parent c7f84f6 commit 2e8c96d

7 files changed

+30
-25
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased][unreleased]
9+
10+
- Fix typings: `router.resolve()` and `context.next()` always return a promise now
11+
([#187](https://github.com/kriasoft/universal-router/pull/187))
12+
813
## [9.0.0] - 2020-02-27
914

1015
- Update [path-to-regexp](https://github.com/pillarjs/path-to-regexp) from v3 to v6, see

dist/universal-router-sync.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router-sync.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/universal-router.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/UniversalRouter.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ import {
1717
RegexpToFunctionOptions,
1818
} from 'path-to-regexp'
1919

20-
/**
21-
* Params is a key/value object that represents extracted URL parameters.
22-
*/
23-
export interface RouteParams {
24-
[paramName: string]: string | string[]
25-
}
26-
2720
/**
2821
* In addition to a URL path string, any arbitrary data can be passed to
2922
* the `router.resolve()` method, that becomes available inside action functions.
@@ -39,6 +32,15 @@ export interface ResolveContext extends RouterContext {
3932
pathname: string
4033
}
4134

35+
/**
36+
* Params is a key/value object that represents extracted URL parameters.
37+
*/
38+
export interface RouteParams {
39+
[paramName: string]: string | string[]
40+
}
41+
42+
export type RouteResult<T> = T | null | undefined | Promise<T | null | undefined>
43+
4244
export interface RouteContext<R = any, C extends RouterContext = RouterContext>
4345
extends ResolveContext {
4446
/**
@@ -67,8 +69,6 @@ export interface RouteContext<R = any, C extends RouterContext = RouterContext>
6769
next: (resume?: boolean) => Promise<R>
6870
}
6971

70-
export type RouteResult<T> = T | Promise<T | null | undefined> | null | undefined
71-
7272
/**
7373
* A Route is a singular route in your application. It contains a path, an
7474
* action function, and optional children which are an array of Route.
@@ -267,7 +267,7 @@ class UniversalRouter<R = any, C extends RouterContext = RouterContext> {
267267
* the first route that matches provided URL path string and whose action function
268268
* returns anything other than `null` or `undefined`.
269269
*/
270-
resolve(pathnameOrContext: string | ResolveContext): RouteResult<R> {
270+
resolve(pathnameOrContext: string | ResolveContext): Promise<RouteResult<R>> {
271271
const context: ResolveContext = {
272272
router: this,
273273
...this.options.context,
@@ -290,7 +290,7 @@ class UniversalRouter<R = any, C extends RouterContext = RouterContext> {
290290
resume: boolean,
291291
parent: Route<R, C> | false = !matches.done && matches.value.route,
292292
prevResult?: RouteResult<R>,
293-
): RouteResult<R> {
293+
): Promise<RouteResult<R>> {
294294
const routeToSkip = prevResult === null && !matches.done && matches.value.route
295295
matches = nextMatches || matchResult.next(routeToSkip)
296296
nextMatches = null

src/UniversalRouterSync.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@ import {
1717
RegexpToFunctionOptions,
1818
} from 'path-to-regexp'
1919

20-
/**
21-
* Params is a key/value object that represents extracted URL parameters.
22-
*/
23-
export interface RouteParams {
24-
[paramName: string]: string | string[]
25-
}
26-
2720
/**
2821
* In addition to a URL path string, any arbitrary data can be passed to
2922
* the `router.resolve()` method, that becomes available inside action functions.
@@ -39,6 +32,15 @@ export interface ResolveContext extends RouterContext {
3932
pathname: string
4033
}
4134

35+
/**
36+
* Params is a key/value object that represents extracted URL parameters.
37+
*/
38+
export interface RouteParams {
39+
[paramName: string]: string | string[]
40+
}
41+
42+
export type RouteResultSync<T> = T | null | undefined
43+
4244
export interface RouteContext<R = any, C extends RouterContext = RouterContext>
4345
extends ResolveContext {
4446
/**
@@ -64,11 +66,9 @@ export interface RouteContext<R = any, C extends RouterContext = RouterContext>
6466
/**
6567
* Middleware style function which can continue resolving.
6668
*/
67-
next: (resume?: boolean) => R
69+
next: (resume?: boolean) => RouteResultSync<R>
6870
}
6971

70-
export type RouteResultSync<T> = T | null | undefined
71-
7272
/**
7373
* A Route is a singular route in your application. It contains a path, an
7474
* action function, and optional children which are an array of Route.

0 commit comments

Comments
 (0)