-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a function to verify and return Abilities. (#1252)
Given a list of strings representing capability names (`ServiceAbility`s), verify that all the strings are valid `ServiceAbility`s and return `ServiceAbility[]`. `ServiceAbility[]` is still just a list of strings, but this helps us play nice with Typescript. Inspired by #1250
- Loading branch information
Showing
4 changed files
with
66 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { abilitiesAsStrings } from '@web3-storage/capabilities' | ||
|
||
const setOfAbilities = new Set(abilitiesAsStrings) | ||
|
||
/** | ||
* Verify and return Abilities. | ||
* | ||
* Given a list of strings representing capability names (Abilities), | ||
* verify that all the strings are valid Abilities and return Abilities[]. | ||
* | ||
* Abilities[] is still just a list of strings, but this helps us play | ||
* nice with Typescript. | ||
* | ||
* @param {string[]} abilities | ||
* @returns {import('@web3-storage/capabilities/types').ServiceAbility[]} | ||
*/ | ||
export function asAbilities(abilities) { | ||
for (const ability of abilities) { | ||
if ( | ||
!setOfAbilities.has( | ||
/** @type {import('@web3-storage/capabilities/types').ServiceAbility} */ ( | ||
ability | ||
) | ||
) | ||
) { | ||
throw new Error(`${ability} is not a supported capability`) | ||
} | ||
} | ||
return /** @type {import('@web3-storage/capabilities/types').ServiceAbility[]} */ ( | ||
abilities | ||
) | ||
} |
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,21 @@ | ||
import assert from 'assert' | ||
import { asAbilities } from '../src/ability.js' | ||
|
||
describe('abilities', () => { | ||
it('should return the passed argument if all abilities are valid', async () => { | ||
const abilities = ['store/add', 'upload/add'] | ||
assert.equal(asAbilities(abilities), abilities) | ||
}) | ||
|
||
it('should throw an error if one of the abilities is not supported', async () => { | ||
assert.throws( | ||
() => { | ||
asAbilities(['foo/bar']) | ||
}, | ||
{ | ||
name: 'Error', | ||
message: 'foo/bar is not a supported capability', | ||
} | ||
) | ||
}) | ||
}) |