chore: Auto reload frontend of client is out of date (#1270)
* Move editor version to header Add editor version check for API endpoints * fix: Editor update auto-reload Bump RME * fix: Only redirect if editor header exists * lint
This commit is contained in:
@@ -692,12 +692,13 @@ router.post('documents.create', auth(), async ctx => {
|
||||
const {
|
||||
title = '',
|
||||
text = '',
|
||||
editorVersion,
|
||||
publish,
|
||||
collectionId,
|
||||
parentDocumentId,
|
||||
index,
|
||||
} = ctx.body;
|
||||
const editorVersion = ctx.headers['x-editor-version'];
|
||||
|
||||
ctx.assertUuid(collectionId, 'collectionId must be an uuid');
|
||||
if (parentDocumentId) {
|
||||
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be an uuid');
|
||||
@@ -787,10 +788,11 @@ router.post('documents.update', auth(), async ctx => {
|
||||
publish,
|
||||
autosave,
|
||||
done,
|
||||
editorVersion,
|
||||
lastRevision,
|
||||
append,
|
||||
} = ctx.body;
|
||||
const editorVersion = ctx.headers['x-editor-version'];
|
||||
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
ctx.assertPresent(title || text, 'title or text is required');
|
||||
if (append) ctx.assertPresent(text, 'Text is required while appending');
|
||||
|
||||
@@ -25,6 +25,7 @@ import validation from '../middlewares/validation';
|
||||
import methodOverride from '../middlewares/methodOverride';
|
||||
import cache from './middlewares/cache';
|
||||
import apiWrapper from './middlewares/apiWrapper';
|
||||
import editor from './middlewares/editor';
|
||||
|
||||
const api = new Koa();
|
||||
const router = new Router();
|
||||
@@ -36,6 +37,7 @@ api.use(methodOverride());
|
||||
api.use(cache());
|
||||
api.use(validation());
|
||||
api.use(apiWrapper());
|
||||
api.use(editor());
|
||||
|
||||
// routes
|
||||
router.use('/', auth.routes());
|
||||
|
||||
17
server/api/middlewares/editor.js
Normal file
17
server/api/middlewares/editor.js
Normal file
@@ -0,0 +1,17 @@
|
||||
// @flow
|
||||
import { type Context } from 'koa';
|
||||
import pkg from 'rich-markdown-editor/package.json';
|
||||
import { EditorUpdateError } from '../../errors';
|
||||
|
||||
export default function editor() {
|
||||
return async function editorMiddleware(ctx: Context, next: () => Promise<*>) {
|
||||
const editorVersion = ctx.headers['x-editor-version'];
|
||||
|
||||
// As the client can only ever be behind the server there's no need for a
|
||||
// more strict check of version infront/behind here
|
||||
if (editorVersion && editorVersion !== pkg.version) {
|
||||
throw new EditorUpdateError();
|
||||
}
|
||||
return next();
|
||||
};
|
||||
}
|
||||
@@ -45,3 +45,9 @@ export function ParamRequiredError(
|
||||
export function ValidationError(message: string = 'Validation failed') {
|
||||
return httpErrors(400, message, { id: 'validation_error' });
|
||||
}
|
||||
|
||||
export function EditorUpdateError(
|
||||
message: string = 'The client editor is out of date and must be reloaded'
|
||||
) {
|
||||
return httpErrors(400, message, { id: 'editor_update_required' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user