Skip to content

Commit 71f9db0

Browse files
committed
replace ky with native fetch
1 parent 443e10a commit 71f9db0

File tree

4 files changed

+60
-19
lines changed

4 files changed

+60
-19
lines changed

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

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// import { promises as fs } from 'fs'
22
import type * as notion from 'notion-types'
3-
import ky, { type Options as KyOptions } from 'ky'
3+
// import ky, { type Options as KyOptions } from 'ky'
44
import {
55
getBlockCollectionId,
66
getPageContentBlockIds,
@@ -707,6 +707,51 @@ export class NotionAPI {
707707
})
708708
}
709709

710+
// public async fetch<T>({
711+
// endpoint,
712+
// body,
713+
// kyOptions,
714+
// headers: clientHeaders
715+
// }: {
716+
// endpoint: string
717+
// body: object
718+
// kyOptions?: KyOptions
719+
// headers?: any
720+
// }): Promise<T> {
721+
// const headers: any = {
722+
// ...clientHeaders,
723+
// ...this._kyOptions?.headers,
724+
// ...kyOptions?.headers,
725+
// 'Content-Type': 'application/json'
726+
// }
727+
728+
// if (this._authToken) {
729+
// headers.cookie = `token_v2=${this._authToken}`
730+
// }
731+
732+
// if (this._activeUser) {
733+
// headers['x-notion-active-user-header'] = this._activeUser
734+
// }
735+
736+
// const url = `${this._apiBaseUrl}/${endpoint}`
737+
738+
// const res = await ky.post(url, {
739+
// mode: 'no-cors',
740+
// ...this._kyOptions,
741+
// ...kyOptions,
742+
// json: body,
743+
// headers
744+
// })
745+
746+
// // TODO: we're awaiting the first fetch and then separately awaiting
747+
// // `res.json()` because there seems to be some weird error which repros
748+
// // sporadically when loading collections where the body is already used.
749+
// // No idea why, but from my testing, separating these into two separate
750+
// // steps seems to fix the issue locally for me...
751+
// // console.log(endpoint, { bodyUsed: res.bodyUsed })
752+
753+
// return res.json<T>()
754+
// }
710755
public async fetch<T>({
711756
endpoint,
712757
body,
@@ -735,21 +780,17 @@ export class NotionAPI {
735780

736781
const url = `${this._apiBaseUrl}/${endpoint}`
737782

738-
const res = await ky.post(url, {
739-
mode: 'no-cors',
740-
...this._kyOptions,
741-
...kyOptions,
742-
json: body,
743-
headers
783+
const res = await fetch(url, {
784+
method: 'POST',
785+
headers,
786+
body: JSON.stringify(body),
787+
...kyOptions
744788
})
745789

746-
// TODO: we're awaiting the first fetch and then separately awaiting
747-
// `res.json()` because there seems to be some weird error which repros
748-
// sporadically when loading collections where the body is already used.
749-
// No idea why, but from my testing, separating these into two separate
750-
// steps seems to fix the issue locally for me...
751-
// console.log(endpoint, { bodyUsed: res.bodyUsed })
790+
if (!res.ok) {
791+
throw new Error(`HTTP error! status: ${res.status}`)
792+
}
752793

753-
return res.json<T>()
794+
return res.json() as Promise<T>
754795
}
755796
}

packages/react-notion-x/src/third-party/collection-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export function getCollectionGroups(
3434
}
3535

3636
// TODO: review dates format based on value.type ('week'|'month'|'year')
37-
queryValue = format(new Date(queryLabel), 'MMM d, YYY hh:mm aa')
37+
queryValue = format(new Date(queryLabel), 'MMM d, yyy hh:mm aa')
3838
}
3939

4040
return {

packages/react-notion-x/src/third-party/eval-formula.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ function evalFunctionFormula(
300300

301301
case 'object':
302302
if (value instanceof Date) {
303-
return format(value as Date, 'MMM d, YYY')
303+
return format(value as Date, 'MMM d, yyy')
304304
} else {
305305
// shouldn't ever get here
306306
return `${value}`

packages/react-notion-x/src/third-party/property.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export function PropertyImpl(props: IPropertyProps) {
9292
}
9393

9494
if (content instanceof Date) {
95-
content = format(content, 'MMM d, YYY hh:mm aa')
95+
content = format(content, 'MMM d, yyy hh:mm aa')
9696
}
9797
} catch {
9898
// console.log('error evaluating formula', schema.formula, err)
@@ -437,15 +437,15 @@ export function PropertyImpl(props: IPropertyProps) {
437437
const renderCreatedTimeValue = React.useMemo(
438438
() =>
439439
function CreatedTimeProperty() {
440-
return format(new Date(block!.created_time), 'MMM d, YYY hh:mm aa')
440+
return format(new Date(block!.created_time), 'MMM d, yyy hh:mm aa')
441441
},
442442
[block]
443443
)
444444

445445
const renderLastEditedTimeValue = React.useMemo(
446446
() =>
447447
function LastEditedTimeProperty() {
448-
return format(new Date(block!.last_edited_time), 'MMM d, YYY hh:mm aa')
448+
return format(new Date(block!.last_edited_time), 'MMM d, yyy hh:mm aa')
449449
},
450450
[block]
451451
)

0 commit comments

Comments
 (0)