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

Commit c1064d5

Browse files
committed
Add support for keys array in server variables
1 parent e1fe1d6 commit c1064d5

File tree

5 files changed

+44
-4
lines changed

5 files changed

+44
-4
lines changed

packages/api-explorer/__tests__/lib/Oas.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('server variables', () => {
6464
).toBe('https://domh.example.com');
6565
});
6666

67-
it.skip('should fetch user variables from keys array', () => {
67+
it('should fetch user variables from keys array', () => {
6868
expect(
6969
new Oas(
7070
{
@@ -75,7 +75,9 @@ describe('server variables', () => {
7575
{ keys: [{ name: 1, username: 'domh' }] },
7676
).url(),
7777
).toBe('https://domh.example.com');
78+
});
7879

80+
it.skip('should fetch user variables from selected app', () => {
7981
expect(
8082
new Oas(
8183
{
@@ -89,6 +91,7 @@ describe('server variables', () => {
8991
).toBe('https://readme.example.com');
9092
});
9193

94+
9295
// Test encodeURI
9396
it('should pass through if no default set', () => {
9497
expect(new Oas({ servers: [{ url: 'https://example.com/{path}' }] }).url()).toBe(

packages/api-explorer/__tests__/lib/__snapshots__/Oas.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`operation() should return a default when no operation 1`] = `
44
Operation {
55
"method": "get",
66
"oas": Oas {
7-
"user": undefined,
7+
"user": Object {},
88
},
99
"parameters": Array [],
1010
"path": "/unknown",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const getUserVariable = require('../../src/lib/get-user-variable');
2+
3+
const topLevelUser = { apiKey: '123456', user: 'user', pass: 'pass' };
4+
const keysUser = { keys: [{ apiKey: '123456', name: 'app-1' }, { apiKey: '7890', name: 'app-2' }] };
5+
6+
it('should return top level property', () => {
7+
expect(getUserVariable(topLevelUser, 'apiKey')).toBe('123456');
8+
});
9+
10+
it('should return first item from keys array if no app selected', () => {
11+
expect(getUserVariable(keysUser, 'apiKey')).toBe('123456');
12+
});
13+
14+
it('should return selected app from keys array if app provided', () => {
15+
expect(getUserVariable(keysUser, 'apiKey', 'app-2')).toBe('7890');
16+
});
17+
18+
it('should return null for anything else', () => {
19+
expect(getUserVariable(topLevelUser, { type: 'unknown' })).toBe(null);
20+
expect(getUserVariable(keysUser, { type: 'unknown' })).toBe(null);
21+
expect(getUserVariable(keysUser, { type: 'unknown' }, 'app-2')).toBe(null);
22+
});

packages/api-explorer/src/lib/Oas.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const getPathOperation = require('./get-path-operation');
2+
const getUserVariable = require('./get-user-variable');
23

34
class Operation {
45
constructor(oas, path, method, operation) {
@@ -66,7 +67,7 @@ class Operation {
6667
class Oas {
6768
constructor(oas, user) {
6869
Object.assign(this, oas);
69-
this.user = user;
70+
this.user = user || {};
7071
}
7172

7273
url() {
@@ -105,7 +106,7 @@ class Oas {
105106
}
106107

107108
return url.replace(/{([-_a-zA-Z0-9[\]]+)}/g, (original, key) => {
108-
if (this.user && this.user[key]) return this.user[key];
109+
if (getUserVariable(this.user, key)) return getUserVariable(this.user, key);
109110
return variables[key] ? variables[key].default : original;
110111
});
111112
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getKey(user, property) {
2+
return user[property] || null;
3+
}
4+
5+
function getUserVariable(user, property, selectedApp = false) {
6+
if (user.keys) {
7+
if (selectedApp) return getKey(user.keys.find(key => key.name === selectedApp), property);
8+
return getKey(user.keys[0], property);
9+
}
10+
11+
return getKey(user, property);
12+
}
13+
14+
module.exports = getUserVariable;

0 commit comments

Comments
 (0)