directus/sdk interceptor #22333
Closed
1Luc1
started this conversation in
Feature Requests
Replies: 2 comments 2 replies
-
Work for that is already ongoing #21474 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for the info. I'll use import { createDirectus, rest } from "@directus/sdk";
import { ofetch } from "ofetch";
const apiFetch = ofetch.create({
onRequestError({ error }) {
return Promise.reject(error);
},
onResponseError({ response }) {
// get error code
const errorCode = response._data.errors[0].extensions.code;
// https://docs.directus.io/reference/introduction.html#error-codes
switch (errorCode) {
case "TOKEN_EXPIRED":
// TODO refresh token
console.log("token is expired -> refresh");
break;
case "INVALID_CREDENTIALS":
console.log("invalid user credentials");
break;
}
return Promise.reject(response);
},
});
const directusClient = createDirectus(import.meta.env.VITE_API_URL, {
globals: {
fetch: apiFetch,
},
}).with(rest());
export default directusClient; |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
I was wondering if there is any good way to handle general responses from the directus sdk calls. Within axios they have interceptors which may be quite use full to handle error responses 400, 401, ...
With the previous client there was something possible to use the internal axios interceptor. Example here.
So is there a way to define an general interceptor / handler and not do it with every request call like below
Beta Was this translation helpful? Give feedback.
All reactions