1
1
// import { promises as fs } from 'fs'
2
2
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'
4
5
import {
5
6
getBlockCollectionId ,
6
7
getPageContentBlockIds ,
@@ -19,23 +20,27 @@ export class NotionAPI {
19
20
private readonly _authToken ?: string
20
21
private readonly _activeUser ?: string
21
22
private readonly _userTimeZone : string
23
+ private readonly _kyOptions ?: KyOptions
22
24
23
25
constructor ( {
24
26
apiBaseUrl = 'https://www.notion.so/api/v3' ,
25
27
authToken,
26
28
activeUser,
27
- userTimeZone = 'America/New_York'
29
+ userTimeZone = 'America/New_York' ,
30
+ kyOptions
28
31
} : {
29
32
apiBaseUrl ?: string
30
33
authToken ?: string
31
34
userLocale ?: string
32
35
userTimeZone ?: string
33
36
activeUser ?: string
37
+ kyOptions ?: KyOptions
34
38
} = { } ) {
35
39
this . _apiBaseUrl = apiBaseUrl
36
40
this . _authToken = authToken
37
41
this . _activeUser = activeUser
38
42
this . _userTimeZone = userTimeZone
43
+ this . _kyOptions = kyOptions
39
44
}
40
45
41
46
public async getPage (
@@ -47,21 +52,21 @@ export class NotionAPI {
47
52
signFileUrls = true ,
48
53
chunkLimit = 100 ,
49
54
chunkNumber = 0 ,
50
- gotOptions
55
+ kyOptions
51
56
} : {
52
57
concurrency ?: number
53
58
fetchMissingBlocks ?: boolean
54
59
fetchCollections ?: boolean
55
60
signFileUrls ?: boolean
56
61
chunkLimit ?: number
57
62
chunkNumber ?: number
58
- gotOptions ?: OptionsOfJSONResponseBody
63
+ kyOptions ?: KyOptions
59
64
} = { }
60
65
) : Promise < notion . ExtendedRecordMap > {
61
66
const page = await this . getPageRaw ( pageId , {
62
67
chunkLimit,
63
68
chunkNumber,
64
- gotOptions
69
+ kyOptions
65
70
} )
66
71
const recordMap = page ?. recordMap as notion . ExtendedRecordMap
67
72
@@ -91,10 +96,9 @@ export class NotionAPI {
91
96
break
92
97
}
93
98
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
+ )
98
102
99
103
recordMap . block = { ...recordMap . block , ...newBlocks }
100
104
}
@@ -143,7 +147,7 @@ export class NotionAPI {
143
147
collectionViewId ,
144
148
collectionView ,
145
149
{
146
- gotOptions
150
+ kyOptions
147
151
}
148
152
)
149
153
@@ -194,7 +198,7 @@ export class NotionAPI {
194
198
// because it is preferable for many use cases as opposed to making these API calls
195
199
// lazily from the client-side.
196
200
if ( signFileUrls ) {
197
- await this . addSignedUrls ( { recordMap, contentBlockIds, gotOptions } )
201
+ await this . addSignedUrls ( { recordMap, contentBlockIds, kyOptions } )
198
202
}
199
203
200
204
return recordMap
@@ -203,11 +207,11 @@ export class NotionAPI {
203
207
public async addSignedUrls ( {
204
208
recordMap,
205
209
contentBlockIds,
206
- gotOptions = { }
210
+ kyOptions = { }
207
211
} : {
208
212
recordMap : notion . ExtendedRecordMap
209
213
contentBlockIds ?: string [ ]
210
- gotOptions ?: OptionsOfJSONResponseBody
214
+ kyOptions ?: KyOptions
211
215
} ) {
212
216
recordMap . signed_urls = { }
213
217
@@ -255,7 +259,7 @@ export class NotionAPI {
255
259
try {
256
260
const { signedUrls } = await this . getSignedFileUrls (
257
261
allFileInstances ,
258
- gotOptions
262
+ kyOptions
259
263
)
260
264
261
265
if ( signedUrls . length === allFileInstances . length ) {
@@ -275,13 +279,13 @@ export class NotionAPI {
275
279
public async getPageRaw (
276
280
pageId : string ,
277
281
{
278
- gotOptions ,
282
+ kyOptions ,
279
283
chunkLimit = 100 ,
280
284
chunkNumber = 0
281
285
} : {
282
286
chunkLimit ?: number
283
287
chunkNumber ?: number
284
- gotOptions ?: OptionsOfJSONResponseBody
288
+ kyOptions ?: KyOptions
285
289
} = { }
286
290
) {
287
291
const parsedPageId = parsePageId ( pageId )
@@ -301,7 +305,7 @@ export class NotionAPI {
301
305
return this . fetch < notion . PageChunk > ( {
302
306
endpoint : 'loadPageChunk' ,
303
307
body,
304
- gotOptions
308
+ kyOptions
305
309
} )
306
310
}
307
311
@@ -314,15 +318,15 @@ export class NotionAPI {
314
318
searchQuery = '' ,
315
319
userTimeZone = this . _userTimeZone ,
316
320
loadContentCover = true ,
317
- gotOptions
321
+ kyOptions
318
322
} : {
319
323
type ?: notion . CollectionViewType
320
324
limit ?: number
321
325
searchQuery ?: string
322
326
userTimeZone ?: string
323
327
userLocale ?: string
324
328
loadContentCover ?: boolean
325
- gotOptions ?: OptionsOfJSONResponseBody
329
+ kyOptions ?: KyOptions
326
330
} = { }
327
331
) {
328
332
const type = collectionView ?. type
@@ -493,27 +497,21 @@ export class NotionAPI {
493
497
} ,
494
498
loader
495
499
} ,
496
- gotOptions
500
+ kyOptions
497
501
} )
498
502
}
499
503
500
- public async getUsers (
501
- userIds : string [ ] ,
502
- gotOptions ?: OptionsOfJSONResponseBody
503
- ) {
504
+ public async getUsers ( userIds : string [ ] , kyOptions ?: KyOptions ) {
504
505
return this . fetch < notion . RecordValues < notion . User > > ( {
505
506
endpoint : 'getRecordValues' ,
506
507
body : {
507
508
requests : userIds . map ( ( id ) => ( { id, table : 'notion_user' } ) )
508
509
} ,
509
- gotOptions
510
+ kyOptions
510
511
} )
511
512
}
512
513
513
- public async getBlocks (
514
- blockIds : string [ ] ,
515
- gotOptions ?: OptionsOfJSONResponseBody
516
- ) {
514
+ public async getBlocks ( blockIds : string [ ] , kyOptions ?: KyOptions ) {
517
515
return this . fetch < notion . PageChunk > ( {
518
516
endpoint : 'syncRecordValues' ,
519
517
body : {
@@ -524,27 +522,24 @@ export class NotionAPI {
524
522
version : - 1
525
523
} ) )
526
524
} ,
527
- gotOptions
525
+ kyOptions
528
526
} )
529
527
}
530
528
531
529
public async getSignedFileUrls (
532
530
urls : types . SignedUrlRequest [ ] ,
533
- gotOptions ?: OptionsOfJSONResponseBody
531
+ kyOptions ?: KyOptions
534
532
) {
535
533
return this . fetch < types . SignedUrlResponse > ( {
536
534
endpoint : 'getSignedFileUrls' ,
537
535
body : {
538
536
urls
539
537
} ,
540
- gotOptions
538
+ kyOptions
541
539
} )
542
540
}
543
541
544
- public async search (
545
- params : notion . SearchParams ,
546
- gotOptions ?: OptionsOfJSONResponseBody
547
- ) {
542
+ public async search ( params : notion . SearchParams , kyOptions ?: KyOptions ) {
548
543
const body = {
549
544
type : 'BlocksInAncestor' ,
550
545
source : 'quick_find_public' ,
@@ -571,24 +566,25 @@ export class NotionAPI {
571
566
return this . fetch < notion . SearchResults > ( {
572
567
endpoint : 'search' ,
573
568
body,
574
- gotOptions
569
+ kyOptions
575
570
} )
576
571
}
577
572
578
573
public async fetch < T > ( {
579
574
endpoint,
580
575
body,
581
- gotOptions ,
576
+ kyOptions ,
582
577
headers : clientHeaders
583
578
} : {
584
579
endpoint : string
585
580
body : object
586
- gotOptions ?: OptionsOfJSONResponseBody
581
+ kyOptions ?: KyOptions
587
582
headers ?: any
588
583
} ) : Promise < T > {
589
584
const headers : any = {
590
585
...clientHeaders ,
591
- ...gotOptions ?. headers ,
586
+ ...this . _kyOptions ?. headers ,
587
+ ...kyOptions ?. headers ,
592
588
'Content-Type' : 'application/json'
593
589
}
594
590
@@ -602,21 +598,13 @@ export class NotionAPI {
602
598
603
599
const url = `${ this . _apiBaseUrl } /${ endpoint } `
604
600
605
- return got
601
+ return ky
606
602
. post ( url , {
607
- ...gotOptions ,
603
+ ...this . _kyOptions ,
604
+ ...kyOptions ,
608
605
json : body ,
609
606
headers
610
607
} )
611
608
. 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
- // })
621
609
}
622
610
}
0 commit comments