Skip to content

Commit 6f5c333

Browse files
Merge pull request #394 from NotionX/feature/support-fetch-and-edge-runtimes
2 parents a5807e5 + b034119 commit 6f5c333

File tree

9 files changed

+187
-97
lines changed

9 files changed

+187
-97
lines changed

packages/notion-client/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@
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.16.1",
2728
"notion-utils": "^6.16.1",
2829
"p-map": "^5.3.0"

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',
@@ -571,24 +566,25 @@ export class NotionAPI {
571566
return this.fetch<notion.SearchResults>({
572567
endpoint: 'search',
573568
body,
574-
gotOptions
569+
kyOptions
575570
})
576571
}
577572

578573
public async fetch<T>({
579574
endpoint,
580575
body,
581-
gotOptions,
576+
kyOptions,
582577
headers: clientHeaders
583578
}: {
584579
endpoint: string
585580
body: object
586-
gotOptions?: OptionsOfJSONResponseBody
581+
kyOptions?: KyOptions
587582
headers?: any
588583
}): Promise<T> {
589584
const headers: any = {
590585
...clientHeaders,
591-
...gotOptions?.headers,
586+
...this._kyOptions?.headers,
587+
...kyOptions?.headers,
592588
'Content-Type': 'application/json'
593589
}
594590

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

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

605-
return got
601+
return ky
606602
.post(url, {
607-
...gotOptions,
603+
...this._kyOptions,
604+
...kyOptions,
608605
json: body,
609606
headers
610607
})
611608
.json()
612-
613-
// return fetch(url, {
614-
// method: 'post',
615-
// body: JSON.stringify(body),
616-
// headers
617-
// }).then((res) => {
618-
// console.log(endpoint, res)
619-
// return res.json()
620-
// })
621609
}
622610
}

packages/notion-client/src/temp

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

packages/notion-client/tsup.config.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
import { defineConfig } from 'tsup'
22

3-
export default defineConfig({
4-
entry: ['src/index.ts'],
5-
outDir: 'build',
6-
target: 'node14',
7-
platform: 'node',
8-
format: ['esm'],
9-
splitting: false,
10-
sourcemap: true,
11-
minify: true,
12-
shims: false
13-
})
3+
export default defineConfig([
4+
{
5+
entry: ['src/index.ts'],
6+
outDir: 'build',
7+
target: 'node14.8',
8+
platform: 'node',
9+
format: ['esm'],
10+
splitting: false,
11+
sourcemap: true,
12+
minify: true,
13+
shims: false
14+
},
15+
{
16+
entry: ['src/browser.ts'],
17+
outDir: 'build',
18+
target: 'es2015',
19+
platform: 'browser',
20+
format: ['esm'],
21+
splitting: false,
22+
sourcemap: true,
23+
minify: true,
24+
shims: false
25+
}
26+
])

0 commit comments

Comments
 (0)