Skip to content

Commit

Permalink
feat: account.plan.get method (#1117)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gozala authored Nov 14, 2023
1 parent 88d7864 commit 1385a9c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
35 changes: 29 additions & 6 deletions packages/w3up-client/src/account.js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
}
}
18 changes: 18 additions & 0 deletions packages/w3up-client/src/capability/plan.js
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions packages/w3up-client/test/account.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, '[email protected]')
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 })

0 comments on commit 1385a9c

Please sign in to comment.