-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
109 lines (94 loc) · 3.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const camelCase = require('camelcase');
const FetchButterCMSData = require('./fetch');
class ButterSource {
static defaultOptions() {
return {
authToken:
process.env.GRIDSOME_BUTTER_AUTHTOKEN || process.env.BUTTER_AUTHTOKEN,
collections: [],
pageTypes: [],
typeName: 'Butter',
locales: [],
preview: false,
levels: 2,
page_size: 100
};
}
constructor(api, options = ButterSource.defaultOptions()) {
this.options = options;
this.api = api;
this.fetchButterCMSData = new FetchButterCMSData(this.options);
api.loadSource(async actions => {
const promises = [
this.createNodesForButterPosts(actions),
this.createNodesForButterPages(actions)
];
if (this.options.collections && this.options.collections.length) {
promises.push(this.createNodesForButterCollections(actions));
}
try {
await Promise.all(promises);
} catch (e) {
console.error('Failed to create all nodes.');
console.error(e);
}
});
}
/****************************************************
STEP ONE: Get all butter posts
****************************************************/
async createNodesForButterPosts(actions) {
const posts = await this.fetchButterCMSData.getBlogPosts();
const contentType = actions.addCollection({
typeName: this.createTypeName('posts')
});
posts.forEach(post => contentType.addNode(post));
}
/****************************************************
STEP TWO: Get all butter collections
****************************************************/
async createNodesForButterCollections(actions) {
const data = await this.fetchButterCMSData.getCollectionsData(
this.options.collections
);
const contentType = actions.addCollection({
typeName: this.createTypeName('collection')
});
contentType.addNode({
data
});
}
/****************************************************
STEP THREE: Get all butter pages
****************************************************/
createNodesForButterPages(actions) {
return Promise.all(
[...this.options.pageTypes, '*'].map(async pageType => {
const pages = await this.fetchButterCMSData.getPagesWithPageType(
pageType
);
const contentType = actions.addCollection({
typeName: this.createTypeName(pageType === '*' ? 'pages' : pageType)
});
pages.forEach(page => {
// remove `fields` as a key from page so it doesn't get added to node
// needs to be renamed to `data` for use
const { fields: data, ...pageData } = page;
// create node
// - by assigning `page_type` explicitly here, we overwrite `pageData.page_type`
contentType.addNode({
...pageData,
data,
page_type: page.page_type || '*'
});
});
})
);
}
createTypeName(typeName = '') {
return camelCase(`${this.options.typeName} ${typeName}`, {
pascalCase: true
});
}
}
module.exports = ButterSource;