How not to use Fetch API
Since Fetch API is supported in any modern browser and current version of Node.js Farfetched uses it to make HTTP calls. It adds zero overhead to client bundle and pretty fast on server side. However, in some cases you may want to switch to XMLHttpRequest or some wrappers about it (e.g. axios). Let us see how it can be done.
No Fetch in Query
You can use createQuery
-factory and passes your handler to it 👇
import { createQuery } from '@farfetched/core';
import axios from 'axios';
const usersQuery = createQuery({
handler: () => axios.get('/users').then((res) => res.data),
});
That is it, usersQuery
is a regular Query that can be used in any function from Farfetched. Of course, you can use any other library to make HTTP calls the same way.
Furthermore, you can consider creating a custom Query factory to simplify Query creation across the application.
No Fetch in Mutation
TIP
Farfetched is build over concept re-using, so replacing Fetch API with other HTTP client in Mutation is a similar to Query case.
You can use createMutation
-factory and passes your handler to it 👇
import { createMutation } from '@farfetched/core';
import axios from 'axios';
const loginMutation = createMutation({
handler: ({ login, password }) =>
axios.post('/login', { login, password }).then((res) => res.data),
});
That is it, loginMutation
is a regular Mutation that can be used in any function from Farfetched. Of course, you can use any other library to make HTTP calls the same way.
Furthermore, you can consider creating a custom Mutation factory to simplify Mutation creation across the application.