Skip to content

Commit

Permalink
Improve TypeScript typings (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy committed May 10, 2019
1 parent fd2a688 commit 9dfb9b4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 21 deletions.
36 changes: 27 additions & 9 deletions src/UniversalRouter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import pathToRegexp = require('path-to-regexp')

export interface Params {
[paramName: string]: any
export interface QueryParams {
[paramName: string]: string | string[]
}

export interface Context {
Expand All @@ -23,33 +23,51 @@ export interface ResolveContext extends Context {

export interface RouteContext<C extends Context, R = any> extends ResolveContext {
router: UniversalRouter<C, R>
route: Route
route: Route<C, R>
baseUrl: string
path: string
params: Params
params: QueryParams
keys: pathToRegexp.Key[]
next: (resume?: boolean) => Promise<R>
}

export type Result<T> = T | Promise<T | void> | void

export interface Route<C extends Context = any, R = any> {
path?: string | RegExp | Array<string | RegExp>
name?: string
parent?: Route | null
parent?: Route<C, R> | null
children?: Routes<C, R> | null
action?: (context: RouteContext<C, R> & C, params: Params) => R | Promise<R> | void
action?: (context: RouteContext<C, R> & C, params: QueryParams) => Result<R>
}

export type Routes<C extends Context = Context, R = any> = Array<Route<C, R>>

export type ResolveRoute<C extends Context = Context, R = any> = (
context: C & RouteContext<C, R>,
params: QueryParams,
) => Result<R>

export type ErrorHandler<C extends Context = Context, R = any> = (
error: Error & { status?: number },
context: C & RouteContext<C, R>,
) => Result<R>

export interface Options<C extends Context = Context, R = any> {
context?: C
baseUrl?: string
resolveRoute?: (context: C & RouteContext<C, R>, params: Params) => any
errorHandler?: (error: Error & { status?: number }, context: C & RouteContext<C, R>) => any
resolveRoute?: ResolveRoute<C, R>
errorHandler?: ErrorHandler<C, R>
}

export default class UniversalRouter<C extends Context = Context, R = any> {
static pathToRegexp: typeof pathToRegexp
constructor(routes: Route<C, R> | Routes<C, R>, options?: Options<C>)
baseUrl: string
errorHandler?: ErrorHandler<C, R>
resolveRoute: ResolveRoute<C, R>
context: C & { router: UniversalRouter<C, R> }
root: Route<C, R>
routesByName?: Map<string, Route<C, R>>
constructor(routes: Route<C, R> | Routes<C, R>, options?: Options<C, R>)
resolve(pathnameOrContext: string | ResolveContext): Promise<R>
}
36 changes: 27 additions & 9 deletions src/UniversalRouterSync.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

import pathToRegexp = require('path-to-regexp')

export interface Params {
[paramName: string]: any
export interface QueryParams {
[paramName: string]: string | string[]
}

export interface Context {
Expand All @@ -23,33 +23,51 @@ export interface ResolveContext extends Context {

export interface RouteContext<C extends Context, R = any> extends ResolveContext {
router: UniversalRouter<C, R>
route: Route
route: Route<C, R>
baseUrl: string
path: string
params: Params
params: QueryParams
keys: pathToRegexp.Key[]
next: (resume?: boolean) => R
}

export type Result<T> = T | void

export interface Route<C extends Context = any, R = any> {
path?: string | RegExp | Array<string | RegExp>
name?: string
parent?: Route | null
parent?: Route<C, R> | null
children?: Routes<C, R> | null
action?: (context: RouteContext<C, R> & C, params: Params) => R | void
action?: (context: RouteContext<C, R> & C, params: QueryParams) => Result<R>
}

export type Routes<C extends Context = Context, R = any> = Array<Route<C, R>>

export type ResolveRoute<C extends Context = Context, R = any> = (
context: C & RouteContext<C, R>,
params: QueryParams,
) => Result<R>

export type ErrorHandler<C extends Context = Context, R = any> = (
error: Error & { status?: number },
context: C & RouteContext<C, R>,
) => Result<R>

export interface Options<C extends Context = Context, R = any> {
context?: C
baseUrl?: string
resolveRoute?: (context: C & RouteContext<C, R>, params: Params) => any
errorHandler?: (error: Error & { status?: number }, context: C & RouteContext<C, R>) => any
resolveRoute?: ResolveRoute<C, R>
errorHandler?: ErrorHandler<C, R>
}

export default class UniversalRouter<C extends Context = Context, R = any> {
static pathToRegexp: typeof pathToRegexp
constructor(routes: Route<C, R> | Routes<C, R>, options?: Options<C>)
baseUrl: string
errorHandler?: ErrorHandler<C, R>
resolveRoute: ResolveRoute<C, R>
context: C & { router: UniversalRouter<C, R> }
root: Route<C, R>
routesByName?: Map<string, Route<C, R>>
constructor(routes: Route<C, R> | Routes<C, R>, options?: Options<C, R>)
resolve(pathnameOrContext: string | ResolveContext): R
}
6 changes: 5 additions & 1 deletion src/generateUrls.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
*/

import { PathFunctionOptions } from 'path-to-regexp'
import UniversalRouter, { Params } from './UniversalRouter'
import UniversalRouter from './UniversalRouter'

export interface Params {
[paramName: string]: any
}

export interface GenerateUrlsOptions extends PathFunctionOptions {
stringifyQueryParams?: (params: Params) => string
Expand Down
4 changes: 2 additions & 2 deletions test/generateUrls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* LICENSE.txt file in the root directory of this source tree.
*/

import UniversalRouter, { Params } from '../src/UniversalRouter'
import generateUrls from '../src/generateUrls'
import UniversalRouter from '../src/UniversalRouter'
import generateUrls, { Params } from '../src/generateUrls'

const router = new UniversalRouter(
[
Expand Down

0 comments on commit 9dfb9b4

Please sign in to comment.