Skip to content

Commit

Permalink
Rename ErrorSuccessInterface to ApiResponse and contain it to api com…
Browse files Browse the repository at this point in the history
…posables
  • Loading branch information
ccali11 committed Jun 21, 2023
1 parent f11227e commit 01570a6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 51 deletions.
16 changes: 8 additions & 8 deletions apps/web/src/composables/users.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref } from 'vue'
import { AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccounts, Account, ExistingUserCheck, ErrorSuccessInterface } from '@casimir/types'
import { AddAccountOptions, ProviderString, RemoveAccountOptions, UserWithAccounts, ExistingUserCheck, ApiResponse } from '@casimir/types'
import useEnvironment from '@/composables/environment'
import * as Session from 'supertokens-web-js/recipe/session'

Expand All @@ -11,7 +11,7 @@ const user = ref<UserWithAccounts>()

export default function useUsers () {

async function addAccount(account: AddAccountOptions): Promise<ErrorSuccessInterface> {
async function addAccount(account: AddAccountOptions): Promise<ApiResponse> {
try {
const requestOptions = {
method: 'POST',
Expand All @@ -29,7 +29,7 @@ export default function useUsers () {
}
}

async function checkIfPrimaryUserExists(provider: ProviderString, address: string): Promise<ExistingUserCheck> {
async function checkIfPrimaryUserExists(provider: ProviderString, address: string): Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
Expand All @@ -40,13 +40,13 @@ export default function useUsers () {
const response = await fetch(`${usersBaseURL}/auth/check-if-primary-address-exists/${provider}/${address}`, requestOptions)
const { error, message, data } = await response.json()
if (error) throw new Error(message)
return data
return { error, message, data }
} catch (error) {
throw new Error(error.message || 'Error checking if primary user exists')
}
}

async function checkIfSecondaryAddress(address: string) : Promise<Account[]> {
async function checkIfSecondaryAddress(address: string) : Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
Expand All @@ -55,9 +55,9 @@ export default function useUsers () {
}
}
const response = await fetch(`${usersBaseURL}/auth/check-secondary-address/${address}`, requestOptions)
const { error, message, data: users } = await response.json()
const { error, message, data } = await response.json()
if (error) throw new Error(message)
return users
return { error, message, data }
} catch (error) {
throw new Error(error.message || 'Error checking if secondary address')
}
Expand Down Expand Up @@ -94,7 +94,7 @@ export default function useUsers () {
return message
}

async function getUser() : Promise<ErrorSuccessInterface> {
async function getUser() : Promise<ApiResponse> {
try {
const requestOptions = {
method: 'GET',
Expand Down
46 changes: 10 additions & 36 deletions apps/web/src/composables/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import useSolana from '@/composables/solana'
import useTrezor from '@/composables/trezor'
import useUsers from '@/composables/users'
import useWalletConnect from '@/composables/walletConnect'
import { Account, CryptoAddress, Currency, ExistingUserCheck, LoginCredentials, MessageRequest, ProviderString, TransactionRequest, ErrorSuccessInterface } from '@casimir/types'
import { Account, CryptoAddress, Currency, LoginCredentials, MessageRequest, ProviderString, TransactionRequest } from '@casimir/types'
import * as Session from 'supertokens-web-js/recipe/session'

// Test ethereum send from address : 0xd557a5745d4560B24D36A68b52351ffF9c86A212
Expand Down Expand Up @@ -64,7 +64,7 @@ export default function useWallet() {
* @param currency
* @returns
*/
async function connectWallet(): Promise<ErrorSuccessInterface> {
async function connectWallet(): Promise<void> {
try { // Sign Up or Login
if (!user?.value?.address) {
await login()
Expand Down Expand Up @@ -103,11 +103,6 @@ export default function useWallet() {
}
await setUserAccountBalances()
console.log('user.value after connecting wallet :>> ', user.value)
return {
error: false,
message: 'Successfully connected wallet',
data: user.value
}
} catch (error) {
loadingUserWallets.value = false
throw new Error(error.message || 'There was an error connecting the wallet')
Expand Down Expand Up @@ -258,14 +253,14 @@ export default function useWallet() {
* @param provider
* @param currency
*/
async function selectAddress(address: any, pathIndex?: string) : Promise<ErrorSuccessInterface> {
async function selectAddress(address: any, pathIndex?: string) : Promise<void> {
try {
address = trimAndLowercaseAddress(address)
setSelectedAddress(address)
setSelectedCurrency('ETH') // TODO: Implement this for other currencies when supported.

if (pathIndex) setSelectedPathIndex(pathIndex)
const { sameAddress, sameProvider } : ExistingUserCheck = await checkIfPrimaryUserExists(selectedProvider.value, selectedAddress.value)
const { data: { sameAddress, sameProvider } } = await checkIfPrimaryUserExists(selectedProvider.value, selectedAddress.value)
if (sameAddress && sameProvider ) {
await connectWallet() // login
return {
Expand All @@ -277,24 +272,12 @@ export default function useWallet() {
throw new Error('Address already exists as a primary address using another provider')
}

const accountsIfSecondaryAddress : Account[] | void = await checkIfSecondaryAddress(selectedAddress.value)
console.log('accountsIfSecondaryAddress :>> ', accountsIfSecondaryAddress)
if (accountsIfSecondaryAddress.length) {
throw new Error(`${selectedAddress.value} already exists as a secondary address on this/these account(s): ${JSON.stringify(accountsIfSecondaryAddress)}`)
} else {
await connectWallet() // sign up or add account
return {
error: false,
message: 'Address does not exist as a primary address using this provider',
}
}
const { data: accountsIfSecondaryAddress } = await checkIfSecondaryAddress(selectedAddress.value)
if (accountsIfSecondaryAddress.length) throw new Error(`${selectedAddress.value} already exists as a secondary address on this/these account(s): ${JSON.stringify(accountsIfSecondaryAddress)}`)
await connectWallet() // sign up or add account
} catch (error) {
// TODO: @shanejearley - What do we want to do here?
console.error('Error in selectAddress: ', error.message)
return {
error: true,
message: error.message
}
throw new Error(error.message || 'There was an error selecting address')
}
}

Expand All @@ -303,7 +286,7 @@ export default function useWallet() {
* @param provider
* @param currency
*/
async function selectProvider(provider: ProviderString, currency: Currency = 'ETH'): Promise<ErrorSuccessInterface> {
async function selectProvider(provider: ProviderString, currency: Currency = 'ETH'): Promise<void> {
console.clear()
try {
if (provider === 'WalletConnect') {
Expand All @@ -323,17 +306,8 @@ export default function useWallet() {
const trezorAddresses = await getTrezorAddress[currency]() as CryptoAddress[]
setUserAddresses(trezorAddresses)
}
return {
error: false,
message: 'Successfully selected provider'
}
} catch (error) {
console.error('selectProvider error: ', error)
return {
error: true,
message: error.message as string || 'Error selecting provider',
data: error.name as string || null
}
throw new Error(`Error selecting provider: ${error.message}`)
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CryptoAddress } from './interfaces/CryptoAddress'
import { Currency } from './interfaces/Currency'
import { DeploymentConfig } from './interfaces/DeploymentConfig'
import { EthersProvider } from './interfaces/EthersProvider'
import { ErrorSuccessInterface } from './interfaces/ErrorSuccessInterface'
import { ApiResponse } from './interfaces/ApiResponse'
import { Event } from './interfaces/Event'
import { ExistingUserCheck } from './interfaces/ExistingUserCheck'
import { GasEstimate } from './interfaces/GasEstimate'
Expand Down Expand Up @@ -39,7 +39,7 @@ export type {
Currency,
DeploymentConfig,
EthersProvider,
ErrorSuccessInterface,
ApiResponse,
Event,
ExistingUserCheck,
GasEstimate,
Expand Down
7 changes: 7 additions & 0 deletions common/types/src/interfaces/ApiResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ExistingUserCheck } from './ExistingUserCheck'

export interface ApiResponse {
error: boolean;
message: string;
data?: any | ExistingUserCheck; // TODO: Can expand this to include more types
}
5 changes: 0 additions & 5 deletions common/types/src/interfaces/ErrorSuccessInterface.ts

This file was deleted.

0 comments on commit 01570a6

Please sign in to comment.