Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a function to verify and return Abilities. #1252

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/capabilities/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,9 +633,9 @@ export type PlanGetFailure = PlanNotFound
// Top
export type Top = InferInvokedCapability<typeof top>

export type Abilities = TupleToUnion<AbilitiesArray>
export type W3UpAbility = TupleToUnion<W3UpAbilityArray>

export type AbilitiesArray = [
export type W3UpAbilityArray = [
Top['can'],
ProviderAdd['can'],
Space['can'],
Expand Down Expand Up @@ -677,3 +677,13 @@ export type AbilitiesArray = [
Usage['can'],
UsageReport['can']
]

/**
* @deprecated use Ability
*/
export type Abilities = W3UpAbility

/**
* @deprecated use AbilityArray
*/
export type AbilitiesArray = W3UpAbilityArray
32 changes: 32 additions & 0 deletions packages/w3up-client/src/ability.js
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').W3UpAbility[]}
*/
export function asAbilities(abilities) {
for (const ability of abilities) {
if (
!setOfAbilities.has(
/** @type {import('@web3-storage/capabilities/types').W3UpAbility} */ (
ability
)
)
) {
throw new Error(`${ability} is not a supported capability`)
}
}
return /** @type {import('@web3-storage/capabilities/types').W3UpAbility[]} */ (
abilities
)
}
1 change: 1 addition & 0 deletions packages/w3up-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { generate } from '@ucanto/principal/rsa'
import { Client } from './client.js'
export * as Result from './result.js'
export * as Account from './account.js'
export * from './ability.js'

/**
* Create a new w3up client.
Expand Down
21 changes: 21 additions & 0 deletions packages/w3up-client/test/ability.test.js
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',
}
)
})
})
Loading