diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index f844614..182d1ad 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -2,9 +2,9 @@ name: Node.js CI on: push: - branches: [master] + branches: [master, canary] pull_request: - branches: [master] + branches: [master, canary] jobs: build: diff --git a/.gitignore b/.gitignore index 8d056c8..c9b053d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .vscode/ node_modules/ +index.d.js \ No newline at end of file diff --git a/.npmignore b/.npmignore index fb95c3e..799a8c8 100644 --- a/.npmignore +++ b/.npmignore @@ -3,4 +3,5 @@ node_modules/ __tests__/ .deepsource.toml .github/ -package-lock.json \ No newline at end of file +package-lock.json +index.d.js \ No newline at end of file diff --git a/README.md b/README.md index cf5e4c4..fd32635 100644 --- a/README.md +++ b/README.md @@ -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)_ diff --git a/__tests__/getFact.js b/__tests__/getFact.js index ece663a..a05287e 100644 --- a/__tests__/getFact.js +++ b/__tests__/getFact.js @@ -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); +// }); diff --git a/index.d.ts b/index.d.ts index 55e07d5..63f3189 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,7 +1,8 @@ -interface AnimeFact { +interface AnimeFacts { _id: number; tags: string[]; fact: string; + length: number; } -export async function getFact(): Promise; \ No newline at end of file +export function getFact(): Promise; diff --git a/index.js b/index.js index e6a7693..1bb875b 100644 --- a/index.js +++ b/index.js @@ -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} + */ -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 + */ diff --git a/package-lock.json b/package-lock.json index c2a0c48..339c1f5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,18 +4,23 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "axios": { - "version": "0.21.1", - "resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz", - "integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==", + "centra": { + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/centra/-/centra-2.4.2.tgz", + "integrity": "sha512-f1RaP0V1HqVNEXfLfjNBthB2yy3KnSGnPCnOPCFLUk9e/Z4rNJ8nBaJNnghflnp88mi1IT8mfmW+HlMS1/H+bg==" + }, + "eventemitter3": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz", + "integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==" + }, + "phin": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/phin/-/phin-3.5.1.tgz", + "integrity": "sha512-jgFO28IaiWAl0xk+zmqVx7neKVokWKU8YTQC5QlB45SZnEE53LH2saqJIcyIV557VX3Gk+TdR4rwWTc3P83DSA==", "requires": { - "follow-redirects": "^1.10.0" + "centra": "^2.4.2" } - }, - "follow-redirects": { - "version": "1.13.2", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.2.tgz", - "integrity": "sha512-6mPTgLxYm3r6Bkkg0vNM0HTjfGrOEtsfbhagQvbxDEsEkpNhw582upBaoRZylzen6krEmxXJgt9Ju6HiI4O7BA==" } } } diff --git a/package.json b/package.json index 739d0f6..03634a9 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } }