From 47218df561e877265c739ffab59b760318ea3143 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 29 Oct 2022 23:35:20 +0300 Subject: Merge server routes from Next version --- vnext/src/api/index.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'vnext/src/api') 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} 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} 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 +}; + + -- cgit v1.2.3