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

Bugfix/transformResponse - fixes #6 #8

Closed

Conversation

Mk-Etlinger
Copy link

@Mk-Etlinger Mk-Etlinger commented Apr 9, 2020

This commit fixes #6.

***** EDIT ****
Addressing 1 below: I am now typecasting transformResponse from the axios.defaults so the function I wrote is no longer included. Addressing 2-4 below: I removed the integration test in favor of unit testing. So logs are gone and questions about tests are in regards to an integration test that is no longer in this PR.

There's a couple of things that should be noted/addressed before merging:

  1. transformResponse - Axios includes a default transformResponse but the types are AxiosTransformer | AxiosTransformer[] | undefined. This means that I wrote a function to pull out the default transformer and always return it as AxiosTransformer[]. That seems a bit overkill but the other option was to type cast it as AxiosTransformer[] or rewrite the above function ourselves.
  2. Testing - I added Nock because I needed a way to return a valid response from #makeRequest. I don't know if this is the best way to do it but I'm happy to change it if needed. If you have suggestions on how to effectively test that transformResponse returns JSON if the response is stringified JSON with Sinon or Chai, let me know.
  3. Testing - Since the test needs a callback to fire, I needed to use done() to tell the test runner it's completed. However, when this test fails, it times out and I'm unsure where to catch the error to call done(error). Any ideas?
  4. Logs - I left some logs in here to make it more clear what's happening. I'll take these out before the merge.

Thanks!

Route.ts Outdated
Comment on lines 74 to 77
const { transformResponse } = axios.defaults;

if (!transformResponse) {
return [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should always exist right? Granted transformResponse is included in axios as a part of the defaults

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or are the defaults being overridden?

Copy link
Author

@Mk-Etlinger Mk-Etlinger Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it should technically exist as an AxiosTransformer[] seen here: https://github.com/axios/axios/blob/master/lib/defaults.js#L57-L65.

I can't think of a case where it wouldn't be available, but Typescript was yelling at me because the types are AxiosTransformer(a function) | AxiosTranformer[] | undefined.

So I could remove getDefaultTransformResponse and do

this.transformResponseFn = axios.defaults.transformResponse as AxiosTransformer[]

Does that look better? It seems like a lot of work just to use a function that we already have available here: https://github.com/Econify/graphql-rest-router/blob/master/Route.ts#L52-L58.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up using the axios.default transform always and type casting it because I can't think of a case where it wouldn't be available.

@regan-karlewicz
Copy link
Contributor

Closing, will address in #53 instead

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

Successfully merging this pull request may close these issues.

transformsResponse returns data as a string, vs a JSON object
3 participants