-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotionAPI.js
99 lines (82 loc) · 2.02 KB
/
NotionAPI.js
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
const https = require("https");
const NOTION_VERSION = "2021-05-13";
class NotionAPI {
constructor(token) {
this.token = token;
}
async getDatabase(databaseId) {
const options = {
host: "api.notion.com",
path: `/v1/databases/${databaseId}`,
method: "GET",
port: 443,
headers: {
"Notion-Version": NOTION_VERSION,
Authorization: `Bearer ${this.token}`,
},
};
return await this._request(options, {});
}
async queryDatabase(databaseId, filter, sorts) {
const options = {
host: "api.notion.com",
path: `/v1/databases/${databaseId}/query`,
method: "POST",
port: 443,
headers: {
"Notion-Version": NOTION_VERSION,
Authorization: `Bearer ${this.token}`,
"Content-Type": "application/json",
},
};
let body = {
filter,
sorts,
};
return await this._request(options, body);
}
async getPage(pageId) {
const options = {
host: "api.notion.com",
path: `/v1/pages/${pageId}`,
method: "GET",
port: 443,
headers: {
"Notion-Version": NOTION_VERSION,
Authorization: `Bearer ${this.token}`,
},
};
return await this._request(options, {});
}
async getBlockChildren(blockId) {
let options = {
host: "api.notion.com",
path: `/v1/blocks/${blockId}/children`,
method: "GET",
headers: {
"Notion-Version": NOTION_VERSION,
Authorization: `Bearer ${this.token}`,
},
};
return this._request(options, {});
}
_request(options, body) {
return new Promise((resolve, reject) => {
let req = https.request(options, (res) => {
let raw = "";
res.on("data", (chunk) => {
raw += chunk;
});
res.on("error", (err) => {
reject(err);
});
res.on("end", () => {
resolve(JSON.parse(raw));
});
});
req.write(JSON.stringify(body));
req.end();
});
}
}
module.exports = NotionAPI;