HunterLarco / twitter-v2

An asynchronous client library for the Twitter REST and Streaming API's
MIT License
163 stars 35 forks source link

expansions missing and example #89

Open tobiasgrossmann opened 3 years ago

tobiasgrossmann commented 3 years ago

let tweetsRawData = await client.get( 'users/1407591434660761602/tweets', { tweet: { fields: ['attachments,created_at,lang,text'], }, media: { fields: ['preview_image_url,type,url'] } }, );

Does not the create the to be expected call to: https://api.twitter.com/2/users/1407591434660761602/tweets?tweet.fields=lang&**expansions**=attachments.media_keys&media.fields=preview_image_url,url

Result is this: https://api.twitter.com/2/users/1407591434660761602/tweets?tweet.fields=attachments%2Ccreated_at%2Clang%2Ctext&media.fields=preview_image_url%2Ctype%2Curl

Also a "hint" in the documentation, that Twitter sends the "inclues" in a 2nd JSON Part would be nice.

brandongregoryscott commented 3 years ago

@tobiasgrossmann The Twitter V2 API does not support arrays out of the box - you would need to join the array into a CSV string yourself, first.

// API expects a CSV string
{ tweet: { fields: 'attachments,created_at,lang,text', }, media: { fields: 'preview_image_url,type,url' } }
// Array.join() works too
{ tweet: { fields: ['attachments', 'created_at', 'lang,text'].join(), }, media: { fields: ['preview_image_url', 'type', 'url'].join() }

See this repl for an example: https://replit.com/@brandongscott/twitter-v289

Not to spam my own library here, but I have built some functions on top of this one to ease consumer use. It provides TypeScript definitions as well as automatic Array -> string conversion for these request parameters. https://github.com/brandongregoryscott/ts-twitter-provider#getting-started