ToolJet/plugins/packages/notion/lib/index.ts
Muhsin Shah C P 22991ae900
[Feature] Notion plugin 🚀 (#2853)
* 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
2022-04-26 14:58:48 +05:30

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',
};
}
}