Skip to content

Commit

Permalink
feat: add missing handling of jsonPath route
Browse files Browse the repository at this point in the history
  • Loading branch information
alferpal committed Jan 24, 2020
1 parent e34532a commit e187497
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 5 deletions.
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
registry=https://registry.npmjs.org
10 changes: 8 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/'
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions lib/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ internals.removeNoneSchemaOptions = function(options) {
'documentationRouteTags',
'documentationPage',
'jsonPath',
'jsonRoutePath',
'auth',
'swaggerUIPath',
'routesBasePath',
Expand Down
1 change: 1 addition & 0 deletions lib/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { resolve } = require('path');
module.exports = {
debug: false,
jsonPath: '/swagger.json',
jsonRoutePath: '/swagger.json',
documentationPath: '/documentation',
documentationRouteTags: [],
documentationRoutePlugins: {},
Expand Down
7 changes: 6 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({}),
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -103,7 +108,7 @@ exports.plugin = {
server.route([
{
method: 'GET',
path: settings.jsonPath,
path: settings.jsonRoutePath,
options: {
auth: settings.auth,
cors: settings.cors,
Expand Down
5 changes: 3 additions & 2 deletions optionsreference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `[]`
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions test/Integration/plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down

0 comments on commit e187497

Please sign in to comment.