Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve TypeScript typings #167

Merged
merged 1 commit into from
May 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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