Compare commits
1 commit
53acf493d7
...
75ab7fec5b
Author | SHA1 | Date | |
---|---|---|---|
75ab7fec5b |
9 changed files with 689 additions and 1151 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,4 +2,3 @@ node_modules/
|
|||
dist/
|
||||
coverage/
|
||||
yarn-error.log
|
||||
.DS_Store
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "random-bunny",
|
||||
"version": "2.1.4",
|
||||
"version": "2.1.3",
|
||||
"description": "Get a random subreddit image url",
|
||||
"license": "MIT",
|
||||
"author": "Vylpes",
|
||||
|
@ -38,11 +38,11 @@
|
|||
"@types/jest": "^29.4.0",
|
||||
"@types/node": "^20.0.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.54.0",
|
||||
"@typescript-eslint/parser": "^5.54.0",
|
||||
"eslint": "^8.49.0",
|
||||
"@typescript-eslint/parser": "^6.0.0",
|
||||
"eslint": "^8.35.0",
|
||||
"jest": "^29.4.3",
|
||||
"jest-mock-extended": "^3.0.3",
|
||||
"np": "^8.0.0",
|
||||
"np": "^7.7.0",
|
||||
"ts-jest": "^29.0.5",
|
||||
"ts-mockito": "^2.6.1",
|
||||
"ts-node": "^10.9.1",
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
export enum ErrorCode {
|
||||
Unknown,
|
||||
FailedToFetchReddit,
|
||||
UnableToParseJSON,
|
||||
NoImageResultsFound,
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
export default class ErrorMessages {
|
||||
public static readonly FailedToFetchReddit = "Failed to fetch result from Reddit";
|
||||
public static readonly UnableToParseJSON = "Unable to parse the JSON result";
|
||||
public static readonly NoImageResultsFound = "No image results found in response from Reddit";
|
||||
}
|
|
@ -1,6 +0,0 @@
|
|||
import { ErrorCode } from "../constants/ErrorCode";
|
||||
|
||||
export default interface IError {
|
||||
Code: ErrorCode;
|
||||
Message: string;
|
||||
}
|
|
@ -1,8 +1,6 @@
|
|||
import IError from "./IError.js";
|
||||
import IRedditResult from "./IRedditResult.js";
|
||||
|
||||
export default interface IReturnResult {
|
||||
IsSuccess: boolean;
|
||||
Result?: IRedditResult;
|
||||
Error?: IError;
|
||||
}
|
28
src/index.ts
28
src/index.ts
|
@ -1,10 +1,8 @@
|
|||
import IReturnResult from "./contracts/IReturnResult";
|
||||
import IRedditResult from "./contracts/IRedditResult";
|
||||
import IReturnResult from "./contracts/IReturnResult.js";
|
||||
import IRedditResult from "./contracts/IRedditResult.js";
|
||||
import fetch from "got-cjs";
|
||||
import { List } from 'linqts';
|
||||
import IFetchResult from "./contracts/IFetchResult";
|
||||
import { ErrorCode } from "./constants/ErrorCode";
|
||||
import ErrorMessages from "./constants/ErrorMessages";
|
||||
import IFetchResult from "./contracts/IFetchResult.js";
|
||||
|
||||
const sortable = [
|
||||
'new',
|
||||
|
@ -12,18 +10,14 @@ const sortable = [
|
|||
'top'
|
||||
];
|
||||
|
||||
export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise<IReturnResult> {
|
||||
if (!sortable.includes(sortBy)) sortBy = 'hot';
|
||||
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> {
|
||||
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||
|
||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
||||
|
||||
if (!result) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Error: {
|
||||
Code: ErrorCode.FailedToFetchReddit,
|
||||
Message: ErrorMessages.FailedToFetchReddit,
|
||||
},
|
||||
IsSuccess: false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -31,11 +25,7 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
|
||||
if (!json) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Error: {
|
||||
Code: ErrorCode.UnableToParseJSON,
|
||||
Message: ErrorMessages.UnableToParseJSON,
|
||||
},
|
||||
IsSuccess: false
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,10 +40,6 @@ export default async function randomBunny(subreddit: string, sortBy: string = 'h
|
|||
if (dataWithImages.length == 0) {
|
||||
return {
|
||||
IsSuccess: false,
|
||||
Error: {
|
||||
Code: ErrorCode.NoImageResultsFound,
|
||||
Message: ErrorMessages.NoImageResultsFound,
|
||||
},
|
||||
};
|
||||
} else {
|
||||
random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1)
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import { ErrorCode } from "../src/constants/ErrorCode";
|
||||
import ErrorMessages from "../src/constants/ErrorMessages";
|
||||
import randomBunny from "../src/index";
|
||||
import fetch from "got-cjs";
|
||||
|
||||
|
@ -34,7 +32,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeTruthy();
|
||||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
@ -66,7 +63,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeTruthy();
|
||||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
|
||||
});
|
||||
|
@ -98,7 +94,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeTruthy();
|
||||
expect(result.Result).toBeDefined();
|
||||
expect(result.Error).toBeUndefined();
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
|
||||
});
|
||||
|
@ -110,10 +105,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeFalsy();
|
||||
expect(result.Result).toBeUndefined();
|
||||
expect(result.Error).toBeDefined();
|
||||
|
||||
expect(result.Error!.Code).toBe(ErrorCode.FailedToFetchReddit);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.FailedToFetchReddit);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
@ -127,10 +118,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeFalsy();
|
||||
expect(result.Result).toBeUndefined();
|
||||
expect(result.Error).toBeDefined();
|
||||
|
||||
expect(result.Error!.Code).toBe(ErrorCode.UnableToParseJSON);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.UnableToParseJSON);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
@ -148,10 +135,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeFalsy();
|
||||
expect(result.Result).toBeUndefined();
|
||||
expect(result.Error).toBeDefined();
|
||||
|
||||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
@ -183,10 +166,6 @@ describe('randomBunny', () => {
|
|||
|
||||
expect(result.IsSuccess).toBeFalsy();
|
||||
expect(result.Result).toBeUndefined();
|
||||
expect(result.Error).toBeDefined();
|
||||
|
||||
expect(result.Error!.Code).toBe(ErrorCode.NoImageResultsFound);
|
||||
expect(result.Error!.Message).toBe(ErrorMessages.NoImageResultsFound);
|
||||
|
||||
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue