-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
34 lines (25 loc) · 953 Bytes
/
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
const Commerce = require('@chec/commerce.js')
const CommercejsPlugin = async (api, options) => {
if (!options.publicKey)
throw new Error('gridsome-source-commercejs: Missing Public Key')
const commerce = new Commerce(options.publicKey)
api.loadSource(async actions => {
console.log('🛒 Fetching products from Commerce.js...')
const { data: products } = await commerce.products.list()
const { data: categories } = await commerce.categories.list()
const productCollection = actions.addCollection({
typeName: 'CommercejsProducts',
})
const categoriesCollection = actions.addCollection({
typeName: 'CommercejsCategories',
})
for (const item of products) {
productCollection.addNode(item)
}
for (const item of categories) {
categoriesCollection.addNode(item)
}
console.log('✔️ Done fetching products from Commerce.js!')
})
}
module.exports = CommercejsPlugin