Skip to content

Commit

Permalink
♻️ Extract notion-client usages
Browse files Browse the repository at this point in the history
  • Loading branch information
SilentDepth committed Apr 4, 2023
1 parent 64307a3 commit 90c7e5e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/notion/getAllPosts.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { config as BLOG } from '@/lib/server/config'

import { NotionAPI } from 'notion-client'
import { idToUuid } from 'notion-utils'
import dayjs from 'dayjs'
import api from '@/lib/server/notion-api'
import getAllPageIds from './getAllPageIds'
import getPageProperties from './getPageProperties'
import filterPublishedPosts from './filterPublishedPosts'
Expand All @@ -12,9 +12,7 @@ import filterPublishedPosts from './filterPublishedPosts'
*/
export async function getAllPosts ({ includePages = false }) {
const id = idToUuid(process.env.NOTION_PAGE_ID)
const authToken = process.env.NOTION_ACCESS_TOKEN || null

const api = new NotionAPI({ authToken })
const response = await api.getPage(id)

const collection = Object.values(response.collection)[0]?.value
Expand Down
5 changes: 2 additions & 3 deletions lib/notion/getPageProperties.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { getTextContent, getDateValue } from 'notion-utils'
import { NotionAPI } from 'notion-client'
import api from '@/lib/server/notion-api'

async function getPageProperties (id, block, schema, authToken) {
const api = new NotionAPI({ authToken })
async function getPageProperties (id, block, schema) {
const rawProperties = Object.entries(block?.[id]?.value?.properties || [])
const excludeProperties = ['date', 'select', 'multi_select', 'person']
const properties = {}
Expand Down
4 changes: 1 addition & 3 deletions lib/notion/getPostBlocks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { NotionAPI } from 'notion-client'
import api from '@/lib/server/notion-api'

export async function getPostBlocks (id) {
const authToken = process.env.NOTION_ACCESS_TOKEN || null
const api = new NotionAPI({ authToken })
const pageBlock = await api.getPage(id)
return pageBlock
}
7 changes: 7 additions & 0 deletions lib/server/notion-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { NotionAPI } from 'notion-client'

const { NOTION_ACCESS_TOKEN } = process.env

const client = new NotionAPI({ authToken: NOTION_ACCESS_TOKEN })

export default client

0 comments on commit 90c7e5e

Please sign in to comment.