Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: notion generic output #138

Merged
merged 1 commit into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { useCreateRedirectURLMutation } from 'services/callback/create_redirect_
import { useCreateIntegrationAccountWithLinkMutation } from 'services/integration_account';
import { useGetIntegrationDefinitionSpecQuery } from 'services/integration_definition/get_spec_for_integration_definition';

import { IntegrationIcon, Loader } from 'components';
import { Loader } from 'components';

import { getAllProperties, getValidateObject } from './public_link_utils';

Expand Down
4 changes: 2 additions & 2 deletions integration_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@
"name": "Notion",
"key": "notion",
"icon": "notion.svg",
"version": "0.1.5",
"version": "0.1.6",
"releaseStage": "ALPHA",
"sourceUrl": "https://raw.githubusercontent.com/poozlehq/build-artifacts/main/documentation/notion/0.1.5/index.js",
"sourceUrl": "https://raw.githubusercontent.com/poozlehq/build-artifacts/main/documentation/notion/0.1.6/index.js",
"integrationType": "DOCUMENTATION"
},
{
Expand Down
4 changes: 2 additions & 2 deletions integrations/documentation/notion/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poozle/notion",
"version": "0.1.4",
"version": "0.1.6",
"description": "notion extension for Poozle",
"exports": {
".": {
Expand Down Expand Up @@ -47,7 +47,7 @@
"access": "public"
},
"dependencies": {
"@poozle/engine-idk": "^0.1.18",
"@poozle/engine-idk": "^0.1.21",
"axios": "^1.4.0"
},
"browserslist": {
Expand Down
41 changes: 30 additions & 11 deletions integrations/documentation/notion/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/** Copyright (c) 2023, Poozle, all rights reserved. **/

import {
BaseIntegration,
CheckResponse,
Config,
GenericProxyModel,
SpecificationResponse,
} from '@poozle/engine-idk';
import { BaseIntegration, CheckResponse, Config, SpecificationResponse } from '@poozle/engine-idk';
import axios from 'axios';

import { NotionBlockModel } from 'models/block/block.model';
import { NotionPageModel } from 'models/page/page.model';
import { BlocksPath } from 'models/block/blocks.path';
import { PagePath } from 'models/page/page.path';
import { PagesPath } from 'models/page/pages.path';
import { ProxyPath } from 'proxy';

import spec from './spec';

Expand Down Expand Up @@ -40,8 +36,31 @@ class NotionIntegration extends BaseIntegration {
}
}

models() {
return [new GenericProxyModel(), new NotionPageModel(), new NotionBlockModel()];
paths() {
return [
new ProxyPath(/^\/?proxy$/g, ['GET', 'POST', 'PATCH', 'DELETE']),
/**
* Blocks.
* 1. Fetching the blocks
* 2. Updating the blocks
* 3. Creating a block
*/
new BlocksPath(/^\/?blocks+/g, ['GET', 'POST', 'PATCH']),

/**
* Pages.
* 1. Fetch Pages
* 2. Create Page
* Matches /pages
*/
new PagesPath(/^\/?pages$/g, ['GET', 'POST']),

/**
* Get a page
* Matches /page
*/
new PagePath(/^\/?pages+/g, ['GET']),
];
}
}

Expand Down
14 changes: 0 additions & 14 deletions integrations/documentation/notion/src/models/block/block.model.ts

This file was deleted.

65 changes: 43 additions & 22 deletions integrations/documentation/notion/src/models/block/block.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,35 +103,56 @@ export async function fetchPageBlocks(
};
}

export function extractBlockData(data: SingleBlockResponse): Block {
export function extractContent(data: any): Content | Content[] {
const type = data.type;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const content =
'rich_text' in data[type]
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
data[type].rich_text?.map((richtext: any) => {
return {
annotations: {
bold: richtext.annotations.bold ?? '',
italic: richtext.annotations.italic ?? '',
strikethrough: richtext.annotations.strikethrough ?? '',
underline: richtext.annotations.underline ?? '',
code: richtext.annotations.code ?? '',
color: richtext.annotations.color ?? '',
},
plain_text: richtext.plain_text,
href: richtext.href,
};
})
: data[type];
switch (type) {
case BlockType.equation:
return {
plain_text: data[type].expression,
} as Content;

case BlockType.video:
case BlockType.image:
case BlockType.file:
case BlockType.pdf:
const contentType = data[type].type
return {
href: data[type][contentType]?.url,
} as Content;

case BlockType.bookmark:
return {
href: data[type].url,
} as Content;

default:
return data[type]?.rich_text?.map((richtext: any) => {
return {
annotations: {
bold: richtext.annotations.bold ?? '',
italic: richtext.annotations.italic ?? '',
strikethrough: richtext.annotations.strikethrough ?? '',
underline: richtext.annotations.underline ?? '',
code: richtext.annotations.code ?? '',
color: richtext.annotations.color ?? '',
},
plain_text: richtext.plain_text,
href: richtext.href,
};
});
}
}

export function extractBlockData(data: SingleBlockResponse): Block {
const content = extractContent(data);

const children = data.children
? data.children?.map((data: SingleBlockResponse) => extractBlockData(data))
: [];

const block_data = {
id: data.id.replace(/-/g, ''),
parent_id: data.parent?.type ? data.parent[data.parent.type].replace(/-/g, '') : '',
id: data.id,
parent_id: data.parent?.type ? data.parent[data.parent.type] : '',
block_type: data.type,
content,
children,
Expand Down
20 changes: 13 additions & 7 deletions integrations/documentation/notion/src/models/block/blocks.path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ import {

export class BlocksPath extends BasePath {
async getBlocks(url: string, headers: AxiosHeaders, params: Params) {
const block_id = params.pathParams?.block_id as string;
const block_id = params.pathParams?.parent_id as string;
url += `/${block_id}/children`;
const { blocks, meta } = (await fetchPageBlocks(url, headers, params)) as BlockResponse;

return {
data: blocks.map((blockData: SingleBlockResponse) => ({
...extractBlockData(blockData),
raw_data: blockData,
})),
raw: blocks,
meta: {
has_more: meta.has_more,
next_cursor: meta.next_cursor,
Expand All @@ -33,14 +33,17 @@ export class BlocksPath extends BasePath {
}

async createBlock(url: string, headers: AxiosHeaders, params: Params) {
url += `/${params.pathParams?.block_id}/children`;
url += `/${params.pathParams?.parent_id}/children`;
const body = convertAppendBody(params.requestBody?.data as Block[]);

const block_response = await axios.patch(url, body, { headers });

return block_response.data.results.map(async (blockData: SingleBlockResponse) => {
return extractBlockData(blockData);
});
return {
data: block_response.data.results.map((blockData: SingleBlockResponse) => {
return extractBlockData(blockData);
}),
raw: block_response.data.results,
};
}

async updateBlock(url: string, headers: AxiosHeaders, params: Params) {
Expand All @@ -49,7 +52,10 @@ export class BlocksPath extends BasePath {
const body = convertUpdateBody(params.requestBody as Block);
const response = await axios.patch(url, body, { headers });

return extractBlockData(response.data);
return {
data: extractBlockData(response.data),
raw: response,
};
} catch (e) {
throw new Error(e);
}
Expand Down
18 changes: 0 additions & 18 deletions integrations/documentation/notion/src/models/page/page.model.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import axios, { AxiosHeaders } from 'axios';

import { BASE_URL, convertBlockPage } from './pages.utils';

export class GetPagePath extends BasePath {
export class PagePath extends BasePath {
async fetchSinglePage(url: string, headers: AxiosHeaders, _params: Params) {
const response = await axios({
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import axios, { AxiosHeaders } from 'axios';
// import { BASE_URL, convertBlock, convertPage, fetchPageBlocks } from './pages.utils';
import { BASE_URL, convertPages } from './pages.utils';

export class GetPagesPath extends BasePath {
export class PagesPath extends BasePath {
async fetchData(url: string, headers: AxiosHeaders, params: Params) {
const pagesResponse = await axios.post(
url,
Expand Down
29 changes: 29 additions & 0 deletions integrations/documentation/notion/src/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/** Copyright (c) 2023, Poozle, all rights reserved. **/

import { BasePath, Params } from '@poozle/engine-idk';
import axios, { AxiosHeaders } from 'axios';


export class ProxyPath extends BasePath {
async run(method: string, headers: AxiosHeaders, params: Params): Promise<any> {
const axiosObject: any = {
url: params.url,
method,
headers: headers as any,
};

if (params.requestBody) {
axiosObject['data'] = params.requestBody;
}

if (params.queryParams) {
axiosObject['params'] = params.queryParams;
}

const response = await axios(axiosObject);

return response.data;
}
}

8 changes: 4 additions & 4 deletions integrations/documentation/notion/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1109,10 +1109,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@poozle/engine-idk@^0.1.18":
version "0.1.18"
resolved "https://registry.yarnpkg.com/@poozle/engine-idk/-/engine-idk-0.1.18.tgz#9c5d1eb711ef8e46daa1223732418a76614a5d91"
integrity sha512-PQ/w3QKhwXtF5ySiFvpV150j5+S1i/7dXsywaN2lI6915hCe+zVpWfc3de2GNZney0WpKeBSb6lzAMr9OUrlAg==
"@poozle/engine-idk@^0.1.21":
version "0.1.21"
resolved "https://registry.yarnpkg.com/@poozle/engine-idk/-/engine-idk-0.1.21.tgz#ea3d0a8714e3966deabaac0ae97776150d8c3ec3"
integrity sha512-hg6G74JhLqtrtcFke+t9hjhIpL3Bt/N/QYdbcYNfBAh6XHKJ9nBHePMg33u7AAPDHUMlTxbOi0eSwvNne9qDtA==
dependencies:
axios "^1.3.2"
qs "^6.11.2"
Expand Down