fix(build): stricter request body API removal with mode option (#86) · honojs/cli@1c13813 · GitHub
Skip to content

Commit 1c13813

Browse files
yusukebeusualoma
andauthored
fix(build): stricter request body API removal with mode option (#86)
Co-authored-by: Taku Amano <taku@taaas.jp>
1 parent c471573 commit 1c13813

3 files changed

Lines changed: 120 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions

‎src/commands/build/index.test.ts‎

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,55 @@ describe('buildCommand', () => {
339339
expect(content).toMatch(/parseBody/)
340340
})
341341

342+
it('should keep request body APIs when all method is used', { timeout: 0 }, async () => {
343+
writePackageJSON(dir)
344+
await npmInstall()
345+
writeFileSync(
346+
join(dir, './src/index.ts'),
347+
`
348+
import { Hono } from 'hono'
349+
const app = new Hono()
350+
app.get('/', (c) => c.text('Hello'))
351+
app.all('/data', async (c) => {
352+
const body = await c.req.json()
353+
return c.json(body)
354+
})
355+
export default app
356+
`
357+
)
358+
await program.parseAsync(['node', 'hono', 'build', '--optimize'])
359+
360+
const content = readFileSync(join(dir, './dist/index.js'), 'utf-8')
361+
expect(content).toMatch(/parseBody/)
362+
})
363+
364+
it('should keep request body APIs when middleware is used', { timeout: 0 }, async () => {
365+
writePackageJSON(dir)
366+
await npmInstall()
367+
writeFileSync(
368+
join(dir, './src/index.ts'),
369+
`
370+
import { Hono } from 'hono'
371+
const app = new Hono()
372+
app.use(async (c, next) => {
373+
if (c.req.method === 'POST') {
374+
const body = await c.req.json()
375+
c.set('body', body)
376+
}
377+
await next()
378+
})
379+
app.get('/', (c) => c.text('Hello'))
380+
export default app
381+
`
382+
)
383+
await program.parseAsync(['node', 'hono', 'build', '--optimize'])
384+
385+
const content = readFileSync(join(dir, './dist/index.js'), 'utf-8')
386+
expect(content).toMatch(/parseBody/)
387+
})
388+
342389
it(
343-
'should keep request body APIs when --no-request-body-api-removal is specified',
390+
'should keep request body APIs when --request-body-api-removal disable is specified',
344391
{ timeout: 0 },
345392
async () => {
346393
writePackageJSON(dir)
@@ -359,13 +406,65 @@ describe('buildCommand', () => {
359406
'hono',
360407
'build',
361408
'--optimize',
362-
'--no-request-body-api-removal',
409+
'--request-body-api-removal',
410+
'disable',
363411
])
364412

365413
const content = readFileSync(join(dir, './dist/index.js'), 'utf-8')
366414
expect(content).toMatch(/parseBody/)
367415
}
368416
)
417+
418+
it(
419+
'should remove request body APIs when --request-body-api-removal force is specified',
420+
{ timeout: 0 },
421+
async () => {
422+
writePackageJSON(dir)
423+
await npmInstall()
424+
writeFileSync(
425+
join(dir, './src/index.ts'),
426+
`
427+
import { Hono } from 'hono'
428+
const app = new Hono()
429+
app.post('/data', async (c) => {
430+
const body = await c.req.json()
431+
return c.json(body)
432+
})
433+
export default app
434+
`
435+
)
436+
await program.parseAsync([
437+
'node',
438+
'hono',
439+
'build',
440+
'--optimize',
441+
'--request-body-api-removal',
442+
'force',
443+
])
444+
445+
const content = readFileSync(join(dir, './dist/index.js'), 'utf-8')
446+
expect(content).not.toMatch(/parseBody/)
447+
expect(content).not.toMatch(/#cachedBody/)
448+
}
449+
)
450+
451+
it('should print a JSON error with an invalid mode', async () => {
452+
const log = vi.spyOn(console, 'log').mockImplementation(() => {})
453+
await program.parseAsync([
454+
'node',
455+
'hono',
456+
'build',
457+
'--optimize',
458+
'--request-body-api-removal',
459+
'hoge',
460+
])
461+
const parsed = JSON.parse(log.mock.calls[0][0] as string)
462+
expect(parsed.ok).toBe(false)
463+
expect(parsed.error.code).toBe('INVALID_OPTION')
464+
expect(process.exitCode).toBe(1)
465+
process.exitCode = undefined
466+
log.mockRestore()
467+
})
369468
})
370469

371470
describe('Hono API removal', () => {

‎src/commands/build/index.ts‎

Lines changed: 17 additions & 8 deletions

0 commit comments

Comments
 (0)