Skip to content

Commit

Permalink
refactor: limit query opts, add hiro header
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Jan 29, 2024
1 parent d13a678 commit 68c557f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/app/common/api/fetch-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { analytics } from '@shared/utils/analytics';

const leatherHeaders: HeadersInit = {
'x-leather-version': VERSION,
'x-hiro-product': 'leather',
};

function isErrorCode(statusCode: number) {
Expand All @@ -30,6 +31,13 @@ export async function wrappedFetch(input: RequestInfo, init: RequestInit = {}) {
return resp;
}

axios.interceptors.request.use(request => {
if (request.url?.includes('hiro.so'))
Object.entries(leatherHeaders).forEach(([key, value]) => request.headers.set(key, value));

return request;
});

axios.interceptors.response.use(response => {
if (isErrorCode(response.status)) trackApiError(response.config.url ?? '', response.status);
return response;
Expand Down
2 changes: 2 additions & 0 deletions src/app/query/stacks/contract/contract.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,7 @@ export function useGetContractInterface(transactionRequest: ContractCallPayload
transactionRequest?.contractAddress,
],
queryFn: fetchContractInterface,
staleTime: 30 * 60 * 1000,
refetchOnWindowFocus: false,
});
}
9 changes: 9 additions & 0 deletions src/app/query/stacks/transactions/transactions-by-id.query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { useStacksClientUnanchored } from '@app/store/common/api-clients.hooks';

import { useHiroApiRateLimiter } from '../rate-limiter';

const options = {
staleTime: 30 * 1000,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: true,
} as const;

export function useTransactionsById(txids: string[]) {
const client = useStacksClientUnanchored();
const limiter = useHiroApiRateLimiter();
Expand All @@ -19,6 +26,7 @@ export function useTransactionsById(txids: string[]) {
return {
queryKey: ['transaction-by-id', txid],
queryFn: () => transactionByIdFetcher(txid),
...options,
};
}),
});
Expand All @@ -37,5 +45,6 @@ export function useTransactionById(txid: string) {
return useQuery({
queryKey: ['transaction-by-id', txid],
queryFn: () => transactionByIdFetcher(txid),
...options,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { useCurrentNetworkState } from '@app/store/networks/networks.hooks';
import { useHiroApiRateLimiter } from '../rate-limiter';

const queryOptions: UseQueryOptions = {
refetchInterval: 10_000,
refetchOnMount: 'always',
refetchOnReconnect: 'always',
refetchOnWindowFocus: 'always',
staleTime: 60 * 1000,
refetchInterval: 30_000,
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: true,
};

export function useGetAccountTransactionsWithTransfersQuery() {
Expand Down

0 comments on commit 68c557f

Please sign in to comment.