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

feat: add typescript support for vanjs-cone (auto-inferred by ChatGP… #190

Closed
wants to merge 1 commit into from
Closed
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
69 changes: 69 additions & 0 deletions addons/van_cone/src/spa.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
export type RouteHandler = (args: {
params: Record<string, string>;
query: Record<string, string>;
}) => void;

export type Route = {
name: string;
path: string;
handler: RouteHandler;
matcher: RegExp;
params: string[];
};

export type RouteParamsResult =
| {
route: Route;
params: Record<string, string>;
}
| undefined;

export type ParsedUrl = {
path: string;
queryString: string;
};

export class Router {
private routes: Route[];
private prefix: string;

add(name: string, path: string, handler: RouteHandler): this {
this.routes.push(this.createRoute(name, path, handler));
return this;
}

setPrefix(prefix: string): this;

dispatch(url: string): Route | false;

getRoute(url: string): Route | undefined;

formatUrl(
routeName: string,
params: Record<string, any>,
query: Record<string, any>
): string;

private createRoute(name: string, path: string, handler: RouteHandler): Route;

private findRouteParams(path: string): RouteParamsResult;

private getMatchedParams(
route: Route,
path: string
): Record<string, string> | false;

private getQueryParams(query: string): Record<string, string>;

private parseUrl(url: string): ParsedUrl;

private stripPrefix(url: string, prefix: string): string;
}

export function createCone(
routerElement: Element,
routes: { name: string; path: string; callable: Function; title?: string }[],
defaultNavState?: any
): any; /* Add your specific types here */

export default createCone;