forked from cloudinary-community/gatsby-source-cloudinary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
57 lines (47 loc) · 1.79 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
const cloudinary = require('cloudinary')
exports.sourceNodes = ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions
delete configOptions.plugins
// Configure Cloudinary
cloudinary.config({
cloud_name: configOptions.cloudName,
api_key: configOptions.apiKey,
api_secret: configOptions.apiSecret
})
const processMedia = media => {
const nodeId = createNodeId(`cloudinary-media-${media.public_id}`)
const nodeContent = JSON.stringify(media)
const nodeData = Object.assign({}, media, {
id: nodeId,
parent: null,
children: [],
internal: {
type: `CloudinaryMedia`,
content: nodeContent,
contentDigest: createContentDigest(media),
},
})
return nodeData
}
const {resourceType, prefix, tags, maxResults, type} = configOptions
const queryParams = new Object;
if(!!resourceType){queryParams.resource_type = resourceType}
if(!!tags){queryParams.tags = tags}
if(!!maxResults){queryParams.max_results = maxResults}
if(!!type){queryParams.type = type}
if(!!prefix && !!type){queryParams.prefix = prefix}
return (
cloudinary.v2.api.resources(queryParams,(error, result)=>{
if(result.resources.length > 0){
result.resources.forEach((mediaItem)=>{
const nodeData = processMedia(mediaItem)
createNode(nodeData)
console.log("Created Cloudinary file node")
})
}else{
console.log('\n ~Yikes! No Cloudinary files found and nodes not created. Try a better query.')
}
if(error){console.log(error)}
})
)
}