From cf35d2f14434018d6811a9a820df7d601dbaa3eb Mon Sep 17 00:00:00 2001 From: notkyoyo Date: Sun, 25 Apr 2021 18:13:22 +0530 Subject: [PATCH] Changed the code base to match with the new version of API --- README.md | 19 +++--- __tests__/getFact.js | 12 ++-- index.js | 141 ++++++++++++++++++++++++------------------- package-lock.json | 2 +- package.json | 2 +- 5 files changed, 101 insertions(+), 75 deletions(-) diff --git a/README.md b/README.md index a8c1b63..fd88a13 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Need support? _[Click here](https://discord.gg/yyW389c)_ +### First make sure you have a [Animu](https://animu.ml) API Token. Join _[this](https://discord.gg/yyW389c)_ discord server to get one. + ## Installation: ### NPM @@ -30,14 +32,17 @@ $ yarn add anime-facts ```javascript -const { getFact } = require("anime-facts"); -getFact().then((fact) => console.log(fact)); +const AnimeFact =require("anime-facts"); +const api = new AnimeFact("YOUR TOKEN"); + +api.getFact().then((res) => console.log(res)); ``` ### Using Query: ```javascript -const { getFact } = require("anime-facts"); +const AnimeFact =require("anime-facts"); +const api = new AnimeFact("YOUR TOKEN"); // 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)); +api.getFact(null, 1, 100).then((res) => console.log(res)); // Returns with: { @@ -49,9 +54,9 @@ getFact(null, 1, 100).then((fact) => console.log(fact)); ``` ## Functions -| **Functions** | **Description** | **Usage** | -| :-----------: | ---------------------------- | ------------------ | -| getFact | Generate random anime facts. | `random.getFact()` | +| **Functions** | **Description** | +| :-----------: | ---------------------------- | +| getFact | Generate random anime facts. | ## Credits [@LamkasDev](https://github.com/LamkasDev) for adding facts to prior database. _PR [#1](https://github.com/notkyoyo/anime-facts/pull/1)_\ diff --git a/__tests__/getFact.js b/__tests__/getFact.js index a05287e..60ba9e4 100644 --- a/__tests__/getFact.js +++ b/__tests__/getFact.js @@ -1,5 +1,7 @@ -const { getFact } = require("../index"); -getFact().then((res) => console.log(res)); -// .catch((err) => { -// console.log(err); -// }); +const AnimeFact = require("../index"); +const api = new AnimeFact("YOUR TOKEN"); + +api.getFact().then((res) => console.log(res)); +// if successful: { body } + +// if failed, it will throw an error diff --git a/index.js b/index.js index 1bb875b..4ed60b3 100644 --- a/index.js +++ b/index.js @@ -1,71 +1,90 @@ +const EventEmitter = require("events"); const fetch = require("phin"); -/** - * 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} - */ +class AnimeFact extends EventEmitter { + /** + * The main class + * @param {token} [auth] - the authorization token + */ + constructor(token) { + super(); -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; + this._auth = token; } - 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, - }; + + /** + * 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} + */ + + async getFact(tags, minLength, maxLength) { + const auth = this._auth; + if (!auth) throw new TypeError("Missing authorization token"); + 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}`, + headers: { + Auth: auth, + "Content-Type": "application/json", + }, + parse: "json", }) - .catch((err) => { - throw err; - }); + .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 }; +module.exports = AnimeFact; /** * @typedef {object} Fact diff --git a/package-lock.json b/package-lock.json index 608912b..c59fdee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "anime-facts", - "version": "3.2.8", + "version": "4.2.8", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 7fd49ce..d800496 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "anime-facts", - "version": "3.2.8", + "version": "4.2.8", "description": "Generate random anime facts.", "main": "index.js", "types": "index.d.ts",