Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds optional parameter for custom url to swagger-ui css and js bundle #63

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const swagger =
exclude = [],
swaggerOptions = {},
theme = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`,
customSwaggerUiBundleUrl = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js`,
autoDarkMode = true
}: ElysiaSwaggerConfig<Path> = {
documentation: {},
Expand All @@ -36,9 +37,6 @@ export const swagger =
const schema = {}
let totalRoutes = 0

if (!version)
version = `https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css`
Copy link
Author

@BeGj BeGj Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed nonsense code..?


const info = {
title: 'Elysia Documentation',
description: 'Development documentation',
Expand Down Expand Up @@ -109,7 +107,7 @@ export const swagger =
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js" crossorigin></script>
<script src="${customSwaggerUiBundleUrl}" crossorigin></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle(${stringifiedSwaggerOptions});
Expand Down
11 changes: 10 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
| 'parameterMacro'
>
/**
* Custom Swagger CSS
* Url to custom swagger css.
* Overrides version parameter if set
* Default is https://unpkg.com/swagger-ui-dist@${version}/swagger-ui.css.
*/
theme?: string | {
light: string
Expand All @@ -71,4 +73,11 @@ export interface ElysiaSwaggerConfig<Path extends string = '/swagger'> {
* Using poor man dark mode 😭
*/
autoDarkMode?: boolean

/**
* Url to custom swagger js bundle.
* Overrides version parameter if set
* Default is https://unpkg.com/swagger-ui-dist@${version}/swagger-ui-bundle.js
*/
customSwaggerUiBundleUrl?: string
}
54 changes: 54 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,60 @@ describe('Swagger', () => {
).toBe(true)
})


it('use custom swagger bundle url', async () => {
const app = new Elysia().use(
swagger({
customSwaggerUiBundleUrl: 'example.com/swagger-ui-bundle.js'
})
)

const res = await app.handle(req('/swagger')).then((x) => x.text())
expect(
res.includes(
'example.com/swagger-ui-bundle.js'
)
).toBe(true)
})

it('use custom swagger css url - string', async () => {
const app = new Elysia().use(
swagger({
theme: 'example.com/swagger-ui.css'
})
)

const res = await app.handle(req('/swagger')).then((x) => x.text())
expect(
res.includes(
'example.com/swagger-ui.css'
)
).toBe(true)
})

it('use custom swagger css url - dark and light', async () => {
const app = new Elysia().use(
swagger({
theme: {
dark: 'example.com/dark/swagger-ui.css',
light: 'example.com/light/swagger-ui.css',
}
})
)

const res = await app.handle(req('/swagger')).then((x) => x.text())
expect(
res.includes(
'example.com/dark/swagger-ui.css'
)
).toBe(true)
expect(
res.includes(
'example.com/light/swagger-ui.css'
)
).toBe(true)
})

it('follow title and description', async () => {
const app = new Elysia().use(
swagger({
Expand Down