Plugin to transform and validate axios responses using typestack class-transformer and class-validator.
At some point in your application apply the plugin as default response transformer:
axios.defaults.transformResponse = classTransformValidateResponse;
To apply the transformer use the requests config as follows:
await axios.get('https://api.example.com', {
classTransformConfig: {
targetClass: TestClass, // <- Class to transform the response to
dataRetriever: (data: any) => data.data // <- (optional) Function to retrieve the data from the response
}
});
To stop your IDE from yelling at you about the classTransformConfig
property, you can add the following typings to your project:
export * from "axios-transform-validate-response";
Run npm test