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

replace request with node-fetch #13

Merged
merged 7 commits into from
Aug 19, 2020

Conversation

newhouse
Copy link
Contributor

@newhouse newhouse commented Aug 18, 2020

Description of the change

Replace request with node-fetch. Also:

  • add support for streams while we're at it.
  • add User-Agent option and default.

image

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Related issues

Fixes #7

Checklists

Development

  • The code changed/added as part of this pull request has been covered with tests
  • All tests related to the changed code pass in development
  • No previous tests unrelated to the changed code fail in development

Code review

  • This pull request has a descriptive title and information useful to a reviewer. There may be a screenshot or screencast attached.
  • At least one reviewer has been requested
  • Changes have been reviewed by at least one other engineer
  • The relevant project board has been selected in Projects to auto-link to this pull request

Copy link
Contributor Author

@newhouse newhouse left a comment

Choose a reason for hiding this comment

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

I need to add tests perhaps...and def update the documentation.

src/index.js Outdated Show resolved Hide resolved
yarn.lock Show resolved Hide resolved
@newhouse newhouse marked this pull request as ready for review August 18, 2020 21:45
return this.requestREST(`/api/v1/fill/${pdfTemplateID}.pdf`, {
method: 'POST',
json: payload,
encoding: null,
Copy link
Contributor

Choose a reason for hiding this comment

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

All good without specifying an encoding? It doesnt try to translate into UTF8 by default?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is my understanding that for the request library, the encoding: null option said "I want the response to be a Buffer", which makes sense b/c we wrote the client to deal with buffers as the happy-path response.

With node-fetch, you explicitly tell it how you'd like to consume the response via the response.buffer() (or similar) calls, and there is no UTF-stuff that happens when dealing with the stream or buffer types. There is, for example, when you call response.json() to consume it.

Anywho, I think we're good and this worked for me no problem locally.

From the request documentation:

encoding - encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)

Relevant node-fetch docs: https://www.npmjs.com/package/node-fetch#interface-body

@newhouse
Copy link
Contributor Author

@benogle plz have another look now that I've updated docs and tests...ended up changing a few things.

Copy link
Contributor

@ralbayaty ralbayaty left a comment

Choose a reason for hiding this comment

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

Nice to have more details in the docs now!

@@ -1,8 +1,15 @@
const request = require('request')
const fetch = require('node-fetch')
Copy link
Contributor

Choose a reason for hiding this comment

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

🥳

const defaultOptions = {
baseURL: 'https://app.useanvil.com',
userAgent: `${description}/${version}`,
Copy link
Contributor

Choose a reason for hiding this comment

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

noice

if (!options.apiKey && !options.accessToken) throw new Error('apiKey or accessToken required')

this.options = Object.assign({}, defaultOptions, options)
Copy link
Contributor

Choose a reason for hiding this comment

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

for a custom supplied userAgent would we still want to tack on the /<version>? Would let us know which client version they were using. I suppose this isn't used currently though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do that...but it sort of felt to me like if the User was overriding it that we shouldn't tinker with things. We decided to leave this undocumented for now anyways.

Another thing we could consider is always adding a custom header indicating that it came from our node module.

For now, it's nice to get our client in the user agent header though!

README.md Outdated
Comment on lines 68 to 70
baseUrl: 'https://app.useanvil.com', // Optional. String setting the base URL for all client requests
userAgent: 'Anvil API Client/<version_number>' // Optional. String setting the User-Agent header in all requests
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure we want to put these front and center. They probably shouldnt be used. I'd be ok if they were undocumented

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed.

@newhouse newhouse merged commit b11ef2c into master Aug 19, 2020
@newhouse newhouse deleted the newhouse/use-node-fetch/node-anvil-7 branch August 19, 2020 19:27
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.

Replace request by node-fetch
3 participants