Skip to content

Instantly share code, notes, and snippets.

@zomars
Created May 30, 2022 21:52
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 zomars/448de86f347a8567f034682e42ba2071 to your computer and use it in GitHub Desktop.
Save zomars/448de86f347a8567f034682e42ba2071 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