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

Cannot reuse an options object #1118

Closed
blubberbo opened this issue Mar 11, 2020 · 10 comments · Fixed by #1051
Closed

Cannot reuse an options object #1118

blubberbo opened this issue Mar 11, 2020 · 10 comments · Fixed by #1051
Labels
bug Something does not work as it should ✭ help wanted ✭

Comments

@blubberbo
Copy link

Describe the bug

  • Node.js version: v12.14.0
  • OS & version: OSx Catalina

When trying to make an API call, I am getting the following error: (node:73533) UnhandledPromiseRejectionWarning: TypeError: The url option cannot be used if the input is a valid URL. . When I copy and paste the exact URL being tried into Postman, it works fine

Actual behavior

The error is being thrown and the API call is not being made.
...

Expected behavior

The API would be made.
...

Code to reproduce

Unfortunately, I cannot give an entirely repoduceable code snippet, because it requires a personal API key, but I believe the problem happens before the request is actually made. This is the code used to made the call:

     getSummonerBySummonerId(summonerId: string): Promise<any> {
    return got(
      encodeURI(
        'https://na1.api.riotgames.com/lol/summoner/v4/summoners/xWheQXcH0U1J20HleXa5dFJ137QleMldaJtSCxHK9ljsnKAv',
      ),
      lolApiConstants.httpOptions,
    );
  }

const lolApiConstants = {
  baseURL: 'https://na1.api.riotgames.com',
  httpOptions: {
    headers: {
      'content-type': 'application/json',
      'X-Riot-Token': process.env.LOL_API_KEY,
    },
    responseType: 'json',
    resolveBodyOnly: true,
  },
};
(node:73533) UnhandledPromiseRejectionWarning: TypeError: The `url` option cannot be used if the input is a valid URL.
    at Object.exports.normalizeArguments (/Users/blubberbo/Web Projects/Lol-Pickem/lol-pickem-api/node_modules/got/dist/source/normalize-arguments.js:241:23)
    at got (/Users/blubberbo/Web Projects/Lol-Pickem/lol-pickem-api/node_modules/got/dist/source/create.js:52:58)
    at LolApiService.getSummonerBySummonerId (/Users/blubberbo/Web Projects/Lol-Pickem/lol-pickem-api/src/services/lol-api.service.ts:54:12)
    at GameService.<anonymous> (/Users/blubberbo/Web Projects/Lol-Pickem/lol-pickem-api/src/services/game.service.ts:31:8)
    at Generator.next (<anonymous>)
    at fulfilled (/Users/blubberbo/Web Projects/Lol-Pickem/lol-pickem-api/dist/services/game.service.js:5:58)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:73533) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:73533) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Checklist

  • [x ] I have read the documentation.
  • [x ] I have tried my code with the latest version of Node.js and Got.
@szmarczak
Copy link
Collaborator

Unfortunately, I cannot give an entirely repoduceable code snippet, because it requires a personal API key,

Please make another example using httpbin.org

@szmarczak
Copy link
Collaborator

Unless this is a bug, the error says that you're trying to pass a url option while the input argument is a valid URL.

@szmarczak
Copy link
Collaborator

I have tried my code with the latest version of Node.js and Got.

I see you're using a baseURL option which has been removed in Got 10. Please try again with Got 10.

@blubberbo
Copy link
Author

So I may be calling it incorrectly, because I have been trying different things. The version of Got I have is "got": "^10.6.0", as per my package.json. You'll also notice that I only call httpOptions from lolApiConstants.httpOptions, which doesn't the baseURL.

Furthermore, the strange part is it is just that url that does not work, copy and pasting other URLs into the same method works. Additionally, copy and pasting that url into postman does work

@szmarczak
Copy link
Collaborator

I cannot reproduce: https://runkit.com/szmarczak/5e6aaf0ebe09fd001a74aacb

@blubberbo
Copy link
Author

Right, that's exactly my point - I cannot consistently reproduce the error across multiple URLs - it only happens with that one implementation. I am a little confused by the error itself: The url option cannot be used if the input is a valid URL.

It sounds to me like the error is saying: because the URL is a valid URL, it cannot be used, when can the url option be used? Only when the URL is not a valid url?

Any chance you could help with some clarity on that?

I am not trying to be difficult, just trying my best to troubleshoot!

@szmarczak
Copy link
Collaborator

It says what it literally says, you cannot provide both an input argument and a URL option. Or maybe Got adds a url option by itself, lemme check.

It isn't possible to occur randomly as it is normalisation-based. Here's an example of an input argument:

got('https://example.com')

Here's an example of a url option:

got({url: 'https://example.com'});

Got basically says that it has received

got('https://example.com', {url: 'https://anotherExample.com'})

@szmarczak szmarczak added bug Something does not work as it should ✭ help wanted ✭ labels Mar 12, 2020
@szmarczak
Copy link
Collaborator

Got sets options.url on the user provided object, it should set that on the merged object instead. IOW it means you cannot reuse options.

options.url = url;

@szmarczak szmarczak changed the title UnhandledPromiseRejectionWarning: TypeError: The url option cannot be used if the input is a valid URL. Cannot reuse an options object Mar 12, 2020
@szmarczak
Copy link
Collaborator

I'll fix this later today, need to get some sleep now.

@blubberbo
Copy link
Author

I wanted to follow up that I wasn't able to figure it out, unfortunately, and I switched to using node-fetch. That being said, I wanted to use the Got package and am happy to try and transition back

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should ✭ help wanted ✭
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants