-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
133 lines (126 loc) · 4.26 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
const fetch = require('node-fetch')
const queryString = require('query-string')
const axios = require('axios')
const crypto = require('crypto')
const _ = require('lodash')
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, configOptions) => {
const { createNode } = actions
delete configOptions.plugins
const processPost = post => {
const nodeId = createNodeId(`hubspot-post-${post.id}`)
const nodeContent = JSON.stringify(post)
const nodeContentDigest = crypto
.createHash('md5')
.update(nodeContent)
.digest('hex')
const nodeData = Object.assign({}, post, {
id: nodeId,
parent: null,
children: [],
internal: {
type: `HubspotPost`,
content: nodeContent,
contentDigest: nodeContentDigest
}
})
return nodeData
}
const topics = []
const API_KEY = configOptions.key
if (!API_KEY) throw new Error('No Hubspot API key provided')
const apiCall = async function(url){
const apiResponse = await axios.get(url, {
headers: {
'Authorization': `Bearer ${API_KEY}`,
'Content-Type': 'application/json'
}
})
const data = _.get(apiResponse, 'data')
return data
};
const recursiveCallApi = async function(results, isTopic) {
const nextPageLink = _.get(results, 'paging.next.link', null)
if(isTopic) {
results.objects.map((topic) => {
const topicParsed = {
id: topic.id,
name: topic.name,
slug: topic.slug,
description: topic.description,
}
topics.push(topicParsed)
})
}
if(nextPageLink) {
const resultsRecursived = await apiCall(nextPageLink)
if(!isTopic) {
const cleanData = resultsRecursived.results.map(post => {
const p = {
id: post.id,
title: post.htmlTitle,
body: post.post_body,
state: post.state,
topics: [],
author: post.blog_post_author
? {
id: post.blog_post_author.id,
avatar: post.blog_post_author.avatar,
name: post.blog_post_author.display_name,
full_name: post.blog_post_author.full_name,
bio: post.blog_post_author.bio,
email: post.blog_post_author.email,
facebook: post.blog_post_author.facebook,
google_plus: post.blog_post_author.google_plus,
linkedin: post.blog_post_author.linkedin,
twitter: post.blog_post_author.twitter,
twitter_username: post.blog_post_author.twitter_username,
website: post.blog_post_author.website,
slug: post.blog_post_author.slug
}
: null,
feature_image: {
url: post.featuredImage,
alt_text: post.featuredImageAltText
},
meta: {
title: post.page_title,
description: post.meta_description
},
campaign: post.campaign
? {
id: post.campaign,
name: post.campaign.campaign_name
}
: null,
summary: post.postSummary,
published: post.publish_date,
updated: post.updated,
created: post.created,
slug: post.slug,
url: post.url
}
return p
})
cleanData.forEach(post => {
const nodeData = processPost(post)
createNode(nodeData)
})
}
await recursiveCallApi(resultsRecursived, isTopic)
} else {
return
}
}
const API_ENDPOINT_TOPIC = 'https://api.hubapi.com/blogs/v3/topics?limit=100'
const API_ENDPOINT_POSTS = 'https://api.hubapi.com/cms/v3/blogs/posts?limit=90'
console.log(
'\n gatsby-source-hubspot\n ------------------------- \n Fetching post topics from: \x1b[33m',
`\n ${API_ENDPOINT_TOPIC}\x1b[0m\n`,
' Fetching posts from: \x1b[33m',
`\n ${API_ENDPOINT_POSTS}\x1b[0m\n`
)
const firstCallTopics = await apiCall(API_ENDPOINT_TOPIC)
await recursiveCallApi(firstCallTopics, true)
const firstCallPosts = await apiCall(API_ENDPOINT_POSTS)
await recursiveCallApi(firstCallPosts, false)
}