Created
April 19, 2024 08:51
-
-
Save sonicoder86/2436c1e685056186bdb43f42d6bdca81 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { z } from 'zod' | |
const MAIN_PAGE = 'main' | |
const VIDEOS_PAGE = 'videos' | |
const pages = [MAIN_PAGE, VIDEOS_PAGE] as const | |
type Page = typeof pages[number] | |
const pageDefinitions = { | |
[MAIN_PAGE]: { | |
query: z.object({ | |
email: z.string(), | |
}) | |
}, | |
[VIDEOS_PAGE]: { | |
query: z.object({ | |
page: z.number(), | |
}) | |
} | |
} | |
type PageKeys = keyof typeof pageDefinitions | |
const getDefinition = <T extends PageKeys>(key: T): typeof pageDefinitions[T]['query'] => { | |
return pageDefinitions[key].query; | |
} | |
const useQuery = <T extends PageKeys>(key: T, input: any): z.infer<ReturnType<typeof getDefinition<T>>> => { | |
return getDefinition(key).parse(input); | |
} | |
const result = useQuery(MAIN_PAGE, {}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment