1- // import { promises as fs } from 'fs'
21import type * as notion from 'notion-types'
3- import ky , { type Options as KyOptions } from 'ky '
2+ import got , { type OptionsOfJSONResponseBody as Got_Options } from 'got '
43import {
54 getBlockCollectionId ,
65 getPageContentBlockIds ,
@@ -19,7 +18,7 @@ export class NotionAPI {
1918 private readonly _authToken ?: string
2019 private readonly _activeUser ?: string
2120 private readonly _userTimeZone : string
22- private readonly _kyOptions ?: KyOptions
21+ private readonly _kyOptions ?: Got_Options
2322
2423 constructor ( {
2524 apiBaseUrl = 'https://www.notion.so/api/v3' ,
@@ -33,7 +32,7 @@ export class NotionAPI {
3332 userLocale ?: string
3433 userTimeZone ?: string
3534 activeUser ?: string
36- kyOptions ?: KyOptions
35+ kyOptions ?: Got_Options
3736 } = { } ) {
3837 this . _apiBaseUrl = apiBaseUrl
3938 this . _authToken = authToken
@@ -65,7 +64,7 @@ export class NotionAPI {
6564 throwOnCollectionErrors ?: boolean
6665 collectionReducerLimit ?: number
6766 fetchRelationPages ?: boolean
68- kyOptions ?: KyOptions
67+ kyOptions ?: Got_Options
6968 } = { }
7069 ) : Promise < notion . ExtendedRecordMap > {
7170 const page = await this . getPageRaw ( pageId , {
@@ -232,7 +231,7 @@ export class NotionAPI {
232231
233232 fetchRelationPages = async (
234233 recordMap : notion . ExtendedRecordMap ,
235- kyOptions : KyOptions | undefined
234+ kyOptions : Got_Options | undefined
236235 ) : Promise < notion . BlockMap > => {
237236 const maxIterations = 10
238237
@@ -320,7 +319,7 @@ export class NotionAPI {
320319 } : {
321320 recordMap : notion . ExtendedRecordMap
322321 contentBlockIds ?: string [ ]
323- kyOptions ?: KyOptions
322+ kyOptions ?: Got_Options
324323 } ) {
325324 recordMap . signed_urls = { }
326325
@@ -401,7 +400,7 @@ export class NotionAPI {
401400 } : {
402401 chunkLimit ?: number
403402 chunkNumber ?: number
404- kyOptions ?: KyOptions
403+ kyOptions ?: Got_Options
405404 } = { }
406405 ) {
407406 const parsedPageId = parsePageId ( pageId )
@@ -442,7 +441,7 @@ export class NotionAPI {
442441 userTimeZone ?: string
443442 userLocale ?: string
444443 loadContentCover ?: boolean
445- kyOptions ?: KyOptions
444+ kyOptions ?: Got_Options
446445 } = { }
447446 ) {
448447 const type = collectionView ?. type
@@ -620,7 +619,9 @@ export class NotionAPI {
620619 loader
621620 } ,
622621 kyOptions : {
623- timeout : 60_000 ,
622+ timeout : {
623+ request : 60_000
624+ } ,
624625 ...kyOptions ,
625626 searchParams : {
626627 // TODO: spread kyOptions?.searchParams
@@ -630,7 +631,7 @@ export class NotionAPI {
630631 } )
631632 }
632633
633- public async getUsers ( userIds : string [ ] , kyOptions ?: KyOptions ) {
634+ public async getUsers ( userIds : string [ ] , kyOptions ?: Got_Options ) {
634635 return this . fetch < notion . RecordValues < notion . User > > ( {
635636 endpoint : 'getRecordValues' ,
636637 body : {
@@ -640,7 +641,7 @@ export class NotionAPI {
640641 } )
641642 }
642643
643- public async getBlocks ( blockIds : string [ ] , kyOptions ?: KyOptions ) {
644+ public async getBlocks ( blockIds : string [ ] , kyOptions ?: Got_Options ) {
644645 return this . fetch < notion . PageChunk > ( {
645646 endpoint : 'syncRecordValues' ,
646647 body : {
@@ -657,7 +658,7 @@ export class NotionAPI {
657658
658659 public async getSignedFileUrls (
659660 urls : types . SignedUrlRequest [ ] ,
660- kyOptions ?: KyOptions
661+ kyOptions ?: Got_Options
661662 ) {
662663 return this . fetch < types . SignedUrlResponse > ( {
663664 endpoint : 'getSignedFileUrls' ,
@@ -668,7 +669,7 @@ export class NotionAPI {
668669 } )
669670 }
670671
671- public async search ( params : notion . SearchParams , kyOptions ?: KyOptions ) {
672+ public async search ( params : notion . SearchParams , kyOptions ?: Got_Options ) {
672673 const body = {
673674 type : 'BlocksInAncestor' ,
674675 source : 'quick_find_public' ,
@@ -708,7 +709,7 @@ export class NotionAPI {
708709 } : {
709710 endpoint : string
710711 body : object
711- kyOptions ?: KyOptions
712+ kyOptions ?: Got_Options
712713 headers ?: any
713714 } ) : Promise < T > {
714715 const headers : any = {
@@ -728,21 +729,12 @@ export class NotionAPI {
728729
729730 const url = `${ this . _apiBaseUrl } /${ endpoint } `
730731
731- const res = await ky . post ( url , {
732- mode : 'no-cors' ,
733- ...this . _kyOptions ,
734- ...kyOptions ,
735- json : body ,
736- headers
737- } )
738-
739- // TODO: we're awaiting the first fetch and then separately awaiting
740- // `res.json()` because there seems to be some weird error which repros
741- // sporadically when loading collections where the body is already used.
742- // No idea why, but from my testing, separating these into two separate
743- // steps seems to fix the issue locally for me...
744- // console.log(endpoint, { bodyUsed: res.bodyUsed })
745-
746- return res . json < T > ( )
732+ return got
733+ . post ( url , {
734+ ...kyOptions ,
735+ json : body ,
736+ headers
737+ } )
738+ . json ( )
747739 }
748740}
0 commit comments