Skip to content

Instantly share code, notes, and snippets.

@sonicoder86
Created April 19, 2024 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonicoder86/2436c1e685056186bdb43f42d6bdca81 to your computer and use it in GitHub Desktop.
Save sonicoder86/2436c1e685056186bdb43f42d6bdca81 to your computer and use it in GitHub Desktop.
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