Skip to content

Commit

Permalink
fix(use/koa): Use parsed body from request (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
henrinormak committed Dec 14, 2023
1 parent 84c0e99 commit b290b90
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@types/express": "^4.17.21",
"@types/glob": "^8.1.0",
"@types/koa": "^2.13.12",
"@types/koa-bodyparser": "^4",
"@types/koa-mount": "^4.0.5",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
Expand All @@ -123,6 +124,7 @@
"glob": "^10.3.10",
"graphql": "^16.8.1",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.1",
"koa-mount": "^4.0.0",
"prettier": "^3.1.0",
"rollup": "^4.6.0",
Expand Down
12 changes: 9 additions & 3 deletions src/use/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export type HandlerOptions<
Context
>;

type WithPossibleBody = { body?: string | Record<PropertyKey, unknown> };

/**
* The ready-to-use handler for [Koa](https://expressjs.com).
*
Expand Down Expand Up @@ -82,10 +84,14 @@ export function createHandler<
},
},
body: () => {
if (ctx.body) {
// in case koa has a body parser
return ctx.body;
// in case koa has a body parser
const body =
(ctx.request as WithPossibleBody).body ||
(ctx.req as WithPossibleBody).body;
if (body) {
return body;
}

return new Promise<string>((resolve) => {
let body = '';
ctx.req.on('data', (chunk) => (body += chunk));
Expand Down
15 changes: 15 additions & 0 deletions tests/use.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import express from 'express';
import Fastify from 'fastify';
import Koa from 'koa';
import mount from 'koa-mount';
import bodyparser from 'koa-bodyparser';
import { schema, pong } from './fixtures/simple';

import { createHandler as createHttpHandler } from '../src/use/http';
Expand Down Expand Up @@ -108,6 +109,20 @@ it.each([
] as const;
},
},
{
name: 'koa with bodyparser',
startServer: async () => {
const app = new Koa();
app.use(bodyparser());
app.use(mount('/', createKoaHandler({ schema })));
const server = app.listen({ port: 0 });
const port = (server.address() as net.AddressInfo).port;
return [
`http://localhost:${port}`,
makeDisposeForServer(server),
] as const;
},
},
// no need to test fetch because the handler is pure (gets request, returns response)
// {
// name: 'fetch',
Expand Down
71 changes: 70 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2404,6 +2404,15 @@ __metadata:
languageName: node
linkType: hard

"@types/koa-bodyparser@npm:^4":
version: 4.3.12
resolution: "@types/koa-bodyparser@npm:4.3.12"
dependencies:
"@types/koa": "npm:*"
checksum: 645cc253c6b9b2e98252b1cdc75a4812cd6d3c228e426f9893a755324b7a6936559ec659a0ff288cb2642340b3cc4e2110167f24b84efc8e3b89c04fe67ed883
languageName: node
linkType: hard

"@types/koa-compose@npm:*":
version: 3.2.8
resolution: "@types/koa-compose@npm:3.2.8"
Expand Down Expand Up @@ -3724,6 +3733,18 @@ __metadata:
languageName: node
linkType: hard

"co-body@npm:^6.0.0":
version: 6.1.0
resolution: "co-body@npm:6.1.0"
dependencies:
inflation: "npm:^2.0.0"
qs: "npm:^6.5.2"
raw-body: "npm:^2.3.3"
type-is: "npm:^1.6.16"
checksum: 2484710f70941b42512a349fab0fac8a16430fd56d88ad0de1d8dde129d66597de9d0a8edd8d8164683c55a3fd998457dc74a04a03de2281def8abaaecd1317e
languageName: node
linkType: hard

"co@npm:^4.6.0":
version: 4.6.0
resolution: "co@npm:4.6.0"
Expand Down Expand Up @@ -3972,6 +3993,13 @@ __metadata:
languageName: node
linkType: hard

"copy-to@npm:^2.0.1":
version: 2.0.1
resolution: "copy-to@npm:2.0.1"
checksum: 05ea12875bdc96ae053a3b30148e9d992026035ff2bfcc0b615e8d49d1cf8fc3d1f40843f9a4b7b1b6d9118eeebcba31e621076d7de525828aa9c07d22a81dab
languageName: node
linkType: hard

"core-util-is@npm:~1.0.0":
version: 1.0.3
resolution: "core-util-is@npm:1.0.3"
Expand Down Expand Up @@ -6105,6 +6133,7 @@ __metadata:
"@types/express": "npm:^4.17.21"
"@types/glob": "npm:^8.1.0"
"@types/koa": "npm:^2.13.12"
"@types/koa-bodyparser": "npm:^4"
"@types/koa-mount": "npm:^4.0.5"
"@typescript-eslint/eslint-plugin": "npm:^6.13.1"
"@typescript-eslint/parser": "npm:^6.13.1"
Expand All @@ -6116,6 +6145,7 @@ __metadata:
glob: "npm:^10.3.10"
graphql: "npm:^16.8.1"
koa: "npm:^2.14.2"
koa-bodyparser: "npm:^4.4.1"
koa-mount: "npm:^4.0.0"
prettier: "npm:^3.1.0"
rollup: "npm:^4.6.0"
Expand Down Expand Up @@ -6682,6 +6712,13 @@ __metadata:
languageName: node
linkType: hard

"inflation@npm:^2.0.0":
version: 2.1.0
resolution: "inflation@npm:2.1.0"
checksum: 80c1b5d9ec408105a85f0623c824d668ddf0cadafd8d9716c0737990e5a712ae5f7d6bb0ff216b6648eccb9c6ac69fe06c0d8c58456d168db5bf550c89dd74ed
languageName: node
linkType: hard

"inflight@npm:^1.0.4":
version: 1.0.6
resolution: "inflight@npm:1.0.6"
Expand Down Expand Up @@ -7260,6 +7297,17 @@ __metadata:
languageName: node
linkType: hard

"koa-bodyparser@npm:^4.4.1":
version: 4.4.1
resolution: "koa-bodyparser@npm:4.4.1"
dependencies:
co-body: "npm:^6.0.0"
copy-to: "npm:^2.0.1"
type-is: "npm:^1.6.18"
checksum: c741a99ccacc92ee126edad121fed2d200753348e0dedfd65ec67fcfa513b4db9f791ef3200817358ab2c120bcf8e73488cbd0b7f3c7d522a0b21bbb647ce616
languageName: node
linkType: hard

"koa-compose@npm:^4.1.0":
version: 4.1.0
resolution: "koa-compose@npm:4.1.0"
Expand Down Expand Up @@ -10846,6 +10894,15 @@ __metadata:
languageName: node
linkType: hard

"qs@npm:^6.5.2":
version: 6.11.2
resolution: "qs@npm:6.11.2"
dependencies:
side-channel: "npm:^1.0.4"
checksum: f2321d0796664d0f94e92447ccd3bdfd6b6f3a50b6b762aa79d7f5b1ea3a7a9f94063ba896b82bc2a877ed6a7426d4081e4f16568fdb04f0ee188cca9d8505b4
languageName: node
linkType: hard

"queue-microtask@npm:^1.2.2":
version: 1.2.3
resolution: "queue-microtask@npm:1.2.3"
Expand Down Expand Up @@ -10888,6 +10945,18 @@ __metadata:
languageName: node
linkType: hard

"raw-body@npm:^2.3.3":
version: 2.5.2
resolution: "raw-body@npm:2.5.2"
dependencies:
bytes: "npm:3.1.2"
http-errors: "npm:2.0.0"
iconv-lite: "npm:0.4.24"
unpipe: "npm:1.0.0"
checksum: 863b5171e140546a4d99f349b720abac4410338e23df5e409cfcc3752538c9caf947ce382c89129ba976f71894bd38b5806c774edac35ebf168d02aa1ac11a95
languageName: node
linkType: hard

"rc@npm:^1.2.8":
version: 1.2.8
resolution: "rc@npm:1.2.8"
Expand Down Expand Up @@ -12650,7 +12719,7 @@ __metadata:
languageName: node
linkType: hard

"type-is@npm:^1.6.16, type-is@npm:~1.6.18":
"type-is@npm:^1.6.16, type-is@npm:^1.6.18, type-is@npm:~1.6.18":
version: 1.6.18
resolution: "type-is@npm:1.6.18"
dependencies:
Expand Down

0 comments on commit b290b90

Please sign in to comment.