From 8808b0abfce363b3ca4cb698fe0817d68c6e6e4f Mon Sep 17 00:00:00 2001 From: newhouse Date: Wed, 1 Jun 2022 11:19:05 -0400 Subject: [PATCH 1/3] allow multiple server display via option --- config-example.yml | 6 ++ examples/config.yml | 8 ++ src/index.js | 1 + src/spectaql/index.js | 1 + .../default/helpers/generateApiEndpoints.js | 35 ++++++++ .../layout/content/introduction/welcome.hbs | 4 +- test/src/helpers/generateApiEndpoints.test.js | 86 +++++++++++++++++++ 7 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/themes/default/helpers/generateApiEndpoints.js create mode 100644 test/src/helpers/generateApiEndpoints.test.js diff --git a/config-example.yml b/config-example.yml index adcc0f16..9a629879 100644 --- a/config-example.yml +++ b/config-example.yml @@ -44,6 +44,12 @@ spectaql: # Default: true errorOnInterpolationReferenceNotFound: true + # Would you like to display all the servers listed in the servers area of your config? Otherwise + # it will try to display just the one marked "production: true". + # + # Default: false + displayAllServers: false + introspection: ############################################## diff --git a/examples/config.yml b/examples/config.yml index 64d0ad50..7af66f1a 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -1,6 +1,7 @@ spectaql: logoFile: ./test/fixtures/logo.png faviconFile: ./test/fixtures/favicon.png + # displayAllServers: true introspection: removeTrailingPeriodFromDescriptions: false @@ -29,6 +30,13 @@ info: description: Some important stuff we wanted you to know. Supports `markdown` servers: + - url: https://staging.example.com/graphql + description: Staging + # production: true + headers: + - name: Authorization + example: Bearer + comment: Your foo from bar - url: https://example.com/graphql description: Production production: true diff --git a/src/index.js b/src/index.js index aa251667..cfdb310b 100644 --- a/src/index.js +++ b/src/index.js @@ -61,6 +61,7 @@ const defaults = Object.freeze({ // is set, then use these: const spectaqlOptionDefaults = Object.freeze({ errorOnInterpolationReferenceNotFound: true, + displayAllServers: false, }) const introspectionOptionDefaults = Object.freeze({ diff --git a/src/spectaql/index.js b/src/spectaql/index.js index c5998472..eb898c76 100644 --- a/src/spectaql/index.js +++ b/src/spectaql/index.js @@ -24,6 +24,7 @@ function run(opts) { const server = servers.find((server) => server.production === true) || servers[0] + const headers = server?.headers ? server.headers .map((header) => { diff --git a/src/themes/default/helpers/generateApiEndpoints.js b/src/themes/default/helpers/generateApiEndpoints.js new file mode 100644 index 00000000..3ce06558 --- /dev/null +++ b/src/themes/default/helpers/generateApiEndpoints.js @@ -0,0 +1,35 @@ +module.exports = function (options) { + const { url, server, servers } = options?.data?.root || {} + + const displayAllServers = + options?.data?.root?.allOptions?.specData?.spectaql?.displayAllServers === + true + if (!displayAllServers) { + if (url) { + return url + } + if (server?.url) { + return server.url + } + const fallbackServer = + servers?.find((server) => server.url && server.production) || + servers?.find((server) => server.url) + if (fallbackServer) { + return fallbackServer.url + } + } + + if (!servers?.length) { + return '<>' + } + + return servers + .map( + (server) => + (server.description?.trim() || 'Endpoint') + + ':\n' + + (server.url?.trim() || '<>') + + '\n' + ) + .join('') +} diff --git a/src/themes/default/views/partials/layout/content/introduction/welcome.hbs b/src/themes/default/views/partials/layout/content/introduction/welcome.hbs index 09e943f3..c337522f 100644 --- a/src/themes/default/views/partials/layout/content/introduction/welcome.hbs +++ b/src/themes/default/views/partials/layout/content/introduction/welcome.hbs @@ -34,10 +34,10 @@ {{/if}} - {{#if url}} + {{#if (or url server servers)}}
API Endpoints
-
{{url}}
+
{{generateApiEndpoints}}
{{/if}} diff --git a/test/src/helpers/generateApiEndpoints.test.js b/test/src/helpers/generateApiEndpoints.test.js new file mode 100644 index 00000000..a96fbd3d --- /dev/null +++ b/test/src/helpers/generateApiEndpoints.test.js @@ -0,0 +1,86 @@ +const generateApiEndpoints = require('../../../dist/themes/default/helpers/generateApiEndpoints') + +describe('generateApiEndpoints', function () { + def('displayAllServers', false) + def('production', true) + + def('options', () => ({ + data: { + root: { + allOptions: { + specData: { + spectaql: { + displayAllServers: $.displayAllServers, + }, + }, + }, + url: $.url, + server: $.server, + servers: $.servers, + }, + }, + })) + + def('url', () => 'foorl') + def('server', () => ({ + url: 'serverFoorl', + })) + def('servers', () => [ + { + url: 'server1url', + description: 'Server One', + }, + { + url: 'server2url', + description: 'Server Two', + production: $.production, + }, + ]) + + context('displayAllServers is false', function () { + it('uses url', function () { + expect(generateApiEndpoints($.options)).to.eql('foorl') + }) + + context('url is falsy', function () { + def('url', () => undefined) + + it('uses server.url', function () { + expect(generateApiEndpoints($.options)).to.eql('serverFoorl') + }) + + context('server is falsy', function () { + def('server', () => undefined) + + it('uses production server url', function () { + expect(generateApiEndpoints($.options)).to.eql('server2url') + }) + + context('production is false', function () { + def('production', () => undefined) + + it('uses first server url', function () { + expect(generateApiEndpoints($.options)).to.eql('server1url') + }) + }) + }) + }) + }) + + context('displayAllServers is true', function () { + def('displayAllServers', () => true) + + it('works', function () { + expect(generateApiEndpoints($.options)).to.eql( + 'Server One:\nserver1url\nServer Two:\nserver2url\n' + ) + }) + + context('servers are empty', function () { + def('servers', () => []) + it('uses default message', function () { + expect(generateApiEndpoints($.options)).to.eql('<>') + }) + }) + }) +}) From 29af7ca3033113040434f4110029646dba41bf36 Mon Sep 17 00:00:00 2001 From: newhouse Date: Wed, 1 Jun 2022 11:22:33 -0400 Subject: [PATCH 2/3] changed comment --- examples/config.yml | 7 +------ src/spectaql/index.js | 2 +- src/themes/default/helpers/generateApiEndpoints.js | 1 + 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/examples/config.yml b/examples/config.yml index 7af66f1a..73b873c5 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -1,7 +1,7 @@ spectaql: logoFile: ./test/fixtures/logo.png faviconFile: ./test/fixtures/favicon.png - # displayAllServers: true + displayAllServers: true introspection: removeTrailingPeriodFromDescriptions: false @@ -32,11 +32,6 @@ info: servers: - url: https://staging.example.com/graphql description: Staging - # production: true - headers: - - name: Authorization - example: Bearer - comment: Your foo from bar - url: https://example.com/graphql description: Production production: true diff --git a/src/spectaql/index.js b/src/spectaql/index.js index eb898c76..6ea68c12 100644 --- a/src/spectaql/index.js +++ b/src/spectaql/index.js @@ -30,7 +30,7 @@ function run(opts) { .map((header) => { const { name, example, comment } = header if (name && example) { - return [comment ? `// ${comment}` : '', `${name}: ${example}`] + return [comment ? `# ${comment}` : '', `${name}: ${example}`] .filter(Boolean) .join('\n') } diff --git a/src/themes/default/helpers/generateApiEndpoints.js b/src/themes/default/helpers/generateApiEndpoints.js index 3ce06558..fc177f95 100644 --- a/src/themes/default/helpers/generateApiEndpoints.js +++ b/src/themes/default/helpers/generateApiEndpoints.js @@ -26,6 +26,7 @@ module.exports = function (options) { return servers .map( (server) => + '# ' + (server.description?.trim() || 'Endpoint') + ':\n' + (server.url?.trim() || '<>') + From 32aa3d6d046e2a585d231f74c2f02e20c7e69501 Mon Sep 17 00:00:00 2001 From: newhouse Date: Wed, 1 Jun 2022 11:24:05 -0400 Subject: [PATCH 3/3] fixed tests --- test/src/helpers/generateApiEndpoints.test.js | 2 +- test/src/spectaql/index.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/helpers/generateApiEndpoints.test.js b/test/src/helpers/generateApiEndpoints.test.js index a96fbd3d..3892d6cd 100644 --- a/test/src/helpers/generateApiEndpoints.test.js +++ b/test/src/helpers/generateApiEndpoints.test.js @@ -72,7 +72,7 @@ describe('generateApiEndpoints', function () { it('works', function () { expect(generateApiEndpoints($.options)).to.eql( - 'Server One:\nserver1url\nServer Two:\nserver2url\n' + '# Server One:\nserver1url\n# Server Two:\nserver2url\n' ) }) diff --git a/test/src/spectaql/index.test.js b/test/src/spectaql/index.test.js index 1dd8f64f..30e22bbb 100644 --- a/test/src/spectaql/index.test.js +++ b/test/src/spectaql/index.test.js @@ -80,7 +80,7 @@ describe('index', function () { const result = spectaql($.opts) expect(result).to.be.ok expect(result.headers).to.eql( - `// Get your Foo from Bar\nFoo: Bar ` + `# Get your Foo from Bar\nFoo: Bar ` ) }) })