|
| 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 | +} |
0 commit comments