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 2 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
26 changes: 26 additions & 0 deletions packages/w3up-client/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import {
Store as StoreCapabilities,
Upload as UploadCapabilities,
abilitiesAsStrings,
} from '@web3-storage/capabilities'
import { CAR } from '@ucanto/transport'
import { Base } from './base.js'
Expand Down Expand Up @@ -54,6 +55,31 @@ export class Client extends Base {
this.coupon = new CouponAPI(agentData, options)
}

/**
* 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/access').Abilities[]}
*/
static abilities(abilities) {
travis marked this conversation as resolved.
Show resolved Hide resolved
for (const ability of abilities) {
if (
!abilitiesAsStrings.includes(
travis marked this conversation as resolved.
Show resolved Hide resolved
/** @type {import('@web3-storage/access').Abilities} */ (ability)
travis marked this conversation as resolved.
Show resolved Hide resolved
)
) {
throw new Error(`${ability} is not a supported capability`)
}
}
return /** @type {import('@web3-storage/access').Abilities[]} */ (abilities)
}

did() {
return this._agent.did()
}
Expand Down
13 changes: 13 additions & 0 deletions packages/w3up-client/test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -480,4 +480,17 @@ describe('Client', () => {
assert.equal(typeof client.capability.upload.remove, 'function')
})
})

describe('abilities', () => {
it('should return the passed argument if all abilities are valid', async () => {
const abilities = ['store/add', 'upload/add']
assert.equal(Client.abilities(abilities), abilities)
})

it('should throw an error if one of the abilities is not supported', async () => {
assert.throws(() => {
travis marked this conversation as resolved.
Show resolved Hide resolved
Client.abilities(['foo/bar'])
})
})
})
})
Loading