forked from NotionX/react-notion-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotion-api.ts
614 lines (552 loc) · 15.8 KB
/
notion-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// import { promises as fs } from 'fs'
import type * as notion from 'notion-types'
import ky, { type Options as KyOptions } from 'ky'
import {
getBlockCollectionId,
getPageContentBlockIds,
parsePageId,
uuidToId
} from 'notion-utils'
import pMap from 'p-map'
import type * as types from './types'
/**
* Main Notion API client.
*/
export class NotionAPI {
private readonly _apiBaseUrl: string
private readonly _authToken?: string
private readonly _activeUser?: string
private readonly _userTimeZone: string
private readonly _kyOptions?: KyOptions
constructor({
apiBaseUrl = 'https://www.notion.so/api/v3',
authToken,
activeUser,
userTimeZone = 'America/New_York',
kyOptions
}: {
apiBaseUrl?: string
authToken?: string
userLocale?: string
userTimeZone?: string
activeUser?: string
kyOptions?: KyOptions
} = {}) {
this._apiBaseUrl = apiBaseUrl
this._authToken = authToken
this._activeUser = activeUser
this._userTimeZone = userTimeZone
this._kyOptions = kyOptions
}
public async getPage(
pageId: string,
{
concurrency = 3,
fetchMissingBlocks = true,
fetchCollections = true,
signFileUrls = true,
chunkLimit = 100,
chunkNumber = 0,
kyOptions
}: {
concurrency?: number
fetchMissingBlocks?: boolean
fetchCollections?: boolean
signFileUrls?: boolean
chunkLimit?: number
chunkNumber?: number
kyOptions?: KyOptions
} = {}
): Promise<notion.ExtendedRecordMap> {
const page = await this.getPageRaw(pageId, {
chunkLimit,
chunkNumber,
kyOptions
})
const recordMap = page?.recordMap as notion.ExtendedRecordMap
if (!recordMap?.block) {
throw new Error(`Notion page not found "${uuidToId(pageId)}"`)
}
// ensure that all top-level maps exist
recordMap.collection = recordMap.collection ?? {}
recordMap.collection_view = recordMap.collection_view ?? {}
recordMap.notion_user = recordMap.notion_user ?? {}
// additional mappings added for convenience
// note: these are not native notion objects
recordMap.collection_query = {}
recordMap.signed_urls = {}
if (fetchMissingBlocks) {
// eslint-disable-next-line no-constant-condition
while (true) {
// fetch any missing content blocks
const pendingBlockIds = getPageContentBlockIds(recordMap).filter(
(id) => !recordMap.block[id]
)
if (!pendingBlockIds.length) {
break
}
const newBlocks = await this.getBlocks(pendingBlockIds, kyOptions).then(
(res) => res.recordMap.block
)
recordMap.block = { ...recordMap.block, ...newBlocks }
}
}
const contentBlockIds = getPageContentBlockIds(recordMap)
// Optionally fetch all data for embedded collections and their associated views.
// NOTE: We're eagerly fetching *all* data for each collection and all of its views.
// This is really convenient in order to ensure that all data needed for a given
// Notion page is readily available for use cases involving server-side rendering
// and edge caching.
if (fetchCollections) {
const allCollectionInstances: Array<{
collectionId: string
collectionViewId: string
}> = contentBlockIds.flatMap((blockId) => {
const block = recordMap.block[blockId]?.value
const collectionId =
block &&
(block.type === 'collection_view' ||
block.type === 'collection_view_page') &&
getBlockCollectionId(block, recordMap)
if (collectionId) {
return block.view_ids?.map((collectionViewId) => ({
collectionId,
collectionViewId
}))
} else {
return []
}
})
// fetch data for all collection view instances
await pMap(
allCollectionInstances,
async (collectionInstance) => {
const { collectionId, collectionViewId } = collectionInstance
const collectionView =
recordMap.collection_view[collectionViewId]?.value
try {
const collectionData = await this.getCollectionData(
collectionId,
collectionViewId,
collectionView,
{
kyOptions
}
)
// await fs.writeFile(
// `${collectionId}-${collectionViewId}.json`,
// JSON.stringify(collectionData.result, null, 2)
// )
recordMap.block = {
...recordMap.block,
...collectionData.recordMap.block
}
recordMap.collection = {
...recordMap.collection,
...collectionData.recordMap.collection
}
recordMap.collection_view = {
...recordMap.collection_view,
...collectionData.recordMap.collection_view
}
recordMap.notion_user = {
...recordMap.notion_user,
...collectionData.recordMap.notion_user
}
recordMap.collection_query![collectionId] = {
...recordMap.collection_query![collectionId],
[collectionViewId]: (collectionData.result as any)?.reducerResults
}
} catch (err: any) {
// It's possible for public pages to link to private collections, in which case
// Notion returns a 400 error
console.warn('NotionAPI collectionQuery error', pageId, err.message)
console.error(err)
}
},
{
concurrency
}
)
}
// Optionally fetch signed URLs for any embedded files.
// NOTE: Similar to collection data, we default to eagerly fetching signed URL info
// because it is preferable for many use cases as opposed to making these API calls
// lazily from the client-side.
if (signFileUrls) {
await this.addSignedUrls({ recordMap, contentBlockIds, kyOptions })
}
return recordMap
}
public async addSignedUrls({
recordMap,
contentBlockIds,
kyOptions = {}
}: {
recordMap: notion.ExtendedRecordMap
contentBlockIds?: string[]
kyOptions?: KyOptions
}) {
recordMap.signed_urls = {}
if (!contentBlockIds) {
contentBlockIds = getPageContentBlockIds(recordMap)
}
const allFileInstances = contentBlockIds.flatMap((blockId) => {
const block = recordMap.block[blockId]?.value
if (
block &&
(block.type === 'pdf' ||
block.type === 'audio' ||
(block.type === 'image' && block.file_ids?.length) ||
block.type === 'video' ||
block.type === 'file' ||
block.type === 'page')
) {
const source =
block.type === 'page'
? block.format?.page_cover
: block.properties?.source?.[0]?.[0]
// console.log(block, source)
if (source) {
if (source.includes('secure.notion-static.com') || source.includes('prod-files-secure')) {
return {
permissionRecord: {
table: 'block',
id: block.id
},
url: source
};
}
return []
}
}
return []
})
if (allFileInstances.length > 0) {
try {
const { signedUrls } = await this.getSignedFileUrls(
allFileInstances,
kyOptions
)
if (signedUrls.length === allFileInstances.length) {
for (const [i, file] of allFileInstances.entries()) {
const signedUrl = signedUrls[i]
if (!signedUrl) continue
const blockId = file.permissionRecord.id
if (!blockId) continue
recordMap.signed_urls[blockId] = signedUrl
}
}
} catch (err) {
console.warn('NotionAPI getSignedfileUrls error', err)
}
}
}
public async getPageRaw(
pageId: string,
{
kyOptions,
chunkLimit = 100,
chunkNumber = 0
}: {
chunkLimit?: number
chunkNumber?: number
kyOptions?: KyOptions
} = {}
) {
const parsedPageId = parsePageId(pageId)
if (!parsedPageId) {
throw new Error(`invalid notion pageId "${pageId}"`)
}
const body = {
pageId: parsedPageId,
limit: chunkLimit,
chunkNumber,
cursor: { stack: [] },
verticalColumns: false
}
return this.fetch<notion.PageChunk>({
endpoint: 'loadPageChunk',
body,
kyOptions
})
}
public async getCollectionData(
collectionId: string,
collectionViewId: string,
collectionView: any,
{
limit = 9999,
searchQuery = '',
userTimeZone = this._userTimeZone,
loadContentCover = true,
kyOptions
}: {
type?: notion.CollectionViewType
limit?: number
searchQuery?: string
userTimeZone?: string
userLocale?: string
loadContentCover?: boolean
kyOptions?: KyOptions
} = {}
) {
const type = collectionView?.type
const isBoardType = type === 'board'
const groupBy = isBoardType
? collectionView?.format?.board_columns_by
: collectionView?.format?.collection_group_by
let filters = []
if (collectionView?.format?.property_filters) {
filters = collectionView.format?.property_filters.map(
(filterObj: any) => {
//get the inner filter
return {
filter: filterObj?.filter?.filter,
property: filterObj?.filter?.property
}
}
)
}
//Fixes formula filters from not working
if (collectionView?.query2?.filter?.filters) {
filters.push(...collectionView.query2.filter.filters)
}
let loader: any = {
type: 'reducer',
reducers: {
collection_group_results: {
type: 'results',
limit,
loadContentCover
}
},
sort: [],
...collectionView?.query2,
filter: {
filters,
operator: 'and'
},
searchQuery,
userTimeZone
}
if (groupBy) {
const groups =
collectionView?.format?.board_columns ||
collectionView?.format?.collection_groups ||
[]
const iterators = [isBoardType ? 'board' : 'group_aggregation', 'results']
const operators = {
checkbox: 'checkbox_is',
url: 'string_starts_with',
text: 'string_starts_with',
select: 'enum_is',
multi_select: 'enum_contains',
created_time: 'date_is_within',
undefined: 'is_empty'
}
const reducersQuery: Record<string, any> = {}
for (const group of groups) {
const {
property,
value: { value, type }
} = group
for (const iterator of iterators) {
const iteratorProps =
iterator === 'results'
? {
type: iterator,
limit
}
: {
type: 'aggregation',
aggregation: {
aggregator: 'count'
}
}
const isUncategorizedValue = value === undefined
const isDateValue = value?.range
// TODO: review dates reducers
const queryLabel = isUncategorizedValue
? 'uncategorized'
: isDateValue
? value.range?.start_date || value.range?.end_date
: value?.value || value
const queryValue =
!isUncategorizedValue && (isDateValue || value?.value || value)
reducersQuery[`${iterator}:${type}:${queryLabel}`] = {
...iteratorProps,
filter: {
operator: 'and',
filters: [
{
property,
filter: {
operator: !isUncategorizedValue
? operators[type as keyof typeof operators]
: 'is_empty',
...(!isUncategorizedValue && {
value: {
type: 'exact',
value: queryValue
}
})
}
}
]
}
}
}
}
const reducerLabel = isBoardType ? 'board_columns' : `${type}_groups`
loader = {
type: 'reducer',
reducers: {
[reducerLabel]: {
type: 'groups',
groupBy,
...(collectionView?.query2?.filter && {
filter: collectionView?.query2?.filter
}),
groupSortPreference: groups.map((group: any) => group?.value),
limit
},
...reducersQuery
},
...collectionView?.query2,
searchQuery,
userTimeZone,
//TODO: add filters here
filter: {
filters,
operator: 'and'
}
}
}
// if (isBoardType) {
// console.log(
// JSON.stringify(
// {
// collectionId,
// collectionViewId,
// loader,
// groupBy: groupBy || 'NONE',
// collectionViewQuery: collectionView.query2 || 'NONE'
// },
// null,
// 2
// )
// )
// }
return this.fetch<notion.CollectionInstance>({
endpoint: 'queryCollection',
body: {
collection: {
id: collectionId
},
collectionView: {
id: collectionViewId
},
loader
},
kyOptions
})
}
public async getUsers(userIds: string[], kyOptions?: KyOptions) {
return this.fetch<notion.RecordValues<notion.User>>({
endpoint: 'getRecordValues',
body: {
requests: userIds.map((id) => ({ id, table: 'notion_user' }))
},
kyOptions
})
}
public async getBlocks(blockIds: string[], kyOptions?: KyOptions) {
return this.fetch<notion.PageChunk>({
endpoint: 'syncRecordValues',
body: {
requests: blockIds.map((blockId) => ({
// TODO: when to use table 'space' vs 'block'?
table: 'block',
id: blockId,
version: -1
}))
},
kyOptions
})
}
public async getSignedFileUrls(
urls: types.SignedUrlRequest[],
kyOptions?: KyOptions
) {
return this.fetch<types.SignedUrlResponse>({
endpoint: 'getSignedFileUrls',
body: {
urls
},
kyOptions
})
}
public async search(params: notion.SearchParams, kyOptions?: KyOptions) {
const body = {
type: 'BlocksInAncestor',
source: 'quick_find_public',
ancestorId: parsePageId(params.ancestorId),
sort: {
field: 'relevance'
},
limit: params.limit || 20,
query: params.query,
filters: {
isDeletedOnly: false,
isNavigableOnly: false,
excludeTemplates: true,
requireEditPermissions: false,
ancestors: [],
createdBy: [],
editedBy: [],
lastEditedTime: {},
createdTime: {},
...params.filters
}
}
return this.fetch<notion.SearchResults>({
endpoint: 'search',
body,
kyOptions
})
}
public async fetch<T>({
endpoint,
body,
kyOptions,
headers: clientHeaders
}: {
endpoint: string
body: object
kyOptions?: KyOptions
headers?: any
}): Promise<T> {
const headers: any = {
...clientHeaders,
...this._kyOptions?.headers,
...kyOptions?.headers,
'Content-Type': 'application/json'
}
if (this._authToken) {
headers.cookie = `token_v2=${this._authToken}`
}
if (this._activeUser) {
headers['x-notion-active-user-header'] = this._activeUser
}
const url = `${this._apiBaseUrl}/${endpoint}`
return ky
.post(url, {
...this._kyOptions,
...kyOptions,
json: body,
headers
})
.json<T>()
}
}