diff --git a/packages/api-client/src/index.ts b/packages/api-client/src/index.ts index fdb802506..2e1e1a3ec 100644 --- a/packages/api-client/src/index.ts +++ b/packages/api-client/src/index.ts @@ -31,3 +31,4 @@ export { XHRUploadClient } from './platform/xhr-upload-client' export { clearNodeAuthState, nodeAuthState, setNodeAuthState } from './state/node-auth' export * from './types' export { withJWTRetry } from './utils/jwt-retry' +export type { Override, RawDecimal } from './utils/types' diff --git a/packages/api-client/src/modules/labrinth/payout/v3.ts b/packages/api-client/src/modules/labrinth/payout/v3.ts index 65847a979..95275794b 100644 --- a/packages/api-client/src/modules/labrinth/payout/v3.ts +++ b/packages/api-client/src/modules/labrinth/payout/v3.ts @@ -1,6 +1,33 @@ import { AbstractModule } from '../../../core/abstract-module' +import type { Override, RawDecimal } from '../../../utils/types' import type { Labrinth } from '../types' +type RawPayoutBalance = Override< + Labrinth.Payout.v3.PayoutBalance, + { + available: RawDecimal + withdrawn_lifetime: RawDecimal + withdrawn_ytd: RawDecimal + pending: RawDecimal + dates: Record + } +> + +type RawTransactionItem = + | Override< + Extract, + { + amount: RawDecimal + fee: RawDecimal | null + } + > + | Override< + Extract, + { + amount: RawDecimal + } + > + export class LabrinthPayoutV3Module extends AbstractModule { public getModuleID(): string { return 'labrinth_payout_v3' @@ -12,11 +39,22 @@ export class LabrinthPayoutV3Module extends AbstractModule { * @returns Promise resolving to the user's payout balance */ public async getBalance(): Promise { - return this.client.request('/payout/balance', { + const balance = await this.client.request('/payout/balance', { api: 'labrinth', version: 3, method: 'GET', }) + + return { + ...balance, + available: Number(balance.available), + withdrawn_lifetime: Number(balance.withdrawn_lifetime), + withdrawn_ytd: Number(balance.withdrawn_ytd), + pending: Number(balance.pending), + dates: Object.fromEntries( + Object.entries(balance.dates).map(([date, amount]) => [date, Number(amount)]), + ), + } } /** @@ -25,11 +63,26 @@ export class LabrinthPayoutV3Module extends AbstractModule { * @returns Promise resolving to an array of transaction items */ public async getHistory(): Promise { - return this.client.request('/payout/history', { + const history = await this.client.request('/payout/history', { api: 'labrinth', version: 3, method: 'GET', }) + + return history.map((transaction) => { + if (transaction.type === 'withdrawal') { + return { + ...transaction, + amount: Number(transaction.amount), + fee: transaction.fee === null ? null : Number(transaction.fee), + } + } + + return { + ...transaction, + amount: Number(transaction.amount), + } + }) } /** diff --git a/packages/api-client/src/utils/types.ts b/packages/api-client/src/utils/types.ts new file mode 100644 index 000000000..54ee5c3a0 --- /dev/null +++ b/packages/api-client/src/utils/types.ts @@ -0,0 +1,2 @@ +export type Override = Omit & R +export type RawDecimal = string | number