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.

132 lines
3.4 KiB

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