Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

And there we go... again! #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 29 additions & 24 deletions lib/Endpoints.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
module.exports = [
'sfw',
'nsfw',
'neko',
'lewdneko',
'shinobu',
'trap',
'bully',
'cry',
'hug',
'kiss',
'lick',
'pat',
'smug',
'highfive',
'nom',
'bite',
'slap',
'wink',
'poke',
'dance',
'cringe',
'blush'
]
module.exports = {
sfw: [
'waifu',
'neko',
'shinobu',
'bully',
'cry',
'hug',
'kiss',
'lick',
'pat',
'smug',
'highfive',
'nom',
'bite',
'slap',
'wink',
'poke',
'dance',
'cringe',
'blush'
],
nsfw: [
'waifu',
'neko',
'trap',
'blowjob'
]
}
40 changes: 16 additions & 24 deletions lib/WaifuPics.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,31 @@
const fetch = require('node-fetch')
const ENDPOINTS = require('./Endpoints.js')
const endpoints = require('./Endpoints.js')

const API_URL = 'https://waifu.pics/api'

async function parseResponse (response) {
if (!response.ok) {
const failure = await response.text()

throw new Error(failure)
}

return response.json()
/**
* @param {string} endpoint An endpoint contained in {@link https://waifu.pics/api/endpoints}
* @returns {Promise}
*/
function _request (endpoint) {
return fetch(`${API_URL}${!endpoint.startsWith('/') ? `/${endpoint}` : endpoint}`).then((response) => response.json())
}

const WaifuPics = {
/**
* @param {string} endpoint An endpoint contained in {@link https://waifu.pics/api/endpoints}
* @returns {Promise}
*/
fetch (endpoint) {
return fetch(`${API_URL}${!endpoint.startsWith('/') ? `/${endpoint}` : endpoint}`).then(parseResponse)
},

/**
* @returns {Promise<string[]>}
* @returns {Promise<Object>}
*/
endpoints () {
return this.fetch('/endpoints')
return _request('/endpoints')
}
}

for (let endpoint of ENDPOINTS) {
WaifuPics[endpoint] = function () {
return this.fetch(endpoint)
}
}
Object.keys(endpoints).forEach((type) => {
WaifuPics[type] = {}

endpoints[type].forEach((category) => {
WaifuPics[type][category] = () => _request(`/${type}/${category}`)
})
})

module.exports = WaifuPics