Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ app.use((ctx) => {
);
```

- **enableRawChecking**: support the already parsed body on the raw request by override and prioritize the parsed value over the sended payload. (default is `false`)
- **enableRawChecking**: support the already parsed body on the raw request by override and prioritize the parsed value over the sent payload. (default is `false`)

- **parsedMethods**: declares the HTTP methods where bodies will be parsed, default `['POST', 'PUT', 'PATCH']`.
- **parsedMethods**: declares the HTTP methods where bodies will be parsed, default `['POST', 'PUT', 'PATCH', 'QUERY']`.

- **disableBodyParser**: you can dynamic disable body parser by set `ctx.disableBodyParser = true`.
- **disableBodyParser**: you can dynamically disable the body parser by setting `ctx.disableBodyParser = true`.

```js
app.use((ctx, next) => {
Expand Down
2 changes: 1 addition & 1 deletion src/body-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ declare module 'http' {
export function bodyParserWrapper(opts: BodyParserOptions = {}) {
const {
patchNode = false,
parsedMethods = ['POST', 'PUT', 'PATCH'],
parsedMethods = ['POST', 'PUT', 'PATCH', 'QUERY'],
detectJSON,
onError,
enableTypes = ['json', 'form'],
Expand Down
2 changes: 1 addition & 1 deletion src/body-parser.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type BodyType = (typeof supportedBodyTypes)[number];
export type BodyParserOptions = {
/**
* declares the HTTP methods where bodies will be parsed.
* @default ['POST', 'PUT', 'PATCH']
* @default ['POST', 'PUT', 'PATCH', 'QUERY']
*/
parsedMethods?: string[];
/**
Expand Down
29 changes: 29 additions & 0 deletions test/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,35 @@ describe('test/body-parser.test.ts', () => {
});
});

describe('parsedMethods', () => {
it('should parse json body on QUERY request', async () => {
const app = createApp();

app.use(async (ctx) => {
expect(ctx.request.body).toEqual({ foo: 'bar' });
expect(ctx.request.rawBody).toEqual('{"foo":"bar"}');
ctx.body = ctx.request.body;
});

await new request.Test(app.callback(), 'QUERY', '/')
.send({ foo: 'bar' })
.expect({ foo: 'bar' });
});

it('should not parse body on QUERY request when parsedMethods omits it', async () => {
const app = createApp({ parsedMethods: ['POST', 'PUT', 'PATCH'] });

app.use(async (ctx) => {
expect(ctx.request.body).toBeUndefined();
ctx.body = ctx.request.body;
});

await new request.Test(app.callback(), 'QUERY', '/')
.send({ foo: 'bar' })
.expect({});
});
});

describe('onError', () => {
const app = createApp({
onError({}, ctx) {
Expand Down
Loading