diff --git a/packages/w3up-client/src/account.js b/packages/w3up-client/src/account.js index 9cd4ae857..d4329211c 100644 --- a/packages/w3up-client/src/account.js +++ b/packages/w3up-client/src/account.js @@ -1,5 +1,6 @@ import * as API from './types.js' import * as Access from './capability/access.js' +import * as Plan from './capability/plan.js' import { Delegation, importAuthorization } from '@web3-storage/access/agent' import { add as provision, AccountDID } from '@web3-storage/access/provider' import { fromEmail, toEmail } from '@web3-storage/did-mailto' @@ -98,17 +99,20 @@ export const login = async ({ agent }, email) => { } } +/** + * @typedef {object} Model + * @property {API.DidMailto} id + * @property {API.Agent} agent + * @property {API.Delegation[]} proofs + */ + export class Account { /** - * @typedef {object} AccountModel - * @property {API.DidMailto} id - * @property {API.Agent} agent - * @property {API.Delegation[]} proofs - * - * @param {AccountModel} model + * @param {Model} model */ constructor(model) { this.model = model + this.plan = new AccountPlan(model) } get agent() { return this.model.agent @@ -158,3 +162,22 @@ export class Account { return await importAuthorization(agent, this) } } + +export class AccountPlan { + /** + * @param {Model} model + */ + constructor(model) { + this.model = model + } + + /** + * Gets information about the plan associated with this account. + */ + async get() { + return await Plan.get(this.model, { + account: this.model.id, + proofs: this.model.proofs, + }) + } +} diff --git a/packages/w3up-client/src/capability/plan.js b/packages/w3up-client/src/capability/plan.js new file mode 100644 index 000000000..aa729ee05 --- /dev/null +++ b/packages/w3up-client/src/capability/plan.js @@ -0,0 +1,18 @@ +import * as API from '../types.js' +import * as Plan from '@web3-storage/capabilities/plan' + +/** + * Gets the plan currently associated with the account. + * + * @param {{agent: API.Agent}} client + * @param {object} options + * @param {API.AccountDID} options.account + * @param {API.Delegation[]} [options.proofs] + */ +export const get = async ({ agent }, { account, proofs = [] }) => { + const receipt = await agent.invokeAndExecute(Plan.get, { + with: account, + proofs, + }) + return receipt.out +} diff --git a/packages/w3up-client/test/account.test.js b/packages/w3up-client/test/account.test.js index 8170dad20..9e1c706f8 100644 --- a/packages/w3up-client/test/account.test.js +++ b/packages/w3up-client/test/account.test.js @@ -180,6 +180,26 @@ export const testAccount = { assert.deepEqual(client.spaces().length, 1, 'spaces had been added') }, + + 'check account plan': async ( + assert, + { client, mail, grantAccess, plansStorage } + ) => { + const login = Account.login(client, 'alice@web.mail') + await grantAccess(await mail.take()) + const account = Result.try(await login) + + const { error } = await account.plan.get() + assert.ok(error) + + Result.unwrap( + await plansStorage.set(account.did(), 'did:web:free.web3.storage') + ) + + const { ok: plan } = await account.plan.get() + + assert.ok(plan?.product, 'did:web:free.web3.storage') + }, } Test.test({ Account: testAccount })