You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

109 lines
2.7 KiB

import Axios, { AxiosInstance } from 'axios'
import { Account, Password, Stack, Transaction, User } from '../types'
import { JWT, setJWT, wipeJWT } from '../utils/jwt'
import { DataBuddy } from '@dank-inc/data-buddy'
export type ApiParams = {
baseURL?: string
mock?: boolean
}
export interface Api {
mock?: boolean
users: DataBuddy<User>
accounts: DataBuddy<Account>
stacks: DataBuddy<Stack>
transactions: DataBuddy<Transaction>
axios: AxiosInstance
}
export class Api {
constructor({ mock, baseURL }: ApiParams) {
this.mock = mock
this.axios = Axios.create({ baseURL })
}
login = async (name: string, password: string): Promise<JWT> => {
const { data } = await this.axios.post<JWT>(`/dj-rest-auth/login/`, {
name,
password,
})
setJWT(data)
return data
}
logout = () => {
wipeJWT()
}
getUser = async (id: string) => {
const { data } = await this.axios.get<User>(`users/${id}`)
return data
}
updateUser = async (id: string, body: Partial<User>) => {
const { data } = await this.axios.patch<User>(`users/${id}`, body)
return data
}
createUser = async (body: Omit<User, 'id'> & Password) => {
const { data } = await this.axios.post<User>(`users`, body)
return data
}
getAccounts = async () => {
const { data } = await this.axios.get<Account[]>('accounts')
return data
}
getAccount = async (id: string) => {
const data = await this.axios.get<Account>(`accounts/${id}`)
return data
}
updateAccount = async (id: string, body: Partial<Account>) => {
const { data } = await this.axios.patch<Account>(`accounts/${id}`, body)
return data
}
createAccount = async (body: Omit<Account, 'id'>) => {
const { data } = await this.axios.post<Transaction>('accounts', body)
return data
}
deleteAccount = async () => {}
getStacks = async (): Promise<Stack[]> => {
const { data } = await this.axios.get('stacks')
return data
}
updateStack = async (id: string, body: Partial<Stack>) => {
const { data } = await this.axios.patch<Stack>(`stacks/${id}`, body)
return data
}
createStack = async () => {}
deleteStack = async () => {}
getTransactions = async () => {
const { data } = await this.axios.get('transactions')
return data
}
updateTransaction = async (id: string, body: Partial<Transaction>) => {
const { data } = await this.axios.patch<Transaction>(
`transactions/${id}`,
body,
)
return data
}
createTransaction = async (body: Omit<Transaction, 'id'>) => {
const { data } = await this.axios.post<Transaction>('transactions', body)
return data
}
deleteTransaction = async () => {}
}
export const logOut = async () => {
wipeJWT()
}