Skip to content

Commit

Permalink
feat: add additonal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
santese committed Jan 26, 2023
1 parent 12121ec commit c8fa3f5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const duo = new Duo({

const accounts = await duo.accounts.getAll()

const account = await duo.accounts.create('New Account')
const account = await duo.accounts.create({
name: 'Account Name'
})
```

## Test
Expand Down
22 changes: 21 additions & 1 deletion src/lib/accounts/accounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,32 @@ describe('Accounts', () => {
expect(mockAxios.post).toHaveBeenCalledWith('/accounts/v1/account/list')
})

it('gets an account by id', async () => {
const data = {
response: [account],
}
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data })
const res = await accounts.getById('1')
expect(res).toEqual(account)
expect(mockAxios.post).toHaveBeenCalledWith('/accounts/v1/account/list')
})

it('gets an account by name', async () => {
const data = {
response: [account],
}
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data })
const res = await accounts.getByName('Account')
expect(res).toEqual(account)
expect(mockAxios.post).toHaveBeenCalledWith('/accounts/v1/account/list')
})

it('creates an account', async () => {
const data = {
response: account,
}
jest.spyOn(mockAxios, 'post').mockResolvedValue({ data })
const res = await accounts.create('Account')
const res = await accounts.create({ name: 'Account' })
expect(res).toEqual(account)
expect(mockAxios.post).toHaveBeenCalledWith(
'/accounts/v1/account/create',
Expand Down
17 changes: 13 additions & 4 deletions src/lib/accounts/accounts.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import { AxiosInstance } from 'axios'
import { Account } from './accounts.types'
import { Account, CreateAccount } from './accounts.types'

export class Accounts {
constructor(private readonly httpAgent: AxiosInstance) {}

async getAll(): Promise<Account[]> {
const { data: res } = await this.httpAgent.post('/accounts/v1/account/list')

return res.response
}

async create(name: string): Promise<Account> {
async getById(accountId: string) {
const accounts = await this.getAll()
return accounts.find((account) => account.account_id === accountId)
}

async getByName(name: string) {
const accounts = await this.getAll()
return accounts.find((account) => account.name === name)
}

async create(account: CreateAccount): Promise<Account> {
const { data: res } = await this.httpAgent.post(
'/accounts/v1/account/create',
{},
{
params: {
name,
...account,
},
},
)
Expand Down
4 changes: 4 additions & 0 deletions src/lib/accounts/accounts.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export interface Account {
api_hostname: string
name: string
}

export interface CreateAccount {
name: string
}

0 comments on commit c8fa3f5

Please sign in to comment.