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

Transitioning from request #249

Open
drmrbrewer opened this issue Mar 4, 2020 · 13 comments
Open

Transitioning from request #249

drmrbrewer opened this issue Mar 4, 2020 · 13 comments
Labels
type: question Request for information or clarification. Not an issue.

Comments

@drmrbrewer
Copy link

drmrbrewer commented Mar 4, 2020

I'm coming to gaxios from request. In the latter I'm used to specifying gzip: true in my request options, to ensure compression is used in the response (if available). This does not appear to be available in gaxios so I'm wondering if it is effectively enabled by default? Not that I'd ever want to, but is it possible in gaxios to disable compression?

And it seems to me that the following drop-in replacements can also be made when transitioning from request to gaxios... please correct me if I'm wrong!

  • json: true becomes responseType: 'json' (though this is the default anyway so could be omitted)
  • body: { a: x, b: y } becomes data: { a: x, b: y }
  • qs: { a: x, b: y } becomes params: { a: x, b: y }

What about an equivalent for auth: { bearer: bearerToken }? See here.

And an equivalent to resolveWithFullResponse? See here.

@yoshi-automation yoshi-automation added triage me I really want to be triaged. 🚨 This issue needs some love. labels Mar 5, 2020
@JustinBeckwith JustinBeckwith added the type: question Request for information or clarification. Not an issue. label Mar 9, 2020
@JustinBeckwith
Copy link
Contributor

Greetings! We can take a look at this in a bit, but for now, have you considered using teeny-request?
https://github.com/googleapis/teeny-request/

It was written to be mostly API compatible with request, but on top of node-fetch :)

@yoshi-automation yoshi-automation removed triage me I really want to be triaged. 🚨 This issue needs some love. triage me labels Mar 9, 2020
@drmrbrewer
Copy link
Author

That looks interesting! However I'd prefer a library that does async/await out of the box (request doesn't, but I use a wrapper... bit clunky though).

Would also be useful to have your input on the following: https://stackoverflow.com/q/60532289/4070848

Perhaps I'm being too optimistic to hope for something that does everything that request does (only better)!

Thanks!

@bcoe bcoe added type: question Request for information or clarification. Not an issue. and removed type: question Request for information or clarification. Not an issue. labels Mar 11, 2020
@drmrbrewer
Copy link
Author

@JustinBeckwith sure you're busy but would be great to have even some brief response on this one. Would be useful to all I'm sure. Thanks!

@JustinBeckwith
Copy link
Contributor

JustinBeckwith commented Sep 1, 2020

👋 thanks for the bump

In the latter I'm used to specifying gzip: true in my request options, to ensure compression is used in the response (if available). This does not appear to be available in gaxios so I'm wondering if it is effectively enabled by default?

Yes! We're using node-fetch under the hood, so that option is enabled by default.

Not that I'd ever want to, but is it possible in gaxios to disable compression?

As of now I don't think you can 😆 We could certainly add this option if it was a big deal (easy enough). If we chose to support it, I would probably use the decompress flag documented in axios as the interface.

And it seems to me that the following drop-in replacements can also be made when transitioning from request to gaxios... please correct me if I'm wrong!

All of those were correct

What about an equivalent for auth: { bearer: bearerToken }? See here.

At the end of the day, that's just a header right? So you should be able to set:

gaxios.request({
  url: '...',
  headers: {
    auth: 'bearer xxxxx'
  },
});

And an equivalent to resolveWithFullResponse? See here.

It does this by default! The response you get back from the promise is the full response. The body is in response.data.

Hope this helps!

@drmrbrewer
Copy link
Author

Super helpful, thanks!

@drmrbrewer
Copy link
Author

@JustinBeckwith having been migrating the auth parameter of my previous options for the request module, I have discovered that there is slightly more to it than just turning it into a header.

Perhaps there is justification for an additional auth parameter for gaxios, as a helper?

I note that axios has an auth parameter (see here) similar to what is offered by request (see here).

It appears that both request's and axios's implementation deals with auth.user and auth.pass sub-parameters. These are seemingly converted internally into the following header:

'Authorization': 'Basic ' + base64encodedData

where:

var base64encodedData = Buffer.from(auth.user + ':' + auth.pass).toString('base64');

see e.g. here.

In addition, request's implementation (but not axios's) deals with the auth.bearer sub-parameter, and likely turns it into the following header:

'Authorization': 'Bearer ' + auth.bearer

So having the auth parameter in gaxios would make life a bit easier, but it's not essential because you can make up your own headers without it. It would be a "nice to have".

@JustinBeckwith
Copy link
Contributor

Makes sense. Just to set expectations, we're not likely to add this any time soon - but thank you for digging!

@drmrbrewer
Copy link
Author

Yep, no worries... I've already done the manual conversion into appropriate Authorisation headers so likely I wouldn't use an auth parameter in gaxios anytime soon anyway!

@drmrbrewer
Copy link
Author

drmrbrewer commented Sep 2, 2020

@JustinBeckwith on the same topic of transitioning from request... can gaxios do something equivalent to what is possible with request as described here and here?

@JustinBeckwith
Copy link
Contributor

Yes! I don't know if we documented this but:
https://github.com/googleapis/gaxios/blob/master/src/gaxios.ts#L52

@drmrbrewer
Copy link
Author

So what would the gaxios equivalent of request('http://google.com/').pipe(res) and req.pipe(request('http://google.com/')).pipe(res) be?

@drmrbrewer
Copy link
Author

drmrbrewer commented Oct 22, 2020

@JustinBeckwith any hints on my question just above? I'll give the first one a go myself:

const { request } = require('gaxios');
(await request({ url: 'http://google.com/', responseType: 'stream' })).data.pipe(res);

What about the second one, i.e. the gaxios equivalent of:

req.pipe(request(url)).pipe(res);

This is used for example in this sort of context.

Or in the following context, to redirect a request (whether it be a POST or GET request) to a different domain and pipe the response back to the client that sent the request:

const url = 'https://my.new.url.com' + req.originalUrl;
req.pipe(request(url)).pipe(res);

@drmrbrewer
Copy link
Author

@JustinBeckwith sure you're busy, but would be great to know the answer to the above question on piping into and out of gaxios, as a replacement for the deprecated request module which does this really straightforwardly. Would be useful to all I'm sure. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

4 participants