feat: Convert all app files to JS

This commit is contained in:
Faruk AYDIN
2024-01-05 17:44:21 +01:00
parent b95478b635
commit 43dba351c3
1030 changed files with 5114 additions and 6436 deletions

View File

@@ -1,4 +1,4 @@
import defineAction from '../../../../helpers/define-action';
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Create tweet',
@@ -8,7 +8,7 @@ export default defineAction({
{
label: 'Tweet body',
key: 'tweet',
type: 'string' as const,
type: 'string',
required: true,
description: 'The content of your new tweet.',
variables: true,

View File

@@ -0,0 +1,4 @@
import createTweet from './create-tweet/index.js';
import searchUser from './search-user/index.js';
export default [createTweet, searchUser];

View File

@@ -1,4 +0,0 @@
import createTweet from './create-tweet';
import searchUser from './search-user';
export default [createTweet, searchUser];

View File

@@ -0,0 +1,35 @@
import defineAction from '../../../../helpers/define-action.js';
export default defineAction({
name: 'Search user',
key: 'searchUser',
description: 'Search a user on Twitter',
arguments: [
{
label: 'Username',
key: 'username',
type: 'string',
required: true,
description: 'The username of the Twitter user you want to search for',
variables: true,
},
],
async run($) {
const { data } = await $.http.get(
`/2/users/by/username/${$.step.parameters.username}`,
{
params: {
expansions: 'pinned_tweet_id',
'tweet.fields':
'attachments,author_id,context_annotations,conversation_id,created_at,edit_controls,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,public_metrics,organic_metrics,promoted_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld',
'user.fields':
'created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld',
},
}
);
$.setActionItem({
raw: data.data,
});
},
});

View File

@@ -1,30 +0,0 @@
import defineAction from '../../../../helpers/define-action';
export default defineAction({
name: 'Search user',
key: 'searchUser',
description: 'Search a user on Twitter',
arguments: [
{
label: 'Username',
key: 'username',
type: 'string' as const,
required: true,
description: 'The username of the Twitter user you want to search for',
variables: true,
},
],
async run($) {
const { data } = await $.http.get(`/2/users/by/username/${$.step.parameters.username}`, {
params: {
expansions: 'pinned_tweet_id',
'tweet.fields': 'attachments,author_id,context_annotations,conversation_id,created_at,edit_controls,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,public_metrics,organic_metrics,promoted_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld',
'user.fields': 'created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld'
}
});
$.setActionItem({
raw: data.data
});
},
});

View File

@@ -1,9 +1,8 @@
import { IField, IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
export default async function generateAuthUrl($: IGlobalVariable) {
export default async function generateAuthUrl($) {
const oauthRedirectUrlField = $.app.auth.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
(field) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = oauthRedirectUrlField.value;

View File

@@ -1,13 +1,13 @@
import generateAuthUrl from './generate-auth-url';
import verifyCredentials from './verify-credentials';
import isStillVerified from './is-still-verified';
import generateAuthUrl from './generate-auth-url.js';
import verifyCredentials from './verify-credentials.js';
import isStillVerified from './is-still-verified.js';
export default {
fields: [
{
key: 'oAuthRedirectUrl',
label: 'OAuth Redirect URL',
type: 'string' as const,
type: 'string',
required: true,
readOnly: true,
value: '{WEB_APP_URL}/app/twitter/connections/add',
@@ -19,7 +19,7 @@ export default {
{
key: 'consumerKey',
label: 'API Key',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,
@@ -30,7 +30,7 @@ export default {
{
key: 'consumerSecret',
label: 'API Secret',
type: 'string' as const,
type: 'string',
required: true,
readOnly: false,
value: null,

View File

@@ -0,0 +1,8 @@
import getCurrentUser from '../common/get-current-user.js';
const isStillVerified = async ($) => {
const user = await getCurrentUser($);
return !!user;
};
export default isStillVerified;

View File

@@ -1,9 +0,0 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../common/get-current-user';
const isStillVerified = async ($: IGlobalVariable) => {
const user = await getCurrentUser($);
return !!user;
};
export default isStillVerified;

View File

@@ -1,7 +1,6 @@
import { IGlobalVariable } from '@automatisch/types';
import { URLSearchParams } from 'url';
const verifyCredentials = async ($: IGlobalVariable) => {
const verifyCredentials = async ($) => {
const response = await $.http.post(
`/oauth/access_token?oauth_verifier=${$.auth.data.oauth_verifier}&oauth_token=${$.auth.data.accessToken}`,
null

View File

@@ -1,20 +1,12 @@
import { Token } from 'oauth-1.0a';
import { URLSearchParams } from 'node:url';
import { IJSONObject, TBeforeRequest } from '@automatisch/types';
import oauthClient from './oauth-client';
import oauthClient from './oauth-client.js';
type RequestDataType = {
url: string;
method: string;
data?: IJSONObject;
};
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
const addAuthHeader = ($, requestConfig) => {
const { baseURL, url, method, data, params } = requestConfig;
const token: Token = {
key: $.auth.data?.accessToken as string,
secret: $.auth.data?.accessSecret as string,
const token = {
key: $.auth.data?.accessToken,
secret: $.auth.data?.accessSecret,
};
const searchParams = new URLSearchParams(params);
@@ -26,7 +18,7 @@ const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
fullUrl = `${fullUrl}?${stringifiedParams}`;
}
const requestData: RequestDataType = {
const requestData = {
url: fullUrl,
method,
};

View File

@@ -1,6 +1,4 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
const getCurrentUser = async ($: IGlobalVariable): Promise<IJSONObject> => {
const getCurrentUser = async ($) => {
const response = await $.http.get('/2/users/me');
const currentUser = response.data.data;

View File

@@ -1,11 +1,9 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
const getUserByUsername = async ($: IGlobalVariable, username: string) => {
const getUserByUsername = async ($, username) => {
const response = await $.http.get(`/2/users/by/username/${username}`);
if (response.data.errors) {
const errorMessages = response.data.errors
.map((error: IJSONObject) => error.detail)
.map((error) => error.detail)
.join(' ');
throw new Error(`Error occured while fetching user data: ${errorMessages}`);

View File

@@ -1,19 +1,12 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import { URLSearchParams } from 'url';
import { omitBy, isEmpty } from 'lodash';
import omitBy from 'lodash/omitBy.js';
import isEmpty from 'lodash/isEmpty.js';
type GetUserFollowersOptions = {
userId: string;
};
const getUserFollowers = async (
$: IGlobalVariable,
options: GetUserFollowersOptions
) => {
const getUserFollowers = async ($, options) => {
let response;
do {
const params: IJSONObject = {
const params = {
pagination_token: response?.data?.meta?.next_token,
};
@@ -33,7 +26,7 @@ const getUserFollowers = async (
for (const follower of response.data.data) {
$.pushTriggerItem({
raw: follower,
meta: { internalId: follower.id as string },
meta: { internalId: follower.id },
});
}
}

View File

@@ -1,21 +1,16 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import { URLSearchParams } from 'url';
import omitBy from 'lodash/omitBy';
import isEmpty from 'lodash/isEmpty';
import getCurrentUser from './get-current-user';
import getUserByUsername from './get-user-by-username';
import omitBy from 'lodash/omitBy.js';
import isEmpty from 'lodash/isEmpty.js';
import getCurrentUser from './get-current-user.js';
import getUserByUsername from './get-user-by-username.js';
type IGetUserTweetsOptions = {
currentUser: boolean;
};
const fetchTweets = async ($: IGlobalVariable, username: string) => {
const fetchTweets = async ($, username) => {
const user = await getUserByUsername($, username);
let response;
do {
const params: IJSONObject = {
const params = {
since_id: $.flow.lastInternalId,
pagination_token: response?.data?.meta?.next_token,
};
@@ -29,11 +24,11 @@ const fetchTweets = async ($: IGlobalVariable, username: string) => {
response = await $.http.get(requestPath);
if (response.data.meta.result_count > 0) {
response.data.data.forEach((tweet: IJSONObject) => {
response.data.data.forEach((tweet) => {
const dataItem = {
raw: tweet,
meta: {
internalId: tweet.id as string,
internalId: tweet.id,
},
};
@@ -45,17 +40,14 @@ const fetchTweets = async ($: IGlobalVariable, username: string) => {
return $.triggerOutput;
};
const getUserTweets = async (
$: IGlobalVariable,
options: IGetUserTweetsOptions
) => {
let username: string;
const getUserTweets = async ($, options) => {
let username;
if (options.currentUser) {
const currentUser = await getCurrentUser($);
username = currentUser.username as string;
username = currentUser.username;
} else {
username = $.step.parameters.username as string;
username = $.step.parameters.username;
}
await fetchTweets($, username);

View File

@@ -1,11 +1,10 @@
import { IGlobalVariable } from '@automatisch/types';
import crypto from 'crypto';
import OAuth from 'oauth-1.0a';
const oauthClient = ($: IGlobalVariable) => {
const oauthClient = ($) => {
const consumerData = {
key: $.auth.data.consumerKey as string,
secret: $.auth.data.consumerSecret as string,
key: $.auth.data.consumerKey,
secret: $.auth.data.consumerSecret,
};
return new OAuth({

View File

@@ -1,8 +1,8 @@
import defineApp from '../../helpers/define-app';
import addAuthHeader from './common/add-auth-header';
import auth from './auth';
import actions from './actions';
import triggers from './triggers';
import defineApp from '../../helpers/define-app.js';
import addAuthHeader from './common/add-auth-header.js';
import auth from './auth/index.js';
import actions from './actions/index.js';
import triggers from './triggers/index.js';
export default defineApp({
name: 'Twitter',

View File

@@ -0,0 +1,6 @@
import myTweets from './my-tweets/index.js';
import newFollowerOfMe from './new-follower-of-me/index.js';
import searchTweets from './search-tweets/index.js';
import userTweets from './user-tweets/index.js';
export default [myTweets, newFollowerOfMe, searchTweets, userTweets];

View File

@@ -1,6 +0,0 @@
import myTweets from './my-tweets';
import newFollowerOfMe from './new-follower-of-me';
import searchTweets from './search-tweets';
import userTweets from './user-tweets';
export default [myTweets, newFollowerOfMe, searchTweets, userTweets];

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import getUserTweets from '../../common/get-user-tweets';
import defineTrigger from '../../../../helpers/define-trigger.js';
import getUserTweets from '../../common/get-user-tweets.js';
export default defineTrigger({
name: 'My tweets',

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import myFollowers from './my-followers';
import defineTrigger from '../../../../helpers/define-trigger.js';
import myFollowers from './my-followers.js';
export default defineTrigger({
name: 'New follower of me',

View File

@@ -0,0 +1,15 @@
import getCurrentUser from '../../common/get-current-user.js';
import getUserByUsername from '../../common/get-user-by-username.js';
import getUserFollowers from '../../common/get-user-followers.js';
const myFollowers = async ($) => {
const { username } = await getCurrentUser($);
const user = await getUserByUsername($, username);
const tweets = await getUserFollowers($, {
userId: user.id,
});
return tweets;
};
export default myFollowers;

View File

@@ -1,16 +0,0 @@
import { IGlobalVariable } from '@automatisch/types';
import getCurrentUser from '../../common/get-current-user';
import getUserByUsername from '../../common/get-user-by-username';
import getUserFollowers from '../../common/get-user-followers';
const myFollowers = async ($: IGlobalVariable) => {
const { username } = await getCurrentUser($);
const user = await getUserByUsername($, username as string);
const tweets = await getUserFollowers($, {
userId: user.id,
});
return tweets;
};
export default myFollowers;

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import searchTweets from './search-tweets';
import defineTrigger from '../../../../helpers/define-trigger.js';
import searchTweets from './search-tweets.js';
export default defineTrigger({
name: 'Search tweets',
@@ -11,7 +11,7 @@ export default defineTrigger({
{
label: 'Search Term',
key: 'searchTerm',
type: 'string' as const,
type: 'string',
required: true,
},
],

View File

@@ -1,14 +1,14 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
import qs from 'qs';
import { omitBy, isEmpty } from 'lodash';
import omitBy from 'lodash/omitBy.js';
import isEmpty from 'lodash/isEmpty.js';
const searchTweets = async ($: IGlobalVariable) => {
const searchTerm = $.step.parameters.searchTerm as string;
const searchTweets = async ($) => {
const searchTerm = $.step.parameters.searchTerm;
let response;
do {
const params: IJSONObject = {
const params = {
query: searchTerm,
since_id: $.flow.lastInternalId,
pagination_token: response?.data?.meta?.next_token,
@@ -27,11 +27,11 @@ const searchTweets = async ($: IGlobalVariable) => {
}
if (response.data.meta.result_count > 0) {
response.data.data.forEach((tweet: IJSONObject) => {
response.data.data.forEach((tweet) => {
const dataItem = {
raw: tweet,
meta: {
internalId: tweet.id as string,
internalId: tweet.id,
},
};

View File

@@ -1,5 +1,5 @@
import defineTrigger from '../../../../helpers/define-trigger';
import getUserTweets from '../../common/get-user-tweets';
import defineTrigger from '../../../../helpers/define-trigger.js';
import getUserTweets from '../../common/get-user-tweets.js';
export default defineTrigger({
name: 'User tweets',
@@ -10,7 +10,7 @@ export default defineTrigger({
{
label: 'Username',
key: 'username',
type: 'string' as const,
type: 'string',
required: true,
},
],