-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgatsby-node.js
49 lines (42 loc) · 1.25 KB
/
gatsby-node.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
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
const filterProductSkus = (product, skus) => {
return skus.filter(sku => sku.product === product.id);
};
exports.createPages = async ({ actions: { createPage } }) => {
const allProducts = [];
const allSkus = [];
// Fetch all skus and add to sku list array
await stripe.skus.list({ limit: 100 }).autoPagingEach(item => {
allSkus.push(item);
});
// Fetch all products and add to product list
await stripe.products
.list({
limit: 100
})
.autoPagingEach(item => {
allProducts.push(item);
});
if (allProducts.length > 0) {
// Create a single product page as the frontpage
const product = allProducts[0];
const productSkus = filterProductSkus(product, allSkus);
console.log(product);
console.log("this is product on line 29 in gatsby-node 🦄 ");
createPage({
path: `/`,
component: require.resolve("./src/templates/product.js"),
context: {
product,
productSkus
}
});
if (allProducts.length > 1) {
console.warn(
"More than one product, time to create a different front page"
);
}
} else {
console.error("You need at least one product to build this site");
}
};