Skip to content

Commit

Permalink
feat: getByIdOrSearch
Browse files Browse the repository at this point in the history
Allows to search a cipher by its id.
If the id is null or the cipher doesn't exist, then search an
existing cipher by other criterias, and get the first one
found based on a specific order
  • Loading branch information
edas committed Sep 3, 2019
1 parent 395dc1c commit 2f61c7b
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/WebVaultClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import MicroEE from 'microee'

import eq from 'lodash/eq'
import get from 'lodash/get'
import orderBy from 'lodash/orderBy'

import { Utils } from './@bitwarden/jslib/misc/utils'

Expand Down Expand Up @@ -392,6 +394,32 @@ class WebVaultClient {
})
}

/**
* Search a cipher, first by id
* otherwise by a search forwarded to `getAllDecryptedFor`
* and get the first one in the order asked
* @param {integer} id - uuid of a cipher
* @param {object} search - as described in `getAllDecryptedFor`
* @param {Array|function|string} sort - given to lodash.sortBy
* @return {Cipher} encrypted cipher
*/
async getByIdOrSearch(id, search, sort) {
if (id) {
const cipher = await this.get(id)
if (cipher) return cipher
}
if (search) {
const all = await this.getAllDecryptedFor(search)
const first = orderBy(all, sort)[0]
const id = get(first, 'id')
if (id) {
const searchedCipher = await this.get(id)
if (searchedCipher) return searchedCipher
}
}
return null
}

/**
* @private
* test if two parameters match
Expand Down

0 comments on commit 2f61c7b

Please sign in to comment.