fastify / fastify-swagger-ui

Serve Swagger-UI for Fastify
MIT License
132 stars 40 forks source link

Fix: add ability to change default logo #93

Closed vserpokryl closed 10 months ago

vserpokryl commented 10 months ago

Checklist

vserpokryl commented 10 months ago

Fix for https://github.com/fastify/fastify-swagger-ui/issues/92

Uzlopak commented 10 months ago

Yeah, hmm. It fixes it for backwards compatibility. But what if someone provides an svg as logo?

vserpokryl commented 10 months ago

@Uzlopak

Yeah, hmm. It fixes it for backwards compatibility. But what if someone provides an svg as logo?

It's fine, because user always provides content as buffer/string, and then this content serialize to base64

@fastify/swagger-ui/lib/swagger-initializer.js

const logoBase64 = Buffer.from(opts.logo.content).toString('base64')
const logoData = `data:${opts.logo.type};base64,${logoBase64}`

@fastify/swagger-ui/types/index.d.ts

type FastifySwaggerUILogo = {
  type: string;
  content: string | Buffer;
}
vserpokryl commented 10 months ago

@Uzlopak the src replacement logic in the img was the same before, nothing has changed here

vserpokryl commented 10 months ago

@Uzlopak Small example:

import fastify from 'fastify';
import swagger from '@fastify/swagger';
import swaggerUI from '@fastify/swagger-ui';

const app = fastify({ logger: true });

(async () => {
  const response = await fetch('https://www.reshot.com/preview-assets/icons/CZ2NMXUGQ8/code-CZ2NMXUGQ8.svg');
  const content = await response.arrayBuffer();

  app.register(swagger);

  app.register(swaggerUI, {
    routePrefix: '/docs',
    logo: {
      content,
      type: 'image/svg+xml',
    },
  });

  await app.listen({ host: '127.0.0.1', port: 3001 });
})();
gurgunday commented 10 months ago

CI using yarn failed

Eomm commented 10 months ago

Could we add a test to avoid regression for next swaggerUI bumps?

vserpokryl commented 10 months ago

@Eomm lib/swagger-initializer.js returns custom initializer as js code To test this feature, we need to be able to launch a browser that displays the full page This requires additional dependencies, for example selenium-webdriver

Eomm commented 10 months ago

I was thinking about Playwright if you are willing to try it. But yeah, we can open a new issue for this