random-bunny/src/index.ts

88 lines
2.4 KiB
TypeScript
Raw Normal View History

import IReturnResult from "./contracts/IReturnResult";
import IRedditResult from "./contracts/IRedditResult";
import fetch from "got-cjs";
import { List } from 'linqts';
import IFetchResult from "./contracts/IFetchResult";
import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages";
2021-12-01 20:32:20 +00:00
const sortable = [
'new',
'hot',
'top'
];
export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise<IReturnResult> {
if (!sortable.includes(sortBy)) sortBy = 'hot';
2021-12-01 20:32:20 +00:00
Fix fetch not using error system when unable to fetch (#100) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Fix the fetch not failing gracefully using the new error handler #91 ## Type of change Please delete options that are not relevant. - [x] Bug fix (non-breaking change which fixes an issue) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - Updated the tests to be more accurate to the fetching package by using a promise reject - Tested the code by turning my wifi off and ensuring the output is the new error handler # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [x] I have added tests that provde my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/100 Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
2023-10-20 17:31:38 +01:00
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`)
.then((res) => {
return res;
})
.catch(() => {
return null;
});
2021-12-01 20:32:20 +00:00
if (!result) {
return {
IsSuccess: false,
Error: {
Code: ErrorCode.FailedToFetchReddit,
Message: ErrorMessages.FailedToFetchReddit,
},
2021-12-01 20:32:20 +00:00
}
}
2021-12-01 20:39:02 +00:00
const json = JSON.parse(result.body);
2021-12-01 20:32:20 +00:00
if (!json) {
return {
IsSuccess: false,
Error: {
Code: ErrorCode.UnableToParseJSON,
Message: ErrorMessages.UnableToParseJSON,
},
2021-12-01 20:32:20 +00:00
}
}
const data: IFetchResult[] = json.data.children;
2023-02-22 18:18:50 +00:00
const dataWithImages = new List<IFetchResult>(data)
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
.ToArray();
2021-12-01 20:32:20 +00:00
let random = 0;
2021-12-01 20:32:20 +00:00
if (dataWithImages.length == 0) {
return {
2023-02-22 18:18:50 +00:00
IsSuccess: false,
Error: {
Code: ErrorCode.NoImageResultsFound,
Message: ErrorMessages.NoImageResultsFound,
},
};
} else {
random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1)
}
const randomSelect = dataWithImages[random];
2023-02-22 18:18:50 +00:00
const randomData = randomSelect.data;
const redditResult: IRedditResult = {
Archived: randomData['archived'],
Downs: randomData['downs'],
Hidden: randomData['hidden'],
Permalink: randomData['permalink'],
Subreddit: randomData['subreddit'],
SubredditSubscribers: randomData['subreddit_subscribers'],
Title: randomData['title'],
Ups: randomData['ups'],
Url: randomData['url']
};
2021-12-01 20:32:20 +00:00
return {
2023-02-22 18:18:50 +00:00
IsSuccess: true,
Result: redditResult
};
2021-12-01 20:32:20 +00:00
}