PLhery / node-twitter-api-v2

Strongly typed, full-featured, light, versatile yet powerful Twitter API v1.1 and v2 client for Node.js.
https://www.npmjs.com/package/twitter-api-v2
Apache License 2.0
1.25k stars 175 forks source link

[bug] v2.search with only options object parameter throws 400 #430

Closed itsjustbrian closed 1 year ago

itsjustbrian commented 1 year ago

Describe the bug After https://github.com/PLhery/node-twitter-api-v2/pull/254 you could call the v2.search method with just an options object instead of a string for the query and an optional options object. But the query provided in the options objects is:

  1. Unintentionally overridden by undefined, leading to a 400 from Twitter, and
  2. Unenforced by Typescript even though it's required by the Twitter API

To Reproduce Please indicate all steps that lead to this bug:

  1. Create client with read access
  2. Query v2.search using just an options object with a query key. For example:

    const response = await client.v2.search({
      query: 'example'
    })

Expected behavior Query completes without throwing

Actual behavior Query throws 400 error response from twitter:

Request failed with code 400, data: {
  errors: [
    {
      parameters: [Object],
      message: 'The `query` query parameter can not be empty'
    }
  ],
  title: 'Invalid Request',
  detail: 'One or more parameters to your request was invalid.',
  type: 'https://api.twitter.com/2/problems/invalid-request'
}

Version

Additional context The alternative API using just an options object instead of a query string and an options object was implemented in response to this issue: https://github.com/PLhery/node-twitter-api-v2/issues/252 I think it's worth considering reverting the original change to the API. It makes the code more complex and wasn't really related to the original issue. The original API made enforcement of having a query value very clear with a very clear type error. My PR also enforces it but the type error is much harder to read.

itsjustbrian commented 1 year ago

Fixed by https://github.com/PLhery/node-twitter-api-v2/pull/431