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:
parent
3c4d4c2b59
commit
a86f3cea5f
@ -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 () => {
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user