-
Notifications
You must be signed in to change notification settings - Fork 0
/
notionCreateGCPFile.js
82 lines (73 loc) · 2.1 KB
/
notionCreateGCPFile.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
import fetch from "node-fetch";
const integrationToken = process.env.INTEGRATION_TOKEN;
export async function notionCreateGCPFile(uploadedFileDetails) {
const { fileName, folderName, publicUrl } = uploadedFileDetails;
const pageProperties = {
Name: {
title: [
{
text: {
content: fileName,
},
},
],
},
URL: {
url: publicUrl, // URL for the new page
},
Folder: {
select: {
name: folderName, // Selecting "Logos" as the folder category
},
},
// Assuming you want to link to an existing page in the related database,
// you would need the ID of the page you're linking to. For simplicity,
// this example won't include a direct relation update as it requires
// fetching and specifying existing page IDs in the related database.
};
const databaseId = "1d65013c04d945fbab1e8ee94ccff836";
const emoji = "🖼️";
const children = [
{
object: "block",
type: "image",
image: {
type: "external",
external: {
url: publicUrl,
},
},
},
];
createPageInDatabase(databaseId, pageProperties, emoji, children);
}
async function createPageInDatabase(databaseId, pageProperties, emoji, children) {
var requestBody = { parent: { database_id: databaseId }, properties: pageProperties };
if (emoji) {
requestBody.icon = { type: "emoji", emoji: emoji };
}
if (children) {
requestBody.children = children;
}
console.log(requestBody);
try {
const response = await fetch(`https://api.notion.com/v1/pages`, {
method: "POST",
headers: {
Authorization: `Bearer ${integrationToken}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error(`Network response was not ok: ${response.statusText}`);
}
const data = await response.json();
console.log("Page created:", data.id);
return data;
} catch (error) {
console.error("Error:", error);
return null;
}
}