This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for keys array in server variables
- Loading branch information
1 parent
e1fe1d6
commit c1064d5
Showing
5 changed files
with
44 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
packages/api-explorer/__tests__/lib/get-user-variable.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |