Skip to content

Commit 6f0077f

Browse files
committed
rename to prevent confusion
1 parent be0fb67 commit 6f0077f

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
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 = (limit: number) => string
15+
export type LimitErrorFn = (payloadLimit: number) => string
1616

1717
export type ParserOptions = Partial<{
18-
limit: number
18+
payloadLimit: number
1919
errorFn: LimitErrorFn
2020
}>
2121

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

2424
// Main function
2525
export const p =
26-
<T = any>(fn: (body: any) => any, limit = defaultPayloadLimit, errorFn: LimitErrorFn = defaultErrorFn) =>
26+
<T = any>(fn: (body: any) => any, payloadLimit = defaultPayloadLimit, errorFn: 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 > limit) throw new Error(errorFn(limit))
32+
if (body.length > payloadLimit) throw new Error(errorFn(payloadLimit))
3333
body += chunk
3434
}
3535

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

4949
const json =
50-
({ limit, errorFn }: ParserOptions = {}) =>
50+
({ payloadLimit, errorFn }: 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()) : {}), limit, errorFn)(req, res, next)
53+
req.body = await p((x) => (x ? JSON.parse(x.toString()) : {}), payloadLimit, errorFn)(req, res, next)
5454
} else next()
5555
}
5656

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

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

7373
const urlencoded =
74-
({ limit, errorFn }: ParserOptions = {}) =>
74+
({ payloadLimit, errorFn }: ParserOptions = {}) =>
7575
async (req: ReqWithBody, _res: Response, next: NextFunction) => {
7676
if (hasBody(req.method!)) {
7777
req.body = await p(
7878
(x) => {
7979
const urlSearchParam = new URLSearchParams(x.toString())
8080
return Object.fromEntries(urlSearchParam.entries())
8181
},
82-
limit,
82+
payloadLimit,
8383
errorFn
8484
)(req, _res, next)
8585
} else next()
@@ -129,13 +129,13 @@ type MultipartOptions = Partial<{
129129
}>
130130

131131
const multipart =
132-
({ limit, errorFn, ...opts }: MultipartOptions & ParserOptions = {}) =>
132+
({ payloadLimit, errorFn, ...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-
}, limit, errorFn)(req, res, next)
138+
}, payloadLimit, errorFn)(req, res, next)
139139
next()
140140
} else next()
141141
}

test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ test('should support multiple files', async () => {
387387
}).expect(200)
388388
})
389389

390-
test('should throw on default limit', async () => {
390+
test('should throw on default payloadLimit', async () => {
391391
const server = createServer(async (req: ReqWithBody, res) => {
392392
await text()(req, res, (err) => {
393393
if (err) res.writeHead(413).end(err.message)
@@ -405,9 +405,9 @@ test('should throw on default limit', async () => {
405405
}).expect(413, 'Payload too large. Limit: 104857600 bytes')
406406
})
407407

408-
test('should throw on custom limit', async () => {
408+
test('should throw on custom payloadLimit', async () => {
409409
const server = createServer(async (req: ReqWithBody, res) => {
410-
await text({ limit: 1024 })(req, res, (err) => {
410+
await text({ payloadLimit: 1024 })(req, res, (err) => {
411411
if (err) res.writeHead(413).end(err.message)
412412
else res.end(req.body)
413413
})
@@ -423,9 +423,9 @@ test('should throw on custom limit', async () => {
423423
}).expect(413, 'Payload too large. Limit: 1024 bytes')
424424
})
425425

426-
test('should throw on limit with custom error message', async () => {
426+
test('should throw on payloadLimit with custom error message', async () => {
427427
const server = createServer(async (req: ReqWithBody, res) => {
428-
await text({ limit: 1024, errorFn: (limit) => `Payload too large. Limit: ${limit / 1024}KB` })(req, res, (err) => {
428+
await text({ payloadLimit: 1024, errorFn: (payloadLimit) => `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)