refactor: Restructure twitter app with beforeRequest hook

This commit is contained in:
Faruk AYDIN
2022-10-18 11:13:53 +02:00
committed by Ali BARIN
parent bc402883b3
commit a5e114ac68
14 changed files with 119 additions and 113 deletions

View File

@@ -6,7 +6,6 @@ import {
import { URLSearchParams } from 'url';
import omitBy from 'lodash/omitBy';
import isEmpty from 'lodash/isEmpty';
import generateRequest from './generate-request';
import getCurrentUser from './get-current-user';
import getUserByUsername from './get-user-by-username';
@@ -14,19 +13,7 @@ type IGetUserTweetsOptions = {
currentUser: boolean;
};
const getUserTweets = async (
$: IGlobalVariable,
options: IGetUserTweetsOptions
) => {
let username: string;
if (options.currentUser) {
const currentUser = await getCurrentUser($);
username = currentUser.username as string;
} else {
username = $.step.parameters.username as string;
}
const fetchTweets = async ($: IGlobalVariable, username: string) => {
const user = await getUserByUsername($, username);
let response;
@@ -47,10 +34,7 @@ const getUserTweets = async (
queryParams.toString() ? `?${queryParams.toString()}` : ''
}`;
response = await generateRequest($, {
requestPath,
method: 'GET',
});
response = await $.http.get(requestPath);
if (response.integrationError) {
tweets.error = response.integrationError;
@@ -72,4 +56,26 @@ const getUserTweets = async (
return tweets;
};
const getUserTweets = async (
$: IGlobalVariable,
options: IGetUserTweetsOptions
) => {
let username: string;
if (options.currentUser) {
const currentUser = await getCurrentUser($);
username = currentUser.username as string;
} else {
username = $.step.parameters.username as string;
}
const tweets = await fetchTweets($, username);
tweets.data.sort((tweet, nextTweet) => {
return Number(nextTweet.meta.internalId) - Number(tweet.meta.internalId);
});
return tweets;
};
export default getUserTweets;