Skip to content

Commit

Permalink
Merge pull request #12 from notkyoyo/canary
Browse files Browse the repository at this point in the history
Changed the code base to match with the new version of API
  • Loading branch information
NotKyoyo committed Apr 25, 2021
2 parents cad34c5 + cf35d2f commit 658d114
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 75 deletions.
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
{
Expand All @@ -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)_\
Expand Down
12 changes: 7 additions & 5 deletions __tests__/getFact.js
Original file line number Diff line number Diff line change
@@ -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
141 changes: 80 additions & 61 deletions index.js
Original file line number Diff line number Diff line change
@@ -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<Fact>}
*/
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<Fact>}
*/

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
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit 658d114

Please sign in to comment.