From c1064d575e9a10985a1e8a953ea8ef92cdb43f19 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Thu, 24 Jan 2019 16:45:18 -0800 Subject: [PATCH] Add support for keys array in server variables --- .../api-explorer/__tests__/lib/Oas.test.js | 5 ++++- .../lib/__snapshots__/Oas.test.js.snap | 2 +- .../__tests__/lib/get-user-variable.test.js | 22 +++++++++++++++++++ packages/api-explorer/src/lib/Oas.js | 5 +++-- .../api-explorer/src/lib/get-user-variable.js | 14 ++++++++++++ 5 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 packages/api-explorer/__tests__/lib/get-user-variable.test.js create mode 100644 packages/api-explorer/src/lib/get-user-variable.js diff --git a/packages/api-explorer/__tests__/lib/Oas.test.js b/packages/api-explorer/__tests__/lib/Oas.test.js index 2427b9abb..7f50058f6 100644 --- a/packages/api-explorer/__tests__/lib/Oas.test.js +++ b/packages/api-explorer/__tests__/lib/Oas.test.js @@ -64,7 +64,7 @@ describe('server variables', () => { ).toBe('https://domh.example.com'); }); - it.skip('should fetch user variables from keys array', () => { + it('should fetch user variables from keys array', () => { expect( new Oas( { @@ -75,7 +75,9 @@ describe('server variables', () => { { keys: [{ name: 1, username: 'domh' }] }, ).url(), ).toBe('https://domh.example.com'); + }); + it.skip('should fetch user variables from selected app', () => { expect( new Oas( { @@ -89,6 +91,7 @@ describe('server variables', () => { ).toBe('https://readme.example.com'); }); + // Test encodeURI it('should pass through if no default set', () => { expect(new Oas({ servers: [{ url: 'https://example.com/{path}' }] }).url()).toBe( diff --git a/packages/api-explorer/__tests__/lib/__snapshots__/Oas.test.js.snap b/packages/api-explorer/__tests__/lib/__snapshots__/Oas.test.js.snap index 54b289481..fd370d3b2 100644 --- a/packages/api-explorer/__tests__/lib/__snapshots__/Oas.test.js.snap +++ b/packages/api-explorer/__tests__/lib/__snapshots__/Oas.test.js.snap @@ -4,7 +4,7 @@ exports[`operation() should return a default when no operation 1`] = ` Operation { "method": "get", "oas": Oas { - "user": undefined, + "user": Object {}, }, "parameters": Array [], "path": "/unknown", diff --git a/packages/api-explorer/__tests__/lib/get-user-variable.test.js b/packages/api-explorer/__tests__/lib/get-user-variable.test.js new file mode 100644 index 000000000..6f0efdfcb --- /dev/null +++ b/packages/api-explorer/__tests__/lib/get-user-variable.test.js @@ -0,0 +1,22 @@ +const getUserVariable = require('../../src/lib/get-user-variable'); + +const topLevelUser = { apiKey: '123456', user: 'user', pass: 'pass' }; +const keysUser = { keys: [{ apiKey: '123456', name: 'app-1' }, { apiKey: '7890', name: 'app-2' }] }; + +it('should return top level property', () => { + expect(getUserVariable(topLevelUser, 'apiKey')).toBe('123456'); +}); + +it('should return first item from keys array if no app selected', () => { + expect(getUserVariable(keysUser, 'apiKey')).toBe('123456'); +}); + +it('should return selected app from keys array if app provided', () => { + expect(getUserVariable(keysUser, 'apiKey', 'app-2')).toBe('7890'); +}); + +it('should return null for anything else', () => { + expect(getUserVariable(topLevelUser, { type: 'unknown' })).toBe(null); + expect(getUserVariable(keysUser, { type: 'unknown' })).toBe(null); + expect(getUserVariable(keysUser, { type: 'unknown' }, 'app-2')).toBe(null); +}); diff --git a/packages/api-explorer/src/lib/Oas.js b/packages/api-explorer/src/lib/Oas.js index a014c7f8d..786bc1bd0 100644 --- a/packages/api-explorer/src/lib/Oas.js +++ b/packages/api-explorer/src/lib/Oas.js @@ -1,4 +1,5 @@ const getPathOperation = require('./get-path-operation'); +const getUserVariable = require('./get-user-variable'); class Operation { constructor(oas, path, method, operation) { @@ -66,7 +67,7 @@ class Operation { class Oas { constructor(oas, user) { Object.assign(this, oas); - this.user = user; + this.user = user || {}; } url() { @@ -105,7 +106,7 @@ class Oas { } return url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, (original, key) => { - if (this.user && this.user[key]) return this.user[key]; + if (getUserVariable(this.user, key)) return getUserVariable(this.user, key); return variables[key] ? variables[key].default : original; }); } diff --git a/packages/api-explorer/src/lib/get-user-variable.js b/packages/api-explorer/src/lib/get-user-variable.js new file mode 100644 index 000000000..fc8b458e0 --- /dev/null +++ b/packages/api-explorer/src/lib/get-user-variable.js @@ -0,0 +1,14 @@ +function getKey(user, property) { + return user[property] || null; +} + +function getUserVariable(user, property, selectedApp = false) { + if (user.keys) { + if (selectedApp) return getKey(user.keys.find(key => key.name === selectedApp), property); + return getKey(user.keys[0], property); + } + + return getKey(user, property); +} + +module.exports = getUserVariable;