-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathgatsby-node.js
135 lines (123 loc) · 4.37 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
const fetch = require('node-fetch')
const queryString = require('query-string')
const crypto = require('crypto')
exports.sourceNodes = ({ 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 API_KEY = configOptions.key
const filters = configOptions.filters
? queryString.stringify(configOptions.filters)
: null
const API_ENDPOINT_POST = `https://api.hubapi.com/content/api/v2/blog-posts?hapikey=${API_KEY}${
filters ? '&' + filters : ''
}`
const topicFilters = configOptions.topics && configOptions.topics.filters
? queryString.stringify(configOptions.topics.filters)
: null
const API_ENDPOINT_TOPIC = `https://api.hubapi.com/blogs/v3/topics?hapikey=${API_KEY}${
topicFilters ? '&' + topicFilters : ''
}`
if (!API_KEY) throw new Error('No Hubspot API key provided')
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_POST}\x1b[0m\n`
)
let topics = []
return fetch(API_ENDPOINT_TOPIC)
.then(response => response.json())
.then(data => {
topics = data.objects.map(topic => {
return {
id: topic.id,
name: topic.name,
slug: topic.slug,
description: topic.description
}
})
})
.then(() => {
return fetch(API_ENDPOINT_POST)
.then(response => response.json())
.then(data => {
const cleanData = data.objects.map(post => {
const p = {
id: post.id,
title: post.title,
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.featured_image,
alt_text: post.featured_image_alt_text
},
meta: {
title: post.page_title,
description: post.meta_description
},
campaign: post.campaign
? {
id: post.campaign,
name: post.campaign.campaign_name
}
: null,
summary: post.post_summary,
published: post.publish_date,
updated: post.updated,
created: post.created,
slug: post.slug,
url: post.url
}
if (post.topic_ids.length) {
topics.forEach((topic) => {
if (post.topic_ids.includes(topic.id)) {
p.topics.push(topic)
}
})
}
return p
})
cleanData.forEach(post => {
const nodeData = processPost(post)
createNode(nodeData)
})
})
})
}