Skip to content

Commit

Permalink
chore: refactor for accounts-api response (#10)
Browse files Browse the repository at this point in the history
* chore: undo documents response

* nit: remove 'dev' for tests
  • Loading branch information
sayantank authored Sep 22, 2023
1 parent 44bd362 commit ddf528a
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/accounts/filter-by-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsFilterBody, AccountsRequestResultWithMetadata } from '../types'
import { AccountsFilterBody, AccountsRequestResult } from '../types'
import { Cluster, requestHeaders, tryExtractResultFromResponse } from '../utils'

/** Configures the accounts filterByType request. */
Expand Down Expand Up @@ -36,5 +36,5 @@ export async function accountsFilterByType<T = any>(
method: 'POST',
}
)
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(res)
return tryExtractResultFromResponse<AccountsRequestResult<T[]>>(res)
}
4 changes: 2 additions & 2 deletions src/accounts/filter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsFilterBody, AccountsRequestResultWithMetadata } from '../types'
import { AccountsFilterBody, AccountsRequestResult } from '../types'
import { Cluster, requestHeaders, tryExtractResultFromResponse } from '../utils'

/** Configures the accounts filter request. */
Expand Down Expand Up @@ -33,5 +33,5 @@ export async function accountsFilter<T = any>(
method: 'POST',
}
)
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(res)
return tryExtractResultFromResponse<AccountsRequestResult<T[]>>(res)
}
4 changes: 2 additions & 2 deletions src/accounts/find-by-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsRequestResultWithMetadata } from '../types'
import { AccountsRequestResult } from '../types'
import { Cluster, requestHeaders, tryExtractResultFromResponse } from '../utils'

/** Configures the accounts findByType request. */
Expand Down Expand Up @@ -32,5 +32,5 @@ export async function accountsFindByType<T = any>(
method: 'GET',
}
)
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(res)
return tryExtractResultFromResponse<AccountsRequestResult<T[]>>(res)
}
4 changes: 2 additions & 2 deletions src/accounts/memcmp.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsRequestResultWithMetadata, AccountsMemcmpBody } from '../types'
import { AccountsMemcmpBody, AccountsRequestResult } from '../types'
import { Cluster, requestHeaders, tryExtractResultFromResponse } from '../utils'

/** Configures the accounts memcmp request. */
Expand Down Expand Up @@ -33,5 +33,5 @@ export async function accountsMemcmp<T = any>(
method: 'POST',
}
)
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(res)
return tryExtractResultFromResponse<AccountsRequestResult<T[]>>(res)
}
4 changes: 2 additions & 2 deletions src/accounts/mongodb/find.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsRequestResultWithMetadata, MongoDbFindBody } from '../../types'
import { AccountsRequestResult, MongoDbFindBody } from '../../types'
import {
Cluster,
requestHeaders,
Expand Down Expand Up @@ -33,5 +33,5 @@ export async function mongoDbFind<T = any>(
method: 'POST',
}
)
return tryExtractResultFromResponse<AccountsRequestResultWithMetadata<T>>(res)
return tryExtractResultFromResponse<AccountsRequestResult<T[]>>(res)
}
17 changes: 3 additions & 14 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
* The success case of @link AccountsRequestResult.
*/
export type SuccessAccountsRequestResult<T> = {
error: null | undefined
data: T
data: T | null
error: undefined
}

/**
* The failure case of @link AccountsRequestResult.
*/
export type FailureAccountsRequestResult = {
error: string
data: null | undefined
data: undefined
}

/**
Expand Down Expand Up @@ -44,17 +44,6 @@ export function isSuccessResult<T>(
return result.error == null && result.data != null
}

export type AccountsRequestResultWithMetadata<T> = AccountsRequestResult<
T[]
> & {
metadata: {
count: number
offset: number
limit: number
hasMore: boolean
}
}

// -----------------
// Body Types
// -----------------
Expand Down
27 changes: 6 additions & 21 deletions test/accounts-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ test('accounts-sdk: aggregate', async () => {

assert.equal(status, 200)
assert(result.error == null)
result.data
assert.equal(result.data?.length, 5)
})

Expand All @@ -63,10 +62,7 @@ test('accounts-sdk: filter by type', async (t) => {
offset: 0,
})
assert.equal(status, 200)
spok(t, result, {
metadata: { count: 2, offset: 0, limit: 2, hasMore: true },
error: null,
})
assert(result.error == null)
assert.equal(result.data?.length, 2)
})

Expand All @@ -82,11 +78,9 @@ test('accounts-sdk: filter', async (t) => {
limit: 3,
offset: 0,
})

assert.equal(status, 200)
spok(t, result, {
metadata: { count: 3, offset: 0, limit: 3, hasMore: true },
error: null,
})
assert(result.error == null)
assert.equal(result.data?.length, 3)
})

Expand All @@ -103,10 +97,7 @@ test('accounts-sdk: find by type', async (t) => {
offset: 0,
})
assert.equal(status, 200)
spok(t, result, {
metadata: { count: 2, offset: 0, limit: 2, hasMore: true },
error: null,
})
assert(result.error == null)
assert(
!(result.data as any[]).some((x: any) => x.account_type !== 'CandyMachine')
)
Expand Down Expand Up @@ -165,10 +156,7 @@ test('accounts-sdk: memcmp', async (t) => {
offset: 0,
})
assert.equal(status, 200)
spok(t, result, {
metadata: { count: 3, offset: 0, limit: 3, hasMore: true },
error: null,
})
assert(result.error == null)
assert.equal(result.data?.length, 3)
})

Expand Down Expand Up @@ -233,10 +221,7 @@ test('accounts-sdk: mongodb', async (t) => {

assert.equal(status, 200)

spok(t, result, {
metadata: { count: 5, offset: 10, limit: 5, hasMore: true },
error: null,
})
assert(result.error == null)

const data = result.data
assert(data != null)
Expand Down

0 comments on commit ddf528a

Please sign in to comment.