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

Switch from got to ky + fetch in notion-client #394

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions packages/notion-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@
"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.15.4",
"notion-utils": "^6.15.4",
"p-map": "^5.3.0"
},
"optionalDependencies": {
"ky-universal": "^0.11.0"
},
"ava": {
"snapshotDir": ".snapshots",
"extensions": {
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 @@ -569,24 +564,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 @@ -600,21 +596,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.

Loading