Skip to content

Instantly share code, notes, and snippets.

@zomars
Created May 30, 2022 21:53
Show Gist options
  • Save zomars/171f478a0050e7d52aac23851dc1e76e to your computer and use it in GitHub Desktop.
Save zomars/171f478a0050e7d52aac23851dc1e76e to your computer and use it in GitHub Desktop.
End to end API type safety with SWR and Next.js

Idea

Being able to auto type API endpoint according the use. This could be achieveable using TypeScript template literals.

Example:

Having these API endpoints:

  • /api/users
  • /api/companies

We should be able to use a hook that changes the returned based on the endpoint we ask for:

const { data } = useAPI('users'); // This will return type `{ data: User[] }`
const { data } = useAPI('companies') // This will return type `{ data: Company[] }`

...WIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment