forked from jamaljeantobias/gatsby-source-hubspot-forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
56 lines (54 loc) · 1.72 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
const _ = require(`lodash`)
const Promise = require(`bluebird`)
const path = require(`path`)
const slash = require(`slash`)
const axios = require("axios")
const crypto = require("crypto")
var fs = require("fs")
exports.sourceNodes = async ({ actions }, configOptions) => {
try {
const API_KEY = configOptions.apiKey
if (!API_KEY) throw new Error("No Hubspot API key provided")
const { createNode } = actions
const fetchAllFormNodes = await axios.get(
`https://api.hubapi.com/forms/v2/forms?hapikey=${API_KEY}`
)
const response = await fetchAllFormNodes.data
response.map((item, index) => {
const formNode = {
id: item.guid,
portalId: item.portalId.toString(),
guid: item.guid,
name: item.name,
action: item.action,
method: item.method,
cssClass: item.cssClass,
redirect: item.redirect,
submitText: item.submitText,
followUpId: item.followUpId,
notifyRecipients: item.notifyRecipients,
leadNurturingCampaignId: item.leadNurturingCampaignId,
formFieldGroups: item.formFieldGroups,
metaData: item.metaData,
inlineMessage: item.inlineMessage,
isPublished: item.isPublished,
thankYouMessageJson: item.thankYouMessageJson,
children: [],
parent: `__SOURCE__`,
internal: {
type: `HubspotForm`,
},
}
console.log(` ${index + 1} :Creating Hubspot Form ${item.name}`)
const contentDigest = crypto
.createHash(`md5`)
.update(JSON.stringify(formNode))
.digest(`hex`)
formNode.internal.contentDigest = contentDigest
createNode(formNode)
})
return
} catch (err) {
throw new Error(err)
}
}