Skip to content

Commit ab61023

Browse files
committed
use Request.json
1 parent 0a841a5 commit ab61023

File tree

1 file changed

+2
-21
lines changed

1 file changed

+2
-21
lines changed

test/production/reading-request-body-in-middleware/index.test.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,15 @@ describe('reading request body in middleware', () => {
88
beforeAll(async () => {
99
next = await createNext({
1010
files: {
11-
'src/readBody.js': `
12-
export async function readBody(reader, input = reader.read(), body = "") {
13-
const { value, done } = await input;
14-
const inputText = new TextDecoder().decode(value);
15-
body += inputText;
16-
if (done) {
17-
return body;
18-
}
19-
const next = await reader.read();
20-
return readBody(reader, next, body);
21-
}
22-
`,
23-
2411
'pages/_middleware.js': `
2512
const { NextResponse } = require('next/server');
26-
import { readBody } from '../src/readBody';
2713
2814
export default async function middleware(request) {
2915
if (!request.body) {
3016
return new Response('No body', { status: 400 });
3117
}
3218
33-
const reader = await request.body.getReader();
34-
const body = await readBody(reader);
35-
const json = JSON.parse(body);
19+
const json = await request.json();
3620
3721
if (request.nextUrl.searchParams.has("next")) {
3822
return NextResponse.next();
@@ -52,16 +36,13 @@ describe('reading request body in middleware', () => {
5236

5337
'pages/nested/_middleware.js': `
5438
const { NextResponse } = require('next/server');
55-
import { readBody } from '../../src/readBody';
5639
5740
export default async function middleware(request) {
5841
if (!request.body) {
5942
return new Response('No body', { status: 400 });
6043
}
6144
62-
const reader = await request.body.getReader();
63-
const body = await readBody(reader);
64-
const json = JSON.parse(body);
45+
const json = await request.json();
6546
6647
return new Response(JSON.stringify({
6748
root: false,

0 commit comments

Comments
 (0)