Skip to content

fix::notion-client - Replace ky with got || fix::react-notion-x - LazyImageFull.tsx updated #646

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@
"eslint --fix"
]
}
}
}
2 changes: 1 addition & 1 deletion packages/notion-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test:unit": "vitest run"
},
"dependencies": {
"ky": "catalog:",
"ofetch": "catalog:",
"notion-types": "workspace:*",
"notion-utils": "workspace:*",
"p-map": "catalog:"
Expand Down
105 changes: 60 additions & 45 deletions packages/notion-client/src/notion-api.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// import { promises as fs } from 'fs'
//import ky, { type Options as OfetchOptions } from 'ky'

import type * as notion from 'notion-types'
import ky, { type Options as KyOptions } from 'ky'
import {
getBlockCollectionId,
getPageContentBlockIds,
parsePageId,
uuidToId
} from 'notion-utils'
import { type FetchOptions as OfetchOptions, ofetch } from 'ofetch'
import pMap from 'p-map'

import type * as types from './types'
Expand All @@ -19,27 +21,27 @@ export class NotionAPI {
private readonly _authToken?: string
private readonly _activeUser?: string
private readonly _userTimeZone: string
private readonly _kyOptions?: KyOptions
private readonly _ofetchOptions?: OfetchOptions

constructor({
apiBaseUrl = 'https://www.notion.so/api/v3',
authToken,
activeUser,
userTimeZone = 'America/New_York',
kyOptions
ofetchOptions
}: {
apiBaseUrl?: string
authToken?: string
userLocale?: string
userTimeZone?: string
activeUser?: string
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
} = {}) {
this._apiBaseUrl = apiBaseUrl
this._authToken = authToken
this._activeUser = activeUser
this._userTimeZone = userTimeZone
this._kyOptions = kyOptions
this._ofetchOptions = ofetchOptions
}

public async getPage(
Expand All @@ -54,7 +56,7 @@ export class NotionAPI {
throwOnCollectionErrors = false,
collectionReducerLimit = 999,
fetchRelationPages = false,
kyOptions
ofetchOptions
}: {
concurrency?: number
fetchMissingBlocks?: boolean
Expand All @@ -65,13 +67,13 @@ export class NotionAPI {
throwOnCollectionErrors?: boolean
collectionReducerLimit?: number
fetchRelationPages?: boolean
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
} = {}
): Promise<notion.ExtendedRecordMap> {
const page = await this.getPageRaw(pageId, {
chunkLimit,
chunkNumber,
kyOptions
ofetchOptions
})
const recordMap = page?.recordMap as notion.ExtendedRecordMap

Expand Down Expand Up @@ -100,9 +102,10 @@ export class NotionAPI {
break
}

const newBlocks = await this.getBlocks(pendingBlockIds, kyOptions).then(
(res) => res.recordMap.block
)
const newBlocks = await this.getBlocks(
pendingBlockIds,
ofetchOptions
).then((res) => res.recordMap.block)

recordMap.block = { ...recordMap.block, ...newBlocks }
}
Expand Down Expand Up @@ -152,7 +155,7 @@ export class NotionAPI {
collectionView,
{
limit: collectionReducerLimit,
kyOptions
ofetchOptions
}
)

Expand Down Expand Up @@ -219,11 +222,11 @@ export class NotionAPI {
// because it is preferable for many use cases as opposed to making these API calls
// lazily from the client-side.
if (signFileUrls) {
await this.addSignedUrls({ recordMap, contentBlockIds, kyOptions })
await this.addSignedUrls({ recordMap, contentBlockIds, ofetchOptions })
}

if (fetchRelationPages) {
const newBlocks = await this.fetchRelationPages(recordMap, kyOptions)
const newBlocks = await this.fetchRelationPages(recordMap, ofetchOptions)
recordMap.block = { ...recordMap.block, ...newBlocks }
}

Expand All @@ -232,7 +235,7 @@ export class NotionAPI {

fetchRelationPages = async (
recordMap: notion.ExtendedRecordMap,
kyOptions: KyOptions | undefined
ofetchOptions: OfetchOptions | undefined
): Promise<notion.BlockMap> => {
const maxIterations = 10

Expand Down Expand Up @@ -265,7 +268,7 @@ export class NotionAPI {
try {
const newBlocks = await this.getBlocks(
missingRelationPageIds,
kyOptions
ofetchOptions
).then((res) => res.recordMap.block)
recordMap.block = { ...recordMap.block, ...newBlocks }
} catch (err: any) {
Expand Down Expand Up @@ -316,11 +319,11 @@ export class NotionAPI {
public async addSignedUrls({
recordMap,
contentBlockIds,
kyOptions = {}
ofetchOptions = {}
}: {
recordMap: notion.ExtendedRecordMap
contentBlockIds?: string[]
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
}) {
recordMap.signed_urls = {}

Expand Down Expand Up @@ -372,7 +375,7 @@ export class NotionAPI {
try {
const { signedUrls } = await this.getSignedFileUrls(
allFileInstances,
kyOptions
ofetchOptions
)

if (signedUrls.length === allFileInstances.length) {
Expand All @@ -395,13 +398,13 @@ export class NotionAPI {
public async getPageRaw(
pageId: string,
{
kyOptions,
ofetchOptions,
chunkLimit = 100,
chunkNumber = 0
}: {
chunkLimit?: number
chunkNumber?: number
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
} = {}
) {
const parsedPageId = parsePageId(pageId)
Expand All @@ -421,7 +424,7 @@ export class NotionAPI {
return this.fetch<notion.PageChunk>({
endpoint: 'loadPageChunk',
body,
kyOptions
ofetchOptions
})
}

Expand All @@ -434,15 +437,15 @@ export class NotionAPI {
searchQuery = '',
userTimeZone = this._userTimeZone,
loadContentCover = true,
kyOptions
ofetchOptions
}: {
type?: notion.CollectionViewType
limit?: number
searchQuery?: string
userTimeZone?: string
userLocale?: string
loadContentCover?: boolean
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
} = {}
) {
const type = collectionView?.type
Expand Down Expand Up @@ -626,28 +629,28 @@ export class NotionAPI {
},
loader
},
kyOptions: {
ofetchOptions: {
timeout: 60_000,
...kyOptions,
searchParams: {
// TODO: spread kyOptions?.searchParams
...ofetchOptions,
params: {
// TODO: spread ofetchOptions?.searchParams
src: 'initial_load'
}
}
})
}

public async getUsers(userIds: string[], kyOptions?: KyOptions) {
public async getUsers(userIds: string[], ofetchOptions?: OfetchOptions) {
return this.fetch<notion.RecordValues<notion.User>>({
endpoint: 'getRecordValues',
body: {
requests: userIds.map((id) => ({ id, table: 'notion_user' }))
},
kyOptions
ofetchOptions
})
}

public async getBlocks(blockIds: string[], kyOptions?: KyOptions) {
public async getBlocks(blockIds: string[], ofetchOptions?: OfetchOptions) {
return this.fetch<notion.PageChunk>({
endpoint: 'syncRecordValues',
body: {
Expand All @@ -658,24 +661,27 @@ export class NotionAPI {
version: -1
}))
},
kyOptions
ofetchOptions
})
}

public async getSignedFileUrls(
urls: types.SignedUrlRequest[],
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
) {
return this.fetch<types.SignedUrlResponse>({
endpoint: 'getSignedFileUrls',
body: {
urls
},
kyOptions
ofetchOptions
})
}

public async search(params: notion.SearchParams, kyOptions?: KyOptions) {
public async search(
params: notion.SearchParams,
ofetchOptions?: OfetchOptions
) {
const body = {
type: 'BlocksInAncestor',
source: 'quick_find_public',
Expand Down Expand Up @@ -703,25 +709,25 @@ export class NotionAPI {
return this.fetch<notion.SearchResults>({
endpoint: 'search',
body,
kyOptions
ofetchOptions
})
}

public async fetch<T>({
endpoint,
body,
kyOptions,
ofetchOptions,
headers: clientHeaders
}: {
endpoint: string
body: object
kyOptions?: KyOptions
ofetchOptions?: OfetchOptions
headers?: any
}): Promise<T> {
const headers: any = {
...clientHeaders,
...this._kyOptions?.headers,
...kyOptions?.headers,
...this._ofetchOptions?.headers,
...ofetchOptions?.headers,
'Content-Type': 'application/json'
}

Expand All @@ -735,13 +741,13 @@ export class NotionAPI {

const url = `${this._apiBaseUrl}/${endpoint}`

const res = await ky.post(url, {
/* const res = await ky.post(url, {
mode: 'no-cors',
...this._kyOptions,
...kyOptions,
...this._ofetchOptions,
...ofetchOptions,
json: body,
headers
})
}) */

// TODO: we're awaiting the first fetch and then separately awaiting
// `res.json()` because there seems to be some weird error which repros
Expand All @@ -750,6 +756,15 @@ export class NotionAPI {
// steps seems to fix the issue locally for me...
// console.log(endpoint, { bodyUsed: res.bodyUsed })

return res.json<T>()
/* return res.json<T>() */
const res = ofetch(url, {
method: 'POST',
mode: 'no-cors',
...this._ofetchOptions,
...ofetchOptions,
body,
headers
})
return res
}
}
6 changes: 4 additions & 2 deletions packages/react-notion-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@
"react-fast-compare": "catalog:",
"react-hotkeys-hook": "catalog:",
"react-image": "catalog:",
"react-lazy-images": "catalog:",
"react-modal": "catalog:"
"react-intersection-observer": "catalog:",
"react-modal": "catalog:",
"unionize" : "catalog:"

},
"devDependencies": {
"@types/lodash.throttle": "catalog:",
Expand Down
Loading