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

Move props option to second arg of createRoute utility #421

Merged
merged 24 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3359c35
Routes with a single component working
pleek91 Jan 12, 2025
d7bec30
Commit a working test
pleek91 Jan 12, 2025
574eadc
Add support for multiple named components
pleek91 Jan 12, 2025
5a42680
Simplify created type (though its not correct yet)
pleek91 Jan 12, 2025
12c62a6
Start organizing new types some
pleek91 Jan 12, 2025
8b20533
Add props to the match when creating a route
pleek91 Jan 12, 2025
f6ac1b6
Fix some types and type guards
pleek91 Jan 12, 2025
d7e4391
Make props optional in match type
pleek91 Jan 12, 2025
2473339
Update some types to have defaults for generics. Start fixing the typ…
pleek91 Jan 13, 2025
53e60dc
Fix parent and child arguments being backwards
pleek91 Jan 13, 2025
f136577
Fix tests
pleek91 Jan 13, 2025
23b2fd8
Make sure match aligns with CreatedRouteOptions
pleek91 Jan 13, 2025
9d36255
Fix ToRoute returning an overly specific route when given CreateRoute…
pleek91 Jan 13, 2025
374a2d8
Add todo
pleek91 Jan 13, 2025
fcb3205
Fix broken type test
pleek91 Jan 13, 2025
f697f09
Add type tests for createRoute
pleek91 Jan 14, 2025
aa045cb
Remove unused type
pleek91 Jan 14, 2025
8002742
Add components tests
pleek91 Jan 14, 2025
927867a
Fix missing undefined types for props
pleek91 Jan 14, 2025
15f32a5
shout out to spell checker plugin
stackoverfloweth Jan 15, 2025
5f6a74c
made undefined not a valid 2nd argument
stackoverfloweth Jan 15, 2025
0f41ce9
Restore commented out tests and fix assertions
pleek91 Jan 15, 2025
2b3a711
Remove now unnecessary `| undefined` from `CreateRouteWithProps`
pleek91 Jan 15, 2025
6a29a8d
Merge branch 'main' into create-route-type-refactor
pleek91 Jan 15, 2025
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
24 changes: 8 additions & 16 deletions src/components/routerLink.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ describe('prefetch props', () => {
name: 'route',
path: '/route',
component: echo,
props: callback,
})
}, callback)

const router = createRouter([route], {
initialUrl: '/',
Expand Down Expand Up @@ -604,8 +603,7 @@ describe('prefetch props', () => {
path: '/route',
component: echo,
prefetch,
props: callback,
})
}, callback)

const router = createRouter([route], {
initialUrl: '/',
Expand Down Expand Up @@ -646,8 +644,7 @@ describe('prefetch props', () => {
name: 'route',
path: '/route',
component: echo,
props: callback,
})
}, callback)

const router = createRouter([route], {
initialUrl: '/',
Expand Down Expand Up @@ -690,8 +687,7 @@ describe('prefetch props', () => {
path: '/echo',
component: echo,
prefetch: { props: 'eager' },
props,
})
}, props)

const router = createRouter([home, route], {
initialUrl: '/',
Expand Down Expand Up @@ -735,17 +731,15 @@ describe('prefetch props', () => {
path: '/parent',
component: echo,
prefetch: { props: false },
props: parentProps,
})
}, parentProps)

const child = createRoute({
parent,
name: 'child',
path: '/child',
component: echo,
prefetch: { props: 'eager' },
props: childProps,
})
}, childProps)

const router = createRouter([home, child], {
initialUrl: '/',
Expand Down Expand Up @@ -781,8 +775,7 @@ describe('prefetch props', () => {
path: '/routeB',
component: echo,
prefetch: { props: 'lazy' },
props: callback,
})
}, callback)

const router = createRouter([routeA, routeB], {
initialUrl: '/routeA',
Expand Down Expand Up @@ -889,8 +882,7 @@ describe('prefetch props', () => {
path: '/routeB',
component: echo,
prefetch: { props: 'intent' },
props: callback,
})
}, callback)

const router = createRouter([routeA, routeB], {
initialUrl: '/routeA',
Expand Down
57 changes: 21 additions & 36 deletions src/components/routerView.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,13 @@ test('Binds props and attrs from route', async () => {
name: 'routeA',
path: '/routeA/[param]',
component: echo,
props: (route) => ({ value: route.params.param }),
})
}, (route) => ({ value: route.params.param }))

const routeB = createRoute({
name: 'routeB',
path: '/routeB/[param]',
component: echo,
props: (route) => {
return { value: route.params.param }
},
})
}, (route) => ({ value: route.params.param }))

const router = createRouter([routeA, routeB], {
initialUrl: '/',
Expand Down Expand Up @@ -336,17 +332,13 @@ test('Updates props and attrs when route params change', async () => {
name: 'sync',
path: '/sync/[param]',
component: echo,
props: (route) => ({ value: route.params.param }),
})
}, (route) => ({ value: route.params.param }))

const asyncProps = createRoute({
name: 'async',
path: '/async/[param]',
component: echo,
props: async (route) => {
return { value: route.params.param }
},
})
}, async (route) => ({ value: route.params.param }))

const router = createRouter([syncProps, asyncProps], {
initialUrl: '/',
Expand Down Expand Up @@ -390,19 +382,17 @@ test('Props from route can trigger push', async () => {
name: 'routeA',
path: '/routeA',
component: echo,
props: (__, context) => {
throw context.push('routeB')
},
}, (__, context) => {
throw context.push('routeB')
})

const routeB = createRoute({
name: 'routeB',
path: '/routeB',
component: echo,
props: () => ({
value: 'routeB',
}),
})
}, () => ({
value: 'routeB',
}))

const router = createRouter([routeA, routeB], {
initialUrl: '/',
Expand Down Expand Up @@ -432,9 +422,8 @@ test('Props from route can trigger reject', async () => {
name: 'routeA',
path: '/routeA',
component: echo,
props: (__, context) => {
throw context.reject('NotFound')
},
}, (__, context) => {
throw context.reject('NotFound')
})

const router = createRouter([routeA], {
Expand Down Expand Up @@ -472,19 +461,17 @@ test('prefetched props trigger push when navigation is initiated', async () => {
path: '/routeB',
component: echo,
prefetch: { props: true },
props: (__, { push }) => {
throw push('routeC')
},
}, (__, { push }) => {
throw push('routeC')
})

const routeC = createRoute({
name: 'routeC',
path: '/routeC',
component: echo,
props: () => ({
value: 'routeC',
}),
})
}, () => ({
value: 'routeC',
}))

const router = createRouter([routeA, routeB, routeC], {
initialUrl: '/routeA',
Expand Down Expand Up @@ -523,19 +510,17 @@ test('prefetched async props trigger push when navigation is initiated', async (
path: '/routeB',
component,
prefetch: { props: true },
props: async (__, { push }) => {
throw push('routeC')
},
}, (__, { push }) => {
throw push('routeC')
})

const routeC = createRoute({
name: 'routeC',
path: '/routeC',
component: echo,
props: () => ({
value: 'routeC',
}),
})
}, () => ({
value: 'routeC',
}))

const router = createRouter([routeA, routeB, routeC], {
initialUrl: '/routeA',
Expand Down
2 changes: 1 addition & 1 deletion src/services/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ComponentProps<TComponent extends Component> = TComponent extends Co
? ComponentProps<T>
: TComponent extends FunctionalComponent<infer T>
? T
: never
: {}

type ComponentPropsGetter<TComponent extends Component> = () => MaybePromise<ComponentProps<TComponent>>

Expand Down
15 changes: 7 additions & 8 deletions src/services/createPropStore.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { InjectionKey, reactive } from 'vue'
import { isWithComponent, isWithComponents } from '@/types/createRouteOptions'
import { isWithComponentProps, isWithComponentPropsRecord, PropsGetter } from '@/types/createRouteOptions'
import { getPrefetchOption, PrefetchConfigs, PrefetchStrategy } from '@/types/prefetch'
import { ResolvedRoute } from '@/types/resolved'
import { Route } from '@/types/route'
import { CallbackPushResponse, CallbackRejectResponse, CallbackSuccessResponse, createCallbackContext } from './createCallbackContext'
import { CallbackContextPushError } from '@/errors/callbackContextPushError'
import { CallbackContextRejectionError } from '@/errors/callbackContextRejectionError'
import { getPropsValue } from '@/utilities/props'
import { PropsCallbackContext } from '@/types/props'

export const propStoreKey: InjectionKey<PropStore> = Symbol()

type ComponentProps = { id: string, name: string, props?: (params: ResolvedRoute, context: PropsCallbackContext) => unknown }
type ComponentProps = { id: string, name: string, props?: PropsGetter }

type SetPropsResponse = CallbackSuccessResponse | CallbackPushResponse | CallbackRejectResponse

Expand Down Expand Up @@ -117,11 +116,7 @@ export function createPropStore(): PropStore {
}

function getComponentProps(options: Route['matched']): ComponentProps[] {
if (isWithComponents(options)) {
return Object.entries(options.props ?? {}).map(([name, props]) => ({ id: options.id, name, props }))
}

if (isWithComponent(options)) {
if (isWithComponentProps(options)) {
return [
{
id: options.id,
Expand All @@ -131,6 +126,10 @@ export function createPropStore(): PropStore {
]
}

if (isWithComponentPropsRecord(options)) {
return Object.entries(options.props).map(([name, props]) => ({ id: options.id, name, props }))
}

return []
}

Expand Down
Loading
Loading