diff --git a/ko-component-router.d.ts b/ko-component-router.d.ts new file mode 100644 index 0000000..7ef97ec --- /dev/null +++ b/ko-component-router.d.ts @@ -0,0 +1,94 @@ +interface KoComponentRouterRoute { + component: string; +} + +type CallbackResult = void | false | Promise; +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; + + getPathFromLocation(): string; + resolvePath(path): any; + update(path: string, push?: boolean): Promise; +} + +interface KoComponentRouterRoutes { + [path: string]: string + | KoComponentRouterRoutes + | Array; +} + +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; + static update(path: string, options: { push?: boolean; force?: boolean; with?: any }): Promise; + 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; + + constructor(); + getPathFromLocation(): string; + resolvePath(path): any; + update(path: string, push?: boolean): Promise; + } + + export = Router; +} diff --git a/package.json b/package.json index c29ba6d..c85727a 100644 --- a/package.json +++ b/package.json @@ -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" ],