Skip to content

Commit

Permalink
feat: add endpoint for creating new blocks
Browse files Browse the repository at this point in the history
Will be used for creating extracts.
  • Loading branch information
aalemayhu committed Oct 2, 2022
1 parent 8d42768 commit 3d0650b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/lib/notion/NotionAPIWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import sanitizeTags from '../anki/sanitizeTags';
import ParserRules from '../parser/ParserRules';
import Settings from '../parser/Settings';
import isHeading from './helpers/isHeading';
import { ParagraphBlockObject } from './types';

const ANON_LIMIT = 21 * 2;
const PATREON_LIMIT = 100 * 2;
Expand Down Expand Up @@ -69,6 +70,17 @@ class NotionAPIWrapper {
});
}

async createBlock(
parent: string,
newBlock: ParagraphBlockObject
): Promise<ListBlockChildrenResponse> {
return this.notion.blocks.children.append({
block_id: parent,
/* @ts-ignore */
children: [newBlock],
});
}

async getDatabase(id: string): Promise<GetDatabaseResponse> {
return this.notion.databases.retrieve({ database_id: id });
}
Expand Down
16 changes: 16 additions & 0 deletions src/lib/notion/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type ParagraphBlockObject = {
paragraph: {
color: string;
text: [
{
type: string;
text: {
content: string;
};
annotations: {
color: string;
};
}
];
};
};
15 changes: 15 additions & 0 deletions src/routes/notion/createBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express';
import NotionAPIWrapper from '../../lib/notion/NotionAPIWrapper';

export default async function deleteBlock(
api: NotionAPIWrapper,
req: express.Request,
res: express.Response
) {
const { id } = req.params;
if (!id) {
return res.status(400).send();
}
const block = await api.createBlock(id, req.body.newBlock);
res.json(block);
}
8 changes: 8 additions & 0 deletions src/routes/notion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ensureResponse from './helpers/ensureResponse';
import { captureException } from '@sentry/node';
import renderBlock from './renderBlock';
import deleteBlock from './deleteBlock';
import createBlock from './createBlock';

const router = express.Router();

Expand Down Expand Up @@ -126,6 +127,13 @@ router.get('/block/:id', RequireAuthentication, async (req, res) =>
}, res)
);

router.post('/block/:id', RequireAuthentication, async (req, res) => {
ensureResponse(async () => {
const api = await ConfigureNotionAPI(req, res);
return createBlock(api, req, res);
}, res);
});

router.delete('/block/:id', RequireAuthentication, async (req, res) =>
ensureResponse(async () => {
if (!res.locals.patreon) {
Expand Down

0 comments on commit 3d0650b

Please sign in to comment.