-
Notifications
You must be signed in to change notification settings - Fork 2
/
gatsby-node.js
137 lines (122 loc) · 4.19 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
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
const { createFileNodeFromBuffer } = require(`gatsby-source-filesystem`);
const prestashop = require(`./src/prestashop`);
const webservice = require(`./src/client/webservice`);
const link = require(`./src/link`);
const pageParser = require(`./src/parser/page`);
const productParser = require(`./src/parser/product`);
exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object({
url: Joi.string().required().description(`The URL of the PrestaShop installation.`),
key: Joi.string().required().description(`The PrestaShop webservice key.`),
locale: Joi.string().description(`The locale "iso_code" of the language.`),
images: Joi.object({
download: Joi.boolean().description(`A value indicating whether download images from products.`).default(false),
format: Joi.object({
product: Joi.string().default('medium_default'),
category: Joi.string().default('category_default'),
manufacturer: Joi.string().default('manufacturer_default')
})
.description(`Image formats.`)
.default(),
extension: Joi.string().description(`Image extension.`).default('jpg')
})
.description(`Image settings.`)
.default()
});
};
exports.sourceNodes = async (
{ actions: { createNode }, reporter, createContentDigest, createNodeId },
pluginOptions
) => {
if (pluginOptions.url && pluginOptions.key) {
const client = webservice.create(pluginOptions);
const languages = await prestashop.languages(client, { filters: { iso_code: pluginOptions.locale } });
if (languages && languages.length > 0) {
client.defaults.params.language = languages[0].id;
reporter.success(`[gatsby-source-prestashop] Loaded "${languages[0].name}" language.`);
}
const products = await prestashop.products(client);
const pages = await prestashop.pages(client);
products.forEach((product) => {
const content = productParser.parse(product, pluginOptions);
createNode({
...content,
id: createNodeId(`PrestashopProduct-${content.id}`),
parent: null,
children: [],
imageData: {
productId: product.id,
imageId: product.id_default_image,
link: link.image.product(pluginOptions.url, {
id: product.id_default_image,
format: pluginOptions.images.format.product,
extension: pluginOptions.images.extension.toLowerCase()
})
},
internal: {
type: `PrestashopProduct`,
content: JSON.stringify(content),
contentDigest: createContentDigest(content)
}
});
});
pages.forEach((page) => {
const content = pageParser.parse(page);
createNode({
...content,
id: createNodeId(`PrestashopPage-${content.id}`),
parent: null,
children: [],
internal: {
type: `PrestashopPage`,
content: content.content,
contentDigest: createContentDigest(content)
}
});
});
}
};
exports.onCreateNode = async ({ node, actions: { createNode }, cache, createNodeId }, pluginOptions) => {
if (pluginOptions.images.download && node.internal.type === 'PrestashopProduct' && node.imageData !== null) {
if (node.imageData.imageId) {
const client = webservice.create(pluginOptions);
const buffer = await prestashop.image(client, node.imageData);
if (buffer !== null) {
const fileNode = await createFileNodeFromBuffer({
buffer,
cache,
createNode,
createNodeId,
name: node.imageData.imageId
});
if (fileNode) {
node.image___NODE = fileNode.id;
}
}
}
}
};
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type PrestashopPage implements Node {
id: ID!
title: String!
keywords: String
excerpt: String
slug: String!
content: String
}
type PrestashopProduct implements Node {
id: ID!
productId: Int
name: String!
excerpt: String
description: String
manufacturerName: String
manufacturerId: Int
link: String!
image: File @link(from: "image___NODE")
}
`);
};