Skip to content

Commit faf593b

Browse files
authored
fix: number parsing regression (#5906)
1 parent e3d6a49 commit faf593b

3 files changed

Lines changed: 58 additions & 2 deletions

File tree

packages/api-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ export { XHRUploadClient } from './platform/xhr-upload-client'
3131
export { clearNodeAuthState, nodeAuthState, setNodeAuthState } from './state/node-auth'
3232
export * from './types'
3333
export { withJWTRetry } from './utils/jwt-retry'
34+
export type { Override, RawDecimal } from './utils/types'

packages/api-client/src/modules/labrinth/payout/v3.ts

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
11
import { AbstractModule } from '../../../core/abstract-module'
2+
import type { Override, RawDecimal } from '../../../utils/types'
23
import type { Labrinth } from '../types'
34

5+
type RawPayoutBalance = Override<
6+
Labrinth.Payout.v3.PayoutBalance,
7+
{
8+
available: RawDecimal
9+
withdrawn_lifetime: RawDecimal
10+
withdrawn_ytd: RawDecimal
11+
pending: RawDecimal
12+
dates: Record<string, RawDecimal>
13+
}
14+
>
15+
16+
type RawTransactionItem =
17+
| Override<
18+
Extract<Labrinth.Payout.v3.TransactionItem, { type: 'withdrawal' }>,
19+
{
20+
amount: RawDecimal
21+
fee: RawDecimal | null
22+
}
23+
>
24+
| Override<
25+
Extract<Labrinth.Payout.v3.TransactionItem, { type: 'payout_available' }>,
26+
{
27+
amount: RawDecimal
28+
}
29+
>
30+
431
export class LabrinthPayoutV3Module extends AbstractModule {
532
public getModuleID(): string {
633
return 'labrinth_payout_v3'
@@ -12,11 +39,22 @@ export class LabrinthPayoutV3Module extends AbstractModule {
1239
* @returns Promise resolving to the user's payout balance
1340
*/
1441
public async getBalance(): Promise<Labrinth.Payout.v3.PayoutBalance> {
15-
return this.client.request<Labrinth.Payout.v3.PayoutBalance>('/payout/balance', {
42+
const balance = await this.client.request<RawPayoutBalance>('/payout/balance', {
1643
api: 'labrinth',
1744
version: 3,
1845
method: 'GET',
1946
})
47+
48+
return {
49+
...balance,
50+
available: Number(balance.available),
51+
withdrawn_lifetime: Number(balance.withdrawn_lifetime),
52+
withdrawn_ytd: Number(balance.withdrawn_ytd),
53+
pending: Number(balance.pending),
54+
dates: Object.fromEntries(
55+
Object.entries(balance.dates).map(([date, amount]) => [date, Number(amount)]),
56+
),
57+
}
2058
}
2159

2260
/**
@@ -25,11 +63,26 @@ export class LabrinthPayoutV3Module extends AbstractModule {
2563
* @returns Promise resolving to an array of transaction items
2664
*/
2765
public async getHistory(): Promise<Labrinth.Payout.v3.TransactionItem[]> {
28-
return this.client.request<Labrinth.Payout.v3.TransactionItem[]>('/payout/history', {
66+
const history = await this.client.request<RawTransactionItem[]>('/payout/history', {
2967
api: 'labrinth',
3068
version: 3,
3169
method: 'GET',
3270
})
71+
72+
return history.map((transaction) => {
73+
if (transaction.type === 'withdrawal') {
74+
return {
75+
...transaction,
76+
amount: Number(transaction.amount),
77+
fee: transaction.fee === null ? null : Number(transaction.fee),
78+
}
79+
}
80+
81+
return {
82+
...transaction,
83+
amount: Number(transaction.amount),
84+
}
85+
})
3386
}
3487

3588
/**
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export type Override<T, R> = Omit<T, keyof R> & R
2+
export type RawDecimal = string | number

0 commit comments

Comments
 (0)