Skip to content

Commit 3a97527

Browse files
feat: remove got; add ky for fetch support to notion-client
1 parent 16d1870 commit 3a97527

File tree

9 files changed

+195
-97
lines changed

9 files changed

+195
-97
lines changed

packages/notion-client/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,28 @@
99
"main": "./build/index.js",
1010
"module": "./build/index.js",
1111
"types": "./build/index.d.ts",
12+
"browser": "./build/browser.js",
1213
"sideEffects": false,
1314
"files": [
1415
"build"
1516
],
1617
"engines": {
17-
"node": ">=12"
18+
"node": ">=14.8"
1819
},
1920
"scripts": {
2021
"build": "tsup",
2122
"watch": "tsup --watch --silent --onSuccess 'echo build successful'",
2223
"test": "ava"
2324
},
2425
"dependencies": {
25-
"got": "^11.8.1",
26+
"ky": "^0.31.4",
2627
"notion-types": "^6.15.4",
2728
"notion-utils": "^6.15.4",
2829
"p-map": "^5.3.0"
2930
},
31+
"optionalDependencies": {
32+
"ky-universal": "^0.11.0"
33+
},
3034
"ava": {
3135
"snapshotDir": ".snapshots",
3236
"extensions": {

packages/notion-client/src/browser.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './notion-api'
2+
export * from './types'

packages/notion-client/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './notion-api'
1+
export * from './notion-api-universal'
22
export * from './types'

packages/notion-client/src/notion-api.test.ts renamed to packages/notion-client/src/notion-api-universal.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

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

55
const pageIdFixturesSuccess = [
66
'067dd719-a912-471e-a9a3-ac10710e7fdf',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// This adds Node.js support for `fetch` and `abort-controller`
2+
import 'ky-universal'
3+
4+
export { NotionAPI } from './notion-api'

packages/notion-client/src/notion-api.ts

Lines changed: 39 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// import { promises as fs } from 'fs'
22
import * as notion from 'notion-types'
3-
import got, { OptionsOfJSONResponseBody } from 'got'
3+
import ky from 'ky'
4+
import type { Options as KyOptions } from 'ky'
45
import {
56
getBlockCollectionId,
67
getPageContentBlockIds,
@@ -19,23 +20,27 @@ export class NotionAPI {
1920
private readonly _authToken?: string
2021
private readonly _activeUser?: string
2122
private readonly _userTimeZone: string
23+
private readonly _kyOptions?: KyOptions
2224

2325
constructor({
2426
apiBaseUrl = 'https://www.notion.so/api/v3',
2527
authToken,
2628
activeUser,
27-
userTimeZone = 'America/New_York'
29+
userTimeZone = 'America/New_York',
30+
kyOptions
2831
}: {
2932
apiBaseUrl?: string
3033
authToken?: string
3134
userLocale?: string
3235
userTimeZone?: string
3336
activeUser?: string
37+
kyOptions?: KyOptions
3438
} = {}) {
3539
this._apiBaseUrl = apiBaseUrl
3640
this._authToken = authToken
3741
this._activeUser = activeUser
3842
this._userTimeZone = userTimeZone
43+
this._kyOptions = kyOptions
3944
}
4045

4146
public async getPage(
@@ -47,21 +52,21 @@ export class NotionAPI {
4752
signFileUrls = true,
4853
chunkLimit = 100,
4954
chunkNumber = 0,
50-
gotOptions
55+
kyOptions
5156
}: {
5257
concurrency?: number
5358
fetchMissingBlocks?: boolean
5459
fetchCollections?: boolean
5560
signFileUrls?: boolean
5661
chunkLimit?: number
5762
chunkNumber?: number
58-
gotOptions?: OptionsOfJSONResponseBody
63+
kyOptions?: KyOptions
5964
} = {}
6065
): Promise<notion.ExtendedRecordMap> {
6166
const page = await this.getPageRaw(pageId, {
6267
chunkLimit,
6368
chunkNumber,
64-
gotOptions
69+
kyOptions
6570
})
6671
const recordMap = page?.recordMap as notion.ExtendedRecordMap
6772

@@ -91,10 +96,9 @@ export class NotionAPI {
9196
break
9297
}
9398

94-
const newBlocks = await this.getBlocks(
95-
pendingBlockIds,
96-
gotOptions
97-
).then((res) => res.recordMap.block)
99+
const newBlocks = await this.getBlocks(pendingBlockIds, kyOptions).then(
100+
(res) => res.recordMap.block
101+
)
98102

99103
recordMap.block = { ...recordMap.block, ...newBlocks }
100104
}
@@ -143,7 +147,7 @@ export class NotionAPI {
143147
collectionViewId,
144148
collectionView,
145149
{
146-
gotOptions
150+
kyOptions
147151
}
148152
)
149153

@@ -194,7 +198,7 @@ export class NotionAPI {
194198
// because it is preferable for many use cases as opposed to making these API calls
195199
// lazily from the client-side.
196200
if (signFileUrls) {
197-
await this.addSignedUrls({ recordMap, contentBlockIds, gotOptions })
201+
await this.addSignedUrls({ recordMap, contentBlockIds, kyOptions })
198202
}
199203

200204
return recordMap
@@ -203,11 +207,11 @@ export class NotionAPI {
203207
public async addSignedUrls({
204208
recordMap,
205209
contentBlockIds,
206-
gotOptions = {}
210+
kyOptions = {}
207211
}: {
208212
recordMap: notion.ExtendedRecordMap
209213
contentBlockIds?: string[]
210-
gotOptions?: OptionsOfJSONResponseBody
214+
kyOptions?: KyOptions
211215
}) {
212216
recordMap.signed_urls = {}
213217

@@ -255,7 +259,7 @@ export class NotionAPI {
255259
try {
256260
const { signedUrls } = await this.getSignedFileUrls(
257261
allFileInstances,
258-
gotOptions
262+
kyOptions
259263
)
260264

261265
if (signedUrls.length === allFileInstances.length) {
@@ -275,13 +279,13 @@ export class NotionAPI {
275279
public async getPageRaw(
276280
pageId: string,
277281
{
278-
gotOptions,
282+
kyOptions,
279283
chunkLimit = 100,
280284
chunkNumber = 0
281285
}: {
282286
chunkLimit?: number
283287
chunkNumber?: number
284-
gotOptions?: OptionsOfJSONResponseBody
288+
kyOptions?: KyOptions
285289
} = {}
286290
) {
287291
const parsedPageId = parsePageId(pageId)
@@ -301,7 +305,7 @@ export class NotionAPI {
301305
return this.fetch<notion.PageChunk>({
302306
endpoint: 'loadPageChunk',
303307
body,
304-
gotOptions
308+
kyOptions
305309
})
306310
}
307311

@@ -314,15 +318,15 @@ export class NotionAPI {
314318
searchQuery = '',
315319
userTimeZone = this._userTimeZone,
316320
loadContentCover = true,
317-
gotOptions
321+
kyOptions
318322
}: {
319323
type?: notion.CollectionViewType
320324
limit?: number
321325
searchQuery?: string
322326
userTimeZone?: string
323327
userLocale?: string
324328
loadContentCover?: boolean
325-
gotOptions?: OptionsOfJSONResponseBody
329+
kyOptions?: KyOptions
326330
} = {}
327331
) {
328332
const type = collectionView?.type
@@ -493,27 +497,21 @@ export class NotionAPI {
493497
},
494498
loader
495499
},
496-
gotOptions
500+
kyOptions
497501
})
498502
}
499503

500-
public async getUsers(
501-
userIds: string[],
502-
gotOptions?: OptionsOfJSONResponseBody
503-
) {
504+
public async getUsers(userIds: string[], kyOptions?: KyOptions) {
504505
return this.fetch<notion.RecordValues<notion.User>>({
505506
endpoint: 'getRecordValues',
506507
body: {
507508
requests: userIds.map((id) => ({ id, table: 'notion_user' }))
508509
},
509-
gotOptions
510+
kyOptions
510511
})
511512
}
512513

513-
public async getBlocks(
514-
blockIds: string[],
515-
gotOptions?: OptionsOfJSONResponseBody
516-
) {
514+
public async getBlocks(blockIds: string[], kyOptions?: KyOptions) {
517515
return this.fetch<notion.PageChunk>({
518516
endpoint: 'syncRecordValues',
519517
body: {
@@ -524,27 +522,24 @@ export class NotionAPI {
524522
version: -1
525523
}))
526524
},
527-
gotOptions
525+
kyOptions
528526
})
529527
}
530528

531529
public async getSignedFileUrls(
532530
urls: types.SignedUrlRequest[],
533-
gotOptions?: OptionsOfJSONResponseBody
531+
kyOptions?: KyOptions
534532
) {
535533
return this.fetch<types.SignedUrlResponse>({
536534
endpoint: 'getSignedFileUrls',
537535
body: {
538536
urls
539537
},
540-
gotOptions
538+
kyOptions
541539
})
542540
}
543541

544-
public async search(
545-
params: notion.SearchParams,
546-
gotOptions?: OptionsOfJSONResponseBody
547-
) {
542+
public async search(params: notion.SearchParams, kyOptions?: KyOptions) {
548543
const body = {
549544
type: 'BlocksInAncestor',
550545
source: 'quick_find_public',
@@ -569,24 +564,25 @@ export class NotionAPI {
569564
return this.fetch<notion.SearchResults>({
570565
endpoint: 'search',
571566
body,
572-
gotOptions
567+
kyOptions
573568
})
574569
}
575570

576571
public async fetch<T>({
577572
endpoint,
578573
body,
579-
gotOptions,
574+
kyOptions,
580575
headers: clientHeaders
581576
}: {
582577
endpoint: string
583578
body: object
584-
gotOptions?: OptionsOfJSONResponseBody
579+
kyOptions?: KyOptions
585580
headers?: any
586581
}): Promise<T> {
587582
const headers: any = {
588583
...clientHeaders,
589-
...gotOptions?.headers,
584+
...this._kyOptions?.headers,
585+
...kyOptions?.headers,
590586
'Content-Type': 'application/json'
591587
}
592588

@@ -600,21 +596,13 @@ export class NotionAPI {
600596

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

603-
return got
599+
return ky
604600
.post(url, {
605-
...gotOptions,
601+
...this._kyOptions,
602+
...kyOptions,
606603
json: body,
607604
headers
608605
})
609606
.json()
610-
611-
// return fetch(url, {
612-
// method: 'post',
613-
// body: JSON.stringify(body),
614-
// headers
615-
// }).then((res) => {
616-
// console.log(endpoint, res)
617-
// return res.json()
618-
// })
619607
}
620608
}

packages/notion-client/src/temp

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)