-
Notifications
You must be signed in to change notification settings - Fork 143
/
notion.ts
145 lines (122 loc) · 4.05 KB
/
notion.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
import { Client } from '@notionhq/client';
import { NotionPage, Repo } from './types';
import { DatabasesQueryResponse } from '@notionhq/client/build/src/api-endpoints';
import { get, save } from './cache';
// TODO: add assertion
const databaseId = process.env.NOTION_DATABASE_ID as string;
const NAMESPACE = 'notion-page';
export class Notion {
private notion: Client;
constructor() {
this.notion = new Client({
auth: process.env.NOTION_API_KEY,
});
this.pages = get(NAMESPACE, {});
console.log(`Notion: restored from cache, count is ${Object.keys(this.pages).length}`);
}
save() {
save(NAMESPACE, this.pages);
}
pages: Record<string, { id: string }> = {};
hasPage(name: string) {
return !!this.pages[name];
}
/**
* full-sync pages in database
*/
async fullSyncIfNeeded() {
if (Object.keys(this.pages).length) {
console.log(`Notion: skipped sync due to cache`);
return;
}
console.log('Notion: Start to get all pages');
let hasNext = true;
let cursor: string | undefined = undefined;
while (hasNext) {
const database: DatabasesQueryResponse = await this.notion.databases.query({
database_id: databaseId,
page_size: 100,
start_cursor: cursor,
});
this.addPages(database.results as NotionPage[]);
hasNext = database.has_more;
// @ts-ignore
cursor = database.next_cursor;
}
console.log(`Notion: Get all pages success, count is ${Object.keys(this.pages).length}`);
this.save();
}
addPages(pages: NotionPage[]) {
pages.forEach((page) => {
this.pages[page.properties.Name.title[0].plain_text] = {
id: page.id,
};
});
this.save();
}
async insertPage(repo: Repo) {
if (repo.description && repo.description.length >= 2000) {
repo.description = repo.description.substr(0, 120) + '...'
}
const data = await this.notion.pages.create({
parent: {
database_id: databaseId,
},
properties: {
Name: {
type: 'title',
title: [
{
type: 'text',
text: {
content: repo.nameWithOwner,
},
},
],
},
Type: {
type: 'select',
select: {
name: 'Star',
},
},
Link: {
type: 'url',
url: repo.url,
},
Description: {
type: 'rich_text',
rich_text: [
{
type: 'text',
text: {
content: repo.description || '',
},
},
],
},
'Primary Language': {
type: 'select',
select: {
name: repo?.primaryLanguage?.name || 'null',
},
},
'Repository Topics': {
type: 'multi_select',
multi_select: repo.repositoryTopics || [],
},
'Starred At': {
type: 'date',
date: {
start: repo.starredAt,
end: repo.starredAt,
},
},
},
});
this.pages[repo.nameWithOwner] = { id: data.id };
console.log(`insert page ${repo.nameWithOwner} success, page id is ${data.id}`);
this.save();
}
}
export const notion = new Notion();