From c44e492efa8be6cf323bb3545f7ac8c29aada5b4 Mon Sep 17 00:00:00 2001 From: BearTS Date: Mon, 8 Feb 2021 00:56:35 +0530 Subject: [PATCH] Version 1.2.5 * Change Of Endpoint of AnimalFact * Addition Of Image API --- README.md | 23 ++++++++++-- index.d.ts | 2 +- package.json | 2 +- src/tamakoapi.js | 93 ++++++++++++++++++++++++++++++------------------ 4 files changed, 81 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index ca39c86..8d513fa 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.d.ts b/index.d.ts index 8b8809b..d7d6091 100644 --- a/index.d.ts +++ b/index.d.ts @@ -6,7 +6,7 @@ export class API { public pokemon(name: string): Promise; public animequote(): Promise; public animalfact(name: string): Promise; - public image(category: string): Promise; + public image(name: string): Promise; public joke(): Promise; public on(error): error } diff --git a/package.json b/package.json index d43d8af..0b91c96 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/tamakoapi.js b/src/tamakoapi.js index 50cd657..5520446 100644 --- a/src/tamakoapi.js +++ b/src/tamakoapi.js @@ -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; @@ -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); @@ -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; } @@ -71,15 +75,15 @@ 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; } @@ -87,20 +91,20 @@ class TAMAKOAPI extends EventEmitter{ }; - /**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; } @@ -108,18 +112,39 @@ class TAMAKOAPI extends EventEmitter{ }; + /**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; }; @@ -127,20 +152,20 @@ class TAMAKOAPI extends EventEmitter{ 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; } @@ -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; }