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: account.plan.get method #1117

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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 })
Loading