Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
171 lines (156 loc) · 4.24 KB
/
Copy pathserver.ts
File metadata and controls
171 lines (156 loc) · 4.24 KB
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
import { config } from "dotenv"
import express from "express"
import { Client, isFullDatabase } from "@notionhq/client"
config()
const app = express()
const notion = new Client({ auth: process.env.NOTION_KEY })
// http://expressjs.com/en/starter/static-files.html
app.use(express.static("public"))
app.use(express.json()) // for parsing application/json
// http://expressjs.com/en/starter/basic-routing.html
app.get("/", function (_request, response) {
response.sendFile(__dirname + "/views/index.html")
})
// Create new database. The page ID is set in the environment variables.
app.post("/databases", async function (request, response) {
const pageId = process.env.NOTION_PAGE_ID
const title = request.body.dbName
try {
const newDb = await notion.databases.create({
parent: {
type: "page_id",
page_id: pageId,
},
title: [
{
type: "text",
text: {
content: title,
},
},
],
initial_data_source: {
properties: {
Name: {
title: {},
},
},
},
})
// Check if we have full database permissions
if (!isFullDatabase(newDb)) {
response.json({
message: "error",
error: "No read permissions on database",
})
return
}
// Return the database info along with the first data source ID
const dataSourceId = newDb.data_sources[0].id
response.json({
message: "success!",
data: {
...newDb,
dataSourceId, // Add data source ID for easy access by the client
},
})
} catch (error) {
response.json({ message: "error", error })
}
})
// Create new page. The data source ID is provided in the web form.
app.post("/pages", async function (request, response) {
const { dbID, pageName, header } = request.body
try {
const newPage = await notion.pages.create({
parent: {
type: "data_source_id",
data_source_id: dbID, // Note: dbID will now be a data source ID from the client
},
properties: {
Name: {
title: [
{
text: {
content: pageName,
},
},
],
},
},
children: [
{
object: "block",
heading_2: {
rich_text: [
{
text: {
content: header,
},
},
],
},
},
],
})
response.json({ message: "success!", data: newPage })
} catch (error) {
response.json({ message: "error", error })
}
})
// Create new block (page content). The page ID is provided in the web form.
app.post("/blocks", async function (request, response) {
const { pageID, content } = request.body
try {
const newBlock = await notion.blocks.children.append({
block_id: pageID, // a block ID can be a page ID
children: [
{
// Use a paragraph as a default but the form or request can be updated to allow for other block types: https://developers.notion.com/reference/block#keys
paragraph: {
rich_text: [
{
text: {
content: content,
},
},
],
},
},
],
})
response.json({ message: "success!", data: newBlock })
} catch (error) {
response.json({ message: "error", error })
}
})
// Create new page comments. The page ID is provided in the web form.
app.post("/comments", async function (request, response) {
const { pageID, comment } = request.body
try {
const newComment = await notion.comments.create({
parent: {
page_id: pageID,
},
rich_text: [
{
text: {
content: comment,
},
},
],
})
response.json({ message: "success!", data: newComment })
} catch (error) {
response.json({ message: "error", error })
}
})
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
const address = listener.address()
if (typeof address === "object") {
console.log(`Your app is listening on port ${address.port}`)
} else {
console.log(`Your app is listening on socket ${address}`)
}
})