axios-mock-adapter icon indicating copy to clipboard operation
axios-mock-adapter copied to clipboard

How to avoid using JSON.parse in mocked Post?

Open comod opened this issue 2 years ago • 0 comments

I am a little bit confused about the workflow of how everything communicates with each other. As far as i understand axios converts post data automatically into json-string. so i have to decode it on the api side? There is no other way to do this? my goal is to keep everything as simple and easy as possible. But for the moment this seems how apis works. So i have to do this with my mock too, right?

mock.onPost(endpoint).reply((request: AxiosRequestConfig) => {
  const data = <RequestParams>JSON.parse(request.data)

Again: Is there no better way as always repeat these line of json decoding? For Instance:

mock.onPost(endpoint).reply((request: AxiosRequestConfig<RequestParams>) => {
  const foo = request.data.foo

RequestParams is an interface

interface RequestParams {
  foo: whatever
}

I tried to use interceptors, but this would be wrong anyways

comod avatar Mar 24 '22 17:03 comod