Skip to content

Commit

Permalink
Merge pull request #394 from NotionX/feature/support-fetch-and-edge-r…
Browse files Browse the repository at this point in the history
…untimes
  • Loading branch information
transitive-bullshit authored Oct 31, 2024
2 parents a5807e5 + b034119 commit 6f5c333
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 97 deletions.
5 changes: 3 additions & 2 deletions packages/notion-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@
"main": "./build/index.js",
"module": "./build/index.js",
"types": "./build/index.d.ts",
"browser": "./build/browser.js",
"sideEffects": false,
"files": [
"build"
],
"engines": {
"node": ">=12"
"node": ">=14.8"
},
"scripts": {
"build": "tsup",
"watch": "tsup --watch --silent --onSuccess 'echo build successful'",
"test": "ava"
},
"dependencies": {
"got": "^11.8.1",
"ky": "^0.31.4",
"notion-types": "^6.16.1",
"notion-utils": "^6.16.1",
"p-map": "^5.3.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/notion-client/src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './notion-api'
export * from './types'
2 changes: 1 addition & 1 deletion packages/notion-client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './notion-api'
export * from './notion-api-universal'
export * from './types'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { NotionAPI } from './notion-api'
import { NotionAPI } from './notion-api-universal'

const pageIdFixturesSuccess = [
'067dd719-a912-471e-a9a3-ac10710e7fdf',
Expand Down
4 changes: 4 additions & 0 deletions packages/notion-client/src/notion-api-universal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This adds Node.js support for `fetch` and `abort-controller`
import 'ky-universal'

export { NotionAPI } from './notion-api'
90 changes: 39 additions & 51 deletions packages/notion-client/src/notion-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import { promises as fs } from 'fs'
import * as notion from 'notion-types'
import got, { OptionsOfJSONResponseBody } from 'got'
import ky from 'ky'
import type { Options as KyOptions } from 'ky'
import {
getBlockCollectionId,
getPageContentBlockIds,
Expand All @@ -19,23 +20,27 @@ export class NotionAPI {
private readonly _authToken?: string
private readonly _activeUser?: string
private readonly _userTimeZone: string
private readonly _kyOptions?: KyOptions

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

public async getPage(
Expand All @@ -47,21 +52,21 @@ export class NotionAPI {
signFileUrls = true,
chunkLimit = 100,
chunkNumber = 0,
gotOptions
kyOptions
}: {
concurrency?: number
fetchMissingBlocks?: boolean
fetchCollections?: boolean
signFileUrls?: boolean
chunkLimit?: number
chunkNumber?: number
gotOptions?: OptionsOfJSONResponseBody
kyOptions?: KyOptions
} = {}
): Promise<notion.ExtendedRecordMap> {
const page = await this.getPageRaw(pageId, {
chunkLimit,
chunkNumber,
gotOptions
kyOptions
})
const recordMap = page?.recordMap as notion.ExtendedRecordMap

Expand Down Expand Up @@ -91,10 +96,9 @@ export class NotionAPI {
break
}

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

recordMap.block = { ...recordMap.block, ...newBlocks }
}
Expand Down Expand Up @@ -143,7 +147,7 @@ export class NotionAPI {
collectionViewId,
collectionView,
{
gotOptions
kyOptions
}
)

Expand Down Expand Up @@ -194,7 +198,7 @@ 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, gotOptions })
await this.addSignedUrls({ recordMap, contentBlockIds, kyOptions })
}

return recordMap
Expand All @@ -203,11 +207,11 @@ export class NotionAPI {
public async addSignedUrls({
recordMap,
contentBlockIds,
gotOptions = {}
kyOptions = {}
}: {
recordMap: notion.ExtendedRecordMap
contentBlockIds?: string[]
gotOptions?: OptionsOfJSONResponseBody
kyOptions?: KyOptions
}) {
recordMap.signed_urls = {}

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

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

Expand All @@ -314,15 +318,15 @@ export class NotionAPI {
searchQuery = '',
userTimeZone = this._userTimeZone,
loadContentCover = true,
gotOptions
kyOptions
}: {
type?: notion.CollectionViewType
limit?: number
searchQuery?: string
userTimeZone?: string
userLocale?: string
loadContentCover?: boolean
gotOptions?: OptionsOfJSONResponseBody
kyOptions?: KyOptions
} = {}
) {
const type = collectionView?.type
Expand Down Expand Up @@ -493,27 +497,21 @@ export class NotionAPI {
},
loader
},
gotOptions
kyOptions
})
}

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

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

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

public async search(
params: notion.SearchParams,
gotOptions?: OptionsOfJSONResponseBody
) {
public async search(params: notion.SearchParams, kyOptions?: KyOptions) {
const body = {
type: 'BlocksInAncestor',
source: 'quick_find_public',
Expand All @@ -571,24 +566,25 @@ export class NotionAPI {
return this.fetch<notion.SearchResults>({
endpoint: 'search',
body,
gotOptions
kyOptions
})
}

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

Expand All @@ -602,21 +598,13 @@ export class NotionAPI {

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

return got
return ky
.post(url, {
...gotOptions,
...this._kyOptions,
...kyOptions,
json: body,
headers
})
.json()

// return fetch(url, {
// method: 'post',
// body: JSON.stringify(body),
// headers
// }).then((res) => {
// console.log(endpoint, res)
// return res.json()
// })
}
}
31 changes: 0 additions & 31 deletions packages/notion-client/src/temp

This file was deleted.

35 changes: 24 additions & 11 deletions packages/notion-client/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entry: ['src/index.ts'],
outDir: 'build',
target: 'node14',
platform: 'node',
format: ['esm'],
splitting: false,
sourcemap: true,
minify: true,
shims: false
})
export default defineConfig([
{
entry: ['src/index.ts'],
outDir: 'build',
target: 'node14.8',
platform: 'node',
format: ['esm'],
splitting: false,
sourcemap: true,
minify: true,
shims: false
},
{
entry: ['src/browser.ts'],
outDir: 'build',
target: 'es2015',
platform: 'browser',
format: ['esm'],
splitting: false,
sourcemap: true,
minify: true,
shims: false
}
])
Loading

0 comments on commit 6f5c333

Please sign in to comment.