Skip to content

Typed query hook#6034

Merged
kodiakhq[bot] merged 24 commits intomainfrom
util/typed-query
Dec 19, 2022
Merged

Typed query hook#6034
kodiakhq[bot] merged 24 commits intomainfrom
util/typed-query

Conversation

@sean-brydon
Copy link
Copy Markdown
Member

A hook that allows you to have type safe router queries. This was a nightmare - was working on something a lot messier than this for booking limits and decided to spend some time cleaning it up and learning a thing or two about conditional generics

Pass in a schema to the hook that you would like to keep type safe
e.g.
CleanShot 2022-12-15 at 11 30 32@2x

results will be fully typesafe
CleanShot 2022-12-15 at 11 31 52@2x
Setting values
CleanShot 2022-12-15 at 11 32 31@2x
remove by key
CleanShot 2022-12-15 at 11 32 50@2x
push item to key
CleanShot 2022-12-15 at 11 33 08@2x
Remove by key and value
CleanShot 2022-12-15 at 11 33 36@2x

@vercel
Copy link
Copy Markdown

vercel Bot commented Dec 15, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
cal ✅ Ready (Inspect) Visit Preview Dec 19, 2022 at 10:37PM (UTC)

Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated

// push item to existing key
function pushItemToKey<J extends SchemaKeys>(
key: J,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can filter this to only get the keys that rep arrays

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I honestly couldnt get this working - I was trying the ArrayKeys type above but was getting an error about not being able to set J to typeof

Maybe @zomars has more TS knowledge to debug this

Copy link
Copy Markdown
Contributor

@zomars zomars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty impressive @sean-brydon will need a little bit of more time to review properly

Comment on lines +6 to +14
export const queryNumberArray = z
.string()
.or(z.number())
.or(z.array(z.number()))
.transform((a) => {
if (typeof a === "string") return a.split(",").map((a) => Number(a));
if (Array.isArray(a)) return a;
return [a];
});
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support number arrays as a final "type" but input can be string so we can parse it into a number array

query params come in as strings before being parsed

Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment on lines +50 to +51
const search = new URLSearchParams(newValue).toString();
router.replace({ query: search }, undefined, { shallow: true });
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to do this to prevent duplicate values Nextjs actually recommends you construct query params this way

Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment on lines +37 to +39
let parsedQuery: InferedSchema = useMemo(() => {
return {} as InferedSchema;
}, []);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Preventing infinite renders

Comment on lines +41 to +42
if (parsedQuerySchema.success) parsedQuery = parsedQuerySchema.data;
else if (!parsedQuerySchema.success) console.error(parsedQuerySchema.error);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safe parse! No crash if the user breaks things

Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Copy link
Copy Markdown
Contributor

@zomars zomars left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merry Xmas @sean-brydon 🙏🏽

Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment thread packages/lib/hooks/useTypedQuery.ts Outdated
Comment on lines +27 to +32
export const schema = z.object({
teamIds: queryNumberArray.optional(),
userIds: queryNumberArray.optional(),
status: z.enum(["upcoming", "recurring", "past", "cancelled", "unconfirmed"]),
eventTypeIds: queryNumberArray.optional(),
});
Copy link
Copy Markdown
Contributor

@zomars zomars Dec 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already fixed this in a cleanup commit

@kodiakhq kodiakhq Bot merged commit 2cddad4 into main Dec 19, 2022
@kodiakhq kodiakhq Bot deleted the util/typed-query branch December 19, 2022 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automerge ♻️ autoupdate tells kodiak to keep this branch up-to-date

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants