Skip to content

Commit ac2f194

Browse files
committed
Add implementation file
1 parent bdb12ec commit ac2f194

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

implementation.js

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Perform a query operation on a Notion database.
3+
* @param {Object} params - The parameters for the operation.
4+
* @param {string} [params.databaseId] - The ID of the Notion database for create and read operations.
5+
* @param {Array} [params.databaseStructure] - A list of properties in the Notion database structure, used to generate filter or sort criteria..
6+
* @param {Object} [params.filter] - Optional filter for querying pages in the read operation.
7+
* @param {Array} [params.sorts] - Optional sorting criteria for page results in the read operation.
8+
* @param {number} [params.pageSize] - The number of results per page for the read operation.
9+
* @param {string} [params.startCursor] - The start cursor for pagination in the read operation.
10+
* @returns {Promise<Object>} - The response data from the Notion API.
11+
*/
12+
async function perform_notion_database_query(params, userSettings) {
13+
const { databaseId, databaseStructure = [] } = params;
14+
const { pluginServer, notionApiKey } = userSettings;
15+
16+
if (!pluginServer) {
17+
throw new Error(
18+
"Missing the Plugin Server URL. Please set it in the plugin settings."
19+
);
20+
}
21+
22+
if (!notionApiKey) {
23+
throw new Error(
24+
"Missing the Notion API Key. Please set it in the plugin settings."
25+
);
26+
}
27+
28+
if (!databaseId) {
29+
throw new Error(
30+
"Missing the Database Id. Please provide specific Database ID or Database URL"
31+
);
32+
}
33+
34+
if (!databaseStructure.length) {
35+
throw new Error(
36+
"Missing the Database Structure Properties. Please provide valid structure of your provided database."
37+
);
38+
}
39+
40+
try {
41+
const result = await queryPages({
42+
pluginServerUrl: pluginServer,
43+
notionApiKey: notionApiKey,
44+
databaseId: databaseId,
45+
databaseStructure: databaseStructure,
46+
filter: params.filter ?? {},
47+
sorts: params.sorts ?? [],
48+
pageSize: params.pageSize ?? 100,
49+
startCursor: params.startCursor,
50+
});
51+
return result;
52+
} catch (error) {
53+
return { error: error.message };
54+
}
55+
}
56+
57+
async function queryPages({
58+
notionApiKey,
59+
pluginServerUrl,
60+
databaseId,
61+
databaseStructure,
62+
filter,
63+
sorts,
64+
pageSize = 100,
65+
startCursor = null,
66+
}) {
67+
const requestBody = {
68+
notionApiKey: notionApiKey,
69+
databaseId: databaseId,
70+
pageSize: pageSize,
71+
databaseStructure: databaseStructure,
72+
};
73+
if (filter) requestBody.filter = filter;
74+
if (sorts) requestBody.sorts = sorts;
75+
if (startCursor) requestBody.startCursor = startCursor;
76+
77+
try {
78+
const response = await fetch(
79+
`${pluginServerUrl}/notion-database/query-pages`,
80+
{
81+
method: "POST",
82+
headers: {
83+
"Content-Type": "application/json",
84+
},
85+
body: JSON.stringify(requestBody),
86+
}
87+
);
88+
89+
if (!response.ok) {
90+
throw new Error(`Error query pages: ${response.statusText}`);
91+
}
92+
93+
const data = await response.json();
94+
return data;
95+
} catch (error) {
96+
throw new Error(`Failed to query pages: ${error.message}`);
97+
}
98+
}

plugin.json

-1
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@
528528
"required": true
529529
}
530530
],
531-
"code": "/**\n * Perform a query operation on a Notion database.\n * @param {Object} params - The parameters for the operation.\n * @param {string} [params.databaseId] - The ID of the Notion database for create and read operations.\n * @param {Array} [params.databaseStructure] - A list of properties in the Notion database structure, used to generate filter or sort criteria..\n * @param {Object} [params.filter] - Optional filter for querying pages in the read operation.\n * @param {Array} [params.sorts] - Optional sorting criteria for page results in the read operation.\n * @param {number} [params.pageSize] - The number of results per page for the read operation.\n * @param {string} [params.startCursor] - The start cursor for pagination in the read operation.\n * @returns {Promise<Object>} - The response data from the Notion API.\n */\nasync function perform_notion_database_query(params, userSettings) {\n const { databaseId, databaseStructure = [] } = params;\n const { pluginServer, notionApiKey } = userSettings;\n\n if (!pluginServer) {\n throw new Error(\n \"Missing the Plugin Server URL. Please set it in the plugin settings.\"\n );\n }\n\n if (!notionApiKey) {\n throw new Error(\n \"Missing the Notion API Key. Please set it in the plugin settings.\"\n );\n }\n\n if (!databaseId) {\n throw new Error(\n \"Missing the Database Id. Please provide specific Database ID or Database URL\"\n );\n }\n\n if (!databaseStructure.length) {\n throw new Error(\n \"Missing the Database Structure Properties. Please provide valid structure of your provided database.\"\n );\n }\n\n try {\n const result = await queryPages({\n pluginServerUrl: pluginServer,\n notionApiKey: notionApiKey,\n databaseId: databaseId,\n databaseStructure: databaseStructure,\n filter: params.filter ?? {},\n sorts: params.sorts ?? [],\n pageSize: params.pageSize ?? 100,\n startCursor: params.startCursor,\n });\n return result;\n } catch (error) {\n return { error: error.message };\n }\n}\n\nasync function queryPages({\n notionApiKey,\n pluginServerUrl,\n databaseId,\n databaseStructure,\n filter,\n sorts,\n pageSize = 100,\n startCursor = null,\n}) {\n const requestBody = {\n notionApiKey: notionApiKey,\n databaseId: databaseId,\n pageSize: pageSize,\n databaseStructure: databaseStructure,\n };\n if (filter) requestBody.filter = filter;\n if (sorts) requestBody.sorts = sorts;\n if (startCursor) requestBody.startCursor = startCursor;\n\n try {\n const response = await fetch(\n `${pluginServerUrl}/notion-database/query-pages`,\n {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n },\n body: JSON.stringify(requestBody),\n }\n );\n\n if (!response.ok) {\n throw new Error(`Error query pages: ${response.statusText}`);\n }\n\n const data = await response.json();\n console.log(JSON.stringify(data));\n return data;\n } catch (error) {\n throw new Error(`Failed to query pages: ${error.message}`);\n }\n}",
532531
"iconURL": "https://registry.npmmirror.com/@lobehub/icons-static-png/latest/files/light/notion.png",
533532
"authenticationType": "AUTH_TYPE_NONE",
534533
"implementationType": "javascript",

0 commit comments

Comments
 (0)