Skip to content
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

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

Open
comod opened this issue Mar 24, 2022 · 0 comments
Open

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

comod opened this issue Mar 24, 2022 · 0 comments

Comments

@comod
Copy link

comod commented Mar 24, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant