Compare commits
No commits in common. "55c11761be5d729cefc31c90f9c615febbbc7de8" and "b2722b487ea8c7cecc682616a3d7601eb37351e9" have entirely different histories.
55c11761be
...
b2722b487e
10 changed files with 435 additions and 568 deletions
9
app.ts
Normal file
9
app.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import randomBunny from "./dist/index.js";
|
||||
|
||||
async function app() {
|
||||
const result = await randomBunny('rabbits', 'hot');
|
||||
|
||||
console.log(result);
|
||||
}
|
||||
|
||||
app();
|
10
package.json
10
package.json
|
@ -23,7 +23,8 @@
|
|||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node dist/cli.js",
|
||||
"start": "ts-node app.ts",
|
||||
"cli": "ts-node src/cli.ts",
|
||||
"test": "jest",
|
||||
"lint": "eslint .",
|
||||
"release": "np --no-publish"
|
||||
|
@ -36,20 +37,21 @@
|
|||
"funding": "https://ko-fi.com/vylpes",
|
||||
"devDependencies": {
|
||||
"@types/eslint": "^8.21.1",
|
||||
"@types/jest": "^29.5.8",
|
||||
"@types/node": "^20.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^6.0.0",
|
||||
"@types/jest": "^29.5.8",
|
||||
"@typescript-eslint/parser": "^5.54.0",
|
||||
"eslint": "^8.49.0",
|
||||
"jest": "^29.7.0",
|
||||
"jest-mock-extended": "^3.0.3",
|
||||
"np": "^9.0.0",
|
||||
"np": "^8.0.0",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-mockito": "^2.6.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.0.0"
|
||||
},
|
||||
"resolutions": {
|
||||
"np/**/got": "^14.0.0",
|
||||
"np/**/got": "^13.0.0",
|
||||
"**/semver": "^7.5.2",
|
||||
"@babel/traverse": "^7.23.2"
|
||||
},
|
||||
|
|
13
src/cli.ts
13
src/cli.ts
|
@ -1,4 +1,4 @@
|
|||
import { Command, Option } from "commander";
|
||||
import { Command } from "commander";
|
||||
import randomBunny from "./index";
|
||||
import ICliOptions from "./contracts/ICliOptions";
|
||||
import { exit } from "process";
|
||||
|
@ -10,15 +10,13 @@ program
|
|||
.description('Get a random image url from a subreddit of your choosing')
|
||||
.version('2.2')
|
||||
.option('-s, --subreddit <subreddit>', 'The subreddit to search', 'rabbits')
|
||||
.option('-j, --json', 'Output as JSON')
|
||||
.option('-q, --query-metadata', 'Include query metadata in result')
|
||||
.addOption(new Option('--sort <sort>', 'Sort by').default('hot').choices(['hot', 'new', 'top']));
|
||||
.option('-j, --json', 'Output as JSON');
|
||||
|
||||
program.parse();
|
||||
|
||||
const options: ICliOptions = program.opts();
|
||||
|
||||
randomBunny(options.subreddit, options.sort)
|
||||
randomBunny(options.subreddit)
|
||||
.then((response) => {
|
||||
if (response.IsSuccess) {
|
||||
const result = response.Result!;
|
||||
|
@ -40,11 +38,6 @@ randomBunny(options.subreddit, options.sort)
|
|||
outputLines.push(`Upvotes = ${result.Ups}`);
|
||||
outputLines.push(`Url = ${result.Url}`);
|
||||
|
||||
if (options.queryMetadata != null) {
|
||||
outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`);
|
||||
outputLines.push(`Query.Sort By = ${response.Query.sortBy}`);
|
||||
}
|
||||
|
||||
console.log(outputLines.join("\n"));
|
||||
exit(0);
|
||||
} else {
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
export default interface ICliOptions {
|
||||
subreddit: string,
|
||||
json?: boolean,
|
||||
sort: string,
|
||||
queryMetadata?: boolean,
|
||||
}
|
|
@ -1,10 +1,8 @@
|
|||
import IError from "./IError.js";
|
||||
import IRedditResult from "./IRedditResult.js";
|
||||
import QueryResult from "./QueryResult.js";
|
||||
|
||||
export default interface IReturnResult {
|
||||
IsSuccess: boolean;
|
||||
Query: QueryResult;
|
||||
Result?: IRedditResult;
|
||||
Error?: IError;
|
||||
}
|
|
@ -1,4 +0,0 @@
|
|||
export default interface QueryResult {
|
||||
subreddit: string,
|
||||
sortBy: string,
|
||||
}
|
18
src/index.ts
18
src/index.ts
|
@ -15,7 +15,7 @@ const sortable = [
|
|||
export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise<IReturnResult> {
|
||||
if (!sortable.includes(sortBy)) sortBy = 'hot';
|
||||
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json?limit=100`)
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`)
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
|
@ -26,10 +26,6 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
if (!result) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Query: {
|
||||
subreddit: subreddit,
|
||||
sortBy: sortBy,
|
||||
},
|
||||
Error: {
|
||||
Code: ErrorCode.FailedToFetchReddit,
|
||||
Message: ErrorMessages.FailedToFetchReddit,
|
||||
|
@ -42,10 +38,6 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
if (!json) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Query: {
|
||||
subreddit: subreddit,
|
||||
sortBy: sortBy,
|
||||
},
|
||||
Error: {
|
||||
Code: ErrorCode.UnableToParseJSON,
|
||||
Message: ErrorMessages.UnableToParseJSON,
|
||||
|
@ -64,10 +56,6 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
if (dataWithImages.length == 0) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Query: {
|
||||
subreddit: subreddit,
|
||||
sortBy: sortBy,
|
||||
},
|
||||
Error: {
|
||||
Code: ErrorCode.NoImageResultsFound,
|
||||
Message: ErrorMessages.NoImageResultsFound,
|
||||
|
@ -95,10 +83,6 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
|
||||
return {
|
||||
IsSuccess: true,
|
||||
Query: {
|
||||
subreddit: subreddit,
|
||||
sortBy: sortBy,
|
||||
},
|
||||
Result: redditResult
|
||||
};
|
||||
}
|
|
@ -70,23 +70,23 @@ describe('subreddit', () => {
|
|||
}, 5000);
|
||||
|
||||
test('GIVEN -s is supplied, EXPECT subreddit to be changed', async () => {
|
||||
const result = await cli(['-s', 'pics'], '.');
|
||||
const result = await cli(['-s', 'horses'], '.');
|
||||
|
||||
const subreddit = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')!
|
||||
.split(' = ')[1];
|
||||
|
||||
expect(subreddit).toBe('pics');
|
||||
expect(subreddit).toBe('Horses');
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN --subreddit is supplied, EXPECT subreddit to be changed', async () => {
|
||||
const result = await cli(['--subreddit', 'pics'], '.');
|
||||
const result = await cli(['--subreddit', 'horses'], '.');
|
||||
|
||||
const subreddit = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Subreddit')!
|
||||
.split(' = ')[1];
|
||||
|
||||
expect(subreddit).toBe('pics');
|
||||
expect(subreddit).toBe('Horses');
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
|
@ -108,64 +108,6 @@ describe('json', () => {
|
|||
}, 5000);
|
||||
});
|
||||
|
||||
describe('sort', () => {
|
||||
test('GIVEN --sort is not supplied, EXPECT sort to be defaulted', async () => {
|
||||
const result = await cli(['-q'], '.');
|
||||
|
||||
const sortBy = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')!
|
||||
.split(' = ')[1];
|
||||
|
||||
expect(sortBy).toBe('hot');
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN --sort is supplied WITH a valid input, EXPECT sort to be used', async () => {
|
||||
const result = await cli(['-q', '--sort', 'new'], '.');
|
||||
|
||||
const sortBy = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0] == 'Query.Sort By')!
|
||||
.split(' = ')[1];
|
||||
|
||||
expect(sortBy).toBe('new');
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN --sort is supplied WITH an invalid input, EXPECT error', async () => {
|
||||
const result = await cli(['-q', '--sort', 'invalid'], '.');
|
||||
|
||||
expect(result.code).toBe(1);
|
||||
expect(result.stderr).toBe("error: option '--sort <sort>' argument 'invalid' is invalid. Allowed choices are hot, new, top.\n");
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
describe('query-metadata', () => {
|
||||
test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => {
|
||||
const result = await cli([], '.');
|
||||
|
||||
const query = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
|
||||
|
||||
expect(query).toBeUndefined();
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN --query-metadata is not supplied, EXPECT no query metadata returned', async () => {
|
||||
const result = await cli(['--query-metadata'], '.');
|
||||
|
||||
const query = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
|
||||
|
||||
expect(query).toBeDefined();
|
||||
}, 5000);
|
||||
|
||||
test('GIVEN -q is not supplied, EXPECT no query metadata returned', async () => {
|
||||
const result = await cli(['-q'], '.');
|
||||
|
||||
const query = result.stdout.split('\n')
|
||||
.find(x => x && x.length > 0 && x.split(' = ')[0].startsWith('Query'));
|
||||
|
||||
expect(query).toBeDefined();
|
||||
}, 5000);
|
||||
});
|
||||
|
||||
function cli(args: string[], cwd: string): Promise<cliResult> {
|
||||
return new Promise(resolve => {
|
||||
exec(`node ${path.resolve('./dist/cli.js')} ${args.join(' ')}`,
|
||||
|
|
|
@ -36,7 +36,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
||||
test('GIVEN sortBy is NOT supplied, expect it to default to hot', async () => {
|
||||
|
@ -68,7 +68,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
|
||||
});
|
||||
|
||||
test('GIVEN sortBy is NOT valid, expect it to default to hot', async () => {
|
||||
|
@ -100,7 +100,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
|
||||
});
|
||||
|
||||
test('GIVEN the fetch fails, EXPECT failure result', async () => {
|
||||
|
@ -115,7 +115,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
||||
test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => {
|
||||
|
@ -132,7 +132,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
||||
test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => {
|
||||
|
@ -153,7 +153,7 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
||||
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', async () => {
|
||||
|
@ -188,6 +188,6 @@ describe('randomBunny', () => {
|
|||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json?limit=100');
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue