Skip to content

Commit a85da63

Browse files
author
Sergei Orlov
committed
✨ Add support for extracting raw data from Notion API
1 parent 37f8153 commit a85da63

File tree

4 files changed

+95
-26
lines changed

4 files changed

+95
-26
lines changed

gatsby-browser.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

gatsby-node.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
1-
/**
2-
* Implement Gatsby's Node APIs in this file.
3-
*
4-
* See: https://www.gatsbyjs.com/docs/node-apis/
5-
*/
6-
// You can delete this file if you're not using it
7-
8-
/**
9-
* You can uncomment the following line to verify that
10-
* your plugin is being loaded in your site.
11-
*
12-
* See: https://www.gatsbyjs.com/docs/creating-a-local-plugin/#developing-a-local-plugin-that-is-outside-your-project
13-
*/
14-
exports.onPreInit = () => console.log("Loaded gatsby-source-notion-api")
1+
const { getPages } = require("./notion")
2+
3+
const NODE_TYPE = "Notion"
4+
5+
exports.sourceNodes = async (
6+
{ actions, createContentDigest, createNodeId, reporter },
7+
pluginOptions,
8+
) => {
9+
const { createNode } = actions
10+
11+
const data = {
12+
pages: await getPages(pluginOptions, reporter),
13+
}
14+
15+
data.pages.forEach((page) =>
16+
createNode({
17+
id: createNodeId(`${NODE_TYPE}-${page.id}`),
18+
raw: page,
19+
parent: null,
20+
children: [],
21+
internal: {
22+
type: NODE_TYPE,
23+
content: JSON.stringify(page),
24+
contentDigest: createContentDigest(page),
25+
},
26+
}),
27+
)
28+
}

gatsby-ssr.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

notion.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
const fetch = require("node-fetch")
2+
3+
const errorMessage = `gatsby-source-notion-api
4+
5+
Could not fetch data from Notion API. Check if "databaseId" and "token" are provided correctly. Make sure the integration using provided token has access to provided database.`
6+
7+
const getPageContent = async ({ page, notionVersion, token }, reporter) => {
8+
let hasMore = true
9+
let pageContent = []
10+
let startCursor = ""
11+
12+
while (hasMore) {
13+
let url = `https://api.notion.com/v1/blocks/${page.id}/children`
14+
15+
if (startCursor) {
16+
url += `?start_cursor=${startCursor}`
17+
}
18+
19+
try {
20+
await fetch(url, {
21+
headers: {
22+
"Content-Type": "application/json",
23+
"Notion-Version": notionVersion,
24+
"Authorization": `Bearer ${token}`,
25+
},
26+
})
27+
.then((res) => res.json())
28+
.then((res) => {
29+
pageContent = pageContent.concat(res.results)
30+
startCursor = res.next_cursor
31+
hasMore = res.has_more
32+
})
33+
} catch (e) {
34+
reporter.panic(errorMessage)
35+
}
36+
}
37+
38+
page.page_content = pageContent
39+
40+
return page
41+
}
42+
43+
const getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, reporter) => {
44+
try {
45+
const db = await fetch(`https://api.notion.com/v1/databases/${databaseId}/query?page_size=100`, {
46+
method: "POST",
47+
body: JSON.stringify({
48+
page_size: 100,
49+
}),
50+
headers: {
51+
"Content-Type": "application/json",
52+
"Notion-Version": notionVersion,
53+
"Authorization": `Bearer ${token}`,
54+
},
55+
}).then((res) => res.json())
56+
57+
for (let page of db.results) {
58+
page = await getPageContent({ page, token, notionVersion }, reporter)
59+
}
60+
61+
return db.results
62+
} catch (e) {
63+
reporter.panic(errorMessage)
64+
}
65+
}
66+
67+
module.exports = { getPages }

0 commit comments

Comments
 (0)