Skip to content

Commit

Permalink
Merge pull request #6 from notkyoyo/canary
Browse files Browse the repository at this point in the history
Added Query Support, changed packages, and so on.
  • Loading branch information
NotKyoyo authored Apr 13, 2021
2 parents df94423 + d7048dd commit 65e8427
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: Node.js CI

on:
push:
branches: [master]
branches: [master, canary]
pull_request:
branches: [master]
branches: [master, canary]

jobs:
build:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
node_modules/
index.d.js
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
__tests__/
.deepsource.toml
.github/
package-lock.json
package-lock.json
index.d.js
50 changes: 42 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,60 @@

Need support? _[Click here](https://discord.gg/yyW389c)_

## Installation
## Installation:

#

### NPM

```bash
$ npm install anime-facts
```

## Usage
### YARN

```bash
$ yarn add anime-facts
```

### Query parameters _[ OPTIONAL ]_
#
| Parameters | Type | Description |
| :-------- | :------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| tags | `String` | Filter random fact by tag(s). Takes a list of one or more tag names, separated by a comma (meaning `AND`) or a pipe (meaning `OR`). A comma separated list will match facts that have **_all_** of the given tags. While a pipe (`\|`) separated list will match facts that have **_either_** of the provided tags.| |
| minLength | `Int` | The minimum Length in characters ( can be combined with `maxLength` ) | |
| maxLength | `Int` | The maximum Length in characters ( can be combined with `minLength` ) |

## Usage:
#

```javascript
const { getFact } = require("anime-facts");
getFact().then((fact) => console.log(fact));
```
### Using Query:
```javascript
const random = require("anime-facts");
random.getFact().then((fact) => console.log(fact));
const { getFact } = require("anime-facts");
// Note currently there are only fews tags, length available in the database. So, it might return the same data multiple times.
getFact(null, 1, 100).then((fact) => console.log(fact));

// Returns with:
{
id: 5,
tags: [ 'TEZUKA Osamu', 'Artist' ],
fact: 'TEZUKA Osamu is the most famous manga artist in Japan.',
length: 54
}
```

## Functions

#
| **Functions** | **Description** | **Usage** |
| :-----------: | ---------------------------- | ------------------ |
| getFact | Generate random anime facts. | `random.getFact()` |

## Credits

[@LamkasDev](https://github.com/LamkasDev) for adding facts to prior database.\
[@xMercyTheDeveloper](https://github.com/xMercyTheDeveloper) for adding a fact to prior database.
#
[@LamkasDev](https://github.com/LamkasDev) for adding facts to prior database. _PR [#1](https://github.com/notkyoyo/anime-facts/pull/1)_\
[@xMercyTheDeveloper](https://github.com/xMercyTheDeveloper) for adding a fact to prior database. _PR [#2](https://github.com/notkyoyo/anime-facts/pull/2)_\
[@Lioness100](https://github.com/Lioness100) for adding types and improving codes in version [2.2.6](https://www.npmjs.com/package/anime-facts/v/2.2.6). _PR [#3](https://github.com/notkyoyo/anime-facts/pull/3) and [#4](https://github.com/notkyoyo/anime-facts/pull/4)_
8 changes: 5 additions & 3 deletions __tests__/getFact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const random = require("../index");

random.getFact().then((r) => console.log(r));
const { getFact } = require("../index");
getFact().then((res) => console.log(res));
// .catch((err) => {
// console.log(err);
// });
5 changes: 3 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
interface AnimeFact {
interface AnimeFacts {
_id: number;
tags: string[];
fact: string;
length: number;
}

export async function getFact(): Promise<AnimeFact>;
export function getFact(): Promise<AnimeFacts>;
81 changes: 74 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
const axios = require("axios");
const fetch = require("phin");

const fact = {
getFact() {
return axios.get(`https://animu.ml/fact`).then((res) => res.data);
},
};
/**
* Gets a fact from the api
* @param {string[]} [tags] - the array of tags
* @param {number} [minLength] - the minLength for query
* @param {number} [maxLength] - the maxLength for query
* @returns {Promise<Fact>}
*/

module.exports = fact;
async function getFact(tags, minLength, maxLength) {
const params = {};
if (tags == undefined) {
params.tags = "";
} else {
params.tags = tags;
}
if (minLength == undefined) {
params.minLength = "";
} else {
params.minLength = minLength;
}
if (maxLength == undefined) {
params.maxLength = "";
} else {
params.maxLength = maxLength;
}
return fetch({
url: `https://animu.ml/fact?tags=${params.tags}&minLength=${params.minLength}&maxLength=${params.maxLength}`,
parse: "json",
})
.then((res) => {
if (res.statusCode !== 200)
switch (res.statusCode) {
case 404:
return {
statusCode: res.statusCode,
body: res.body,
error: "Could not find any fact",
};
break;
case 502:
return {
statusCode: res.statusCode,
body: res.body,
error: "Server down",
};
break;
default:
return {
statusCode: res.statusCode,
body: res.body,
error: "Unknown error",
};
}
return {
id: res.body._id,
tags: res.body.tags || [],
fact: res.body.fact,
length: res.body.length,
};
})
.catch((err) => {
throw err;
});
}

module.exports = { getFact };

/**
* @typedef {object} Fact
* @prop {number} id
* @prop {string[]} tags
* @prop {string} fact
* @prop {number} length
*/
25 changes: 15 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "anime-facts",
"version": "2.2.6",
"version": "3.2.6",
"description": "Generate random anime facts.",
"main": "index.js",
"types": "index.d.ts",
Expand All @@ -25,6 +25,7 @@
},
"homepage": "https://github.com/notkyoyo/anime-facts#readme",
"dependencies": {
"axios": "^0.21.1"
"eventemitter3": "^4.0.7",
"phin": "^3.5.1"
}
}

0 comments on commit 65e8427

Please sign in to comment.