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