diff --git a/.npmrc b/.npmrc new file mode 100644 index 00000000..38f11c64 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +registry=https://registry.npmjs.org diff --git a/index.d.ts b/index.d.ts index b1d35d3d..47d74f1a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -171,11 +171,17 @@ declare namespace hapiswagger { cors?: boolean; /** - * The path of JSON endpoint at describes the API + * The path of JSON endpoint that describes the API * @default '/swagger.json' */ jsonPath?: string; + /** + * The path for the controller that serves the JSON that describes the API.If jsonPath is specified and this parameter is not, it will take jsonPath value.Useful when behind a reverse proxy + * @default '/swagger.json' + */ + jsonRoutePath?: string; + /** * The base path from where the API starts i.e. `/v2/` (note, needs to start with `/`) * @default '/' @@ -312,7 +318,7 @@ declare namespace hapiswagger { /** * The path to all the SwaggerUI assets endpoints. - * If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value + * If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value. Useful when behind a reverse proxy * @default: '/swaggerui/' */ routesBasePath?: string; diff --git a/lib/builder.js b/lib/builder.js index 96d0e69d..b4ce59c8 100644 --- a/lib/builder.js +++ b/lib/builder.js @@ -221,6 +221,7 @@ internals.removeNoneSchemaOptions = function(options) { 'documentationRouteTags', 'documentationPage', 'jsonPath', + 'jsonRoutePath', 'auth', 'swaggerUIPath', 'routesBasePath', diff --git a/lib/defaults.js b/lib/defaults.js index 6ef132ca..b8d0ca32 100644 --- a/lib/defaults.js +++ b/lib/defaults.js @@ -3,6 +3,7 @@ const { resolve } = require('path'); module.exports = { debug: false, jsonPath: '/swagger.json', + jsonRoutePath: '/swagger.json', documentationPath: '/documentation', documentationRouteTags: [], documentationRoutePlugins: {}, diff --git a/lib/index.js b/lib/index.js index 4cdd3e78..7ce847f4 100644 --- a/lib/index.js +++ b/lib/index.js @@ -15,6 +15,7 @@ const Utilities = require('../lib/utilities'); const schema = Joi.object({ debug: Joi.boolean(), jsonPath: Joi.string(), + jsonRoutePath: Joi.string(), documentationPath: Joi.string(), documentationRouteTags: Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())), documentationRoutePlugins: Joi.object().default({}), @@ -71,6 +72,10 @@ exports.plugin = { settings.routesBasePath = options.swaggerUIPath; } + if (!options.jsonRoutePath && options.jsonPath) { + settings.jsonRoutePath = options.jsonPath; + } + settings.log = (tags, data) => { tags.unshift('hapi-swagger'); if (settings.debug) { @@ -103,7 +108,7 @@ exports.plugin = { server.route([ { method: 'GET', - path: settings.jsonPath, + path: settings.jsonRoutePath, options: { auth: settings.auth, cors: settings.cors, diff --git a/optionsreference.md b/optionsreference.md index 52bb3523..d342048f 100644 --- a/optionsreference.md +++ b/optionsreference.md @@ -11,7 +11,8 @@ #### JSON (JSON endpoint needed to create UI) -- `jsonPath`: (string) The path of JSON endpoint at describes the API - default: `/swagger.json` +- `jsonPath`: (string) The path of the JSON endpoint that describes the API - default: `/swagger.json` +- `jsonRoutePath`: (string) The path for the controller that serves the JSON that describes the API. If jsonPath is specified and this parameter is not, it will take jsonPath value. Useful when behind a reverse proxy - default: `/swagger.json` - `basePath`: (string) The base path from where the API starts i.e. `/v2/` (note, needs to start with `/`) - default: `/` - `pathPrefixSize`: (number) Selects what segment of the URL path is used to group endpoints - default: `1` - `pathReplacements` : (array) methods for modifying path and group names in documentation - default: `[]` @@ -52,7 +53,7 @@ - `swaggerUI`: (boolean) Add files that support SwaggerUI. Only removes files if `documentationPage` is also set to false - default: `true` - `swaggerUIPath`: (string) The path of to all the SwaggerUI resources - default: `/swaggerui/` -- `routesBasePath`: (string) The path to all the SwaggerUI assets endpoints. If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value - default: `/swaggerui/` +- `routesBasePath`: (string) The path to all the SwaggerUI assets endpoints. If swaggerUIPath is specified and this parameter is not, it will take swaggerUIPath value. Useful when behind a reverse proxy - default: `/swaggerui/` - `documentationPage`: (boolean) Add documentation page - default: `true` - `documentationPath`: (string) The path of the documentation page - default: `/documentation` - `templates`: (string) The directory path used by `hapi-swagger` and `@hapi/vision` to resolve and load the templates to render `swagger-ui` interface. The directory must contain `index.html` and `debug.html` templates. Default is `templates` directory in this package. diff --git a/test/Integration/plugin-test.js b/test/Integration/plugin-test.js index 1a63c253..81813934 100644 --- a/test/Integration/plugin-test.js +++ b/test/Integration/plugin-test.js @@ -109,6 +109,26 @@ lab.experiment('plugin', () => { expect(response.statusCode).to.equal(200); }); + lab.test('repathed jsonPath url and jsonRoutePath', async () => { + const jsonRoutePath = '/testRoute/test.json'; + + const server = await Helper.createServer( + { + ...swaggerOptions, + jsonRoutePath + }, + routes + ); + + const notFoundResponse = await server.inject({ method: 'GET', url: '/test.json' }); + + expect(notFoundResponse.statusCode).to.equal(404); + + const okResponse = await server.inject({ method: 'GET', url: jsonRoutePath }); + + expect(okResponse.statusCode).to.equal(200); + }); + lab.test('repathed documentationPath url', async () => { const server = await Helper.createServer(swaggerOptions, routes); const response = await server.inject({ method: 'GET', url: '/testdoc' });