diff options
Diffstat (limited to 'vnext/src/api/index.js')
-rw-r--r-- | vnext/src/api/index.js | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/vnext/src/api/index.js b/vnext/src/api/index.js index fac845e8..2af6c867 100644 --- a/vnext/src/api/index.js +++ b/vnext/src/api/index.js @@ -281,4 +281,75 @@ export function fetchUserUri(profileUrl) { } }; +/** + * Fetch Tweet content + * + * @param {string} url Tweet URL + * @returns {Promise<string>} HTML content + */ + const embeddedTweet = async (url = '') => { + const response = await axios.get('https://publish.twitter.com/oembed', { + params: { + 'dnt': true, + 'omit_script': true, + 'url': url + } + }); + return response.data; +}; + +/** + * Checks if HTTP error code is redirection code + * + * @param {number} code HTTP error code + * @returns {boolean} is HTTP request redirected or not + */ +function isHttpRedirected(code = 200) { + return [301, 302].includes(code); +} + +/** + * Checks if HTTP error code is successful code + * + * @param {number} code HTTP error code + * @returns {boolean} is HTTP request successful or not + */ +function isHttpSuccessful(code = 200) { + return code >= 200 && code < 300; +} + +/** + * Resolves shortened url to actual one + * + * @param {string} url URL to resolve + * @returns {Promise<string>} full URL + */ +function expandShortenedLink(url = '') { + return new Promise((resolve, reject) => { + axios.head(url, { + maxRedirects: 0 + }).then(response => { + if (isHttpSuccessful(response.status)) { + // URL is not redirected + resolve(url); + return; + } + if (isHttpRedirected(response.status)) { + resolve(/** @type { string } */ (response.headers['Location'])); + return; + } + // Error case + reject('Invalid response'); + }).catch(error => { + reject(error); + }); + }); +} + +export { + embeddedTweet, + expandShortenedLink +}; + + |