Skip to content
This repository has been archived by the owner on Nov 28, 2022. It is now read-only.

Commit

Permalink
Add support for keys array in server variables
Browse files Browse the repository at this point in the history
  • Loading branch information
domharrington committed Jan 25, 2019
1 parent e1fe1d6 commit c1064d5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion packages/api-explorer/__tests__/lib/Oas.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand All @@ -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(
{
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 22 additions & 0 deletions packages/api-explorer/__tests__/lib/get-user-variable.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
5 changes: 3 additions & 2 deletions packages/api-explorer/src/lib/Oas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const getPathOperation = require('./get-path-operation');
const getUserVariable = require('./get-user-variable');

class Operation {
constructor(oas, path, method, operation) {
Expand Down Expand Up @@ -66,7 +67,7 @@ class Operation {
class Oas {
constructor(oas, user) {
Object.assign(this, oas);
this.user = user;
this.user = user || {};
}

url() {
Expand Down Expand Up @@ -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;
});
}
Expand Down
14 changes: 14 additions & 0 deletions packages/api-explorer/src/lib/get-user-variable.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit c1064d5

Please sign in to comment.