Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry Notion API #145

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@notionhq/client": "^2.2.5",
"@supercharge/promise-pool": "^2.4.0",
"astro": "^2.5.6",
"async-retry": "^1.3.3",
"katex": "^0.16.7",
"mermaid": "^10.1.0",
"metascraper": "^5.34.4",
Expand All @@ -34,6 +35,7 @@
},
"devDependencies": {
"@nrwl/nx-cloud": "^15.0.2",
"@types/async-retry": "^1.4.5",
"@types/js-base64": "^3.3.1",
"@types/metascraper": "^5.14.1",
"@types/metascraper-description": "^5.14.1",
Expand Down
138 changes: 119 additions & 19 deletions src/lib/notion/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs, { createWriteStream } from 'node:fs'
import retry from 'async-retry'
import {
NOTION_API_SECRET,
DATABASE_ID,
Expand Down Expand Up @@ -48,7 +49,7 @@ import type {
Reference,
} from '../interfaces'
// eslint-disable-next-line @typescript-eslint/no-var-requires
import { Client } from '@notionhq/client'
import { Client, APIResponseError } from '@notionhq/client'

const client = new Client({
auth: NOTION_API_SECRET,
Expand All @@ -57,6 +58,8 @@ const client = new Client({
let postsCache: Post[] | null = null
let dbCache: Database | null = null

const numberOfRetry = 2

export async function getAllPosts(): Promise<Post[]> {
if (postsCache !== null) {
return Promise.resolve(postsCache)
Expand Down Expand Up @@ -91,9 +94,25 @@ export async function getAllPosts(): Promise<Post[]> {

let results: responses.PageObject[] = []
while (true) {
const res = (await client.databases.query(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.QueryDatabaseResponse
const res = await retry(
async (bail) => {
try {
return (await client.databases.query(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.QueryDatabaseResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

results = results.concat(res.results)

Expand Down Expand Up @@ -216,9 +235,25 @@ export async function getAllBlocksByBlockId(blockId: string): Promise<Block[]> {
}

while (true) {
const res = (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
const res = await retry(
async (bail) => {
try {
return (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

results = results.concat(res.results)

Expand Down Expand Up @@ -295,9 +330,26 @@ export async function getBlock(blockId: string): Promise<Block> {
const params: requestParams.RetrieveBlock = {
block_id: blockId,
}
const res = (await client.blocks.retrieve(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockResponse

const res = await retry(
async (bail) => {
try {
return (await client.blocks.retrieve(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

return _buildBlock(res)
}
Expand Down Expand Up @@ -366,9 +418,25 @@ export async function getDatabase(): Promise<Database> {
database_id: DATABASE_ID,
}

const res = (await client.databases.retrieve(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveDatabaseResponse
const res = await retry(
async (bail) => {
try {
return (await client.databases.retrieve(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveDatabaseResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

let icon: FileObject | Emoji | null = null
if (res.icon) {
Expand Down Expand Up @@ -703,9 +771,25 @@ async function _getTableRows(blockId: string): Promise<TableRow[]> {
}

while (true) {
const res = (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
const res = await retry(
async (bail) => {
try {
return (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

results = results.concat(res.results)

Expand Down Expand Up @@ -752,9 +836,25 @@ async function _getColumns(blockId: string): Promise<Column[]> {
}

while (true) {
const res = (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
const res = await retry(
async (bail) => {
try {
return (await client.blocks.children.list(
params as any // eslint-disable-line @typescript-eslint/no-explicit-any
)) as responses.RetrieveBlockChildrenResponse
} catch (error: unknown) {
if (error instanceof APIResponseError) {
if (error.status && error.status >= 400 && error.status < 500) {
bail(error)
}
}
throw error
}
},
{
retries: numberOfRetry,
}
)

results = results.concat(res.results)

Expand Down