fix: coalesce falsy body to {} (#4929)
This commit is contained in:
12
server/middlewares/coaleseBody.ts
Normal file
12
server/middlewares/coaleseBody.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { Next } from "koa";
|
||||||
|
import { AppContext } from "@server/types";
|
||||||
|
|
||||||
|
// remove after https://github.com/koajs/koa-body/issues/218 is resolved
|
||||||
|
export default function coalesceBody() {
|
||||||
|
return function coalesceBodyMiddleware(ctx: AppContext, next: Next) {
|
||||||
|
if (!ctx.request.body) {
|
||||||
|
ctx.request.body = {};
|
||||||
|
}
|
||||||
|
return next();
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -7,6 +7,7 @@ import userAgent, { UserAgentContext } from "koa-useragent";
|
|||||||
import env from "@server/env";
|
import env from "@server/env";
|
||||||
import { NotFoundError } from "@server/errors";
|
import { NotFoundError } from "@server/errors";
|
||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
|
import coalesceBody from "@server/middlewares/coaleseBody";
|
||||||
import { AppState, AppContext } from "@server/types";
|
import { AppState, AppContext } from "@server/types";
|
||||||
import apiKeys from "./apiKeys";
|
import apiKeys from "./apiKeys";
|
||||||
import attachments from "./attachments";
|
import attachments from "./attachments";
|
||||||
@@ -45,6 +46,7 @@ api.use(
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
api.use(coalesceBody());
|
||||||
api.use<BaseContext, UserAgentContext>(userAgent);
|
api.use<BaseContext, UserAgentContext>(userAgent);
|
||||||
api.use(apiWrapper());
|
api.use(apiWrapper());
|
||||||
api.use(editor());
|
api.use(editor());
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import bodyParser from "koa-body";
|
|||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { AuthenticationError } from "@server/errors";
|
import { AuthenticationError } from "@server/errors";
|
||||||
import auth from "@server/middlewares/authentication";
|
import auth from "@server/middlewares/authentication";
|
||||||
|
import coalesceBody from "@server/middlewares/coaleseBody";
|
||||||
import { Collection, Team, View } from "@server/models";
|
import { Collection, Team, View } from "@server/models";
|
||||||
import AuthenticationHelper from "@server/models/helpers/AuthenticationHelper";
|
import AuthenticationHelper from "@server/models/helpers/AuthenticationHelper";
|
||||||
import { AppState, AppContext, APIContext } from "@server/types";
|
import { AppState, AppContext, APIContext } from "@server/types";
|
||||||
@@ -71,6 +72,7 @@ router.get("/redirect", auth(), async (ctx: APIContext) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
app.use(bodyParser());
|
app.use(bodyParser());
|
||||||
|
app.use(coalesceBody());
|
||||||
app.use(router.routes());
|
app.use(router.routes());
|
||||||
|
|
||||||
export default app;
|
export default app;
|
||||||
|
|||||||
Reference in New Issue
Block a user