-
Notifications
You must be signed in to change notification settings - Fork 1
/
gridsome.server.js
144 lines (130 loc) · 3.86 KB
/
gridsome.server.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/
// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`
const Butter = require('buttercms')
const { isArray } = require('util')
let butterCMS = undefined
try {
const apiKey = process.env.GRIDSOME_APP_BUTTER_CMS_API_KEY
const preview = process.env.GRIDSOME_APP_BUTTER_CMS_PREVIEW !== 'false'
butterCMS = Butter(apiKey, preview)
} catch (error) {
// eslint-disable-next-line no-console
console.error('Error while getting Butter CMS:', error)
}
function createCollection(addCollection, typeName, data) {
if (!data) {
return
}
const collection = addCollection({
typeName: typeName,
})
if (isArray(data)) {
data.forEach((value) => {
collection.addNode(value)
})
return
}
collection.addNode(data)
}
module.exports = function (api) {
api.loadSource(async ({ addCollection }) => {
const errorCollection = addCollection({
typeName: 'Error',
})
await butterCMS.content
.retrieve(['navigation_menu'])
.then((res) => {
const data = res.data?.data?.navigation_menu[0]?.menu_items?.map(
(item, index) => {
return {
index,
...item,
}
}
)
createCollection(addCollection, 'MenuItems', data)
})
.catch((e) => {
errorCollection.addNode({ errorMessage: e.message })
createCollection(addCollection, 'MenuItems', {
label: '',
url: '',
})
})
await butterCMS.page
.retrieve('landing-page', 'landing-page-with-components')
.then((res) => {
createCollection(addCollection, 'HomePageData', res.data.data)
})
.catch((e) => {
errorCollection.addNode({ errorMessage: e.message })
createCollection(addCollection, 'HomePageData', {
fields: {
seo: {
title: '',
description: '',
},
body: {
type: '',
fields: {
headline: '',
subheadline: '',
image: '',
button_label: '',
button_url: '',
scroll_anchor_id: '',
image_position: '',
features: {
headline: '',
description: '',
icon: '',
},
testimonial: {
quote: '',
name: '',
title: '',
},
},
},
},
})
})
await butterCMS.post
.list()
.then((res) => {
const data = res.data?.data?.map((blog) => {
// We need to do this because GraphQL won't create the field featured_image if it's null and therefor we can't query it
if (!blog.featured_image) {
blog.featured_image = ''
}
return blog
})
createCollection(addCollection, 'BlogPosts', data)
})
.catch((e) => {
errorCollection.addNode({ errorMessage: e.message })
createCollection(addCollection, 'BlogPosts', {
title: '',
slug: '',
summary: '',
featured_image_alt: '',
featured_image: '',
})
})
await butterCMS.category
.list()
.then((res) => {
createCollection(addCollection, 'Categories', res?.data?.data)
})
.catch((e) => {
errorCollection.addNode({ errorMessage: e.message })
createCollection(addCollection, 'Categories', { name: '', slug: '' })
})
})
api.createPages(({ createPage }) => {
// Use the Pages API here: https://gridsome.org/docs/pages-api/
})
}