Skip to content

Commit eafa122

Browse files
committed
change payloadLimitErrorFn signature
1 parent 6f0077f commit eafa122

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

src/index.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,24 @@ export const hasBody = (method: string) => ['POST', 'PUT', 'PATCH', 'DELETE'].in
1212

1313
const defaultPayloadLimit = 104857600 // 100KB
1414

15-
export type LimitErrorFn = (payloadLimit: number) => string
15+
export type LimitErrorFn = (payloadLimit: number) => Error
1616

1717
export type ParserOptions = Partial<{
1818
payloadLimit: number
19-
errorFn: LimitErrorFn
19+
payloadLimitErrorFn: LimitErrorFn
2020
}>
2121

22-
const defaultErrorFn: LimitErrorFn = (payloadLimit) => `Payload too large. Limit: ${payloadLimit} bytes`
22+
const defaultErrorFn: LimitErrorFn = (payloadLimit) => new Error(`Payload too large. Limit: ${payloadLimit} bytes`)
2323

2424
// Main function
2525
export const p =
26-
<T = any>(fn: (body: any) => any, payloadLimit = defaultPayloadLimit, errorFn: LimitErrorFn = defaultErrorFn) =>
26+
<T = any>(fn: (body: any) => any, payloadLimit = defaultPayloadLimit, payloadLimitErrorFn: LimitErrorFn = defaultErrorFn) =>
2727
async (req: ReqWithBody<T>, _res: Response, next: (err?: any) => void) => {
2828
try {
2929
let body = ''
3030

3131
for await (const chunk of req) {
32-
if (body.length > payloadLimit) throw new Error(errorFn(payloadLimit))
32+
if (body.length > payloadLimit) throw payloadLimitErrorFn(payloadLimit)
3333
body += chunk
3434
}
3535

@@ -47,31 +47,31 @@ const custom =
4747
}
4848

4949
const json =
50-
({ payloadLimit, errorFn }: ParserOptions = {}) =>
50+
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
5151
async (req: ReqWithBody, res: Response, next: NextFunction) => {
5252
if (hasBody(req.method!)) {
53-
req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}), payloadLimit, errorFn)(req, res, next)
53+
req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}), payloadLimit, payloadLimitErrorFn)(req, res, next)
5454
} else next()
5555
}
5656

5757
const raw =
58-
({ payloadLimit, errorFn }: ParserOptions = {}) =>
58+
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
5959
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
6060
if (hasBody(req.method!)) {
61-
req.body = await p((x) => x, payloadLimit, errorFn)(req, _res, next)
61+
req.body = await p((x) => x, payloadLimit, payloadLimitErrorFn)(req, _res, next)
6262
} else next()
6363
}
6464

6565
const text =
66-
({ payloadLimit, errorFn }: ParserOptions = {}) =>
66+
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
6767
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
6868
if (hasBody(req.method!)) {
69-
req.body = await p((x) => x.toString(), payloadLimit, errorFn)(req, _res, next)
69+
req.body = await p((x) => x.toString(), payloadLimit, payloadLimitErrorFn)(req, _res, next)
7070
} else next()
7171
}
7272

7373
const urlencoded =
74-
({ payloadLimit, errorFn }: ParserOptions = {}) =>
74+
({ payloadLimit, payloadLimitErrorFn }: ParserOptions = {}) =>
7575
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
7676
if (hasBody(req.method!)) {
7777
req.body = await p(
@@ -80,7 +80,7 @@ const urlencoded =
8080
return Object.fromEntries(urlSearchParam.entries())
8181
},
8282
payloadLimit,
83-
errorFn
83+
payloadLimitErrorFn
8484
)(req, _res, next)
8585
} else next()
8686
}
@@ -129,13 +129,13 @@ type MultipartOptions = Partial<{
129129
}>
130130

131131
const multipart =
132-
({ payloadLimit, errorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
132+
({ payloadLimit, payloadLimitErrorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
133133
async (req: ReqWithBody, res: Response, next: NextFunction) => {
134134
if (hasBody(req.method!)) {
135135
req.body = await p((x) => {
136136
const boundary = getBoundary(req.headers['content-type']!)
137137
if (boundary) return parseMultipart(x, boundary, opts)
138-
}, payloadLimit, errorFn)(req, res, next)
138+
}, payloadLimit, payloadLimitErrorFn)(req, res, next)
139139
next()
140140
} else next()
141141
}

test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ test('should throw on custom payloadLimit', async () => {
425425

426426
test('should throw on payloadLimit with custom error message', async () => {
427427
const server = createServer(async (req: ReqWithBody, res) => {
428-
await text({ payloadLimit: 1024, errorFn: (payloadLimit) => `Payload too large. Limit: ${payloadLimit / 1024}KB` })(req, res, (err) => {
428+
await text({ payloadLimit: 1024, payloadLimitErrorFn: (payloadLimit) => new Error(`Payload too large. Limit: ${payloadLimit / 1024}KB`) })(req, res, (err) => {
429429
if (err) res.writeHead(413).end(err.message)
430430
else res.end(req.body)
431431
})

0 commit comments

Comments
 (0)