-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
249 lines (231 loc) · 6.35 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const fetch = require("node-fetch");
const { createRemoteFileNode } = require(`gatsby-source-filesystem`);
const SELLABLE_NODE_TYPE = `SpreadshirtSellable`;
const PRODUCTTYPE_NODE_TYPE = `SpreadshirtProductType`;
const CURRENCY_NODE_TYPE = `SpreadshirtCurrency`;
fetchApi = async (apiKey, resource) => {
try {
const requestOptions = {
method: "GET",
headers: {
"User-Agent":
"Gatsby-source-spreadshirt/0.1 (Devine.be; simon.vanherweghe@howest.be)",
Authorization: `SprdAuth apiKey="${apiKey}"`,
},
redirect: "follow",
};
const response = await fetch(
`https://api.spreadshirt.net/api/v1/${resource}`,
requestOptions
);
if (!response.ok) {
// NOT res.status >= 200 && res.status < 300
console.log(response.statusText);
throw new Error({
statusCode: response.status,
body: response.statusText,
});
}
return await response.json();
} catch (error) {
console.log(error);
return error;
}
};
getAllSellables = async (apiKey, shopId, locale) =>
await fetchApi(
apiKey,
`shops/${shopId}/sellables?page=0&mediaType=json&locale=${locale}`
);
getProductType = async (apiKey, shopId, locale, id) =>
await fetchApi(
apiKey,
`shops/${shopId}/productTypes/${id}?page=0&mediaType=json&locale=${locale}`
);
getCurrency = async (apiKey, id) =>
await fetchApi(apiKey, `currencies/${id}?mediaType=json`);
exports.onPreInit = () => console.log("Loaded gatsby-source-spreadshirt");
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
createTypes(`
type Price implements Node {
amount: Float!
currency: ${CURRENCY_NODE_TYPE} @link(from: "price.currencyId" by: "currencyId" )
}
type Appearance {
id: String!
name: String
colors: [Color]
printTypes: [PrintType]
resources: [Resource]
}
type Color {
index: Int!
value: String!
}
type PrintType {
href: String!
id: String!
}
type Resource {
mediaType: String!
href: String!
type: String
}
type Size {
id: String!
name: String
group: String
weight: Float
measures: [Measure]
}
type Measure {
name: String!
value: MeasureValue!
}
type MeasureValue {
value: Int!
unit: String!
}
type PreviewImage {
url: String!
type: String!
}
type StockState {
available: Boolean!
quantity: Int!
appearance: Appearance
size: Size
}
type ${SELLABLE_NODE_TYPE} implements Node {
id: ID!
sellableId: String!
name: String!
slug: String!
productType: ${PRODUCTTYPE_NODE_TYPE} @link(from: "productTypeId" by: "productTypeId" )
price: Price!
remoteImage: File @link
appearanceIds: [String!]
defaultAppearanceId: String!
}
type ${PRODUCTTYPE_NODE_TYPE} implements Node {
id: ID!
name: String!
shortDescription: String!
description: String!
sizeFitHint: String!
appearances: [Appearance!]
sizes: [Size!]
stockStates: [StockState!]
remoteImage: File @link
}
type ${CURRENCY_NODE_TYPE} implements Node {
id: ID!
}`);
};
exports.sourceNodes = async (
{ actions, createContentDigest, createNodeId, getNodesByType },
pluginOptions
) => {
const { createNode } = actions;
const { shopId, apiKey, locale } = pluginOptions;
const productTypeIds = new Set();
const currencyIds = new Set();
const sellables = await getAllSellables(apiKey, shopId, locale);
if (sellables.sellables) {
sellables.sellables.forEach((sellable) => {
productTypeIds.add(sellable.productTypeId);
currencyIds.add(sellable.price.currencyId);
createNode({
...sellable,
id: createNodeId(`${SELLABLE_NODE_TYPE}-${sellable.sellableId}`),
parent: null,
children: [],
internal: {
type: SELLABLE_NODE_TYPE,
content: JSON.stringify(sellable),
contentDigest: createContentDigest(sellable),
},
});
});
console.info(`Sellables created: ${sellables.sellables.length}`);
}
const productTypes = await Promise.all(
Array.from(productTypeIds).map(async (productTypeId) => {
return await getProductType(apiKey, shopId, locale, productTypeId);
})
);
productTypes.forEach((productType) => {
createNode({
...productType,
id: createNodeId(`${PRODUCTTYPE_NODE_TYPE}-${productType.id}`),
productTypeId: productType.id,
parent: null,
children: [],
internal: {
type: PRODUCTTYPE_NODE_TYPE,
content: JSON.stringify(productType),
contentDigest: createContentDigest(productType),
},
});
});
console.info(`ProductTypes created: ${productTypes.length}`);
const currencies = await Promise.all(
Array.from(currencyIds).map(async (currencyId) => {
return await getCurrency(apiKey, currencyId);
})
);
currencies.forEach((currency) => {
createNode({
...currency,
id: createNodeId(`${CURRENCY_NODE_TYPE}-${currency.id}`),
currencyId: currency.id,
parent: null,
children: [],
internal: {
type: CURRENCY_NODE_TYPE,
content: JSON.stringify(currency),
contentDigest: createContentDigest(currency),
},
});
});
console.info(`Currencies created: ${currencies.length}`);
return;
};
exports.onCreateNode = async ({
node,
actions: { createNode },
createNodeId,
getCache,
}) => {
if (node.internal.type === SELLABLE_NODE_TYPE) {
const fileNode = await createRemoteFileNode({
// the url of the remote image to generate a node for
url: node.previewImage.url,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
});
if (fileNode) {
node.remoteImage = fileNode.id;
}
}
if (node.internal.type === PRODUCTTYPE_NODE_TYPE) {
const sizes = node.resources.filter((res) => res.type === "size");
if (sizes.length === 1) {
const url = sizes[0].href;
const fileNode = await createRemoteFileNode({
// the url of the remote image to generate a node for
url,
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
});
if (fileNode) {
node.remoteImage = fileNode.id;
}
}
}
};