UNPKG

791 BJavaScriptView Raw
1'use strict';
2
3const { config } = require('./package.json');
4const client = new (require('wp-api-client'))(config.baseUrl);
5
6const about = () => client.fetchPage(config.aboutPage);
7const count = () => client.countPosts({ categories: config.haikus });
8const fetchPosts = options => client.fetchPosts({ ...options, categories: config.haikus });
9
10module.exports = {
11 about,
12 count,
13 fetchLatest,
14 fetchPosts,
15 fetchRandom
16};
17
18async function fetchLatest() {
19 const { items: posts } = await fetchPosts({ pageSize: 1 });
20 return posts[0];
21}
22
23async function fetchRandom() {
24 const max = await count();
25 const offset = random(max);
26 const { items: posts } = await fetchPosts({ offset });
27 return posts[0];
28}
29
30function random(max) {
31 return Math.floor(Math.random() * Math.floor(max));
32}