mirror of
https://github.com/ToolJet/ToolJet
synced 2026-04-25 07:27:28 +00:00
* initialized the plugin * Added token field and notion svg icon * added notion js client * Added token authentication * Added database query and retrieve operations * Working on other operations of database and page * Added archive page & get single property operations * Resolved an issue & changed placeholders * Added operations of block * Added start cursor to page & database * Added operations of user endpoint * changed icon & cover from object to fields * Updated documentation
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { QueryResult, QueryService, ConnectionTestResult } from '@tooljet-plugins/common';
|
|
import { SourceOptions, QueryOptions } from './types';
|
|
import { Client } from '@notionhq/client';
|
|
import { blockOperations, databaseOperations, pageOperations, userOperations } from './operations';
|
|
|
|
export default class Notion implements QueryService {
|
|
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
|
|
const notionClient = await this.getConnection(sourceOptions);
|
|
const { resource, operation } = queryOptions;
|
|
let result: any;
|
|
|
|
switch (resource) {
|
|
case 'database':
|
|
result = await databaseOperations(notionClient, queryOptions, operation);
|
|
break;
|
|
case 'page':
|
|
result = await pageOperations(notionClient, queryOptions, operation);
|
|
break;
|
|
case 'block':
|
|
result = await blockOperations(notionClient, queryOptions, operation);
|
|
break;
|
|
case 'user':
|
|
result = await userOperations(notionClient, queryOptions, operation);
|
|
break;
|
|
}
|
|
|
|
return {
|
|
status: 'ok',
|
|
data: result,
|
|
};
|
|
}
|
|
|
|
async getConnection(sourceOptions: SourceOptions, _options?: object): Promise<Client> {
|
|
const { token } = sourceOptions;
|
|
|
|
// Initializing a client
|
|
const notion = new Client({
|
|
auth: token,
|
|
});
|
|
|
|
return notion;
|
|
}
|
|
|
|
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
|
|
const notion = await this.getConnection(sourceOptions);
|
|
|
|
if (!notion) {
|
|
throw new Error('Invalid token');
|
|
}
|
|
|
|
await notion.search({});
|
|
|
|
return {
|
|
status: 'ok',
|
|
};
|
|
}
|
|
}
|