Skip to content

Commit

Permalink
fix(twitter): remove twitter auth methods that is not working anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
async3619 committed Dec 8, 2022
1 parent 84a7278 commit c712e96
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 137 deletions.
21 changes: 1 addition & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,7 @@ specify watching interval in millisecond format. minimal value is `60000`.
```json5
{
"type": "twitter", // required
// one of these...
"auth": {
"type": "api-key", // required
"apiKey": "API key of your twitter app", // string, required
"apiSecret": "API secret of your twitter app" // string, required
},
// or ...
"auth": {
"type": "bearer-token", // required
"bearerToken": "bearer token of your twitter app" // string, required
},
// or ...
"auth": {
"type": "basic", // required
"username": "user id of your twitter account (e.g. @user_account)", // string, required
"password": "password of your twitter account" // string, required
}
"bearerToken": "bearer token of your twitter app" // string, required
}
```

Expand Down
66 changes: 3 additions & 63 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,12 @@
"type": "string",
"const": "twitter"
},
"auth": {
"anyOf": [
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "api-key"
},
"apiKey": {
"type": "string"
},
"apiSecret": {
"type": "string"
}
},
"required": [
"type",
"apiKey",
"apiSecret"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "bearer-token"
},
"bearerToken": {
"type": "string"
}
},
"required": [
"type",
"bearerToken"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"type": {
"type": "string",
"const": "basic"
},
"username": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [
"type",
"username",
"password"
],
"additionalProperties": false
}
]
"bearerToken": {
"type": "string"
}
},
"required": [
"auth",
"bearerToken",
"type"
],
"additionalProperties": false
Expand Down
37 changes: 5 additions & 32 deletions src/watchers/twitter/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,16 @@ import { TwitterApiRateLimitPlugin } from "@twitter-api-v2/plugin-rate-limit";

import { UserData } from "@repositories/models/user";

import { TwitterApiKeyAuth, TwitterBasicAuth, TwitterBearerTokenAuth } from "@watchers/twitter/types";

export class TwitterHelper {
private static createClient(configs: TwitterApiKeyAuth | TwitterBasicAuth | TwitterBearerTokenAuth) {
private static createClient(bearerToken: string) {
const plugins: ITwitterApiClientPlugin[] = [new TwitterApiRateLimitPlugin()];

switch (configs.type) {
case "api-key":
return new TwitterApi({ appKey: configs.apiKey, appSecret: configs.apiSecret }, { plugins }).readOnly;

case "basic":
return new TwitterApi({ username: configs.username, password: configs.password }, { plugins }).readOnly;

case "bearer-token":
return new TwitterApi(configs.bearerToken, { plugins }).readOnly;

default:
throw new Error("Invalid auth type");
}
return new TwitterApi(bearerToken, { plugins }).readOnly;
}

private twitterClient: TwitterApiReadOnly;

public constructor(private readonly configs: TwitterApiKeyAuth | TwitterBasicAuth | TwitterBearerTokenAuth) {
this.twitterClient = TwitterHelper.createClient(this.configs);
public constructor(private readonly bearerToken: string) {
this.twitterClient = TwitterHelper.createClient(this.bearerToken);
}

public async getFollowers(): Promise<Omit<UserData, "from">[]> {
Expand All @@ -44,18 +29,6 @@ export class TwitterHelper {
}

public async initialize() {
if (this.configs.type === "api-key") {
await this.twitterClient.appLogin();
}
}

public getHashData() {
return this.configs;
}
public hydrate(configs: TwitterApiKeyAuth | TwitterBasicAuth | TwitterBearerTokenAuth): void {
this.twitterClient = TwitterHelper.createClient(configs);
}
public serialize(): TwitterApiKeyAuth | TwitterBasicAuth | TwitterBearerTokenAuth {
return this.configs;
return;
}
}
4 changes: 2 additions & 2 deletions src/watchers/twitter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { TwitterHelper } from "@watchers/twitter/helper";
export class TwitterWatcher extends BaseWatcher<"Twitter"> {
private readonly helper: TwitterHelper;

public constructor({ auth }: TwitterWatcherOptions) {
public constructor({ bearerToken }: TwitterWatcherOptions) {
super("Twitter");
this.helper = new TwitterHelper(auth);
this.helper = new TwitterHelper(bearerToken);
}

public async initialize() {
Expand Down
21 changes: 1 addition & 20 deletions src/watchers/twitter/types.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,5 @@
import { BaseWatcherOptions } from "@watchers/base";

export interface TwitterApiKeyAuth {
type: "api-key";
apiKey: string;
apiSecret: string;
}

export interface TwitterBearerTokenAuth {
type: "bearer-token";
bearerToken: string;
}

export interface TwitterBasicAuth {
type: "basic";
username: string;
password: string;
}

export type TwitterAuth = TwitterApiKeyAuth | TwitterBearerTokenAuth | TwitterBasicAuth;

export interface TwitterWatcherOptions extends BaseWatcherOptions<"twitter"> {
auth: TwitterAuth;
bearerToken: string;
}

0 comments on commit c712e96

Please sign in to comment.