Develop Todo App
Create farrow.config.js#
First, in the farrow.config.js configuration file, configure the rules for generating farrow-api.
Generate the interface http://localhost:3003/api/todo to the local ${__dirname}/src/api/todo.ts via codegen.
After running farrow dev, new files will be added in the directory specified by the dist configuration.
As above, we can see that the type definition of the interface, as well as the call function for the api client object, is generated.
Import the generated modules#
Then, in the src/App.tsx component, we can directly import the types and objects contained in the generated module.
Implement TodoItem component#
Then we add the component implementation, defining the TodoItem component first, which uses the generated Todo type directly.
Implement App component#
Then define the App component.
In the handleAddTodo function, we call the api.addTodo interface and process the Tagged Unions type it returns.
The pattern is the same in other interfaces such as handleRemove.
In summary, the best practice for farrow-api is to
- On the server side, use
Tagged Unionsto encode theinput/outputof the interface - On the client side, use
result.typeto distinguish between differentcasesof data consumption.
The finished code can be found in the repository farrow-js/farrow-vite-todo-app.