Compare commits

...

27 commits

Author SHA1 Message Date
Vylpes 2885d6365b Add drone deployment (#60)
All checks were successful
continuous-integration/drone/push Build is passing
- Added to Drone CI script to auto deploy on tag push
- Added script to tag the project

Fixes #35

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/60
2023-04-28 17:32:17 +01:00
Vylpes c0ca0e6697 Update licence copyright to 2023 (#59)
All checks were successful
continuous-integration/drone/push Build is passing
- Update licence copyright to 2020-2023

Fixes #46

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/59
2023-04-28 17:31:55 +01:00
Vylpes 2c8711cd5e Fix documentation code breaks (#58)
All checks were successful
continuous-integration/drone/push Build is passing
- Remove conflicting licence file (multiple, but different cases)
- Fix documentation missing code breaks

Fixes #37

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/58
2023-04-28 17:31:34 +01:00
Vylpes 06485e0a88 Setup Jest (#57)
All checks were successful
continuous-integration/drone/push Build is passing
- Setup Jest
- Add test scripts
- Configure Drone

Fixes #1

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/57
2023-04-22 13:20:30 +01:00
Vylpes 2ab2fe04fd Merge pull request 'Update dependency typescript to v5' (#55) from renovate/typescript-5.x into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/55
2023-04-17 17:34:24 +01:00
Vylpes a8b19f1367 Merge pull request 'Update dependency eslint to v8' (#44) from renovate/eslint-8.x into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/44
2023-04-17 17:33:20 +01:00
Vylpes ab1ac9f908 Merge branch 'develop' into renovate/eslint-8.x
All checks were successful
continuous-integration/drone/push Build is passing
2023-04-17 17:33:06 +01:00
Ethan Lane 44dd8188f2 Merge branch 'master' into develop
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-30 18:42:58 +01:00
RenovateBot 409a5f4abb Update dependency typescript to v5
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-20 00:02:45 +00:00
RenovateBot 27a8fa0a88 Update dependency eslint to v8
All checks were successful
continuous-integration/drone/push Build is passing
2023-03-06 00:01:43 +00:00
Vylpes 12b1871f6c Merge pull request 'Update dependency got to v12' (#45) from renovate/got-12.x into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/45
Reviewed-by: Vylpes <ethan@vylpes.com>
2023-02-27 18:35:19 +00:00
Ethan Lane 2d7046bd22 Add renovate to pipelines
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-27 18:34:31 +00:00
Ethan Lane cd541ddd4f Update project to allow for ESM modules 2023-02-27 18:33:32 +00:00
RenovateBot c4ba04677d Update dependency got to v12 2023-02-27 00:01:42 +00:00
Vylpes 6ffcb7a6ba Merge pull request 'Add Gitea templates' (#52) from feature/10-templates into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/52
Reviewed-by: VylpesTester <tester@vylpes.com>
2023-02-24 20:31:35 +00:00
Vylpes 263a84dd4c Merge branch 'develop' into feature/10-templates
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-24 20:31:27 +00:00
Vylpes 3a9d5b3063 Merge pull request 'Remove MaxTries variable' (#51) from feature/34-remove-max-tries into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/51
Reviewed-by: VylpesTester <tester@vylpes.com>
2023-02-24 20:31:22 +00:00
Vylpes 99e6f0e586 Merge branch 'develop' into feature/34-remove-max-tries
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-24 20:31:10 +00:00
Ethan Lane d02c6e7731 Add Gitea templates
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-24 17:33:39 +00:00
Vylpes 24f9444af6 Merge pull request 'Update dependency @types/node to v18' (#43) from renovate/node-18.x into develop
All checks were successful
continuous-integration/drone/push Build is passing
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/43
Reviewed-by: Vylpes <ethan@vylpes.com>
2023-02-22 18:25:16 +00:00
Ethan Lane 948f513b53 Updatr documentation to remove maxTries variable
All checks were successful
continuous-integration/drone/push Build is passing
2023-02-22 18:19:47 +00:00
Ethan Lane 089f693413 Remove maxTries variable 2023-02-22 18:18:50 +00:00
Ethan Lane bb9f092731 Merge branch 'master' into feature/34-remove-max-tries 2023-02-22 18:15:30 +00:00
RenovateBot fdd49a94b9 Update dependency @types/node to v18 2022-12-28 18:51:48 +00:00
Ethan Lane f6d934c80f Merge remote-tracking branch 'origin/renovate/configure' into develop 2022-12-28 18:49:09 +00:00
Ethan Lane 9f5cd2ae80 2.0.3 2022-12-18 15:30:12 +00:00
Vylpes 031a5455b5 hotfix/2.0.3 (#41)
- Bump minimatch from 3.0.4 to 3.1.2
- Update discord links

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/41
Reviewed-by: VylpesTester <tester@vylpes.com>
2022-12-18 15:27:03 +00:00
19 changed files with 4271 additions and 324 deletions

View file

@ -1,3 +1,47 @@
---
kind: pipeline
name: deployment
steps:
- name: build
image: node
commands:
- yarn install --frozen-lockfile
- yarn build
- name: test
image: node
commands:
- yarn test
depends_on:
- build
- name: lint
image: node
commands:
- yarn lint
depends_on:
- build
- name: deploy
image: plugins/npm
settings:
username: vylpes
password:
from_secret: npm_password
email: ethan@vylpes.com
token:
from_secret: npm_token
depends_on:
- test
- lint
trigger:
branch:
- master
event:
- tag
---
kind: pipeline
name: integration
@ -6,17 +50,25 @@ steps:
image: node
commands:
- yarn install --frozen-lockfile
- yarn build
- name: test
image: node
commands:
- yarn test
depends_on:
- build
- name: lint
image: node
commands:
- yarn lint
depends_on:
- build
trigger:
branch:
- develop
- feature/*
- hotfix/*
- renovate/*
event:
- push

1
.eslintignore Normal file
View file

@ -0,0 +1 @@
dist/

View file

@ -44,6 +44,7 @@
"module": "writable",
"require": "writable",
"process": "writable",
"console": "writable"
"console": "writable",
"jest": "writable"
}
}

18
.gitea/ISSUE_TEMPLATE.md Normal file
View file

@ -0,0 +1,18 @@
Epic: \
Story Points:
---
*No description*
## Acceptance Criteria
*No acceptance criteria*
## Subtasks
*No subtasks*
## Notes
*No notes*

View file

@ -0,0 +1,29 @@
# 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.
Fixes # (issue)
## Type of change
Please delete options that are not relevant.
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update
# 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.
# Checklist
- [ ] My code follows the style guidelines of this project
- [ ] 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
- [ ] My changes generate no new warnings
- [ ] I have added tests that provde my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

View file

@ -1 +1,5 @@
app.ts
tests/
jest.config.cjs
jest.setup.js

View file

@ -196,7 +196,10 @@ function ban (member) {
}).catch(err => {
// handle error here
});
}# Contributing to Random Bunny
}
```
# Contributing to Random Bunny
First off, thanks for taking the time to contribute!

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2020 Vylpes
Copyright (c) 2020-2023 Vylpes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

4
app.ts
View file

@ -1,7 +1,7 @@
import randomBunny from "./dist";
import randomBunny from "./dist/index.js";
async function app() {
const result = await randomBunny('rabbits', 'hot', 100);
const result = await randomBunny('rabbits', 'hot');
console.log(result);
}

4
jest.config.cjs Normal file
View file

@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
setupFiles: [ "./jest.setup.js" ]
}

3
jest.setup.js Normal file
View file

@ -0,0 +1,3 @@
jest.setTimeout(1 * 1000); // 1 second
jest.resetModules();
jest.resetAllMocks();

21
license
View file

@ -1,21 +0,0 @@
MIT License
Copyright (c) 2020 Vylpes
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -4,7 +4,7 @@
"description": "Get a random subreddit image url",
"license": "MIT",
"author": "Vylpes",
"main": "./dist/index.js",
"exports": "./dist/index.js",
"typings": "./dist/index.d.ts",
"keywords": [
"rabbit",
@ -17,14 +17,15 @@
],
"dependencies": {
"glob-parent": "^6.0.0",
"got": "^11.8.3",
"got-cjs": "^12.5.4",
"linqts": "^1.14.4"
},
"scripts": {
"build": "tsc",
"start": "ts-node app.ts",
"test": "echo none",
"lint": "eslint ."
"test": "jest",
"lint": "eslint .",
"release": "np --no-publish"
},
"bugs": {
"url": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny/issues",
@ -33,10 +34,19 @@
"homepage": "https://gitea.vylpes.xyz/RabbitLabs/random-bunny",
"funding": "https://ko-fi.com/vylpes",
"devDependencies": {
"@types/node": "^16.11.11",
"eslint": "^7.17.0",
"ts-node": "^10.4.0",
"typescript": "^4.5.2"
"@types/eslint": "^8.21.1",
"@types/jest": "^29.4.0",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.35.0",
"jest": "^29.4.3",
"jest-mock-extended": "^3.0.3",
"np": "^7.7.0",
"ts-jest": "^29.0.5",
"ts-mockito": "^2.6.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.0"
},
"files": [
"dist"

View file

@ -21,7 +21,7 @@ yarn add random-bunny
import randomBunny from "random-bunny";
// ... In an async function
const result = await randomBunny('rabbits', 'hot', 100);
const result = await randomBunny('rabbits', 'hot');
console.log(result);
```
@ -29,7 +29,7 @@ console.log(result);
### `randomBunny()`
Returns a `json string` for a random post. Accepts 3 arguments: `subreddit`, `sortby` ('new', 'hot', 'top'), `maxTries?` (default 100)
Returns a `json string` for a random post. Accepts 2 arguments: `subreddit`, and `sortby` ('new', 'hot', 'top')
The json string which gets returned consists of:
- archived
@ -44,8 +44,6 @@ The json string which gets returned consists of:
`sortBy` will default to 'hot' if not given or invalid
`maxTries` prevents the script from rerolling too many times. The script rerolls the randomiser if the post its given doesn't contain an image. Default 100.
## Notes
* Node 4 or newer.

View file

@ -1,4 +1,4 @@
import IRedditResult from "./IRedditResult";
import IRedditResult from "./IRedditResult.js";
export default interface IReturnResult {
IsSuccess: boolean;

View file

@ -1,8 +1,8 @@
import IReturnResult from "./contracts/IReturnResult";
import IRedditResult from "./contracts/IRedditResult";
import fetch from "got";
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 IFetchResult from "./contracts/IFetchResult.js";
const sortable = [
'new',
@ -10,7 +10,7 @@ const sortable = [
'top'
];
export default async function randomBunny(subreddit: string, sortBy?: string, maxTries = 100): Promise<IReturnResult> {
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`);
@ -35,13 +35,18 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
.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() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
let random = 0;
if (dataWithImages.length == 0) {
return {
IsSuccess: false,
};
} else if (dataWithImages.length > 1) {
random = Math.floor((Math.random() * dataWithImages.length - 1) + 0); // Between 0 and (size - 1)
}
const randomSelect = dataWithImages[random];
if (!randomSelect) continue;
const randomData = randomSelect.data;
const redditResult: IRedditResult = {
@ -61,8 +66,3 @@ export default async function randomBunny(subreddit: string, sortBy?: string, ma
Result: redditResult
};
}
return {
IsSuccess: false
}
}

172
tests/index.test.ts Normal file
View file

@ -0,0 +1,172 @@
import randomBunny from "../src/index";
import fetch from "got-cjs";
jest.mock('got-cjs');
const fetchMock = jest.mocked(fetch);
describe('randomBunny', () => {
test('GIVEN subreddit AND sortBy is supplied, EXPECT successful result', async() => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN sortBy is NOT supplied, expect it to default to hot', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
});
test('GIVEN sortBy is NOT valid, expect it to default to hot', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.jpg',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'invalid');
expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
});
test('GIVEN the fetch fails, EXPECT failure result', async () => {
fetchMock.mockResolvedValue(null);
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN the result is NOT valid JSON, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify(null),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN randomSelect does NOT find a response, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
test('GIVEN randomSelect does NOT find a valid response, EXPECT failure result', async () => {
fetchMock.mockResolvedValue({
body: JSON.stringify({
data: {
children: [
{
data: {
archived: false,
downs: 0,
hidden: false,
permalink: '/r/Rabbits/comments/12pa5te/someone_told_pickles_its_monday_internal_fury/',
subreddit: 'Rabbits',
subreddit_subscribers: 298713,
title: 'Someone told pickles its Monday… *internal fury*',
ups: 1208,
url: 'https://i.redd.it/cr8xudsnkgua1.webp',
},
},
],
}
}),
});
const result = await randomBunny('rabbits', 'new');
expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
});
});

View file

@ -5,7 +5,7 @@
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"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": "Node16", /* 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. */
// "checkJs": true, /* Report errors in .js files. */
@ -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": "node16", /* 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. */
@ -73,7 +73,11 @@
"./src",
],
"exclude": [
"./tests"
]
"./tests",
"jest.config.cjs",
"jest.setup.js"
],
"ts-node": {
"esm": true
}
}

4153
yarn.lock

File diff suppressed because it is too large Load diff