Skip to content

Commit

Permalink
Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sefinek24 committed Oct 8, 2023
1 parent 1cf7469 commit 99cca17
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const circles = require('./data/emoji/circle.json');
const endpoints = require('./data/endpoints.json');
const { name, version } = require('./package.json');

/**
* Configuration options for HTTP requests.
* @type {Object}
*/
const options = {
method: 'GET',
port: 443,
Expand All @@ -27,6 +31,11 @@ const options = {
},
};

/**
* Fetch content from a given URL using HTTP GET.
* @param {string} url - The URL to fetch content from.
* @returns {Promise} - A Promise that resolves with the fetched JSON data or rejects with an error.
*/
function getContent(url) {
return new Promise((resolve, reject) => {
const req = get(url, options, res => {
Expand All @@ -36,8 +45,8 @@ function getContent(url) {
}

res.setEncoding('utf8');
let rawData = '';

let rawData = '';
res.on('data', (chunk) => {
rawData += chunk;
});
Expand All @@ -59,21 +68,69 @@ function getContent(url) {
});
}

/**
* SefinekAPI class for accessing various random content endpoints.
* @class
*/
class SefinekAPI {
constructor() {
Object.keys(endpoints).forEach(endpoint => {
/**
* Access a specific random content endpoint.
*
* @method
* @returns {Promise} - A Promise that resolves with the fetched content or rejects with an error.
*/
this[endpoint] = () => getContent(`https://api.sefinek.net/api/v2/random/${endpoints[endpoint]}`);
});
}
}

module.exports = {
/**
* Get a random Unicode emoji.
* @returns {string} - A random Unicode emoji.
*/
unicode: () => unicode[Math.floor(Math.random() * unicode.length)],

/**
* Get a random emoji.
* @returns {object} - A random emoji.
*/
emojis: () => emojis[Math.floor(Math.random() * emojis.length)],

/**
* Get a random cat emoji.
* @returns {object} - A random cat emoji.
*/
cats: () => cats[Math.floor(Math.random() * cats.length)],

/**
* Get a random heart emoji.
* @returns {object} - A random heart emoji.
*/
hearts: () => hearts[Math.floor(Math.random() * hearts.length)],

/**
* Get a random food emoji.
* @returns {object} - A random food emoji.
*/
foods: () => foods[Math.floor(Math.random() * foods.length)],

/**
* Get a random circle emoji.
* @returns {object} - A random circle emoji.
*/
circles: () => circles[Math.floor(Math.random() * circles.length)],

/**
* Access various random content endpoints using the SefinekAPI class.
*/
Kaomojis: SefinekAPI,

/**
* Get the module version.
* @returns {string} - Returns the package version.
*/
version,
};

0 comments on commit 99cca17

Please sign in to comment.