Skip to content

Commit

Permalink
Merge pull request #452 from Rishikant181/dev
Browse files Browse the repository at this point in the history
v2.5.1
  • Loading branch information
Rishikant181 authored Feb 6, 2024
2 parents 5133b2f + a2a8ee1 commit 3eca7f2
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 31 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rettiwt-api",
"version": "2.5.0",
"version": "2.5.1",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"description": "An API for fetching data from TwitterAPI, without any rate limits!",
Expand Down Expand Up @@ -30,10 +30,11 @@
"homepage": "https://rishikant181.github.io/Rettiwt-API/",
"dependencies": {
"axios": "1.6.3",
"class-validator": "0.14.1",
"commander": "11.1.0",
"https-proxy-agent": "7.0.2",
"rettiwt-auth": "2.1.0",
"rettiwt-core": "3.3.0"
"rettiwt-core": "3.3.1"
},
"devDependencies": {
"@types/node": "20.4.1",
Expand Down
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const program = createCommand('rettiwt')
program
.option('-k, --key <string>', 'The API key to use for authentication')
.option('-l, --log', 'Enable logging to console')
.option('-p, --proxy <string>', 'The URL to the proxy to use');
.option('-p, --proxy <string>', 'The URL to the proxy to use')
.option('-t, --timeout <number>', 'The timout (in milli-seconds) to use for requests');

// Parsing the program to get supplied options
program.parse();
Expand All @@ -29,6 +30,7 @@ const rettiwt: Rettiwt = new Rettiwt({
apiKey: process.env.API_KEY ?? (program.opts().key as string),
logging: program.opts().log ? true : false,
proxyUrl: program.opts().proxy as URL,
timeout: program.opts().timeout ? Number(program.opts().timeout) : undefined,
});

// Adding sub-commands
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export * from './enums/Api';
export * from './enums/Http';
export * from './enums/Logging';

// ARGS MODELS
export * from './models/args/TweetArgs';

// ERROR MODELS
export * from './models/errors/ApiError';
export * from './models/errors/HttpError';
Expand All @@ -29,6 +32,5 @@ export * from './services/public/TweetService';
export * from './services/public/UserService';

// TYPES
export * from './types/args/TweetMediaArgs';
export * from './types/RettiwtConfig';
export * from './types/ErrorHandler';
98 changes: 98 additions & 0 deletions src/models/args/TweetArgs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// PACKAGES
import {
ArrayMaxSize,
IsArray,
IsNotEmpty,
IsNumberString,
IsObject,
IsOptional,
IsString,
MaxLength,
validateSync,
} from 'class-validator';
import { DataValidationError } from 'rettiwt-core';

/**
* The arguments specifying the tweet to be posted.
*/
export class TweetArgs {
/**
* The text content of the tweet.
*
* @remarks Length must be \<= 280 characters.
*/
@IsNotEmpty()
@IsString()
@MaxLength(280)
public text: string;

/**
* The media content of the tweet.
*
* @remarks Max number of media that can be posted is 4.
*/
@IsOptional()
@IsArray()
@ArrayMaxSize(4)
@IsObject({ each: true })
public media?: TweetMediaArgs[];

/**
* @param tweet - The tweet arguments specifying the tweet.
*/
public constructor(tweet: TweetArgs) {
this.text = tweet.text;
this.media = tweet.media ? tweet.media.map((item) => new TweetMediaArgs(item)) : undefined;

// Validating this object
const validationResult = validateSync(this);

// If validation error occured
if (validationResult.length) {
throw new DataValidationError(validationResult);
}
}
}

/**
* The arguments specifying the media to be posted in a single tweet.
*
* @public
*/
export class TweetMediaArgs {
/**
* The path to the media file.
*
* @remarks The size of the media file must be \<= 5242880 bytes.
*/
@IsNotEmpty()
@IsString()
public path: string;

/**
* The list of id of users to be tagged in the media.
*
* @remarks Max number of tags is 10.
*/
@IsOptional()
@IsArray()
@ArrayMaxSize(10)
@IsNumberString(undefined, { each: true })
public tags?: string[];

/**
* @param media - The media arguments specifying the media.
*/
public constructor(media: TweetMediaArgs) {
this.path = media.path;
this.tags = media.tags ?? [];

// Validating this object
const validationResult = validateSync(this);

// If validation error occured
if (validationResult.length) {
throw new DataValidationError(validationResult);
}
}
}
15 changes: 9 additions & 6 deletions src/services/public/TweetService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IRettiwtConfig } from '../../types/RettiwtConfig';
import { Tweet } from '../../models/data/Tweet';
import { User } from '../../models/data/User';
import { CursoredData } from '../../models/data/CursoredData';
import { ITweetMediaArgs } from '../../types/args/TweetMediaArgs';
import { TweetArgs, TweetMediaArgs } from '../../models/args/TweetArgs';

/**
* Handles fetching of data related to tweets.
Expand Down Expand Up @@ -224,7 +224,7 @@ export class TweetService extends FetcherService {
* Post a tweet.
*
* @param text - The text to be posted, length must be \<= 280 characters.
* @param media - The list of media to post in the tweet.
* @param media - The list of media to post in the tweet, max number of media must be \<= 4.
* @returns Whether posting was successful or not.
*
* @example Posting a simple text
Expand Down Expand Up @@ -263,18 +263,21 @@ export class TweetService extends FetcherService {
*
* @public
*/
public async tweet(text: string, media?: ITweetMediaArgs[]): Promise<boolean> {
public async tweet(text: string, media?: TweetMediaArgs[]): Promise<boolean> {
// Converting JSON args to object
const tweet: TweetArgs = new TweetArgs({ text: text, media: media });

/** Stores the list of media that has been uploaded */
const uploadedMedia: MediaArgs[] = [];

// If tweet includes media, upload the media items
if (media) {
for (const item of media) {
if (tweet.media) {
for (const item of tweet.media) {
// Uploading the media item and getting it's allocated id
const id: string = await this.upload(item.path);

// Storing the uploaded media item
uploadedMedia.push({ id: id, tags: item.tags });
uploadedMedia.push(new MediaArgs({ id: id, tags: item.tags }));
}
}

Expand Down
16 changes: 0 additions & 16 deletions src/types/args/TweetMediaArgs.ts

This file was deleted.

10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ chokidar@^3.5.2:
optionalDependencies:
fsevents "~2.3.2"

[email protected]:
[email protected], class-validator@^0.14.1:
version "0.14.1"
resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.14.1.tgz#ff2411ed8134e9d76acfeb14872884448be98110"
integrity sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==
Expand Down Expand Up @@ -1090,10 +1090,10 @@ [email protected]:
cookiejar "2.1.4"
https-proxy-agent "7.0.2"

[email protected].0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/rettiwt-core/-/rettiwt-core-3.3.0.tgz#c856b1be47137c5289da65edd4560f2d95a76d73"
integrity sha512-m75NzF9eGO/2mxRJpakWF7nmSjXyaYuGLIxKlekP0xl5MNZNKlE3r/yV0lHADZOajCkW+XYXN7AbB0lphgOoMg==
[email protected].1:
version "3.3.1"
resolved "https://registry.yarnpkg.com/rettiwt-core/-/rettiwt-core-3.3.1.tgz#09ab32e66ba5b55d9ba9896e8dcd780f1b1f4e50"
integrity sha512-TNJFM1UQyfGT8FXPeqePMUk7qzBfoet2/Nzn7+Ma4ibyKdn+XCqaA1c10se02qYomQ7Lolc/KCZvhUPjwCJFXQ==
dependencies:
axios "1.6.3"
class-validator "0.14.1"
Expand Down

0 comments on commit 3eca7f2

Please sign in to comment.