Skip to content

Commit

Permalink
feat(server,code-gen): combine json & multipart 'createBodyParsers' i…
Browse files Browse the repository at this point in the history
…nto a single 'createBodyParser'

This allows us to iterate on `R.body` & R.files` combining them in the future.

BREAKING CHANGE:
- Removed `createBodyParsers` use `createBodyParser` instead.
- `createBodyParser` by default does not parse multipart bodies. This should be explicitly enabled with `multipart: true`. Configure its options with 'multipartOptions' to set a custom `maxFileSize` for example.
- Changed the `jsonLimit` default to '5mb'.
- The generated Koa router now accepts a single body parser instead of the previous body parser pair returned by 'createBodyParsers'.
- The updated usage looks something like:
```js
app.use(
  router(
    createBodyParser({
      multipart: true,
      multipartOptions: {
        maxFileSize: 15 * 1024 * 2024,
      },
    })
  )
);
```
  • Loading branch information
dirkdev98 committed May 6, 2023
1 parent d600ac5 commit df9e43e
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 284 deletions.
105 changes: 0 additions & 105 deletions benchmark/router.bench.js

This file was deleted.

2 changes: 1 addition & 1 deletion docs/features/file-handling.md
Expand Up @@ -26,7 +26,7 @@ Files are handled separately by the generator and validators, and are put on
[formidable](https://www.npmjs.com/package/formidable). In the generated api
clients we generate the correct type (`ReadableStream` or `Blob`) depending on
the context. And allow for setting custom file parsing options
`createBodyParsers` provided by `@compas/server`
`createBodyParser` provided by `@compas/server`

## Saving files

Expand Down
4 changes: 2 additions & 2 deletions docs/features/http-server.md
Expand Up @@ -69,8 +69,8 @@ These two together are explained in
## Other middleware
- `createBodyParsers` can be used to create a json body parser, and a separate
multipart body parser.
- `createBodyParser` can be used to create a middleware that is able to parse
json or multipart bodies.
- `compose` can be used to manually compose multiple middleware in to a single
callable middleware
- `sendFile` is a utility to easily send out files. Supports partial responses,
Expand Down
6 changes: 3 additions & 3 deletions examples/crud-basic/src/services.js
@@ -1,4 +1,4 @@
import { createBodyParsers, getApp } from "@compas/server";
import { createBodyParser, getApp } from "@compas/server";
import {
createTestPostgresDatabase,
newPostgresConnection,
Expand All @@ -16,7 +16,7 @@ export async function injectServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

export async function injectTestServices() {
Expand All @@ -25,7 +25,7 @@ export async function injectTestServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions examples/crud-inline/src/services.js
@@ -1,4 +1,4 @@
import { createBodyParsers, getApp } from "@compas/server";
import { createBodyParser, getApp } from "@compas/server";
import {
createTestPostgresDatabase,
newPostgresConnection,
Expand All @@ -16,7 +16,7 @@ export async function injectServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

export async function injectTestServices() {
Expand All @@ -25,7 +25,7 @@ export async function injectTestServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions examples/crud-nested/src/services.js
@@ -1,4 +1,4 @@
import { createBodyParsers, getApp } from "@compas/server";
import { createBodyParser, getApp } from "@compas/server";
import {
createTestPostgresDatabase,
newPostgresConnection,
Expand All @@ -16,7 +16,7 @@ export async function injectServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

export async function injectTestServices() {
Expand All @@ -25,7 +25,7 @@ export async function injectTestServices() {

injectCrud();

app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions examples/default/src/service.js
@@ -1,4 +1,4 @@
import { createBodyParsers, getApp } from "@compas/server";
import { createBodyParser, getApp } from "@compas/server";
import { environment, isProduction, newLogger, uuid } from "@compas/stdlib";
import {
createTestPostgresDatabase,
Expand Down Expand Up @@ -97,7 +97,7 @@ async function createAppAndLoadControllers() {
);

// Use the generated router
app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));

postRegisterCrud({ sql });

Expand Down
4 changes: 2 additions & 2 deletions examples/with-auth/src/service.js
@@ -1,4 +1,4 @@
import { createBodyParsers, getApp } from "@compas/server";
import { createBodyParser, getApp } from "@compas/server";
import { environment, isProduction, newLogger, uuid } from "@compas/stdlib";
import {
createTestPostgresDatabase,
Expand Down Expand Up @@ -99,7 +99,7 @@ async function createAppAndLoadControllers() {
);

// Use the generated router
app.use(router(createBodyParsers()));
app.use(router(createBodyParser()));

// Controller imports;
// These files are not imported in any other file, but since they are needed to add the
Expand Down
5 changes: 3 additions & 2 deletions packages/code-gen/src/crud/e2e/modifiers.test.js
@@ -1,7 +1,7 @@
import { mainTestFn, test } from "@compas/cli";
import {
closeTestApp,
createBodyParsers,
createBodyParser,
createTestAppAndClient,
getApp,
} from "@compas/server";
Expand Down Expand Up @@ -95,7 +95,8 @@ test("code-gen/crud/e2e/modifiers", async (t) => {
} = await import(pathJoin(generatedDirectory, "./role/apiClient.js"));

const api = getApp();
setBodyParsers(createBodyParsers({}, {}));
const bodyParser = createBodyParser({});
setBodyParsers({ bodyParser, multipartBodyParser: bodyParser });
api.use(router);

const modifierCallResults = [];
Expand Down
8 changes: 4 additions & 4 deletions packages/code-gen/src/experimental/router/js-koa.js
Expand Up @@ -249,14 +249,14 @@ export function jsKoaBuildRouterFile(file, routesPerGroup, contextNamesMap) {
fileContextAddLinePrefix(file, ` * `);
fileWrite(file, `The full router and dispatching\n`);

fileWrite(file, `@param {import("@compas/server").BodyParserPair} parsers`);
fileWrite(file, `@param {import("@compas/server").Middleware} bodyParser`);
fileWrite(file, `@returns {import("@compas/server").Middleware}`);

fileContextRemoveLinePrefix(file, 1);
fileWrite(file, `/`);
fileContextRemoveLinePrefix(file, 2);

fileBlockStart(file, `export function router(parsers)`);
fileBlockStart(file, `export function router(bodyParser)`);

fileWrite(file, `const routes = {`);
fileContextSetIndent(file, 1);
Expand All @@ -281,9 +281,9 @@ export function jsKoaBuildRouterFile(file, routesPerGroup, contextNamesMap) {
fileWrite(file, `ctx.request.params = params;`);

if (route.files) {
fileWrite(file, `await parsers.multipartBodyParser(ctx);`);
fileWrite(file, `await bodyParser(ctx);`);
} else if (route.body || route.query) {
fileWrite(file, `await parsers.bodyParser(ctx);`);
fileWrite(file, `await bodyParser(ctx);`);
}

if (route.params) {
Expand Down
11 changes: 9 additions & 2 deletions packages/code-gen/test/legacy/e2e/server-and-client.test.js
Expand Up @@ -5,7 +5,7 @@ import { pathToFileURL } from "url";
import { mainTestFn, test } from "@compas/cli";
import {
closeTestApp,
createBodyParsers,
createBodyParser,
createTestAppAndClient,
getApp,
} from "@compas/server";
Expand Down Expand Up @@ -540,7 +540,14 @@ async function buildTestApp(importDir) {
},
});
app.use(commonImport.router);
commonImport.setBodyParsers(createBodyParsers({}));

const bodyParser = createBodyParser({
multipart: true,
});
commonImport.setBodyParsers({
bodyParser,
multipartBodyParser: bodyParser,
});

controllerImport.serverHandlers.routeWithReferencedTypes = (ctx, next) => {
ctx.body = ctx.validatedQuery;
Expand Down
4 changes: 2 additions & 2 deletions packages/server/index.d.ts
@@ -1,9 +1,9 @@
export { getApp } from "./src/app.js";
export { createBodyParser } from "./src/middleware/body.js";
export { compose } from "./src/middleware/compose.js";
export type Application = import("./src/app").KoaApplication;
export type Context<S, C, R> = import("koa").ParameterizedContext<S, C, R>;
export type Middleware = import("koa").Middleware;
export type Next = import("koa").Next;
export type BodyParserPair = import("./src/middleware/body").BodyParserPair;
export { createBodyParsers, compose } from "./src/middleware/index.js";
export { closeTestApp, createTestAppAndClient } from "./src/testing.js";
//# sourceMappingURL=index.d.ts.map
7 changes: 2 additions & 5 deletions packages/server/index.js
Expand Up @@ -15,10 +15,7 @@
* @typedef {import("koa").Next} Next
*/

/**
* @typedef {import("./src/middleware/body").BodyParserPair} BodyParserPair
*/

export { getApp } from "./src/app.js";
export { createBodyParsers, compose } from "./src/middleware/index.js";
export { createBodyParser } from "./src/middleware/body.js";
export { compose } from "./src/middleware/compose.js";
export { closeTestApp, createTestAppAndClient } from "./src/testing.js";
12 changes: 5 additions & 7 deletions packages/server/src/app.js
@@ -1,12 +1,10 @@
import { isProduction } from "@compas/stdlib";
import Koa from "koa";
import {
defaultHeaders,
errorHandler,
healthHandler,
logMiddleware,
notFoundHandler,
} from "./middleware/index.js";
import { errorHandler } from "./middleware/error.js";
import { defaultHeaders } from "./middleware/headers.js";
import { healthHandler } from "./middleware/health.js";
import { logMiddleware } from "./middleware/log.js";
import { notFoundHandler } from "./middleware/notFound.js";

/**
* @typedef {ReturnType<getApp>} KoaApplication
Expand Down

0 comments on commit df9e43e

Please sign in to comment.