@fastify/swagger-ui
Advanced tools
Comparing version 4.1.1 to 4.2.0
'use strict' | ||
function indexHtml (opts) { | ||
return (hasTrailingSlash) => { | ||
const prefix = hasTrailingSlash ? `.${opts.staticPrefix}` : `${opts.prefix}${opts.staticPrefix}` | ||
const hasLeadingSlash = /^\//.test(opts.prefix) | ||
return (url) => { | ||
const hasTrailingSlash = /\/$/.test(url) | ||
const prefix = hasTrailingSlash ? `.${opts.staticPrefix}` : `${hasLeadingSlash ? '.' : ''}${opts.prefix}${opts.staticPrefix}` | ||
return `<!-- HTML for static distribution bundle build --> | ||
@@ -7,0 +9,0 @@ <!DOCTYPE html> |
@@ -115,6 +115,5 @@ 'use strict' | ||
handler: (req, reply) => { | ||
const hasTrailingSlash = /\/$/.test(req.url) | ||
reply | ||
.header('content-type', 'text/html; charset=utf-8') | ||
.send(indexHtmlContent(hasTrailingSlash)) // trailing slash alters the relative urls generated in the html | ||
.send(indexHtmlContent(req.url)) // trailing slash alters the relative urls generated in the html | ||
} | ||
@@ -121,0 +120,0 @@ }) |
{ | ||
"name": "@fastify/swagger-ui", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "Serve Swagger-ui for Fastify", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -541,3 +541,3 @@ 'use strict' | ||
test('/documentation should display index html with correct asset urls', async (t) => { | ||
t.plan(4) | ||
t.plan(6) | ||
const fastify = Fastify() | ||
@@ -552,8 +552,52 @@ await fastify.register(fastifySwagger, swaggerOption) | ||
t.equal(res.payload.includes('href="/documentation/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="/documentation/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="/documentation/index.css"'), false) | ||
t.equal(res.payload.includes('src="/documentation/theme/theme-js.js"'), false) | ||
t.equal(res.payload.includes('href="./documentation/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./documentation/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./documentation/index.css"'), false) | ||
t.equal(res.payload.includes('src="./documentation/theme/theme-js.js"'), false) | ||
let cssRes = await fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation/static/index.css' | ||
}) | ||
t.equal(cssRes.statusCode, 200) | ||
cssRes = await fastify.inject({ | ||
method: 'GET', | ||
url: './documentation/static/index.css' | ||
}) | ||
t.equal(cssRes.statusCode, 200) | ||
}) | ||
/** | ||
* This emulates when the server is inside an NGINX application that routes by path | ||
*/ | ||
test('/documentation should display index html with correct asset urls when nested', async (t) => { | ||
t.plan(5) | ||
const fastify = Fastify() | ||
await fastify.register( | ||
async () => { | ||
await fastify.register(fastifySwagger, swaggerOption) | ||
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } }) | ||
}, | ||
{ | ||
prefix: '/swagger-app' | ||
} | ||
) | ||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/swagger-app/documentation' | ||
}) | ||
t.equal(res.payload.includes('href="./documentation/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./documentation/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./documentation/index.css"'), false) | ||
t.equal(res.payload.includes('src="./documentation/theme/theme-js.js"'), false) | ||
const cssRes = await fastify.inject({ | ||
method: 'GET', | ||
url: '/swagger-app/documentation/static/index.css' | ||
}) | ||
t.equal(cssRes.statusCode, 200) | ||
}) | ||
test('/documentation/ should display index html with correct asset urls', async (t) => { | ||
@@ -587,8 +631,25 @@ t.plan(4) | ||
t.equal(res.payload.includes('href="/docs/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="/docs/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="/docs/index.css"'), false) | ||
t.equal(res.payload.includes('src="/docs/theme/theme-js.js"'), false) | ||
t.equal(res.payload.includes('href="./docs/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./docs/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./docs/index.css"'), false) | ||
t.equal(res.payload.includes('src="./docs/theme/theme-js.js"'), false) | ||
}) | ||
test('/docs should display index html with correct asset urls when documentation prefix is set with no leading slash', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, swaggerOption) | ||
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: 'docs' }) | ||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/docs' | ||
}) | ||
t.equal(res.payload.includes('href="docs/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="docs/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="docs/index.css"'), false) | ||
t.equal(res.payload.includes('src="docs/theme/theme-js.js"'), false) | ||
}) | ||
test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => { | ||
@@ -610,1 +671,52 @@ t.plan(4) | ||
}) | ||
test('/documentation/ should display index html with correct asset urls', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, swaggerOption) | ||
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] } }) | ||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/documentation/' | ||
}) | ||
t.equal(res.payload.includes('href="./static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./index.css"'), false) | ||
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false) | ||
}) | ||
test('/docs should display index html with correct asset urls when documentation prefix is set', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, swaggerOption) | ||
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' }) | ||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/docs' | ||
}) | ||
t.equal(res.payload.includes('href="./docs/static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./docs/static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./docs/index.css"'), false) | ||
t.equal(res.payload.includes('src="./docs/theme/theme-js.js"'), false) | ||
}) | ||
test('/docs/ should display index html with correct asset urls when documentation prefix is set', async (t) => { | ||
t.plan(4) | ||
const fastify = Fastify() | ||
await fastify.register(fastifySwagger, swaggerOption) | ||
await fastify.register(fastifySwaggerUi, { theme: { js: [{ filename: 'theme-js.js' }] }, routePrefix: '/docs' }) | ||
const res = await fastify.inject({ | ||
method: 'GET', | ||
url: '/docs/' | ||
}) | ||
t.equal(res.payload.includes('href="./static/index.css"'), true) | ||
t.equal(res.payload.includes('src="./static/theme/theme-js.js"'), true) | ||
t.equal(res.payload.includes('href="./index.css"'), false) | ||
t.equal(res.payload.includes('src="./theme/theme-js.js"'), false) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2574030
91
8738