stuff, with a side of things

This commit is contained in:
Elijah Lucian
2021-07-15 13:51:57 -06:00
parent c62f88953e
commit 8e7fc05cd1
20 changed files with 21741 additions and 230 deletions

View File

@@ -1,49 +0,0 @@
import { DataBuddy } from '@dank-inc/data-buddy'
import { Account, Stack, Transaction, User } from '../../types'
export const users = new DataBuddy<User>([
{
id: 'mock-user',
name: 'TestUser42',
email: 'testuser@email.com',
},
])
export const accounts = new DataBuddy<Account>([
{
id: 'home',
name: 'Home Expenses',
details: 'ya',
users: ['42'],
income: 1000,
expenses: 500,
},
])
export const stacks = new DataBuddy<Stack>([
{
id: 'ccrap',
name: 'crap',
account: 'asdf',
amount: 200,
details: 'for all my crap!',
transactions: [],
},
{
id: 'shit',
name: 'shit',
account: 'home',
amount: 500,
details: 'for all my shit!',
transactions: [],
},
{
id: 'poo',
name: 'poo',
account: 'home',
amount: 800,
details: 'for all my poo!',
transactions: [],
},
])
export const transactions = new DataBuddy<Transaction>([])

View File

@@ -1,8 +1,7 @@
import Axios, { AxiosInstance } from 'axios'
import { Account, Password, Stack, Transaction, User, uuid } from '../types'
import { Account, Password, Stack, Transaction, User } 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
@@ -21,24 +20,10 @@ export interface Api {
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,
@@ -49,18 +34,15 @@ export class Api {
}
logout = () => {
if (this.mock) return true
wipeJWT()
}
getUser = async (id: uuid) => {
if (this.mock) return this.users.getOne(id)
getUser = async (id: string) => {
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)
updateUser = async (id: string, body: Partial<User>) => {
const { data } = await this.axios.patch<User>(`users/${id}`, body)
return data
}
@@ -71,17 +53,15 @@ export class Api {
}
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)
getAccount = async (id: string) => {
const data = await this.axios.get<Account>(`accounts/${id}`)
return data
}
updateAccount = async (id: uuid, body: Partial<Account>) => {
updateAccount = async (id: string, body: Partial<Account>) => {
const { data } = await this.axios.patch<Account>(`accounts/${id}`, body)
return data
}
@@ -92,12 +72,11 @@ export class Api {
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>) => {
updateStack = async (id: string, body: Partial<Stack>) => {
const { data } = await this.axios.patch<Stack>(`stacks/${id}`, body)
return data
}
@@ -106,12 +85,11 @@ export class Api {
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>) => {
updateTransaction = async (id: string, body: Partial<Transaction>) => {
const { data } = await this.axios.patch<Transaction>(
`transactions/${id}`,
body,