Skip to content
This repository has been archived by the owner on Jan 1, 2023. It is now read-only.

Commit

Permalink
Version 1.2.5
Browse files Browse the repository at this point in the history
* Change Of Endpoint of AnimalFact 
* Addition Of Image API
  • Loading branch information
BearTS authored Feb 7, 2021
1 parent 9f5b75d commit c44e492
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 39 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ tamako.joke()
Returns a fact of the mentioned animal

`name` can be any of the following:
| | | | | | | | | | | |
| - | - | - | - | - | - | - | - | - | - | - |
| dog | cat | panda | fox | birb | koala | kangaroo | racoon | elephant | giraffe | whale |
| | | | | | | | | | | | |
| - | - | - | - | - | - | - | - | - | - | - | - |
| bird | bunny | cat | dog | fox | giraffe | kangaroo | koala | panda | racoon | whale | elephant |

Example
```js
Expand All @@ -132,6 +132,23 @@ tamako.animalfact('dog')
.then(res => console.log(res))
```

## image(name)
Returns an image link

`name` can be any of the following:
| | | | | | | | |
| - | - | - | - | - | - | - | - |
| bird | dog | cat | dog | fox | koala | panda | redpanda |

Example
```js
const { TAMAKOAPI } = require('tamako-api');
const tamako = new TAMAKOAPI();

tamako.image('dog')
.then(res => console.log(res))
```

# Events
`error` - Returns an error if an error is returned from the API.
Example
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class API {
public pokemon(name: string): Promise<Object>;
public animequote(): Promise<String>;
public animalfact(name: string): Promise<Object>;
public image(category: string): Promise<Object>;
public image(name: string): Promise<Object>;
public joke(): Promise<String>;
public on(error): error
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tamako-api",
"version": "1.2.0",
"version": "1.2.5",
"description": "A package for using the Tamako API Easily!",
"main": "index.js",
"scripts": {
Expand Down
93 changes: 59 additions & 34 deletions src/tamakoapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const base = 'http://api.tamako.tech/api'
const fetch = require("node-fetch");
let token;
const EventEmitter = require("events");
class TAMAKOAPI extends EventEmitter{
constructor(options = {}){
class TAMAKOAPI extends EventEmitter {
constructor(options = {}) {
super();

if (typeof options !== 'object'){
throw new err(`TAMAKOAPI: Expected object, received ${typeof(options)}`);
if (typeof options !== 'object') {
throw new err(`TAMAKOAPI: Expected object, received ${typeof(options)}`);
};

this.username = options.username;
Expand All @@ -26,15 +26,19 @@ class TAMAKOAPI extends EventEmitter{
* @param {string} user User id who triggered the chatbot
*/

async chatbot(message, name='Tamako', gender='female', user='123456', prefix='Not Set by Developer', dev='Bear#3437'){
if(!message){
async chatbot(message, name = 'Tamako', gender = 'female', user = '123456', prefix = 'Not Set by Developer', dev = 'Bear#3437') {
if (!message) {
throw new err("No message was provided");
};

const param = { name, gender, user };
const param = {
name,
gender,
user
};

for (const [key, iter] of Object.entries(param)){
if (typeof iter !== 'string'){
for (const [key, iter] of Object.entries(param)) {
if (typeof iter !== 'string') {
throw new err(`Expected ${iter} to be of type string, received ${typeof(iter)}`);
} else {
param[key] = encodeURIComponent(iter);
Expand All @@ -47,14 +51,14 @@ class TAMAKOAPI extends EventEmitter{

const res = await fetch(`${base}/chat?username=${username}&appid=${appid}&appsecret=${secret}&name=${param.name}&gender=${param.gender}&prefix=${param.prefix}&dev=${param.dev}&user=${param.user}&message=${message}`);

if (res.status === 401){
if (res.status === 401) {
this.emit('error', 'Invalid API key was provided');
return undefined;
};

const response = await res.json();

if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
}
Expand All @@ -71,76 +75,97 @@ class TAMAKOAPI extends EventEmitter{
* @returns {animequote.quote} string An anime quote
* @returns {animequote.api} object An API object
*/
async animequote(){
async animequote() {

const res = await fetch(`${base}/anime-quote`);
if(res.status == 401){
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
}
const response = await res.json();
if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
}
return response;
};


/**Returns a fact about animal
/**Returns a fact about animal
* @name animalfact
* @param {string} name The animal to query
* @returns {string} facts Fact about the animal
*/
async animalfact(name){
const res = await fetch(`${base}/animalfact?name=${encodeURIComponent(name)}`);
if(res.status == 401){
async animalfact(name) {
const res = await fetch(`${base}/animalfact/${encodeURIComponent(name)}`);
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
}

const response = await res.json();
if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
}
return response.fact;
};


/**Returns an image
* @name animalfact
* @param {string} name Type to query
* @returns {string} url URL link to the type of image
*/
async image(name) {
const res = await fetch(`${base}/image/${encodeURIComponent(name)}`);
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
}

const response = await res.json();
if (response.error) {
this.emit('error', response.error);
return undefined;
}
return response.url;
};


/**Returns a joke
* @name joke
* @returns {string} Joke
*/
async joke(){
* @name joke
* @returns {string} Joke
*/
async joke() {
const res = await fetch(`${base}/joke`);
if(res.status == 401){
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
};
const response = await res.json();
if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
};

return response.joke;
};

/**Returns lyrics of a song
/**Returns lyrics of a song
* @name lyrics
* @param {string} lyrics Title of the song
* @returns {string} lyrics of the song
*/
async lyrics(query){
if(!query) throw new err("No query was provided to search");
async lyrics(query) {
if (!query) throw new err("No query was provided to search");
const res = await fetch(`${base}/lyrics?name=${encodeURIComponent(query)}`);
if(res.status == 401){
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
}
const response = await res.json();
if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
}
Expand All @@ -151,15 +176,15 @@ class TAMAKOAPI extends EventEmitter{
*
* @param {string} name Name of the pokemon
*/
async pokemon(query){
if(!query) throw new err("No query was provided to search");
async pokemon(query) {
if (!query) throw new err("No query was provided to search");
const res = await fetch(`${base}/pokedex?pokemon=${encodeURIComponent(query.toLowerCase())}`);
if(res.status == 401){
if (res.status == 401) {
this.emit("error", "Check With Bear#3437");
return undefined;
}
const response = await res.json();
if(response.error) {
if (response.error) {
this.emit('error', response.error);
return undefined;
}
Expand Down

0 comments on commit c44e492

Please sign in to comment.