Manage axios interceptors
Closed this issue · 3 comments
Hello, I need to manage Axios interceptor with this library, is there any way to do that?
Thanks
Hi @caox2110,
Honestly, I never have used Axios interceptors, but reading the docs it seems that you could add interceptors to an Axios data-provider instance through its client
property, which stores the axios
instance created with axios.create
internally:
import { Axios } from "@data-provider/axios";
const booksModel = new Axios({
id: "books-model",
url: "/books/:id",
baseUrl: "http://foo.api.com"
});
booksModel.client.interceptors.request.use(function () {/*...*/});
Take into account that every new data-provider
queried instance contains a different axios
instance, so you should add the interceptors again to every new instance created with the .query
method.
Please try the example and let me know it it works or not. This may be an useful feature, so I would like to take it into account for new releases and docs.
You could also use the providers
handler to add the same interceptor to all data-providers having the same tag, which would avoid to always have to add the interceptors to every new instance returned by a query
method:
import { providers } from "@data-provider/core";
import { Axios } from "@data-provider/axios";
providers.getByTag("with-interceptor").onNewProvider((provider) => {
provider.client.interceptors.request.use(function () {/*...*/});
});
const booksModel = new Axios({
id: "books-model",
url: "/books/:id",
baseUrl: "http://foo.api.com",
tags: ["with-interceptor"] // The axios interceptor will be added to this provider
});
const secondBook = booksModel.query({ urlParams: { id: 2 } }); // This queried provider will also have the interceptor.
Closed due to project discontinuation