mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-10 07:55:21 +02:00
typesafe API request client
This commit is contained in:
parent
e7b93123a0
commit
c9c8970fcd
1 changed files with 85 additions and 9 deletions
|
@ -1,10 +1,5 @@
|
||||||
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
|
import type { AxiosInstance, AxiosRequestConfig } from 'axios'
|
||||||
import type {
|
import type {
|
||||||
Account,
|
|
||||||
AccountBalance,
|
|
||||||
AccountDetails,
|
|
||||||
Identity,
|
|
||||||
Transaction,
|
|
||||||
GetAccountResponse,
|
GetAccountResponse,
|
||||||
GetAccountsResponse,
|
GetAccountsResponse,
|
||||||
GetAccountBalancesResponse,
|
GetAccountBalancesResponse,
|
||||||
|
@ -13,21 +8,102 @@ import type {
|
||||||
GetTransactionsResponse,
|
GetTransactionsResponse,
|
||||||
DeleteAccountResponse,
|
DeleteAccountResponse,
|
||||||
GetAccountDetailsResponse,
|
GetAccountDetailsResponse,
|
||||||
WebhookData,
|
|
||||||
} from './types'
|
} from './types'
|
||||||
import { DateTime } from 'luxon'
|
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import * as fs from 'fs'
|
import * as fs from 'fs'
|
||||||
import * as https from 'https'
|
import * as https from 'https'
|
||||||
|
|
||||||
const is2xx = (status: number): boolean => status >= 200 && status < 300
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic typed mapping for Teller API
|
* Basic typed mapping for Teller API
|
||||||
*/
|
*/
|
||||||
export class TellerApi {
|
export class TellerApi {
|
||||||
private api: AxiosInstance | null = null
|
private api: AxiosInstance | null = null
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List accounts a user granted access to in Teller Connect
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/accounts
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getAccounts(): Promise<GetAccountsResponse> {
|
||||||
|
return this.get<GetAccountsResponse>(`/accounts`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a single account by id
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/accounts
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getAccount(accountId: string): Promise<GetAccountResponse> {
|
||||||
|
return this.get<GetAccountResponse>(`/accounts/${accountId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete the application's access to an account. Does not delete the account itself.
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/accounts
|
||||||
|
*/
|
||||||
|
|
||||||
|
async deleteAccount(accountId: string): Promise<DeleteAccountResponse> {
|
||||||
|
return this.delete<DeleteAccountResponse>(`/accounts/${accountId}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get account details for a single account
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/account/details
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getAccountDetails(accountId: string): Promise<GetAccountDetailsResponse> {
|
||||||
|
return this.get<GetAccountDetailsResponse>(`/accounts/${accountId}/details`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get account balances for a single account
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/account/balances
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getAccountBalances(accountId: string): Promise<GetAccountBalancesResponse> {
|
||||||
|
return this.get<GetAccountBalancesResponse>(`/accounts/${accountId}/balances`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get transactions for a single account
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/transactions
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getTransactions(accountId: string): Promise<GetTransactionsResponse> {
|
||||||
|
return this.get<GetTransactionsResponse>(`/accounts/${accountId}/transactions`)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a single transaction by id
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/transactions
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getTransaction(
|
||||||
|
accountId: string,
|
||||||
|
transactionId: string
|
||||||
|
): Promise<GetTransactionResponse> {
|
||||||
|
return this.get<GetTransactionResponse>(
|
||||||
|
`/accounts/${accountId}/transactions/${transactionId}`
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get identity for a single account
|
||||||
|
*
|
||||||
|
* https://teller.io/docs/api/identity
|
||||||
|
*/
|
||||||
|
|
||||||
|
async getIdentity(): Promise<GetIdentityResponse> {
|
||||||
|
return this.get<GetIdentityResponse>(`/identity`)
|
||||||
|
}
|
||||||
|
|
||||||
private async getApi(): Promise<AxiosInstance> {
|
private async getApi(): Promise<AxiosInstance> {
|
||||||
const cert = fs.readFileSync('../../../certs/teller-certificate.pem', 'utf8')
|
const cert = fs.readFileSync('../../../certs/teller-certificate.pem', 'utf8')
|
||||||
const key = fs.readFileSync('../../../certs/teller-private-key.pem', 'utf8')
|
const key = fs.readFileSync('../../../certs/teller-private-key.pem', 'utf8')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue