Skip to content

Commit

Permalink
WIP, using single generic for createRoute overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
stackoverfloweth committed Jan 7, 2025
1 parent 1300769 commit 6872817
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 125 deletions.
226 changes: 109 additions & 117 deletions src/services/createRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,147 +14,139 @@ import { Param } from '@/types/paramTypes'
import { Path, ToPath, toPath } from '@/types/path'
import { Query, ToQuery, toQuery } from '@/types/query'
import { RouteMeta } from '@/types/register'
import { Route } from '@/types/route'
import { Route, ToMeta, ToState } from '@/types/route'
import { checkDuplicateParams } from '@/utilities/checkDuplicateKeys'

export function createRoute<
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithoutComponents
& WithoutParent):
Route<
ToName<TName>,
Host<'', {}>,
ToPath<TPath>,
ToQuery<TQuery>,
ToHash<THash>,
TMeta,
TState,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, ToMeta<TMeta>, ToState<TState>> : never

export function createRoute<
const TParent extends Route,
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithoutComponents
& WithParent<TParent>):
Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<TMeta, TParent['meta']>,
CombineState<TState, TParent['state']>,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[...TParent['matches'], CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<ToMeta<TMeta>, TParent['meta']>,
CombineState<ToState<TState>, TParent['state']>
> : never

export function createRoute<
TComponent extends Component,
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
& WithComponent<TComponent, Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState>>
const TComponent extends Component,
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithComponent<TComponent>
& WithoutParent):
Route<
ToName<TName>,
Host<'', {}>,
ToPath<TPath>,
ToQuery<TQuery>,
ToHash<THash>,
TMeta,
TState,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<
ToName<TName>,
Host<'', {}>,
ToPath<TPath>,
ToQuery<TQuery>,
ToHash<THash>,
ToMeta<TMeta>,
ToState<TState>
> : never

export function createRoute<
TComponent extends Component,
const TParent extends Route,
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
& WithComponent<TComponent, Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TParent>>
const TComponent extends Component,
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithComponent<TComponent>
& WithParent<TParent>):
Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<TMeta, TParent['meta']>,
CombineState<TState, TParent['state']>,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[...TParent['matches'], CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<ToMeta<TMeta>, TParent['meta']>,
CombineState<ToState<TState>, TParent['state']>
> : never

export function createRoute<
TComponents extends Record<string, Component>,
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
& WithComponents<TComponents, Route<ToName<TName>, Host<'', {}>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, TMeta, TState>>
const TComponents extends Record<string, Component>,
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithComponents<TComponents>
& WithoutParent):
Route<
ToName<TName>,
Host<'', {}>,
ToPath<TPath>,
ToQuery<TQuery>,
ToHash<THash>,
TMeta,
TState,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<
ToName<TName>,
Host<'', {}>,
ToPath<TPath>,
ToQuery<TQuery>,
ToHash<THash>,
ToMeta<TMeta>,
ToState<TState>
> : never

export function createRoute<
TComponents extends Record<string, Component>,
const TParent extends Route,
const TName extends string | undefined = undefined,
const TPath extends string | Path | undefined = undefined,
const TQuery extends string | Query | undefined = undefined,
const THash extends string | Hash | undefined = undefined,
const TMeta extends RouteMeta = RouteMeta,
const TState extends Record<string, Param> = Record<string, Param>
>(options: CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>
& WithComponents<TComponents, Route<ToName<TName>, Host<'', {}>, CombinePath<TParent['path'], ToPath<TPath>>, CombineQuery<TParent['query'], ToQuery<TQuery>>, CombineHash<TParent['hash'], ToHash<THash>>, CombineMeta<TMeta, TParent['meta']>, CombineState<TState, TParent['state']>, TParent>>
const TComponents extends Record<string, Component>,
const TOptions extends CreateRouteOptions
>(options: TOptions
& WithComponents<TComponents>
& WithParent<TParent>):
Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<TMeta, TParent['meta']>,
CombineState<TState, TParent['state']>,
CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>,
[...TParent['matches'], CreateRouteOptions<TName, TPath, TQuery, THash, TMeta>]
>
TOptions extends CreateRouteOptions<
infer TName extends string | undefined,
infer TPath extends Path | string | undefined,
infer TQuery extends Query | string | undefined,
infer THash extends Hash | string | undefined,
infer TMeta extends RouteMeta,
infer TState extends Record<string, Param>
> ? Route<
ToName<TName>,
Host<'', {}>,
CombinePath<TParent['path'], ToPath<TPath>>,
CombineQuery<TParent['query'], ToQuery<TQuery>>,
CombineHash<TParent['hash'], ToHash<THash>>,
CombineMeta<ToMeta<TMeta>, TParent['meta']>,
CombineState<ToState<TState>, TParent['state']>
> : never

export function createRoute(options: CreateRouteOptions): Route {
const id = createRouteId()
Expand Down
35 changes: 27 additions & 8 deletions src/types/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
import { CreateRouteOptions } from '@/types/createRouteOptions'
import { Hash } from '@/types/hash'
import { Component } from 'vue'
import { CreateRouteOptions, WithoutState, WithState } from '@/types/createRouteOptions'
import { Hash, ToHash } from '@/types/hash'
import { Host } from '@/types/host'
import { Param } from '@/types/paramTypes'
import { Path } from '@/types/path'
import { Path, ToPath } from '@/types/path'
import { PrefetchConfig } from '@/types/prefetch'
import { Query } from '@/types/query'
import { Query, ToQuery } from '@/types/query'
import { RouteMeta } from '@/types/register'
import { WithHooks } from './hooks'
import { ToName } from './name'

/**
* Represents an immutable array of Route instances. Return value of `createRoute`, expected param for `createRouter`.
*/
export type Routes = readonly Route[]

export type ToMeta<TMeta extends RouteMeta | undefined> = TMeta extends undefined ? {} : TMeta
export type ToState<TState extends Record<string, Param> | undefined> = TState extends undefined ? Record<string, Param> : TState

export type CreatedRouteOptions<
TName extends string | undefined = string | undefined,
TPath extends Path | undefined = Path | undefined,
TQuery extends Query | undefined = Query | undefined,
THash extends Hash | undefined = Hash | undefined,
TMeta extends RouteMeta = RouteMeta,
TState extends Record<string, Param> = Record<string, Param>,
TComponents extends Record<string, Component> = Record<string, Component>
> = CreateRouteOptions<ToName<TName>, ToPath<TPath>, ToQuery<TQuery>, ToHash<THash>, ToMeta<TMeta>, ToState<TState>> & {
id: string,
component: TComponents,
}

/**
* Represents the structure of a route within the application. Return value of `createRoute`
* @template TName - Represents the unique name identifying the route, typically a string.
Expand All @@ -26,8 +45,8 @@ export type Route<
THash extends Hash = Hash,
TMeta extends RouteMeta = RouteMeta,
TState extends Record<string, Param> = Record<string, Param>,
TMatched extends CreateRouteOptions = CreateRouteOptions,
TMatches extends CreateRouteOptions[] = CreateRouteOptions[]
TComponents extends Record<string, Component> = Record<string, Component>,
TParentMatches extends CreatedRouteOptions[] = CreatedRouteOptions[]
> = {
/**
* Unique identifier for the route, generated by router.
Expand All @@ -36,12 +55,12 @@ export type Route<
/**
* The specific route properties that were matched in the current route.
*/
matched: TMatched,
matched: CreatedRouteOptions<TName, TPath, TQuery, THash, TMeta, TState, TComponents>,
/**
* The specific route properties that were matched in the current route, including any ancestors.
* Order of routes will be from greatest ancestor to narrowest matched.
*/
matches: TMatches,
matches: [...TParentMatches, CreatedRouteOptions<TName, TPath, TQuery, THash, TMeta, TState, TComponents>],
/**
* Identifier for the route as defined by user. Name must be unique among named routes. Name is used for routing and for matching.
*/
Expand Down

0 comments on commit 6872817

Please sign in to comment.