aboutsummaryrefslogtreecommitdiff
path: root/vnext/src
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2022-10-29 23:35:20 +0300
committerGravatar Vitaly Takmazov2023-01-13 10:37:58 +0300
commit47218df561e877265c739ffab59b760318ea3143 (patch)
tree166dd029133aca8d0d60af50535bb579bd6b8096 /vnext/src
parent3a403e4023ce3b7074a7922e7b504263ddc7a3d1 (diff)
Merge server routes from Next version
Diffstat (limited to 'vnext/src')
-rw-r--r--vnext/src/api/index.js71
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
+};
+
+