Fix type error
This commit is contained in:
parent
1e123de1e1
commit
7e34ce5e9d
6 changed files with 55 additions and 28 deletions
|
@ -17,7 +17,8 @@
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"glob-parent": "^6.0.0",
|
"glob-parent": "^6.0.0",
|
||||||
"got": "^11.8.3"
|
"got": "^11.8.3",
|
||||||
|
"linqts": "^1.14.4"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
import IRedditResult from "./IRedditResult";
|
|
||||||
|
|
||||||
export default interface IFetchResult {
|
export default interface IFetchResult {
|
||||||
IsSuccess: boolean;
|
data: {
|
||||||
Result?: IRedditResult;
|
archived: boolean,
|
||||||
|
downs: number,
|
||||||
|
hidden: boolean,
|
||||||
|
permalink: string,
|
||||||
|
subreddit: string,
|
||||||
|
subreddit_subscribers: number,
|
||||||
|
title: string,
|
||||||
|
ups: number,
|
||||||
|
url: string
|
||||||
|
}
|
||||||
}
|
}
|
7
src/contracts/IReturnResult.ts
Normal file
7
src/contracts/IReturnResult.ts
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
import IRedditResult from "./IRedditResult";
|
||||||
|
|
||||||
|
export default interface IReturnResult
|
||||||
|
{
|
||||||
|
IsSuccess: boolean;
|
||||||
|
Result?: IRedditResult;
|
||||||
|
}
|
49
src/index.ts
49
src/index.ts
|
@ -1,6 +1,8 @@
|
||||||
import IFetchResult from "./contracts/IFetchResult";
|
import IReturnResult from "./contracts/IReturnResult";
|
||||||
import IRedditResult from "./contracts/IRedditResult";
|
import IRedditResult from "./contracts/IRedditResult";
|
||||||
import fetch from "got";
|
import fetch from "got";
|
||||||
|
import { List } from "linqts";
|
||||||
|
import IFetchResult from "./contracts/IFetchResult";
|
||||||
|
|
||||||
const sortable = [
|
const sortable = [
|
||||||
'new',
|
'new',
|
||||||
|
@ -8,7 +10,7 @@ const sortable = [
|
||||||
'top'
|
'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';
|
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot';
|
||||||
|
|
||||||
const result = await fetch(`https://reddit.com/r/${subreddit}/${sortBy}.json`);
|
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 data: IFetchResult[] = json.data.children;
|
||||||
const size = data.length;
|
|
||||||
|
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++) {
|
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 = {
|
const redditResult: IRedditResult = {
|
||||||
Archived: randomSelect['archived'],
|
Archived: randomData['archived'],
|
||||||
Downs: randomSelect['downs'],
|
Downs: randomData['downs'],
|
||||||
Hidden: randomSelect['hidden'],
|
Hidden: randomData['hidden'],
|
||||||
Permalink: randomSelect['permalink'],
|
Permalink: randomData['permalink'],
|
||||||
Subreddit: randomSelect['subreddit'],
|
Subreddit: randomData['subreddit'],
|
||||||
SubredditSubscribers: randomSelect['subreddit_subscribers'],
|
SubredditSubscribers: randomData['subreddit_subscribers'],
|
||||||
Title: randomSelect['title'],
|
Title: randomData['title'],
|
||||||
Ups: randomSelect['ups'],
|
Ups: randomData['ups'],
|
||||||
Url: randomSelect['url']
|
Url: randomData['url']
|
||||||
};
|
};
|
||||||
|
|
||||||
if (redditResult.Url.includes('.jpg')) {
|
return {
|
||||||
return {
|
IsSuccess: true,
|
||||||
IsSuccess: true,
|
Result: redditResult
|
||||||
Result: redditResult
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
/* Basic Options */
|
/* Basic Options */
|
||||||
// "incremental": true, /* Enable incremental compilation */
|
// "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'. */
|
"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. */
|
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||||
|
@ -44,7 +44,7 @@
|
||||||
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
|
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */
|
||||||
|
|
||||||
/* Module Resolution Options */
|
/* 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. */
|
// "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'. */
|
// "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. */
|
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
|
||||||
|
|
|
@ -704,6 +704,11 @@ levn@^0.4.1:
|
||||||
prelude-ls "^1.2.1"
|
prelude-ls "^1.2.1"
|
||||||
type-check "~0.4.0"
|
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:
|
lodash.clonedeep@^4.5.0:
|
||||||
version "4.5.0"
|
version "4.5.0"
|
||||||
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
|
resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef"
|
||||||
|
|
Loading…
Reference in a new issue