Fix type error #32

Closed
Vylpes wants to merge 1 commit from 25-typeerror-cannot-read-properties-of-undefined-reading-data into hotfix/2.0.2
6 changed files with 55 additions and 28 deletions

View file

@ -17,7 +17,8 @@
],
"dependencies": {
"glob-parent": "^6.0.0",
"got": "^11.8.3"
"got": "^11.8.3",
"linqts": "^1.14.4"
},
"scripts": {
"build": "tsc",

View file

@ -1,6 +1,13 @@
import IRedditResult from "./IRedditResult";
export default interface IFetchResult {
IsSuccess: boolean;
Result?: IRedditResult;
data: {
archived: boolean,
downs: number,
hidden: boolean,
permalink: string,
subreddit: string,
subreddit_subscribers: number,
title: string,
ups: number,
url: string
}
}

View file

@ -0,0 +1,7 @@
import IRedditResult from "./IRedditResult";
export default interface IReturnResult
{
IsSuccess: boolean;
Result?: IRedditResult;
}

View file

@ -1,6 +1,8 @@
import IFetchResult from "./contracts/IFetchResult";
import IReturnResult from "./contracts/IReturnResult";
import IRedditResult from "./contracts/IRedditResult";
import fetch from "got";
import { List } from "linqts";
import IFetchResult from "./contracts/IFetchResult";
const sortable = [
'new',
@ -8,7 +10,7 @@ const sortable = [
'top'
];
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IFetchResult> {
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
@ -27,32 +29,37 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
}
}
const data = json.data.children;
const size = data.length;
const data: IFetchResult[] = json.data.children;
const dataWithImages = new List<IFetchResult>(data)
.Where(x => x!.data.url.includes('.jpg') || x!.data.url.includes('.png'))
.ToArray();
for (let i = 0; i < maxTries; i++) {
const random = Math.floor((Math.random() * size - 1) + 0); // Between 0 and (size - 1)
const random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
const randomSelect = data[random].data;
const randomSelect = dataWithImages[random];
if (!randomSelect) continue;
const randomData = randomSelect.data;
const redditResult: IRedditResult = {
Archived: randomSelect['archived'],
Downs: randomSelect['downs'],
Hidden: randomSelect['hidden'],
Permalink: randomSelect['permalink'],
Subreddit: randomSelect['subreddit'],
SubredditSubscribers: randomSelect['subreddit_subscribers'],
Title: randomSelect['title'],
Ups: randomSelect['ups'],
Url: randomSelect['url']
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']
};
if (redditResult.Url.includes('.jpg')) {
return {
IsSuccess: true,
Result: redditResult
};
}
return {
IsSuccess: true,
Result: redditResult
};
}
return {

View file

@ -4,7 +4,7 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es6", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
@ -44,7 +44,7 @@
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */

View file

@ -704,6 +704,11 @@ levn@^0.4.1:
prelude-ls "^1.2.1"
type-check "~0.4.0"
linqts@^1.14.4:
version "1.14.4"
resolved "https://registry.yarnpkg.com/linqts/-/linqts-1.14.4.tgz#0aa0f78fc6be073d7db874e0a0480fda5d06db7d"
integrity sha512-b5sJjG1ZQ8iLSTJV19jWgMLoQicrQVVRkkQN7B/aboU+cf30lgnhIoGM8vEjqPxZFpryDU78PFpuTnfYNIHMeg==
lodash.clonedeep@^4.5.0:
version "4.5.0"
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"