-
-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Data validation in AxiosRequestConfig #4200
Comments
Interesting idea, I wonder if you could write a custom wrapper for axios for your project and use a callback that checks if the AxiosResponse passes Otherwise, I can pick this feature, and write it in axios. Instead of passing the {
firstname: [{
type: "string",
required: true
}],
"lastname": [{
type: "string",
required: true
}]
} That way you can just pass in the type, and get rid of all sort of type checking needed. |
I really like using a data type guard to define the return type of the request. |
Nice proposal! 👍 Wonder if a static-types-only solution would be a potentially less complicated alternative. Eg. supporting configuration of the TypeScript types of the expected request and response for each request path (and checking the type of the request type) would yield most of the benefits without the downside having to run JS code at runtime. Also, no need to duplicate the configuration type PathTypes = [
{
method: 'PUT',
path: `/users/${string}`,
requestBody: {
name: string,
},
responseBody: {
name: string,
favoriteColor: string,
},
},
];
const instance = axios.create<PathTypes>({
baseURL: 'https://example.com/api/',
});
const user = await axios.put(`/users/${id}`, {
namez: 'new name', // ❌ Type error: Property namez does not exist on type
});
user.favoriteColor // ✅ Type: string
user.favoriteColorrr // ❌ Type error: Property favoriteColorrr does not exist on type
Edit: oh hm, I guess not - seems like Lines 392 to 393 in 64dcec5
|
Yeah, it should actually work much better than it currently does. The team is working on a full v2 with much better TS support, I have added this to the backlog so I will try prioritise it. |
should i close the issue ? |
Is your feature request related to a problem? Please describe.
I'm always frustrated when i need to validate the data types from a response after an axios call.
Describe the solution you'd like
like response status, i'd like to validate response data with a validateData in the AxiosRequestConfig.
Also, in typescript, the validateDate could define automatically the returned type.
Describe alternatives you've considered
Wrap my calls in a parent function to check the data type returned.
Additional context
The text was updated successfully, but these errors were encountered: