better wall getter

This commit is contained in:
Daniel Lugo 2020-06-25 11:48:05 -04:00
parent 0cc581c520
commit 2e516eeeef

View file

@ -22,23 +22,24 @@ const getWallTotalPages = async () => {
} }
/** /**
* Won't fail if given an invalid page, will return an empty set.
* @param {number} page * @param {number} page
* @returns {Promise<Common.SchemaTypes.WallPage>} * @returns {Promise<Common.SchemaTypes.WallPage>}
*/ */
const getWallPage = async page => { const getWallPage = async page => {
const totalPages = await getWallTotalPages() const totalPages = await getWallTotalPages()
const empty = {
count: 0,
posts: {}
}
if (page === 0 || totalPages === 0) { if (page === 0 || totalPages === 0) {
return empty return {
count: 0,
posts: {}
}
} }
const actualPageIdx = page < 0 ? totalPages + (page + 1) : page - 1 const actualPageIdx = page < 0 ? totalPages + (page + 1) : page - 1
/**
* @type {Common.SchemaTypes.WallPage}
*/
const thePage = await Utils.tryAndWait( const thePage = await Utils.tryAndWait(
(_, user) => (_, user) =>
new Promise(res => { new Promise(res => {
@ -46,6 +47,7 @@ const getWallPage = async page => {
.get(Key.WALL) .get(Key.WALL)
.get(Key.PAGES) .get(Key.PAGES)
.get(actualPageIdx.toString()) .get(actualPageIdx.toString())
// @ts-ignore
.load(res) .load(res)
}), }),
v => typeof v !== 'object' v => typeof v !== 'object'
@ -62,7 +64,13 @@ const getWallPage = async page => {
} }
}) })
return Common.Schema.isWallPage(clean) ? clean : empty if (!Common.Schema.isWallPage(clean)) {
throw new Error(
`Fetched page not a wall page, instead got: ${JSON.stringify(clean)}`
)
}
return clean
} }
module.exports = { module.exports = {