-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplementation.js
98 lines (89 loc) · 2.92 KB
/
implementation.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
/**
* Perform a query operation on a Notion database.
* @param {Object} params - The parameters for the operation.
* @param {string} [params.databaseId] - The ID of the Notion database for create and read operations.
* @param {Array} [params.databaseStructure] - A list of properties in the Notion database structure, used to generate filter or sort criteria..
* @param {Object} [params.filter] - Optional filter for querying pages in the read operation.
* @param {Array} [params.sorts] - Optional sorting criteria for page results in the read operation.
* @param {number} [params.pageSize] - The number of results per page for the read operation.
* @param {string} [params.startCursor] - The start cursor for pagination in the read operation.
* @returns {Promise<Object>} - The response data from the Notion API.
*/
async function perform_notion_database_query(params, userSettings) {
const { databaseId, databaseStructure = [] } = params;
const { pluginServer, notionApiKey } = userSettings;
if (!pluginServer) {
throw new Error(
"Missing the Plugin Server URL. Please set it in the plugin settings."
);
}
if (!notionApiKey) {
throw new Error(
"Missing the Notion API Key. Please set it in the plugin settings."
);
}
if (!databaseId) {
throw new Error(
"Missing the Database Id. Please provide specific Database ID or Database URL"
);
}
if (!databaseStructure.length) {
throw new Error(
"Missing the Database Structure Properties. Please provide valid structure of your provided database."
);
}
try {
const result = await queryPages({
pluginServerUrl: pluginServer,
notionApiKey: notionApiKey,
databaseId: databaseId,
databaseStructure: databaseStructure,
filter: params.filter ?? {},
sorts: params.sorts ?? [],
pageSize: params.pageSize ?? 100,
startCursor: params.startCursor,
});
return result;
} catch (error) {
return { error: error.message };
}
}
async function queryPages({
notionApiKey,
pluginServerUrl,
databaseId,
databaseStructure,
filter,
sorts,
pageSize = 100,
startCursor = null,
}) {
const requestBody = {
notionApiKey: notionApiKey,
databaseId: databaseId,
pageSize: pageSize,
databaseStructure: databaseStructure,
};
if (filter) requestBody.filter = filter;
if (sorts) requestBody.sorts = sorts;
if (startCursor) requestBody.startCursor = startCursor;
try {
const response = await fetch(
`${pluginServerUrl}/notion-database/query-pages`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
}
);
if (!response.ok) {
throw new Error(`Error query pages: ${response.statusText}`);
}
const data = await response.json();
return data;
} catch (error) {
throw new Error(`Failed to query pages: ${error.message}`);
}
}