Skip to content

Commit

Permalink
feat: add api helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Mar 3, 2025
1 parent 4ba7e0b commit 1148941
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/app/hooks/use-mint-burn-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DetailedEvent } from '@models/ethereum-models';
import { Merchant } from '@models/merchant';
import { useQuery } from '@tanstack/react-query';

import { API } from '@shared/constants/api.constants';
import { API_HELPERS } from '@shared/constants/api.constants';

interface UseMintBurnEventsReturnType {
allMintBurnEvents: DetailedEvent[] | undefined;
Expand All @@ -12,7 +12,8 @@ interface UseMintBurnEventsReturnType {
export function useMintBurnEvents(): UseMintBurnEventsReturnType {
async function fetchMintBurnEvents(ethereumAddress: string): Promise<DetailedEvent[]> {
try {
const response = await fetch(`${API.MINT_BURN_EVENTS}/${ethereumAddress}`);
const apiURL = API_HELPERS.getMintBurnEventsURL({ address: ethereumAddress });
const response = await fetch(apiURL);

if (!response.ok) {
throw new Error(`Error fetching mint burn events`);
Expand Down
5 changes: 3 additions & 2 deletions src/app/hooks/use-points.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PointsData } from '@models/points.models';
import { useQuery } from '@tanstack/react-query';
import { useAccount } from 'wagmi';

import { API } from '@shared/constants/api.constants';
import { API_HELPERS } from '@shared/constants/api.constants';

interface UsePointsReturnType {
userPoints: PointsData | undefined;
Expand All @@ -13,7 +13,8 @@ export function usePoints(): UsePointsReturnType {

async function fetchUserPoints(): Promise<PointsData | undefined> {
try {
const response = await fetch(`${API.POINTS}/${address}`);
const apiURL = API_HELPERS.getPointsURL({ address: address! });
const response = await fetch(apiURL);

if (!response.ok) {
throw new Error(`Error fetching user: ${address} points`);
Expand Down
13 changes: 3 additions & 10 deletions src/app/hooks/use-proof-of-reserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useQuery } from '@tanstack/react-query';
import { unshiftValue } from 'dlc-btc-lib/utilities';
import { pluck } from 'ramda';

import { API } from '@shared/constants/api.constants';
import { API_HELPERS } from '@shared/constants/api.constants';
import { NetworkType } from '@shared/constants/network.constants';

export interface UseProofOfReserveReturnType {
Expand All @@ -24,9 +24,7 @@ interface ProofOfReserveByChainReturnType {
export function useProofOfReserve(): UseProofOfReserveReturnType {
async function fetchProofOfReserve(merchantAddress?: string): Promise<number> {
try {
const apiURL = merchantAddress
? `${API.PROOF_OF_RESERVE}?address=${merchantAddress}`
: `${API.PROOF_OF_RESERVE}`;
const apiURL = API_HELPERS.getProofOfReserveURL({ address: merchantAddress });

const response = await fetch(apiURL);

Expand All @@ -53,14 +51,9 @@ export function useProofOfReserve(): UseProofOfReserveReturnType {
return chain ? chain.toLowerCase() : '';
};

const buildApiUrl = (chainParam: string): string => {
const baseUrl = `${API.PROOF_OF_RESERVE}/${appConfiguration.appEnvironment}`;
return `${baseUrl}?chain=${chainParam}`;
};

try {
const chainParam = formatChainParam(chainName, networkType);
const apiUrl = buildApiUrl(chainParam);
const apiUrl = API_HELPERS.getProofOfReserveURL({ chain: chainParam });

const response = await fetch(apiUrl);
if (!response.ok) {
Expand Down
5 changes: 3 additions & 2 deletions src/app/hooks/use-total-supply.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query';

import { API } from '@shared/constants/api.constants';
import { API_HELPERS } from '@shared/constants/api.constants';

interface UseTotalSupplyReturnType {
totalSupply: number | undefined;
Expand All @@ -9,7 +9,8 @@ interface UseTotalSupplyReturnType {
export function useTotalSupply(): UseTotalSupplyReturnType {
const fetchTotalSupply = async () => {
try {
const response = await fetch(`${API.TOTAL_SUPPLY}`);
const apiUrl = API_HELPERS.getTotalSupplyURL({});
const response = await fetch(apiUrl);

if (!response.ok) {
throw new Error(`Response was not OK: ${response.status}`);
Expand Down
61 changes: 60 additions & 1 deletion src/shared/constants/api.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,69 @@ const API_PATHS = {

const getBaseApiUrl = (): string => BASE_API_URLS[appConfiguration.appEnvironment];

export const API = {
const API = {
BASE: getBaseApiUrl(),
POINTS: `${getBaseApiUrl()}${API_PATHS.POINTS}`,
PROOF_OF_RESERVE: `${getBaseApiUrl()}${API_PATHS.PROOF_OF_RESERVE}`,
MINT_BURN_EVENTS: `${getBaseApiUrl()}${API_PATHS.MINT_BURN_EVENTS}`,
TOTAL_SUPPLY: `${getBaseApiUrl()}${API_PATHS.TOTAL_SUPPLY}`,
};

interface ProofOfReserveOptions {
address?: string;
chain?: string;
}

interface PointsOptions {
address: string;
}

interface MintBurnEventsOptions {
address: string;
}

interface TotalSupplyOptions {
chain?: string;
}

export const API_HELPERS = {
getProofOfReserveURL(options?: ProofOfReserveOptions): string {
const url = new URL(`${API.PROOF_OF_RESERVE}`);

if (!options) return url.toString();

const { address, chain } = options;

if (address) url.searchParams.set('address', address);
if (chain) url.searchParams.set('chain', chain);

return url.toString();
},
getPointsURL(options?: PointsOptions): string {
const url = new URL(`${API.POINTS}`);

if (!options) return url.toString();

const { address } = options;

if (address) url.searchParams.set('address', address);

return url.toString();
},
getMintBurnEventsURL(options: MintBurnEventsOptions): string {
const { address } = options;

const url = new URL(`${API.MINT_BURN_EVENTS}/${address}`);

return url.toString();
},
getTotalSupplyURL(options?: TotalSupplyOptions): string {
if (!options) return `${API.TOTAL_SUPPLY}`;

const { chain } = options;

const url = new URL(`${API.TOTAL_SUPPLY}${chain ? `?chain=${chain}` : ''}`);

return url.toString();
},
};

0 comments on commit 1148941

Please sign in to comment.