refactor: Pass connection, flow and step as params to apps

This commit is contained in:
Faruk AYDIN
2022-08-21 20:25:04 +03:00
parent cd6c5216ff
commit 44e3de8534
18 changed files with 60 additions and 85 deletions

View File

@@ -9,7 +9,7 @@ export default class CreateTweet {
async run() {
const response = await this.client.createTweet.run(
this.client.parameters.tweet as string
this.client.step.parameters.tweet as string
);
return response.data.data;

View File

@@ -1,4 +1,4 @@
import type { IAuthentication, IField } from '@automatisch/types';
import type { IAuthentication, IField, IApp } from '@automatisch/types';
import { URLSearchParams } from 'url';
import TwitterClient from './client';
@@ -10,7 +10,7 @@ export default class Authentication implements IAuthentication {
}
async createAuthData() {
const appFields = this.client.appData.fields.find(
const appFields = this.client.connection.appData.fields.find(
(field: IField) => field.key == 'oAuthRedirectUrl'
);
const callbackUrl = appFields.value;
@@ -30,8 +30,9 @@ export default class Authentication implements IAuthentication {
const responseData = Object.fromEntries(new URLSearchParams(response.data));
return {
consumerKey: this.client.connectionData.consumerKey,
consumerSecret: this.client.connectionData.consumerSecret,
consumerKey: this.client.connection.formattedData.consumerKey as string,
consumerSecret: this.client.connection.formattedData
.consumerSecret as string,
accessToken: responseData.oauth_token,
accessSecret: responseData.oauth_token_secret,
userId: responseData.user_id,

View File

@@ -10,8 +10,8 @@ export default class CreateTweet {
async run(text: string) {
try {
const token = {
key: this.client.connectionData.accessToken as string,
secret: this.client.connectionData.accessSecret as string,
key: this.client.connection.formattedData.accessToken as string,
secret: this.client.connection.formattedData.accessSecret as string,
};
const requestData = {

View File

@@ -9,8 +9,8 @@ export default class GetCurrentUser {
async run() {
const token = {
key: this.client.connectionData.accessToken as string,
secret: this.client.connectionData.accessSecret as string,
key: this.client.connection.formattedData.accessToken as string,
secret: this.client.connection.formattedData.accessSecret as string,
};
const requestPath = '/2/users/me';

View File

@@ -10,8 +10,8 @@ export default class GetUserByUsername {
async run(username: string) {
const token = {
key: this.client.connectionData.accessToken as string,
secret: this.client.connectionData.accessSecret as string,
key: this.client.connection.formattedData.accessToken as string,
secret: this.client.connection.formattedData.accessSecret as string,
};
const requestPath = `/2/users/by/username/${username}`;

View File

@@ -10,8 +10,8 @@ export default class GetUserTweets {
async run(userId: string) {
const token = {
key: this.client.connectionData.accessToken as string,
secret: this.client.connectionData.accessSecret as string,
key: this.client.connection.formattedData.accessToken as string,
secret: this.client.connection.formattedData.accessSecret as string,
};
const requestPath = `/2/users/${userId}/tweets`;

View File

@@ -10,7 +10,7 @@ export default class VerifyAccessToken {
async run() {
try {
return await this.client.httpClient.post(
`/oauth/access_token?oauth_verifier=${this.client.connectionData.oauthVerifier}&oauth_token=${this.client.connectionData.accessToken}`,
`/oauth/access_token?oauth_verifier=${this.client.connection.formattedData.oauthVerifier}&oauth_token=${this.client.connection.formattedData.accessToken}`,
null
);
} catch (error) {

View File

@@ -1,4 +1,4 @@
import { IJSONObject, IApp } from '@automatisch/types';
import { IFlow, IStep, IConnection } from '@automatisch/types';
import OAuth from 'oauth-1.0a';
import crypto from 'crypto';
import HttpClient from '../../../helpers/http-client';
@@ -10,9 +10,9 @@ import GetUserTweets from './endpoints/get-user-tweets';
import CreateTweet from './endpoints/create-tweet';
export default class TwitterClient {
appData: IApp;
connectionData: IJSONObject;
parameters: IJSONObject;
flow: IFlow;
step: IStep;
connection: IConnection;
oauthClient: OAuth;
httpClient: HttpClient;
@@ -25,20 +25,16 @@ export default class TwitterClient {
static baseUrl = 'https://api.twitter.com';
constructor(
appData: IApp,
connectionData: IJSONObject,
parameters: IJSONObject
) {
this.connectionData = connectionData;
this.appData = appData;
this.parameters = parameters;
constructor(connection: IConnection, flow?: IFlow, step?: IStep) {
this.connection = connection;
this.flow = flow;
this.step = step;
this.httpClient = new HttpClient({ baseURL: TwitterClient.baseUrl });
const consumerData = {
key: this.connectionData.consumerKey as string,
secret: this.connectionData.consumerSecret as string,
key: this.connection.formattedData.consumerKey as string,
secret: this.connection.formattedData.consumerSecret as string,
};
this.oauthClient = new OAuth({

View File

@@ -1,8 +1,9 @@
import {
IService,
IAuthentication,
IApp,
IJSONObject,
IFlow,
IStep,
IConnection,
} from '@automatisch/types';
import Authentication from './authentication';
import Triggers from './triggers';
@@ -16,12 +17,8 @@ export default class Twitter implements IService {
triggers: Triggers;
actions: Actions;
constructor(
appData: IApp,
connectionData: IJSONObject,
parameters: IJSONObject
) {
this.client = new TwitterClient(appData, connectionData, parameters);
constructor(connection: IConnection, flow?: IFlow, step?: IStep) {
this.client = new TwitterClient(connection, flow, step);
this.authenticationClient = new Authentication(this.client);
this.triggers = new Triggers(this.client);

View File

@@ -17,7 +17,7 @@ export default class UserTweet {
async getTweets() {
const userResponse = await this.client.getUserByUsername.run(
this.client.parameters.username as string
this.client.step.parameters.username as string
);
const userId = userResponse.data.data.id;