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
Changes from 1 commit
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
21 changes: 17 additions & 4 deletions lib/WaifuPics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ const API_URL = 'https://waifu.pics/api'
* @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(parseResponse)
function request (method, endpoint, options = {}) {
return fetch(`${API_URL}${!endpoint.startsWith('/') ? `/${endpoint}` : endpoint}`, {
method,
...options,
headers: {
'Content-Type': 'application/json'
}
})
.then(parseResponse)
}

async function parseResponse (response) {
Expand All @@ -27,14 +34,20 @@ const WaifuPics = {
*/
endpoints () {
return request('GET', '/endpoints')
}
},

many: {}
}

Object.keys(endpoints).forEach((type) => {
WaifuPics[type] = {}
WaifuPics.many[type] = {}

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

WaifuPics[type][category] = () => request('GET', endpoint)
WaifuPics.many[type][category] = (exclude) => request('POST', '/many' + endpoint, { body: JSON.stringify({ exclude }) })
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the best so far, maybe it will improve further.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if exclude images is effective, I tried to eliminate a large scale of images to see where we were going but it seems to be large enough to always return others

})

Expand Down