paheal
Version:
Simple and user-friendly Wrapper of Rule 34 Paheal
80 lines (64 loc) • 2.12 kB
JavaScript
const pahealService = require('./pahealService')
const utils = new pahealService()
class searchResult {
constructor(medias, url, pagesCount) {
this.results = medias
this.lastPage = pagesCount
this.currentPage = 1;
this.#url = url.slice(0, -1)
}
#url
async #returner() {
const html = await fetch(this.#url + this.currentPage).then(res => res.text())
this.results = utils.parseSearch(html).results
return this
}
/**
* **Method return a Promise, use .then(), or variable**
* @returns {Promise<this>} Next Page of Search
*/
next() {
if (this.currentPage == this.lastPage) return this.first()
this.currentPage++
return this.#returner()
}
/**
* **Method return a Promise, use .then(), or variable**
* @returns {Promise<this>} Previous Page of Search
*/
prev() {
if (this.currentPage == 1) return this.last()
this.currentPage--
return this.#returner()
}
/**
* **Method return a Promise, use .then(), or variable**
* @returns {Promise<this>} First Page of Search
*/
first() {
this.currentPage = 1
return this.#returner()
}
/**
* **Method return a Promise, use .then(), or variable**
* @returns {Promise<this>} Last Page of Search
*/
last() {
this.currentPage = this.lastPage
return this.#returner()
}
/**
* **Method return a Promise, use .then(), or variable**
* @param {number} page
* @returns {Promise<this>} indicated page
*/
getPage(page) {
page = Math.abs(page)
if (typeof page != 'number') return { status: 400, message: 'You entered uncorrect query params for searching', results: 'Check message property.'}
if (page == 0) return this.first()
if (page > this.lastPage) return this.last()
this.currentPage = page
return this.#returner()
}
}
module.exports = searchResult;