0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

feat(serve-static): add onFound option (#3396)

This commit is contained in:
Yusuke Wada 2024-09-10 14:09:19 +09:00 committed by GitHub
parent 3c4d4c2b59
commit a86f3cea5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View File

@ -18,6 +18,11 @@ describe('Serve Static Middleware', () => {
isDir: (path) => {
return path === 'static/hello.world'
},
onFound: (path, c) => {
if (path.endsWith('hello.html')) {
c.header('X-Custom', `Found the file at ${path}`)
}
},
})
app.get('/static/*', serveStatic)
@ -32,6 +37,7 @@ describe('Serve Static Middleware', () => {
expect(res.headers.get('Content-Encoding')).toBeNull()
expect(res.headers.get('Content-Type')).toMatch(/^text\/html/)
expect(await res.text()).toBe('Hello in ./static/hello.html')
expect(res.headers.get('X-Custom')).toBe('Found the file at ./static/hello.html')
})
it('Should return 200 response - /static/sub', async () => {

View File

@ -14,6 +14,7 @@ export type ServeStaticOptions<E extends Env = Env> = {
precompressed?: boolean
mimes?: Record<string, string>
rewriteRequestPath?: (path: string) => string
onFound?: (path: string, c: Context<E>) => void | Promise<void>
onNotFound?: (path: string, c: Context<E>) => void | Promise<void>
}
@ -128,7 +129,7 @@ export const serveStatic = <E extends Env = Env>(
}
}
}
await options.onFound?.(path, c)
return c.body(content)
}