notion-client
Advanced tools
Comparing version 0.2.3 to 0.3.0
export * from './notion-api'; | ||
export * from './types'; |
@@ -14,2 +14,3 @@ "use strict"; | ||
__exportStar(require("./notion-api"), exports); | ||
__exportStar(require("./types"), exports); | ||
//# sourceMappingURL=index.js.map |
import * as notion from 'notion-types'; | ||
import * as types from './types'; | ||
export declare class NotionAPI { | ||
@@ -13,3 +14,3 @@ private readonly _apiBaseUrl; | ||
}); | ||
getPage(pageId: string, { concurrency, fetchCollections }?: { | ||
getPage(pageId: string, { fetchCollections }?: { | ||
concurrency?: number; | ||
@@ -19,5 +20,15 @@ fetchCollections?: boolean; | ||
getPageRaw(pageId: string): Promise<notion.PageChunk>; | ||
getCollectionData(collectionId: string, collectionViewId: string, collectionType?: string): Promise<notion.CollectionInstance>; | ||
getCollectionData(collectionId: string, collectionViewId: string, { type, query, groups, limit, searchQuery, userTimeZone, userLocale, loadContentCover }?: { | ||
type?: notion.CollectionViewType; | ||
query?: any; | ||
groups?: any; | ||
limit?: number; | ||
searchQuery?: string; | ||
userTimeZone?: string; | ||
userLocale?: string; | ||
loadContentCover?: boolean; | ||
}): Promise<notion.CollectionInstance>; | ||
getUsers(userIds: string[]): Promise<notion.RecordValues<notion.User>>; | ||
getBlocks(blockIds: string[]): Promise<notion.PageChunk>; | ||
getSignedFileUrls(urls: types.SignedUrlRequest[]): Promise<types.SignedUrlResponse>; | ||
search(params: notion.SearchParams): Promise<notion.SearchResults>; | ||
@@ -24,0 +35,0 @@ fetch<T>({ endpoint, body }: { |
@@ -26,9 +26,13 @@ "use strict"; | ||
} | ||
getPage(pageId, { concurrency = 2, fetchCollections = true } = {}) { | ||
getPage(pageId, { fetchCollections = true } = {}) { | ||
var _a, _b, _c, _d; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.log('getPageRaw', pageId); | ||
const page = yield this.getPageRaw(pageId); | ||
const recordMap = page.recordMap; | ||
// ensure that all top-level maps exist | ||
recordMap.block = (_a = recordMap.block) !== null && _a !== void 0 ? _a : {}; | ||
recordMap.collection = (_b = recordMap.collection) !== null && _b !== void 0 ? _b : {}; | ||
recordMap.collection_view = (_c = recordMap.collection_view) !== null && _c !== void 0 ? _c : {}; | ||
recordMap.notion_user = (_d = recordMap.notion_user) !== null && _d !== void 0 ? _d : {}; | ||
recordMap.collection_query = {}; | ||
console.log('getPage', pageId); | ||
// fetch any missing content blocks | ||
@@ -46,3 +50,2 @@ while (true) { | ||
} | ||
console.log('getBlocks', pendingBlocks); | ||
const newBlocks = yield this.getBlocks(pendingBlocks).then((res) => res.recordMap.block); | ||
@@ -65,12 +68,19 @@ recordMap.block = Object.assign(Object.assign({}, recordMap.block), newBlocks); | ||
}); | ||
console.log('allCollectionInstances', allCollectionInstances); | ||
// fetch data for all collection view instances | ||
yield p_map_1.default(allCollectionInstances, (collectionInstance) => __awaiter(this, void 0, void 0, function* () { | ||
var _e, _f; | ||
const { collectionId, collectionViewId } = collectionInstance; | ||
const collectionData = yield this.getCollectionData(collectionId, collectionViewId); | ||
const collectionView = (_e = recordMap.collection_view[collectionViewId]) === null || _e === void 0 ? void 0 : _e.value; | ||
const collectionData = yield this.getCollectionData(collectionId, collectionViewId, { | ||
type: collectionView === null || collectionView === void 0 ? void 0 : collectionView.type, | ||
query: collectionView === null || collectionView === void 0 ? void 0 : collectionView.query2, | ||
groups: (_f = collectionView === null || collectionView === void 0 ? void 0 : collectionView.format) === null || _f === void 0 ? void 0 : _f.board_groups2 | ||
}); | ||
recordMap.block = Object.assign(Object.assign({}, recordMap.block), collectionData.recordMap.block); | ||
recordMap.collection = Object.assign(Object.assign({}, recordMap.collection), collectionData.recordMap.collection); | ||
recordMap.collection_view = Object.assign(Object.assign({}, recordMap.collection_view), collectionData.recordMap.collection_view); | ||
recordMap.notion_user = Object.assign(Object.assign({}, recordMap.notion_user), collectionData.recordMap.notion_user); | ||
recordMap.collection_query[collectionId] = Object.assign(Object.assign({}, recordMap.collection_query[collectionId]), { [collectionViewId]: collectionData.result }); | ||
}), { | ||
concurrency | ||
concurrency: 1 | ||
}); | ||
@@ -99,4 +109,26 @@ } | ||
} | ||
getCollectionData(collectionId, collectionViewId, collectionType = 'table') { | ||
getCollectionData(collectionId, collectionViewId, { type = 'table', query = { aggregations: [{ property: 'title', aggregator: 'count' }] }, groups = undefined, limit = 999999, searchQuery = '', userTimeZone = this._userTimeZone, userLocale = this._userLocale, loadContentCover = true } = {}) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
// TODO: All other collection types queries fail with 400 errors. | ||
// My guess is that they require slightly different query params, but since | ||
// their results are the same AFAICT, there's not much point in supporting | ||
// them. | ||
if (type !== 'table' && type !== 'board') { | ||
type = 'table'; | ||
} | ||
const loader = { | ||
type, | ||
limit, | ||
searchQuery, | ||
userTimeZone, | ||
userLocale, | ||
loadContentCover | ||
}; | ||
if (groups) { | ||
// used for 'board' collection view queries | ||
loader.groups = groups; | ||
} | ||
if (type === 'board') { | ||
console.log(JSON.stringify({ query, loader }, null, 2)); | ||
} | ||
return this.fetch({ | ||
@@ -107,11 +139,4 @@ endpoint: 'queryCollection', | ||
collectionViewId, | ||
query: { aggregations: [{ property: 'title', aggregator: 'count' }] }, | ||
loader: { | ||
type: collectionType, | ||
limit: 999999, | ||
searchQuery: '', | ||
userTimeZone: this._userTimeZone, | ||
userLocale: this._userLocale, | ||
loadContentCover: true | ||
} | ||
query, | ||
loader | ||
} | ||
@@ -143,2 +168,12 @@ }); | ||
} | ||
getSignedFileUrls(urls) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return this.fetch({ | ||
endpoint: 'getSignedFileUrls', | ||
body: { | ||
urls | ||
} | ||
}); | ||
}); | ||
} | ||
search(params) { | ||
@@ -145,0 +180,0 @@ return __awaiter(this, void 0, void 0, function* () { |
{ | ||
"name": "notion-client", | ||
"version": "0.2.3", | ||
"version": "0.3.0", | ||
"description": "TypeScript client for the unofficial Notion API.", | ||
@@ -18,4 +18,4 @@ "repository": "saasify-sh/notion", | ||
"got": "^11.5.2", | ||
"notion-types": "^0.2.3", | ||
"notion-utils": "^0.2.2", | ||
"notion-types": "^0.3.0", | ||
"notion-utils": "^0.3.0", | ||
"p-map": "^4.0.0" | ||
@@ -26,3 +26,3 @@ }, | ||
}, | ||
"gitHead": "48f5d736ee36c8ff1764741bf9690bfd044fa030" | ||
"gitHead": "3e4e367405e7347c3d5c88a7ed3d0e6d8825ed06" | ||
} |
export * from './notion-api' | ||
export * from './types' |
import got from 'got' | ||
import pMap from 'p-map' | ||
import { parsePageId } from 'notion-utils' | ||
import * as notion from 'notion-types' | ||
import * as types from './types' | ||
export class NotionAPI { | ||
@@ -32,13 +35,15 @@ private readonly _apiBaseUrl: string | ||
{ | ||
concurrency = 2, | ||
fetchCollections = true | ||
}: { concurrency?: number; fetchCollections?: boolean } = {} | ||
): Promise<notion.ExtendedRecordMap> { | ||
console.log('getPageRaw', pageId) | ||
const page = await this.getPageRaw(pageId) | ||
const recordMap: notion.ExtendedRecordMap = page.recordMap | ||
const recordMap = page.recordMap as notion.ExtendedRecordMap | ||
// ensure that all top-level maps exist | ||
recordMap.block = recordMap.block ?? {} | ||
recordMap.collection = recordMap.collection ?? {} | ||
recordMap.collection_view = recordMap.collection_view ?? {} | ||
recordMap.notion_user = recordMap.notion_user ?? {} | ||
recordMap.collection_query = {} | ||
console.log('getPage', pageId) | ||
// fetch any missing content blocks | ||
@@ -59,3 +64,2 @@ while (true) { | ||
console.log('getBlocks', pendingBlocks) | ||
const newBlocks = await this.getBlocks(pendingBlocks).then( | ||
@@ -86,4 +90,2 @@ (res) => res.recordMap.block | ||
console.log('allCollectionInstances', allCollectionInstances) | ||
// fetch data for all collection view instances | ||
@@ -94,5 +96,13 @@ await pMap( | ||
const { collectionId, collectionViewId } = collectionInstance | ||
const collectionView = | ||
recordMap.collection_view[collectionViewId]?.value | ||
const collectionData = await this.getCollectionData( | ||
collectionId, | ||
collectionViewId | ||
collectionViewId, | ||
{ | ||
type: collectionView?.type, | ||
query: collectionView?.query2, | ||
groups: collectionView?.format?.board_groups2 | ||
} | ||
) | ||
@@ -105,2 +115,12 @@ | ||
recordMap.collection = { | ||
...recordMap.collection, | ||
...collectionData.recordMap.collection | ||
} | ||
recordMap.collection_view = { | ||
...recordMap.collection_view, | ||
...collectionData.recordMap.collection_view | ||
} | ||
recordMap.notion_user = { | ||
@@ -117,3 +137,3 @@ ...recordMap.notion_user, | ||
{ | ||
concurrency | ||
concurrency: 1 | ||
} | ||
@@ -148,4 +168,48 @@ ) | ||
collectionViewId: string, | ||
collectionType: string = 'table' | ||
{ | ||
type = 'table', | ||
query = { aggregations: [{ property: 'title', aggregator: 'count' }] }, | ||
groups = undefined, | ||
limit = 999999, | ||
searchQuery = '', | ||
userTimeZone = this._userTimeZone, | ||
userLocale = this._userLocale, | ||
loadContentCover = true | ||
}: { | ||
type?: notion.CollectionViewType | ||
query?: any | ||
groups?: any | ||
limit?: number | ||
searchQuery?: string | ||
userTimeZone?: string | ||
userLocale?: string | ||
loadContentCover?: boolean | ||
} = {} | ||
) { | ||
// TODO: All other collection types queries fail with 400 errors. | ||
// My guess is that they require slightly different query params, but since | ||
// their results are the same AFAICT, there's not much point in supporting | ||
// them. | ||
if (type !== 'table' && type !== 'board') { | ||
type = 'table' | ||
} | ||
const loader: any = { | ||
type, | ||
limit, | ||
searchQuery, | ||
userTimeZone, | ||
userLocale, | ||
loadContentCover | ||
} | ||
if (groups) { | ||
// used for 'board' collection view queries | ||
loader.groups = groups | ||
} | ||
if (type === 'board') { | ||
console.log(JSON.stringify({ query, loader }, null, 2)) | ||
} | ||
return this.fetch<notion.CollectionInstance>({ | ||
@@ -156,11 +220,4 @@ endpoint: 'queryCollection', | ||
collectionViewId, | ||
query: { aggregations: [{ property: 'title', aggregator: 'count' }] }, | ||
loader: { | ||
type: collectionType, | ||
limit: 999999, | ||
searchQuery: '', | ||
userTimeZone: this._userTimeZone, | ||
userLocale: this._userLocale, | ||
loadContentCover: true | ||
} | ||
query, | ||
loader | ||
} | ||
@@ -196,2 +253,11 @@ }) | ||
public async getSignedFileUrls(urls: types.SignedUrlRequest[]) { | ||
return this.fetch<types.SignedUrlResponse>({ | ||
endpoint: 'getSignedFileUrls', | ||
body: { | ||
urls | ||
} | ||
}) | ||
} | ||
public async search(params: notion.SearchParams) { | ||
@@ -198,0 +264,0 @@ return this.fetch<notion.SearchResults>({ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
333855
30
4544
+ Addednotion-types@0.3.0(transitive)
+ Addednotion-utils@0.3.0(transitive)
- Removednotion-types@0.2.3(transitive)
- Removednotion-utils@0.2.2(transitive)
Updatednotion-types@^0.3.0
Updatednotion-utils@^0.3.0