Skip to content

Commit f4c4912

Browse files
committed
fix: use JsonValue from type-fest as body type fixes: #245
1 parent c3e63af commit f4c4912

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"@types/koa": "^3.0.0",
3636
"co-body": "^6.2.0",
3737
"formidable": "^3.5.4",
38+
"type-fest": "^5.3.1",
3839
"zod": "^4.1.12"
3940
},
4041
"devDependencies": {

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as coBody from 'co-body';
22
import type { Middleware, Next } from 'koa';
3+
import type { JsonValue } from 'type-fest';
34
import type { KoaBodyMiddlewareOptions, ScalarOrArrayFiles } from './types.js';
45
import { KoaBodyMiddlewareOptionsSchema } from './types.js';
56
import {
@@ -17,15 +18,15 @@ export * from './types.js';
1718

1819
declare module 'koa' {
1920
interface Request {
20-
body?: { [key: string]: unknown } | string;
21+
body?: JsonValue;
2122
rawBody?: string;
2223
files?: ScalarOrArrayFiles;
2324
}
2425
}
2526

2627
declare module 'http' {
2728
interface IncomingMessage {
28-
body?: { [key: string]: unknown } | string;
29+
body?: JsonValue;
2930
rawBody?: string;
3031
files?: ScalarOrArrayFiles;
3132
}

src/utils/patch-util.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import type { Context } from 'koa';
2+
import type { JsonValue } from 'type-fest';
23
import type { ParseWithFormidableResult } from './parse-with-formidable.js';
34

4-
export type ParseWithCoBodyTextResult = string;
5-
export type ParseWithCoBodyJsonResult = { [key: string]: unknown };
6-
export type ParseWithCoBodyUrlEncodedResult = { [key: string]: unknown };
75
export type ParseWithCoBodyIncludeUnparsedResult = { parsed: ParseWithCoBodyResult; raw: string };
86

9-
export type ParseWithCoBodyResult =
10-
| ParseWithCoBodyTextResult
11-
| ParseWithCoBodyJsonResult
12-
| ParseWithCoBodyUrlEncodedResult;
7+
export type ParseWithCoBodyResult = JsonValue;
138

149
type PatchOptions = {
1510
isMultipart: string | boolean | null;
@@ -35,7 +30,7 @@ export function patchNodeAndKoa(
3530
ctx.req.body = parsed;
3631
ctx.req.rawBody = raw;
3732
} else {
38-
ctx.req.body = body;
33+
ctx.req.body = body as ParseWithCoBodyResult;
3934
}
4035
}
4136
if (patchKoa) {
@@ -48,7 +43,7 @@ export function patchNodeAndKoa(
4843
ctx.request.body = parsed;
4944
ctx.request.rawBody = raw;
5045
} else {
51-
ctx.request.body = body;
46+
ctx.request.body = body as ParseWithCoBodyResult;
5247
}
5348
}
5449
}

0 commit comments

Comments
 (0)