ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests
MIT License
3.47k stars 245 forks source link

How to avoid using JSON.parse in mocked Post? #333

Open comod opened 2 years ago

comod commented 2 years ago

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