| 
 | 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