feathersjs-ecosystem / feathers-swagger

Add documentation to your FeatherJS services and feed them to Swagger UI.
MIT License
226 stars 63 forks source link

How do we publish it in a child path? #226

Closed ricardopolo closed 1 year ago

ricardopolo commented 2 years ago

We have our feathers API published under a path. Imagine something like api.domain.com/v2

To do that, we use a reverse proxy (Traeffik) So the docs would be available under api.domain.com/v2/docs

However, the swagger file still appears in the root under api.domain.com/docs, while the whole feathers app runs under api.domain.com/v2. What should be doing?

Mairu commented 2 years ago

Hi, I guess you mean the swagger file is requested from api.domain.com/docs but the page is served from api.domain.com/v2/docs? I understood that you have internally have set your feathers app from the root and the reverse proxy serves it than with a subdomain and the path v2, is this correct?

This would mean that you have to customize the index file for your swagger page, by using the uiIndex option of feathers-swagger.

There is the section in the readme for customizing the UI. https://github.com/feathersjs-ecosystem/feathers-swagger/blob/master/README.md#example-with-ui

There you have the definition of the path const url = '/docs';, which you have to adjust. You could also use a templating engine to customize this, and using the path the page is served from.

ricardopolo commented 2 years ago

Thanks. Would be great to be able to change that in the default feathers swagger ui instead of running our UI

Mairu commented 2 years ago

Alternatively you can use your own function instead of using uiIndex: true.

As you can see the default implementation for uiIndex is -> https://github.com/feathersjs-ecosystem/feathers-swagger/blob/6dff498d35d0e8d1c2dc7187efa78fa94ecfbc7c/lib/index.js#L46-L53

So you can also simply "rewrite" that, to add the correct URL as query parameter.

and then use function(req, res) { ... } as uiIndex config value.

huulbaek commented 1 year ago

I'm wondering how to do this in version 2?

Mairu commented 1 year ago

Well, the indexFile option of feathers-swagger.swaggerUI could be used to define a custom HTML file. In addition now with SwaggerUI >= 4.9, there is an initializer script, which you can define by setting the getSwaggerInitializerScript option. There you can specify the url parameter.

See the options in the readme https://github.com/feathersjs-ecosystem/feathers-swagger/blob/3621b7796eb371893dde19a1c40c0a0112fa82b2/README.md#swaggerswaggeruioptions

Here is the default implementation https://github.com/feathersjs-ecosystem/feathers-swagger/blob/3621b7796eb371893dde19a1c40c0a0112fa82b2/lib/swagger-ui-dist.js#L3-L21

But I will see if I can add support for it, for the next version.

Mairu commented 1 year ago

Support for reverse proxy usage with an x-forwarded-prefix header has been added.