Skip to content
This repository was archived by the owner on Dec 19, 2017. It is now read-only.

Typescript type declarations #142

Closed
wants to merge 7 commits into from
Closed
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
94 changes: 94 additions & 0 deletions ko-component-router.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
interface KoComponentRouterRoute {
component: string;
}

type CallbackResult = void | false | Promise<boolean>;
type DoneCallback = (boolean) => void;

type KoComponentRouterPromiseMiddleware = (ctx: KoComponentRouterContext) => CallbackResult;
type KoComponentRouterCallbackMiddleware = (ctx: KoComponentRouterContext, done: DoneCallback) => void;
type KoComponentRouterLifecycleMiddleware = (ctx: KoComponentRouterContext) => {
beforeRender: () => CallbackResult;
afterRender: () => CallbackResult;
beforeDispose: () => CallbackResult;
afterDispose: () => CallbackResult;
};
type KoComponentRouterMiddleware = KoComponentRouterPromiseMiddleware
| KoComponentRouterCallbackMiddleware
| KoComponentRouterLifecycleMiddleware;

interface KoComponentRouterContext {
$child: KoComponentRouterContext;
$parent: KoComponentRouterContext;
addBeforeNavigateCallback(cb: () => CallbackResult);
addBeforeNavigateCallback(cb: (done: DoneCallback) => void);
canonicalPath: string;
element: HTMLElement;
fullPath: string;
params: any;
path: string;
pathname: string;
route: KoComponentRouterRoute;
router: KoComponentRouterInstance;
}

interface KoComponentRouterInstance {
$child: KoComponentRouterInstance;
$parent: KoComponentRouterInstance;
base: string;
ctx: KoComponentRouterContext;
isRoot: boolean;
isNavigating: KnockoutObservable<boolean>;

getPathFromLocation(): string;
resolvePath(path): any;
update(path: string, push?: boolean): Promise<boolean>;
}

interface KoComponentRouterRoutes {
[path: string]: string
| KoComponentRouterRoutes
| Array<string | KoComponentRouterPromiseMiddleware | KoComponentRouterLifecycleMiddleware>;
}

interface KoComponentRouterConfig {
base?: string;
hashbang?: boolean;
}

declare module 'ko-component-router' {
class Router implements KoComponentRouterInstance {
static config: KoComponentRouterConfig;
static middleware: KoComponentRouterMiddleware[];
static plugins: any[];
static routes: KoComponentRouterRoutes;
static head: KoComponentRouterInstance;
static tail: KoComponentRouterInstance;

static canonicalizePath(path: string): string;
static get(index: number): KoComponentRouterInstance;
static getPath(url: string): string;
static parseUrl(url: string): { hash: string, pathname: string, search: string };
static sameOrigin(href: string): boolean;
static setConfig(config: KoComponentRouterConfig): void;
static update(path: string, push?: boolean): Promise<boolean>;
static update(path: string, options: { push?: boolean; force?: boolean; with?: any }): Promise<boolean>;
static use(...middleware: KoComponentRouterMiddleware[]): void;
static usePlugin(...fns): void;
static useRoutes(routes: KoComponentRouterRoutes): void;

$child: KoComponentRouterInstance;
$parent: KoComponentRouterInstance;
base: string;
ctx: KoComponentRouterContext;
isRoot: boolean;
isNavigating: KnockoutObservable<boolean>;

constructor();
getPathFromLocation(): string;
resolvePath(path): any;
update(path: string, push?: boolean): Promise<boolean>;
}

export = Router;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"main": "dist/ko-component-router.js",
"jsnext:main": "dist/modules/index.js",
"module": "dist/modules/index.js",
"types": "ko-component-router.d.ts",
"files": [
"dist"
],
Expand Down