mirror of
https://github.com/ThaUnknown/miru.git
synced 2026-04-05 23:49:44 +00:00
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
import { alRequest } from '@/modules/anilist.js'
|
|
|
|
export default class Sections {
|
|
constructor (data = []) {
|
|
this.sections = []
|
|
this.search = {}
|
|
this.add(data)
|
|
}
|
|
|
|
add (data) {
|
|
for (const { title, variables = {}, type, load = this.createFallbackLoad(variables, type), preview = load(1, 10) } of data) {
|
|
this.sections.push({ load, title, preview })
|
|
}
|
|
}
|
|
|
|
createFallbackLoad (variables, type) {
|
|
return (page = 1, perPage = 50) => {
|
|
const options = { method: 'Search', page, perPage, ...variables, ...this.sanitiseObject(this.search) }
|
|
const res = alRequest(options)
|
|
return this.wrapResponse(res, perPage, type)
|
|
}
|
|
}
|
|
|
|
wrapResponse (res, length, type) {
|
|
res.then(res => {
|
|
this.hasNext = res?.data?.Page.pageInfo.hasNextPage
|
|
})
|
|
return Array.from({ length }, (_, i) => ({ type, data: this.fromPending(res, i) }))
|
|
}
|
|
|
|
async fromPending (arr, i) {
|
|
const { data } = await arr
|
|
return data.Page.media[i]
|
|
}
|
|
|
|
sanitiseObject (object) {
|
|
const safe = {}
|
|
for (const [key, value] of Object.entries(object)) {
|
|
if (value) safe[key] = value
|
|
}
|
|
return safe
|
|
}
|
|
}
|