Stop Writing Your Routes

Rate this content
Bookmark

The more you keep working on an application, the more complicated its routing becomes, and the easier it is to make a mistake. ""Was the route named users or was it user?"", ""Did it have an id param or was it userId?"". If only TypeScript could tell you what are the possible names and params. If only you didn't have to write a single route anymore and let a plugin do it for you. In this talk we will go through what it took to bring automatically typed routes for Vue Router.

This talk has been presented at Vue.js London 2023, check out the latest edition of this JavaScript Conference.

FAQ

Eduardo is a Querty member, developer, and open-source lover with almost seven years of experience in developing libraries such as PNIA and Vue Router.

One of the biggest challenges in designing APIs for open-source libraries is taking into account various factors to ensure the API is user-friendly and minimizes errors.

Making errors hard to make is important for a good API because it prevents users from feeling dumb and enhances their overall experience by making the API more intuitive.

API ergonomics refers to how often a feature is used and how easy it is for users to guess how to use that feature, aiming to minimize context switching and maximize user efficiency.

File-based routing in Vue Router allows defining routes by using a folder structure, which helps in reducing code repetition and improving developer experience by minimizing context switching.

Data loaders are functions that return data and provide a composable for accessing that data, including loading states and errors, while handling SSR and avoiding duplicate requests.

Yes, you can use type routes without file-based routing, but it requires more manual work as the types won't be automatically generated, making it more error-prone.

No, the Unplugging ViewRouter plugin does not support Vue 2 due to significant differences in the router types and time constraints.

Protected routes can be handled by creating navigation guards in the router instance, or by using Meta fields to define pre-configured navigation guards for multiple pages.

Yes, loaders can be defined in a separate file and imported into multiple components. They just need to be exported from the page to indicate that the page is using that loader.

Eduardo San Martin Morote
Eduardo San Martin Morote
30 min
12 May, 2023

Comments

Sign in or register to post your comment.
Video Summary and Transcription
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
Available in Español: Deja de Escribir tus Rutas

1. Designing APIs: Challenges and Considerations

Short description:

Designing API is really hard. It's one of the biggest challenges of any open source library. A good API makes errors hard to make and avoids context switch. Writing different versions of an API and considering the language used are important. The learning process for an API is subjective.

So yeah, my name is Eduardo. Good morning, London. Happy to be here another year. So as a Querty member but also as a developer and open source lover, I've been doing a lot of development of libraries, almost for seven years I think. So not only PNIA and Vue Router but also some of the libraries that are just adjacent to Vue itself. And I have been spending quite a lot of time thinking about how do we design the APIs for these libraries. Sometimes messing up, making mistakes, sometimes improving them afterwards, of course. But the bottom line is designing API is really hard, okay?

And I think it comes without saying that this is one of the biggest challenges of any open source library because you have to factor a lot of, I'm sorry, you have to take into account a lot of different factors. It's from how does these API changes, wait, sorry, I need to change the thing. This is going to be painful. Okay. So you have to take into account a lot of different things into account. Okay? You have to factor the users you have, are you building a new API? Are you not building a new API? And how the API feels to users, because at the end, an API being good or bad is very subjective. And I'm going to show you why.

So one of the things that I feel is very important to a good API is to make errors hard to make. Now, it might be obvious to some and completely new to others, but if you use a library where making errors is actually easy, it makes you feel dumb. Now, nobody likes to feel dumb when they're using something, are they? So it's definitely a big factor in my opinion of how an API can be good or not. Another big thing is avoiding context switch. Now, when we think about APIs, we first think about the code we need to write. But if the actual API goes beyond the code we write, it's not only the files, the folder that we have, but we can actually think about many other things that are part of the API because when we write code, when we develop a program, we're not only writing code itself. And then we have to cut it for different user experiences.

Now, this is very vague to be honest, but it's not the same to write a V1 of an API, so you only have new users, than writing a V2 of an API where you have existing users that are used to something. Also, you have to think about well, what language you are writing for is not the same writing in API in Java or Rust, than in JavaScript or TypeScript where things are evolving very quick. And I like to refer to this little joke, the user learning curve. So, the thing is our experience when learning something, which is also part of an API, the learning process is different and is subjective. So, the joke here is very simple actually. You have kind of like the time recently and then how much you can do with a tool. So, as you can see, Notepad, you can use it very quickly and you are not very efficient.

2. Different Tools and API Ergonomics

Short description:

Different tools have different levels of efficiency and productivity. Notepad is quick but not efficient. Pico allows for more functionality. Visual Studio has many shortcuts but can lead to decreased productivity. Vee, Veeam, and Nveam require learning but result in high productivity. Emacs and Spacemacs are powerful tools. This forms the basis of API ergonomics.

You have kind of like the time recently and then how much you can do with a tool. So, as you can see, Notepad, you can use it very quickly and you are not very efficient. Then you have Pico, which is just a terminal-based tool and you can do a little bit more things. Then you have Visual Studio, which you learn a lot of shortcuts, I think, and I believe the joke is that then you start digging too much into IDE and you spend too much doing things and then you become less productive. And then you have Vee or Veeam or Nveam, which basically you need to learn a lot before even being able to use it at all, but then you're very productive. And then you have Emacs and Spacemacs, which bend time and space, I think, because there is no way to do that in math. And so I'm going to use this as a base for what I call API ergonomics.

QnA

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
Watch video: Vue: Feature Updates
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
TypeScript and React: Secrets of a Happy Marriage
React Advanced 2022React Advanced 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Nuxt on the Edge
Vue.js London 2023Vue.js London 2023
30 min
Nuxt on the Edge
Nuxt is a web framework with many features including server-side rendering, client-side rendering, static site generation, edge site rendering, and more. The Edge is a limited environment running on CDN nodes, such as Cloudflare network. Database options on the Edge include Postgre with Neon, Versel on Neon, Superbase, MySQL with plan scale, HyperDB, and KV with redis and Cloudflare storage. The speaker demonstrates creating a demo with a votes table, handling API requests, adding authentication, saving votes, and displaying results. The roadmap to a full stack Nuxt 3 with an edge-first experience is in progress. Copilot is a helpful tool for developers. Integrating SSO with GitHub and improving the developer experience are important considerations for Nuxt 3.

Workshops on related topic

React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetsov
Mikhail Kuznetsov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced 2022React Advanced 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side.