♻️(convert) reuse existing convert yprovider endpoint for content API
reuse convert service instead of renaming it in content
This commit is contained in:
committed by
Manuel Raynaud
parent
8a8a1460e5
commit
ede0a77665
@@ -1,207 +0,0 @@
|
||||
import { ServerBlockNoteEditor } from '@blocknote/server-util';
|
||||
import request from 'supertest';
|
||||
import { describe, expect, test, vi } from 'vitest';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
vi.mock('../src/env', async (importOriginal) => {
|
||||
return {
|
||||
...(await importOriginal()),
|
||||
COLLABORATION_SERVER_ORIGIN: 'http://localhost:3000',
|
||||
Y_PROVIDER_API_KEY: 'yprovider-api-key',
|
||||
};
|
||||
});
|
||||
|
||||
import { initApp } from '@/servers';
|
||||
|
||||
import {
|
||||
Y_PROVIDER_API_KEY as apiKey,
|
||||
COLLABORATION_SERVER_ORIGIN as origin,
|
||||
} from '../src/env';
|
||||
|
||||
console.error = vi.fn();
|
||||
|
||||
describe('Content API Tests', () => {
|
||||
test('POST /api/content with incorrect API key responds with 401', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', 'wrong-api-key')
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: 'dGVzdA==', // base64 for "test"
|
||||
format: 'json',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Unauthorized: Invalid API Key',
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/content with incorrect Bearer token responds with 401', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', 'Bearer test-secret-api-key')
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: 'dGVzdA==', // base64 for "test"
|
||||
format: 'json',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(401);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Unauthorized: Invalid API Key',
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/content with missing content parameter', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
format: 'json',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Invalid request: missing content',
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/content with empty content', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: '',
|
||||
format: 'json',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Invalid request: missing content',
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/content with missing format parameter', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: 'dGVzdA==',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Invalid format. Must be one of: json, markdown, html',
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/content with invalid format', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: 'dGVzdA==',
|
||||
format: 'invalid',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'Invalid format. Must be one of: json, markdown, html',
|
||||
});
|
||||
});
|
||||
|
||||
test.each([
|
||||
{ authHeader: `Bearer ${apiKey}`, format: 'json' },
|
||||
{ authHeader: `Bearer ${apiKey}`, format: 'markdown' },
|
||||
{ authHeader: `Bearer ${apiKey}`, format: 'html' },
|
||||
])(
|
||||
'POST /api/content with correct content and format $format with Authorization: $authHeader',
|
||||
async ({ authHeader, format }) => {
|
||||
const app = initApp();
|
||||
|
||||
// Create a simple Yjs document for testing using BlockNote
|
||||
const editor = ServerBlockNoteEditor.create();
|
||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||
const base64Content = Buffer.from(yjsUpdate).toString('base64');
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('Origin', origin)
|
||||
.set('Authorization', authHeader)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: base64Content,
|
||||
format: format,
|
||||
});
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body).toHaveProperty('content');
|
||||
expect(response.body).toHaveProperty('format', format);
|
||||
|
||||
// Verify the content based on format
|
||||
if (format === 'json') {
|
||||
const parsedContent = response.body.content;
|
||||
expect(Array.isArray(parsedContent)).toBe(true);
|
||||
expect(parsedContent.length).toBe(2);
|
||||
expect(parsedContent[0].type).toBe('heading');
|
||||
expect(parsedContent[1].type).toBe('paragraph');
|
||||
expect(parsedContent[0].content[0].type).toBe('text');
|
||||
expect(parsedContent[0].content[0].text).toBe('Test Document');
|
||||
expect(parsedContent[1].content[0].type).toBe('text');
|
||||
expect(parsedContent[1].content[0].text).toBe('This is test content.');
|
||||
} else if (format === 'markdown') {
|
||||
expect(typeof response.body.content).toBe('string');
|
||||
expect(response.body.content.trim()).toBe(markdownContent);
|
||||
} else if (format === 'html') {
|
||||
expect(typeof response.body.content).toBe('string');
|
||||
expect(response.body.content).toBe(
|
||||
'<h1>Test Document</h1><p>This is test content.</p>',
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
test('POST /api/content with invalid base64 content returns 500', async () => {
|
||||
const app = initApp();
|
||||
|
||||
const response = await request(app)
|
||||
.post('/api/content')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/json')
|
||||
.send({
|
||||
content: 'invalid-base64-content!@#',
|
||||
format: 'json',
|
||||
});
|
||||
|
||||
expect(response.status).toBe(500);
|
||||
expect(response.body).toStrictEqual({
|
||||
error: 'An error occurred during conversion',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -121,6 +121,31 @@ describe('Server Tests', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('POST /api/convert with unsupported Content-Type returns 415', async () => {
|
||||
const app = initApp();
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'image/png')
|
||||
.send('randomdata');
|
||||
expect(response.status).toBe(415);
|
||||
expect(response.body).toStrictEqual({ error: 'Unsupported Content-Type' });
|
||||
});
|
||||
|
||||
test('POST /api/convert with unsupported Accept returns 406', async () => {
|
||||
const app = initApp();
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'text/markdown')
|
||||
.set('accept', 'image/png')
|
||||
.send('# Header');
|
||||
expect(response.status).toBe(406);
|
||||
expect(response.body).toStrictEqual({ error: 'Unsupported format' });
|
||||
});
|
||||
|
||||
test.each([[apiKey], [`Bearer ${apiKey}`]])(
|
||||
'POST /api/convert with correct content with Authorization: %s',
|
||||
async (authHeader) => {
|
||||
@@ -137,6 +162,8 @@ describe('Server Tests', () => {
|
||||
.post('/api/convert')
|
||||
.set('Origin', origin)
|
||||
.set('Authorization', authHeader)
|
||||
.set('content-type', 'text/markdown')
|
||||
.set('accept', 'application/vnd.yjs.doc')
|
||||
.send(document);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
@@ -150,4 +177,89 @@ describe('Server Tests', () => {
|
||||
expect(blocks).toStrictEqual(expectedBlocks);
|
||||
},
|
||||
);
|
||||
|
||||
test('POST /api/convert Yjs to HTML', async () => {
|
||||
const app = initApp();
|
||||
const editor = ServerBlockNoteEditor.create();
|
||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/vnd.yjs.doc')
|
||||
.set('accept', 'text/html')
|
||||
.send(Buffer.from(yjsUpdate));
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.header['content-type']).toBe('text/html; charset=utf-8');
|
||||
expect(typeof response.text).toBe('string');
|
||||
expect(response.text).toBe(
|
||||
'<h1>Test Document</h1><p>This is test content.</p>',
|
||||
);
|
||||
});
|
||||
|
||||
test('POST /api/convert Yjs to Markdown', async () => {
|
||||
const app = initApp();
|
||||
const editor = ServerBlockNoteEditor.create();
|
||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/vnd.yjs.doc')
|
||||
.set('accept', 'text/markdown')
|
||||
.send(Buffer.from(yjsUpdate));
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.header['content-type']).toBe(
|
||||
'text/markdown; charset=utf-8',
|
||||
);
|
||||
expect(typeof response.text).toBe('string');
|
||||
expect(response.text.trim()).toBe(markdownContent);
|
||||
});
|
||||
|
||||
test('POST /api/convert Yjs to JSON', async () => {
|
||||
const app = initApp();
|
||||
const editor = ServerBlockNoteEditor.create();
|
||||
const markdownContent = '# Test Document\n\nThis is test content.';
|
||||
const blocks = await editor.tryParseMarkdownToBlocks(markdownContent);
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
const yjsUpdate = Y.encodeStateAsUpdate(yDocument);
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/vnd.yjs.doc')
|
||||
.set('accept', 'application/json')
|
||||
.send(Buffer.from(yjsUpdate));
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.header['content-type']).toBe(
|
||||
'application/json; charset=utf-8',
|
||||
);
|
||||
expect(Array.isArray(response.body)).toBe(true);
|
||||
expect(response.body.length).toBe(2);
|
||||
expect(response.body[0].type).toBe('heading');
|
||||
expect(response.body[1].type).toBe('paragraph');
|
||||
expect(response.body[0].content[0].type).toBe('text');
|
||||
expect(response.body[0].content[0].text).toBe('Test Document');
|
||||
expect(response.body[1].content[0].type).toBe('text');
|
||||
expect(response.body[1].content[0].text).toBe('This is test content.');
|
||||
});
|
||||
|
||||
test('POST /api/convert with invalid Yjs content returns 400', async () => {
|
||||
const app = initApp();
|
||||
const response = await request(app)
|
||||
.post('/api/convert')
|
||||
.set('origin', origin)
|
||||
.set('authorization', apiKey)
|
||||
.set('content-type', 'application/vnd.yjs.doc')
|
||||
.set('accept', 'application/json')
|
||||
.send(Buffer.from('notvalidyjs'));
|
||||
expect(response.status).toBe(400);
|
||||
expect(response.body).toStrictEqual({ error: 'Invalid Yjs content' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"build": "tsc -p tsconfig.build.json && tsc-alias",
|
||||
"dev": "cross-env COLLABORATION_LOGGING=true && nodemon --config nodemon.json",
|
||||
"start": "node ./dist/start-server.js",
|
||||
"lint": "eslint . --ext .ts",
|
||||
"lint": "eslint . --ext .ts --fix",
|
||||
"test": "vitest --run --disable-console-intercept"
|
||||
},
|
||||
"engines": {
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { PartialBlock } from '@blocknote/core';
|
||||
import { ServerBlockNoteEditor } from '@blocknote/server-util';
|
||||
import { Request, Response } from 'express';
|
||||
import * as Y from 'yjs';
|
||||
|
||||
import { logger } from '@/utils';
|
||||
|
||||
interface ErrorResponse {
|
||||
error: string;
|
||||
}
|
||||
|
||||
interface ContentRequest {
|
||||
content: string;
|
||||
format: string;
|
||||
}
|
||||
|
||||
const editor = ServerBlockNoteEditor.create();
|
||||
|
||||
export const contentHandler = async (
|
||||
req: Request<object, object | ErrorResponse, ContentRequest, object>,
|
||||
res: Response<object | ErrorResponse>,
|
||||
) => {
|
||||
const { content, format } = req.body;
|
||||
|
||||
if (!content) {
|
||||
res.status(400).json({ error: 'Invalid request: missing content' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (!format || !['json', 'markdown', 'html'].includes(format)) {
|
||||
res
|
||||
.status(400)
|
||||
.json({ error: 'Invalid format. Must be one of: json, markdown, html' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Decode base64 content to Uint8Array
|
||||
const uint8Array = new Uint8Array(Buffer.from(content, 'base64'));
|
||||
|
||||
// Create Yjs document and apply the update
|
||||
const yDocument = new Y.Doc();
|
||||
Y.applyUpdate(yDocument, uint8Array);
|
||||
|
||||
// Convert to blocks
|
||||
const blocks = editor.yDocToBlocks(yDocument, 'document-store');
|
||||
|
||||
let result: string | object | null;
|
||||
|
||||
if (!blocks || blocks.length === 0) {
|
||||
result = null;
|
||||
} else if (format === 'json') {
|
||||
result = blocks;
|
||||
} else if (format === 'markdown') {
|
||||
result = await editor.blocksToMarkdownLossy(blocks as PartialBlock[]);
|
||||
} else if (format === 'html') {
|
||||
result = await editor.blocksToHTMLLossy(blocks as PartialBlock[]);
|
||||
} else {
|
||||
res.status(400).json({ error: 'Unsupported format' });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
content: result,
|
||||
format: format,
|
||||
});
|
||||
} catch (e) {
|
||||
logger('content conversion failed:', e);
|
||||
res.status(500).json({ error: 'An error occurred during conversion' });
|
||||
}
|
||||
};
|
||||
@@ -1,3 +1,4 @@
|
||||
import { PartialBlock } from '@blocknote/core';
|
||||
import { ServerBlockNoteEditor } from '@blocknote/server-util';
|
||||
import { Request, Response } from 'express';
|
||||
import * as Y from 'yjs';
|
||||
@@ -12,29 +13,80 @@ const editor = ServerBlockNoteEditor.create();
|
||||
|
||||
export const convertHandler = async (
|
||||
req: Request<object, Uint8Array | ErrorResponse, Buffer, object>,
|
||||
res: Response<Uint8Array | ErrorResponse>,
|
||||
res: Response<Uint8Array | string | object | ErrorResponse>,
|
||||
) => {
|
||||
if (!req.body || req.body.length === 0) {
|
||||
res.status(400).json({ error: 'Invalid request: missing content' });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Perform the conversion from markdown to Blocknote.js blocks
|
||||
const blocks = await editor.tryParseMarkdownToBlocks(req.body.toString());
|
||||
const contentType = (req.header('content-type') || 'text/markdown').split(
|
||||
';',
|
||||
)[0];
|
||||
const accept = (req.header('accept') || 'application/vnd.yjs.doc').split(
|
||||
';',
|
||||
)[0];
|
||||
|
||||
let blocks: PartialBlock[] | null = null;
|
||||
try {
|
||||
|
||||
// First, convert from the input format to blocks
|
||||
// application/x-www-form-urlencoded is interpreted as Markdown for backward compatibility
|
||||
if (
|
||||
contentType === 'text/markdown' ||
|
||||
contentType === 'application/x-www-form-urlencoded'
|
||||
) {
|
||||
blocks = await editor.tryParseMarkdownToBlocks(req.body.toString());
|
||||
} else if (
|
||||
contentType === 'application/vnd.yjs.doc' ||
|
||||
contentType === 'application/octet-stream'
|
||||
) {
|
||||
try {
|
||||
const ydoc = new Y.Doc();
|
||||
Y.applyUpdate(ydoc, req.body);
|
||||
blocks = editor.yDocToBlocks(ydoc, 'document-store') as PartialBlock[];
|
||||
} catch (e) {
|
||||
logger('Invalid Yjs content:', e);
|
||||
res.status(400).json({ error: 'Invalid Yjs content' });
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
res.status(415).json({ error: 'Unsupported Content-Type' });
|
||||
return;
|
||||
}
|
||||
if (!blocks || blocks.length === 0) {
|
||||
res.status(500).json({ error: 'No valid blocks were generated' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Create a Yjs Document from blocks
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
// Then, convert from blocks to the output format
|
||||
if (accept === 'application/json') {
|
||||
res.status(200).json(blocks);
|
||||
} else {
|
||||
const yDocument = editor.blocksToYDoc(blocks, 'document-store');
|
||||
|
||||
res
|
||||
.status(200)
|
||||
.setHeader('content-type', 'application/octet-stream')
|
||||
.send(Y.encodeStateAsUpdate(yDocument));
|
||||
if (
|
||||
accept === 'application/vnd.yjs.doc' ||
|
||||
accept === 'application/octet-stream'
|
||||
) {
|
||||
res
|
||||
.status(200)
|
||||
.setHeader('content-type', 'application/octet-stream')
|
||||
.send(Y.encodeStateAsUpdate(yDocument));
|
||||
} else if (accept === 'text/markdown') {
|
||||
res
|
||||
.status(200)
|
||||
.setHeader('content-type', 'text/markdown')
|
||||
.send(await editor.blocksToMarkdownLossy(blocks));
|
||||
} else if (accept === 'text/html') {
|
||||
res
|
||||
.status(200)
|
||||
.setHeader('content-type', 'text/html')
|
||||
.send(await editor.blocksToHTMLLossy(blocks));
|
||||
} else {
|
||||
res.status(406).json({ error: 'Unsupported format' });
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger('conversion failed:', e);
|
||||
res.status(500).json({ error: 'An error occurred' });
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from './collaborationResetConnectionsHandler';
|
||||
export * from './collaborationWSHandler';
|
||||
export * from './convertHandler';
|
||||
export * from './contentHandler';
|
||||
export * from './getDocumentConnectionInfoHandler';
|
||||
|
||||
@@ -8,7 +8,6 @@ import expressWebsockets from 'express-ws';
|
||||
import {
|
||||
collaborationResetConnectionsHandler,
|
||||
collaborationWSHandler,
|
||||
contentHandler,
|
||||
convertHandler,
|
||||
getDocumentConnectionInfoHandler,
|
||||
} from '@/handlers';
|
||||
@@ -50,7 +49,7 @@ export const initApp = () => {
|
||||
);
|
||||
|
||||
/**
|
||||
* Route to convert Markdown or BlockNote blocks
|
||||
* Route to convert Markdown or BlockNote blocks and Yjs content
|
||||
*/
|
||||
app.post(
|
||||
routes.CONVERT,
|
||||
@@ -62,11 +61,6 @@ export const initApp = () => {
|
||||
convertHandler,
|
||||
);
|
||||
|
||||
/**
|
||||
* Route to convert base64 Yjs content to different formats
|
||||
*/
|
||||
app.post(routes.CONTENT, httpSecurity, express.json(), contentHandler);
|
||||
|
||||
Sentry.setupExpressErrorHandler(app);
|
||||
|
||||
app.get('/ping', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user