Comparing version 1.2.1 to 1.2.2
@@ -1,1 +0,1 @@ | ||
{"version":2,"languages":{"nodejs-npm":{"specfileHash":"6f95d000219ce164f0b9bc6db6163c12","lockfileHash":"29d0959a841c08da52588f62aa98e2bc","guessedImportsHash":"5e056c500a1c4b6a7110b50d807bade5"}}} | ||
{"version":2,"languages":{"nodejs-npm":{"specfileHash":"8cd789dafd4814f8cdc97e6b118e6fbe","lockfileHash":"c36cafe14ff3c50ab66e083ea1cadd83","guessedImports":["node-fetch"],"guessedImportsHash":"51266e8e25b3d5e637d3474eb13a80d8"}}} |
89
index.js
@@ -1,52 +0,47 @@ | ||
const { request } = require('https'); | ||
const animals = ["cat", "dog", "bird", "panda", "redpanda", "koala", "fox", "whale", "kangaroo", "bunny"]; | ||
const fetch = async (path) => { | ||
return new Promise(resolve => { | ||
request({ | ||
method: 'GET', | ||
host: 'random-api.nitcord.repl.co', | ||
userAgent: null, | ||
headers: {}, | ||
path, | ||
}, res => { | ||
let data = ''; | ||
res.on('data', chunk => data += chunk); | ||
res.once('error', err => { throw new Error(err) }); | ||
res.once('end', () => { | ||
try { | ||
resolve(JSON.parse(data)); | ||
} catch { | ||
throw new Error('Cannot parse JSON data. Please try again later.'); | ||
} | ||
}); | ||
}).end(); | ||
}); | ||
} | ||
const fetch = require('node-fetch'); | ||
const animals = [ | ||
'cat', | ||
'dog', | ||
'bird', | ||
'panda', | ||
'redpanda', | ||
'koala', | ||
'fox', | ||
'whale', | ||
'kangaroo', | ||
'bunny' | ||
]; | ||
const base = 'https://random-api.nitcord.repl.co'; | ||
/** | ||
* @typedef {Object} APIResponse | ||
* @property {string} [name] The animal name. | ||
* @property {string} [image] The animal image URL. | ||
* @property {string} [fact] The animal fact. | ||
*/ | ||
module.exports = { | ||
/** | ||
* Does a GET request showing the image and fact of an animal. This can be specified or random. | ||
* @param {string} type The animal name, or 'random' for a random response. | ||
* @typedef {Object} [APIResponse] | ||
* @property {string} [APIResponse.name] The animal name. | ||
* @property {string} [APIResponse.image] The animal image URL. | ||
* @property {string} [APIResponse.fact] The animal fact. | ||
* @returns {APIResponse} The APIResponse as an object. | ||
*/ | ||
getAsync: async (type) => { | ||
if (!type || typeof type !== 'string' || (!animals.includes(type.toLowerCase()) && type.toLowerCase() !== 'random')) throw new TypeError(`Invalid type, type must be ${[...animals, 'random'].join(', ')}`); | ||
type = type.toLowerCase(); | ||
/** | ||
* Does a GET request showing the image and fact of an animal. This can be specified or random. | ||
* @param {string} [type='random'] The animal name, or 'random' for a random response. | ||
* @returns {APIResponse} The APIResponse as an object. | ||
*/ | ||
async getAsync(type = 'random') { | ||
if (typeof type !== 'string' || ((type = type.toLowerCase(), true) && !animals.includes(type) && type !== 'random')) throw new TypeError(`Invalid type, type must be ${animals.join(', ')}, random`); | ||
if (type === 'random') | ||
type = animals[Math.floor(Math.random() * animals.length)]; | ||
if (type === 'random') type = animals[Math.floor(Math.random() * animals.length)]; | ||
const image = await fetch(`/api/img/${type}`); | ||
const fact = await fetch(`/api/facts/${type}`); | ||
return { | ||
name: type, | ||
image: image.link, | ||
fact: fact.fact | ||
}; | ||
} | ||
const [{ link: image }, { fact }] = await Promise.all([ | ||
fetch(`${base}/api/img/${type}`).then(res => res.json()), | ||
fetch(`${base}/api/facts/${type}`).then(res => res.json()) | ||
]).catch(() => { | ||
throw new Error(`Failed to do a GET request to the '${base}' API`); | ||
}); | ||
return { | ||
name: type, | ||
image, | ||
fact | ||
}; | ||
} | ||
}; |
{ | ||
"name": "animality", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "A simple API wrapper that generates images & facts of any animal", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"dependencies": { | ||
"node-fetch": "^2.6.1" | ||
}, | ||
"devDependencies": {}, | ||
@@ -16,6 +18,6 @@ "scripts": { | ||
"keywords": [ | ||
"animal", | ||
"api", | ||
"random", | ||
"random-api" | ||
"animal", | ||
"api", | ||
"random", | ||
"random-api" | ||
], | ||
@@ -28,2 +30,2 @@ "author": "Hamburger", | ||
"homepage": "https://github.com/VeryHamburger/Animality#readme" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
2
4159
1
42
+ Addednode-fetch@^2.6.1
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)