Solution Part 2
Let us improve the fake APIs
Let us look at the code with the problem highlighted:
API Separation
We should first make them promises instead of just timeouts. We should also take them out of the App
component and into a dedicate file api.ts
in our features/profile
folder.
Now we can import them into App.tsx
Dealing with TypeScript
There are two TypeScript errors here, but it is the same error message in both the cases.
- The first error at
setProfile(data)
- The second error at
setProfile(savedData)
This is happening because our API is not typed yet, the default type for promises upon resolve is unknown
. The simplest fix is to explicitly type the promises.
Even in production projects, this is how it is done. This is because APIs are essentially black boxes and sometimes even live on different systems. TypeScript cannot infer the types due to this constraint and the developer has to manually type them. However, there are tools and libraries that can help with this process better such as tRPC.
Mock Service Worker (MSW) for mocking API calls
MSW is the industry standard way for mocking API calls. This is great in the case of testing or development without proper backend servers. Essentially perfect in our case. This is also framework or library agnostic, which means it can be used with any framework or library and makes it a fundamental tool for frontend developers.
Let us install it in our project:
Since we are in a client side project, to work with MSW on the browser, we will need to register a service worker that helps MSW do its thing.
What is a service worker? They are quite versatile and run in the background. They enable a lot of things like caching, intercepting requests, offline support, push notifications, etc. You can read more about them here.
The below command will create the service worker in the public
directory for us, we also do not have to touch this file at all.
Next, we have to setup the worker with handlers for our API calls. Create a new file called src/mocks/browser.ts
and add the following code:
This still doesn't do much. We have to add the handlers for our API calls. Create a new file called src/mocks/handlers.ts
and add the following code:
What is happening here? We are using the http
method provided by MSW to register the handlers. The first handler is for the fetchProfile
API call and the second is for the saveProfile
API call. We make assumptions based on the backend API calls and structure these handlers.
Now, we can import them into worker file src/mocks/browser.ts
.
Let us now replace our fake APIs with the MSW APIs:
Hmm, things seem to have broken.
Defer and Conditionally Enable MSW
Registering a service worker is an async operation, so we need to defer the rendering of our app until the worker is registered.
In our main.tsx
file, we will add the following code:
We have to also install @types/node
package to access the process
object.
Now check your dev server and you should be able to see the app without any errors. Also, in the browser console, you should see the following message:
Whenever possible, work with the real APIs and only mock the APIs that are not available in your environment. Phase out the mocking as soon as possible. But mocks can help you simulate the behavior of the APIs quite well.
With this you now have a great mocking setup for your app. In the next chapter, we will bring in React Query to better fetch and mutate data.
At this point, your code should be a good match to the branch of the repository: 1-solution-part-2
Last updated on