Add new limit option to limit the requested amount of posts from the Reddit API #203

Merged
Vylpes merged 3 commits from feature/137-limit-option into develop 2024-07-26 18:23:19 +01:00
7 changed files with 30 additions and 4 deletions
Showing only changes of commit 123d609a4f - Show all commits

View file

@ -14,11 +14,12 @@ program
.option('-j, --json', 'Output as JSON') .option('-j, --json', 'Output as JSON')
.option('-q, --query-metadata', 'Include query metadata in result') .option('-q, --query-metadata', 'Include query metadata in result')
.option('-o <file>', 'Output to file') .option('-o <file>', 'Output to file')
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top'])); .addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']))
.addOption(new Option('--limit <limit>', 'The amount of posts to fetch from the reddit api').default(100));
program.parse(); program.parse();
const options: ICliOptions = program.opts(); const options: ICliOptions = program.opts();
randomBunny(options.subreddit, options.sort) randomBunny(options.subreddit, options.sort, options.limit)
.then((response) => exit(CliHelper.Endpoint(response, options))); .then((response) => exit(CliHelper.Endpoint(response, options)));

View file

@ -3,4 +3,5 @@ export enum ErrorCode {
FailedToFetchReddit, FailedToFetchReddit,
UnableToParseJSON, UnableToParseJSON,
NoImageResultsFound, NoImageResultsFound,
LimitOutOfRange,
} }

View file

@ -2,4 +2,5 @@ export default class ErrorMessages {
public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit"; public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit";
public static readonly UnableToParseJSON = "Unable to parse the JSON result"; public static readonly UnableToParseJSON = "Unable to parse the JSON result";
public static readonly NoImageResultsFound = "No image results found in response from Reddit"; public static readonly NoImageResultsFound = "No image results found in response from Reddit";
public static readonly LimitOutOfRange = "Limit must be a number between 1 and 100";
} }

View file

@ -3,5 +3,6 @@ export default interface ICliOptions {
json?: boolean, json?: boolean,
sort: "new" | "hot" | "top", sort: "new" | "hot" | "top",
o?: string, o?: string,
limit: number,
queryMetadata?: boolean, queryMetadata?: boolean,
} }

View file

@ -1,4 +1,5 @@
export default interface QueryResult { export default interface QueryResult {
subreddit: string, subreddit: string,
sortBy: string, sortBy: string,
limit: number,
} }

View file

@ -24,6 +24,7 @@ export default class OutputHelper {
if (options.queryMetadata != null) { if (options.queryMetadata != null) {
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
outputLines.push(`Query.Limit = ${response.Query.limit}`);
} }
return outputLines.join("\n"); return outputLines.join("\n");

View file

@ -7,8 +7,23 @@ import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages"; import ErrorMessages from "./constants/ErrorMessages";
import ImageHelper from "./helpers/imageHelper"; import ImageHelper from "./helpers/imageHelper";
export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot'): Promise<IReturnResult> { export default async function randomBunny(subreddit: string, sortBy: "new" | "hot" | "top" = 'hot', limit: number = 100): Promise<IReturnResult> {
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`) if (limit < 1 || limit > 100) {
return {
IsSuccess: false,
Query: {
subreddit: subreddit,
sortBy: sortBy,
limit: limit,
},
Error: {
Code: ErrorCode.LimitOutOfRange,
Message: ErrorMessages.LimitOutOfRange,
}
};
}
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=${limit}`)
.then((res) => { .then((res) => {
return res; return res;
}) })
@ -22,6 +37,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Query: { Query: {
subreddit: subreddit, subreddit: subreddit,
sortBy: sortBy, sortBy: sortBy,
limit: limit,
}, },
Error: { Error: {
Code: ErrorCode.FailedToFetchReddit, Code: ErrorCode.FailedToFetchReddit,
@ -38,6 +54,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Query: { Query: {
subreddit: subreddit, subreddit: subreddit,
sortBy: sortBy, sortBy: sortBy,
limit: limit,
}, },
Error: { Error: {
Code: ErrorCode.UnableToParseJSON, Code: ErrorCode.UnableToParseJSON,
@ -60,6 +77,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Query: { Query: {
subreddit: subreddit, subreddit: subreddit,
sortBy: sortBy, sortBy: sortBy,
limit: limit,
}, },
Error: { Error: {
Code: ErrorCode.NoImageResultsFound, Code: ErrorCode.NoImageResultsFound,
@ -85,6 +103,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Query: { Query: {
subreddit: subreddit, subreddit: subreddit,
sortBy: sortBy, sortBy: sortBy,
limit: limit,
}, },
Error: { Error: {
Code: ErrorCode.NoImageResultsFound, Code: ErrorCode.NoImageResultsFound,
@ -115,6 +134,7 @@ export default async function randomBunny(subreddit: string, sortBy: "new" | "ho
Query: { Query: {
subreddit: subreddit, subreddit: subreddit,
sortBy: sortBy, sortBy: sortBy,
limit: limit,
}, },
Result: redditResult Result: redditResult
}; };