Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
etag: useETag = true,
extension = true,
indexHTML = true,
hideOpenApiRoute = true,
decodeURI,
silent
}: StaticOptions<Prefix> = {}): Promise<Elysia> {
Expand Down Expand Up @@ -89,11 +90,16 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
if (isBun && absolutePath.endsWith('.html')) {
const htmlBundle = await import(absolutePath)

app.get(pathName, htmlBundle.default)
app.get(pathName, htmlBundle.default, {
detail: { hide: hideOpenApiRoute }
})
if (indexHTML && pathName.endsWith('/index.html'))
app.get(
pathName.replace('/index.html', ''),
htmlBundle.default
htmlBundle.default,
{
detail: { hide: hideOpenApiRoute }
}
)

continue
Expand Down Expand Up @@ -199,7 +205,10 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
headers: initialHeaders
}
: undefined
)
),
{
detail: { hide: hideOpenApiRoute }
}
)

if (indexHTML && pathName.endsWith('/index.html'))
Expand All @@ -214,7 +223,10 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
headers: initialHeaders
}
: undefined
)
),
{
detail: { hide: hideOpenApiRoute }
}
)
}

Expand All @@ -235,11 +247,16 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
const pathName = normalizePath(path.join(prefix, relativePath))
const htmlBundle = await import(absolutePath)

app.get(pathName, htmlBundle.default)
app.get(pathName, htmlBundle.default, {
detail: { hide: hideOpenApiRoute }
})
if (indexHTML && pathName.endsWith('/index.html'))
app.get(
pathName.replace('/index.html', ''),
htmlBundle.default
htmlBundle.default,
{
detail: { hide: hideOpenApiRoute }
}
)
}
}
Expand All @@ -251,7 +268,7 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({
path.join(
assets,
decodeURI
? (fastDecodeURI(params['*']) ?? params['*'])
? fastDecodeURI(params['*']) ?? params['*']
: params['*']
)
)
Expand Down Expand Up @@ -328,6 +345,9 @@ export async function staticPlugin<const Prefix extends string = '/prefix'>({

throw new NotFoundError()
}
},
{
detail: { hide: hideOpenApiRoute }
}
)
}
Expand Down
25 changes: 16 additions & 9 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,21 @@ export interface StaticOptions<Prefix extends string> {
*
* @default false
*/
decodeURI?: boolean
decodeURI?: boolean

/**
* silent
*
* @default false
*
* If set to true, suppresses all logs and warnings from the static plugin
*/
silent?: boolean
/**
* @default true
*
* Hide routes for OpenAPI and swagger document.
*/
hideOpenApiRoute?: boolean

/**
* silent
*
* @default false
*
* If set to true, suppresses all logs and warnings from the static plugin
*/
silent?: boolean
}
22 changes: 22 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,26 @@ describe('Static Plugin', () => {
res = await app.handle(req('/public/html'))
expect(res.status).toBe(404)
})

it('does hide detail for openapi', async () => {
const app = new Elysia().use(staticPlugin())
await app.modules

for (const route of app.routes) {
expect(route.hooks.detail.hide).toBeTrue()
}
})

it('does not hide detail when hideOpenApiRoute is false', async () => {
const app = new Elysia().use(
staticPlugin({
hideOpenApiRoute: false
})
)
await app.modules

for (const route of app.routes) {
expect(route.hooks.detail.hide).toBeFalse()
}
})
})