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