Compare commits

..

12 commits

Author SHA1 Message Date
53acf493d7 Update dependency ts-jest to v29.1.1
Some checks failed
continuous-integration/drone/push Build is failing
2023-09-28 10:43:32 +00:00
037608e059 Add error codes and error messages to failed results (#89)
All checks were successful
continuous-integration/drone/push Build is passing
# Description

- Added an Error Code (enum) and Error Message (string) to all the failed results

#53

## Type of change

Please delete options that are not relevant.

- [x] New feature (non-breaking change which adds functionality)

# How Has This Been Tested?

- This has been tested both manually as well as the unit tests

# 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

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/89
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2023-09-15 15:18:57 +01:00
07c37e2c06 Add default value to sortBy variable (#87)
All checks were successful
continuous-integration/drone/push Build is passing
# Description

- Add default value to the sortBy variable ('hot')

#50

## Type of change

Please delete options that are not relevant.

- [x] New feature (non-breaking change which adds functionality)

# 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

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/87
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2023-09-15 15:15:34 +01:00
ba5ccb54cf Regenerate lockfile
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-13 13:20:54 +01:00
30e84df5b3 Merge branch 'master' into develop
Some checks failed
continuous-integration/drone/push Build is failing
2023-09-13 13:20:07 +01:00
f61a5a7ae5 v2.1.4
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-13 13:15:53 +01:00
9b3b5b20ef Revert to 2.1.3 2023-09-13 13:15:46 +01:00
55d6469a6b v2.1.4
Some checks failed
continuous-integration/drone/push Build is failing
2023-09-13 11:51:46 +01:00
23f15b7577 Upgrade dependency eslint to 8.49.0
All checks were successful
continuous-integration/drone/push Build is passing
2023-09-13 11:50:44 +01:00
e10bdaf115 Update dependency @types/node to v20.6.0 (#85)
All checks were successful
continuous-integration/drone/push Build is passing
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/node) ([source](https://github.com/DefinitelyTyped/DefinitelyTyped)) | devDependencies | minor | [`20.2.3` -> `20.6.0`](https://renovatebot.com/diffs/npm/@types%2fnode/20.2.3/20.6.0) |

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44OS4wIiwidXBkYXRlZEluVmVyIjoiMzYuODkuMCIsInRhcmdldEJyYW5jaCI6ImRldmVsb3AifQ==-->

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/85
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2023-09-11 20:33:40 +01:00
4a85620e47 Update dependency jest-mock-extended to v3.0.5 (#83)
All checks were successful
continuous-integration/drone/push Build is passing
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [jest-mock-extended](https://github.com/marchaos/jest-mock-extended) | devDependencies | patch | [`3.0.4` -> `3.0.5`](https://renovatebot.com/diffs/npm/jest-mock-extended/3.0.4/3.0.5) |

---

### Release Notes

<details>
<summary>marchaos/jest-mock-extended (jest-mock-extended)</summary>

### [`v3.0.5`](https://github.com/marchaos/jest-mock-extended/releases/tag/3.0.5)

[Compare Source](https://github.com/marchaos/jest-mock-extended/compare/3.0.4...3.0.5)

Fixed Deep Mock perf issue with https://github.com/marchaos/jest-mock-extended/pull/123

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNi44OS4wIiwidXBkYXRlZEluVmVyIjoiMzYuODkuMCIsInRhcmdldEJyYW5jaCI6ImRldmVsb3AifQ==-->

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/83
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2023-09-11 20:32:54 +01:00
439bce6fac Update dependency np to v8 (#67)
All checks were successful
continuous-integration/drone/push Build is passing
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [np](https://github.com/sindresorhus/np) | devDependencies | major | [`^7.7.0` -> `^8.0.0`](https://renovatebot.com/diffs/npm/np/7.7.0/8.0.4) |

---

### Release Notes

<details>
<summary>sindresorhus/np (np)</summary>

### [`v8.0.4`](https://github.com/sindresorhus/np/releases/tag/v8.0.4)

[Compare Source](https://github.com/sindresorhus/np/compare/v8.0.3...v8.0.4)

-   Handle first time display of dependencies ([#&#8203;707](https://github.com/sindresorhus/np/issues/707))  [`3f43d78`](https://github.com/sindresorhus/np/commit/3f43d78)

### [`v8.0.3`](https://github.com/sindresorhus/np/releases/tag/v8.0.3)

[Compare Source](https://github.com/sindresorhus/np/compare/v8.0.2...v8.0.3)

-   Fix skipping publish step ([#&#8203;706](https://github.com/sindresorhus/np/issues/706))  [`51dcc2d`](https://github.com/sindresorhus/np/commit/51dcc2d)

### [`v8.0.2`](https://github.com/sindresorhus/np/releases/tag/v8.0.2)

[Compare Source](https://github.com/sindresorhus/np/compare/v8.0.1...v8.0.2)

-   Fix publish not working with Yarn  [`3d448c2`](https://github.com/sindresorhus/np/commit/3d448c2)
-   Include stack trace in errors  [`12fce88`](https://github.com/sindresorhus/np/commit/12fce88)

### [`v8.0.1`](https://github.com/sindresorhus/np/releases/tag/v8.0.1)

[Compare Source](https://github.com/sindresorhus/np/compare/v8.0.0...v8.0.1)

-   Fix a crash in the new dependency check  [`beb7db1`](https://github.com/sindresorhus/np/commit/beb7db1)

### [`v8.0.0`](https://github.com/sindresorhus/np/releases/tag/v8.0.0)

[Compare Source](https://github.com/sindresorhus/np/compare/v7.7.0...v8.0.0)

##### Breaking

-   Require Node.js 16 ([#&#8203;683](https://github.com/sindresorhus/np/issues/683))  [`72879e0`](https://github.com/sindresorhus/np/commit/72879e0)

##### Improvements

-   Add 2FA support for npm version 9+ ([#&#8203;693](https://github.com/sindresorhus/np/issues/693))  [`9cb4bfd`](https://github.com/sindresorhus/np/commit/9cb4bfd)
-   Improve startup time ([#&#8203;688](https://github.com/sindresorhus/np/issues/688))  [`eba203f`](https://github.com/sindresorhus/np/commit/eba203f)
-   Improve the reliability of detecting which files will be included in the package ([#&#8203;682](https://github.com/sindresorhus/np/issues/682))  [`a6ce792`](https://github.com/sindresorhus/np/commit/a6ce792)
-   Add check for new dependencies ([#&#8203;681](https://github.com/sindresorhus/np/issues/681))  [`6867fb9`](https://github.com/sindresorhus/np/commit/6867fb9)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC43NC4yIiwidXBkYXRlZEluVmVyIjoiMzYuODkuMCIsInRhcmdldEJyYW5jaCI6ImRldmVsb3AifQ==-->

Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/random-bunny/pulls/67
Co-authored-by: Renovate Bot <renovate@vylpes.com>
Co-committed-by: Renovate Bot <renovate@vylpes.com>
2023-09-11 20:31:50 +01:00
9 changed files with 1154 additions and 692 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ node_modules/
dist/ dist/
coverage/ coverage/
yarn-error.log yarn-error.log
.DS_Store

View file

@ -1,6 +1,6 @@
{ {
"name": "random-bunny", "name": "random-bunny",
"version": "2.1.3", "version": "2.1.4",
"description": "Get a random subreddit image url", "description": "Get a random subreddit image url",
"license": "MIT", "license": "MIT",
"author": "Vylpes", "author": "Vylpes",
@ -38,11 +38,11 @@
"@types/jest": "^29.4.0", "@types/jest": "^29.4.0",
"@types/node": "^20.0.0", "@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^5.54.0", "@typescript-eslint/eslint-plugin": "^5.54.0",
"@typescript-eslint/parser": "^6.0.0", "@typescript-eslint/parser": "^5.54.0",
"eslint": "^8.35.0", "eslint": "^8.49.0",
"jest": "^29.4.3", "jest": "^29.4.3",
"jest-mock-extended": "^3.0.3", "jest-mock-extended": "^3.0.3",
"np": "^7.7.0", "np": "^8.0.0",
"ts-jest": "^29.0.5", "ts-jest": "^29.0.5",
"ts-mockito": "^2.6.1", "ts-mockito": "^2.6.1",
"ts-node": "^10.9.1", "ts-node": "^10.9.1",

View file

@ -0,0 +1,6 @@
export enum ErrorCode {
Unknown,
FailedToFetchReddit,
UnableToParseJSON,
NoImageResultsFound,
}

View file

@ -0,0 +1,5 @@
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";
}

6
src/contracts/IError.ts Normal file
View file

@ -0,0 +1,6 @@
import { ErrorCode } from "../constants/ErrorCode";
export default interface IError {
Code: ErrorCode;
Message: string;
}

View file

@ -1,6 +1,8 @@
import IError from "./IError.js";
import IRedditResult from "./IRedditResult.js"; import IRedditResult from "./IRedditResult.js";
export default interface IReturnResult { export default interface IReturnResult {
IsSuccess: boolean; IsSuccess: boolean;
Result?: IRedditResult; Result?: IRedditResult;
Error?: IError;
} }

View file

@ -1,8 +1,10 @@
import IReturnResult from "./contracts/IReturnResult.js"; import IReturnResult from "./contracts/IReturnResult";
import IRedditResult from "./contracts/IRedditResult.js"; import IRedditResult from "./contracts/IRedditResult";
import fetch from "got-cjs"; import fetch from "got-cjs";
import { List } from 'linqts'; import { List } from 'linqts';
import IFetchResult from "./contracts/IFetchResult.js"; import IFetchResult from "./contracts/IFetchResult";
import { ErrorCode } from "./constants/ErrorCode";
import ErrorMessages from "./constants/ErrorMessages";
const sortable = [ const sortable = [
'new', 'new',
@ -10,14 +12,18 @@ const sortable = [
'top' 'top'
]; ];
export default async function randomBunny(subreddit: string, sortBy?: string): Promise<IReturnResult> { export default async function randomBunny(subreddit: string, sortBy: string = 'hot'): Promise<IReturnResult> {
if (!sortBy || !sortable.includes(sortBy)) sortBy = 'hot'; if (!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`);
if (!result) { if (!result) {
return { return {
IsSuccess: false IsSuccess: false,
Error: {
Code: ErrorCode.FailedToFetchReddit,
Message: ErrorMessages.FailedToFetchReddit,
},
} }
} }
@ -25,7 +31,11 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
if (!json) { if (!json) {
return { return {
IsSuccess: false IsSuccess: false,
Error: {
Code: ErrorCode.UnableToParseJSON,
Message: ErrorMessages.UnableToParseJSON,
},
} }
} }
@ -40,6 +50,10 @@ export default async function randomBunny(subreddit: string, sortBy?: string): P
if (dataWithImages.length == 0) { if (dataWithImages.length == 0) {
return { return {
IsSuccess: false, IsSuccess: false,
Error: {
Code: ErrorCode.NoImageResultsFound,
Message: ErrorMessages.NoImageResultsFound,
},
}; };
} else { } else {
random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1) random = Math.floor((Math.random() * (dataWithImages.length - 1)) + 0); // Between 0 and (size - 1)

View file

@ -1,3 +1,5 @@
import { ErrorCode } from "../src/constants/ErrorCode";
import ErrorMessages from "../src/constants/ErrorMessages";
import randomBunny from "../src/index"; import randomBunny from "../src/index";
import fetch from "got-cjs"; import fetch from "got-cjs";
@ -32,6 +34,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -63,6 +66,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
}); });
@ -94,6 +98,7 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeTruthy(); expect(result.IsSuccess).toBeTruthy();
expect(result.Result).toBeDefined(); expect(result.Result).toBeDefined();
expect(result.Error).toBeUndefined();
expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/hot.json');
}); });
@ -105,6 +110,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -118,6 +127,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -135,6 +148,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });
@ -166,6 +183,10 @@ describe('randomBunny', () => {
expect(result.IsSuccess).toBeFalsy(); expect(result.IsSuccess).toBeFalsy();
expect(result.Result).toBeUndefined(); 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'); expect(fetchMock).toBeCalledWith('https://reddit.com/r/rabbits/new.json');
}); });

1769
yarn.lock

File diff suppressed because it is too large Load diff