-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
notiondb.ts
268 lines (226 loc) Β· 7.06 KB
/
notiondb.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
import { Document } from "@langchain/core/documents";
import { getEnvironmentVariable } from "@langchain/core/utils/env";
import { BaseDocumentLoader } from "../base.js";
const NOTION_BASE_URL = "https://api.notion.com/v1";
/**
* Interface representing the parameters for the NotionDBLoader class. It
* includes the database ID, Notion integration token, Notion API version,
* and page size limit.
*/
export interface NotionDBLoaderParams {
databaseId: string;
notionIntegrationToken?: string;
notionApiVersion?: string;
pageSizeLimit?: number;
}
/**
* Interface representing a Notion page. It includes properties such as
* the page ID, created time, last edited time, archived status, parent
* type, and properties.
*/
interface NotionPage {
id: string;
object: "page";
created_time: string;
last_edited_time: string;
archived: boolean;
parent: {
type: "database_id" | "page_id";
database_id?: string;
page_id?: string;
};
properties: {
// @see https://developers.notion.com/reference/retrieve-a-page
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[propertyName: string]: any;
};
}
/**
* @deprecated use the `NotionAPILoader` class instead.
* @example
* ```typescript
* const loader = new NotionDBLoader({
* pageSizeLimit: 10,
* databaseId: "{databaseId}",
* notionIntegrationToken: "{notionIntegrationToken}",
* });
* const docs = await loader.load();
* ```
*/
export class NotionDBLoader
extends BaseDocumentLoader
implements NotionDBLoaderParams
{
public integrationToken: string;
public databaseId: string;
public notionApiVersion: string;
public pageSizeLimit: number;
private headers: Record<string, string> = {};
constructor({
databaseId,
notionApiVersion = "2022-06-28",
notionIntegrationToken = getEnvironmentVariable("NOTION_INTEGRATION_TOKEN"),
pageSizeLimit = 50,
}: NotionDBLoaderParams) {
super();
if (!notionIntegrationToken) {
throw new Error("You must provide a Notion integration token.");
}
this.integrationToken = notionIntegrationToken;
this.pageSizeLimit = pageSizeLimit;
this.notionApiVersion = notionApiVersion;
this.databaseId = databaseId;
this.headers = {
Authorization: `Bearer ${this.integrationToken}`,
"Content-Type": "application/json",
"Notion-Version": notionApiVersion,
};
}
/**
* Loads the documents from Notion based on the specified options.
* @returns An array of Document objects.
*/
async load(): Promise<Document[]> {
const pageIds = await this.retrievePageIds();
const documents: Document[] = [];
for (const pageId of pageIds) {
documents.push(await this.loadPage(pageId));
}
return documents;
}
/**
* Retrieves the IDs of the pages in the Notion database.
* @returns An array of page IDs.
*/
private async retrievePageIds(): Promise<string[]> {
const url = `${NOTION_BASE_URL}/databases/${this.databaseId}/query`;
const pageIds: string[] = [];
const query: { page_size: number; start_cursor?: number } = {
page_size: this.pageSizeLimit,
};
let hasMore = true;
while (hasMore) {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(query),
headers: this.headers,
});
const data = await response.json();
if (!response.ok) {
throw new Error(
`Failed to load data from Notion. Please check your integration token and database id.`
);
}
const { results, has_more, next_cursor } = data;
pageIds.push(...(results?.map((page: NotionPage) => page.id) ?? []));
hasMore = has_more;
query.start_cursor = next_cursor;
}
return pageIds;
}
/**
* Loads a Notion page and returns it as a Document object.
* @param pageId The ID of the Notion page to load.
* @returns A Document object representing the loaded Notion page.
*/
private async loadPage(pageId: string): Promise<Document> {
const url = `${NOTION_BASE_URL}/pages/${pageId}`;
const response = await fetch(url, { method: "GET", headers: this.headers });
const data = await response.json();
if (!response.ok) {
throw new Error(
`Unable to fetch page: ${response.status} ${JSON.stringify(data)}`
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const metadata: Record<string, any> = {};
const { properties } = data;
for (const key of Object.keys(properties)) {
const item = properties[key];
const itemType = item.type;
let value;
switch (itemType) {
case "rich_text":
value =
item?.rich_text && item?.rich_text.length > 0
? item?.rich_text[0].plain_text
: null;
break;
case "title":
value =
item?.title && item?.title.length > 0
? item?.title[0].plain_text
: null;
break;
case "multi_select":
if (item?.multi_select && item?.multi_select.length > 0) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
value = item?.multi_select.map((el: any) => el.name);
}
break;
case "url":
value = item?.url ? item.url : null;
break;
default:
break;
}
if (value) {
metadata[key.toLowerCase()] = value;
}
}
metadata.id = pageId;
return {
pageContent: await this.loadBlocks(pageId),
metadata,
};
}
/**
* Loads the blocks of a Notion page and returns them as a string.
* @param blockId The ID of the block to load.
* @param numberOfTabs The number of tabs to use for indentation.
* @returns A string representing the loaded blocks.
*/
private async loadBlocks(blockId: string, numberOfTabs = 0): Promise<string> {
const resultLinesArr = [];
let currentBlockId = blockId;
while (currentBlockId) {
const response = await fetch(
`${NOTION_BASE_URL}/blocks/${currentBlockId}/children`,
{
method: "GET",
headers: this.headers,
}
);
const data = await response.json();
if (!response.ok) {
throw new Error(
`Unable to fetch block: ${response.status} ${JSON.stringify(data)}`
);
}
for (const result of data.results) {
const resultObj = result[result.type];
if (!resultObj.rich_text) {
continue;
}
const curResultTextArr = [];
for (const richText of resultObj.rich_text) {
if (richText.text) {
curResultTextArr.push(
"\t".repeat(numberOfTabs) + richText.text.content
);
}
}
if (result.has_children) {
const childrenText = await this.loadBlocks(
result.id,
numberOfTabs + 1
);
curResultTextArr.push(childrenText);
}
resultLinesArr.push(curResultTextArr.join("\n"));
}
currentBlockId = data.next_cursor;
}
return resultLinesArr.join("\n");
}
}