Skip to content

Commit

Permalink
feat: Add indexPrefix option (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
rozzilla authored Dec 3, 2024
1 parent d6a5af7 commit ff68299
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 81 deletions.
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,20 @@ await fastify.ready()

#### Options

| Option | Default | Description |
| ------------------ | ---------------- | ------------------------------------------------------------------------------------------------------------------------- |
| baseDir | undefined | Specify the directory where all spec files that are included in the main one using $ref will be located. By default, this is the directory where the main spec file is located. Provided value should be an absolute path without trailing slash. |
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
| routePrefix | '/documentation' | Overwrite the default Swagger UI route prefix. |
| staticCSP | false | Enable CSP header for static resources. |
| transformStaticCSP | undefined | Synchronous function to transform CSP header for static resources if the header has been previously set. |
| transformSpecification | undefined | Synchronous function to transform the swagger document. |
| transformSpecificationClone| true | Provide a deepcloned swaggerObject to transformSpecification |
| uiConfig | {} | Configuration options for [Swagger UI](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md). |
| uiHooks | {} | Additional hooks for the documentation's routes. You can provide the `onRequest` and `preHandler` hooks with the same [route's options](https://fastify.dev/docs/latest/Reference/Routes/#routes-options) interface.|
| theme | {} | Add custom JavaScript and CSS to the Swagger UI web page |
| logLevel | info | Allow to define route log level. |
| Option | Default | Description |
| ------------------ | --------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| baseDir | undefined | Specify the directory where all spec files that are included in the main one using $ref will be located. By default, this is the directory where the main spec file is located. Provided value should be an absolute path without trailing slash. |
| initOAuth | {} | Configuration options for [Swagger UI initOAuth](https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/). |
| routePrefix | '/documentation' | Overwrite the default Swagger UI route prefix. |
| indexPrefix | '' | Add an additional prefix. This is for when the Fastify server is behind path based routing. ex. NGINX |
| staticCSP | false | Enable CSP header for static resources. |
| transformStaticCSP | undefined | Synchronous function to transform CSP header for static resources if the header has been previously set. |
| transformSpecification | undefined | Synchronous function to transform the swagger document. |
| transformSpecificationClone| true | Provide a deepcloned swaggerObject to transformSpecification |
| uiConfig | {} | Configuration options for [Swagger UI](https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md). |
| uiHooks | {} | Additional hooks for the documentation's routes. You can provide the `onRequest` and `preHandler` hooks with the same [route's options](https://fastify.dev/docs/latest/Reference/Routes/#routes-options) interface. |
| theme | {} | Add custom JavaScript and CSS to the Swagger UI web page |
| logLevel | info | Allow to define route log level. |

The plugin will expose the documentation with the following APIs:

Expand Down
7 changes: 5 additions & 2 deletions lib/index-html.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict'

function indexHtml (opts) {
const hasLeadingSlash = /^\//.test(opts.prefix)
let routePrefix = opts.prefix
if (opts.indexPrefix) {
routePrefix = `${opts.indexPrefix.replace(/\/$/, '')}/${opts.prefix.replace(/^\//, '')}`
}
return (url) => {
const hasTrailingSlash = /\/$/.test(url)
const prefix = hasTrailingSlash ? `.${opts.staticPrefix}` : `${hasLeadingSlash ? '.' : ''}${opts.prefix}${opts.staticPrefix}`
const prefix = hasTrailingSlash ? `.${opts.staticPrefix}` : `${routePrefix}${opts.staticPrefix}`
return `<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
Expand Down
4 changes: 2 additions & 2 deletions test/prepare.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('Swagger source does not contain sourceMaps', async (t) => {

const includesSourceMap = res.payload.includes('sourceMappingURL')
t.assert.deepStrictEqual(includesSourceMap, false)
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=UTF-8')
t.assert.deepStrictEqual(res.headers['content-type'], 'application/javascript; charset=utf-8')
})

test('Swagger css does not contain sourceMaps', async (t) => {
Expand All @@ -34,5 +34,5 @@ test('Swagger css does not contain sourceMaps', async (t) => {

const includesSourceMap = res.payload.includes('sourceMappingURL')
t.assert.deepStrictEqual(includesSourceMap, false)
t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=UTF-8')
t.assert.deepStrictEqual(res.headers['content-type'], 'text/css; charset=utf-8')
})
Loading

0 comments on commit ff68299

Please sign in to comment.