ctimmerm / axios-mock-adapter

Axios adapter that allows to easily mock requests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to avoid using JSON.parse in mocked Post?

comod opened this issue · comments

commented

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