Skip to content

Commit

Permalink
update API routes to use env variables
Browse files Browse the repository at this point in the history
  • Loading branch information
Jennievon committed Nov 23, 2023
1 parent 963e70b commit 5ebb300
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 43 deletions.
9 changes: 3 additions & 6 deletions tools/obscuroscan_v3/frontend/api/batches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,28 @@ import {
export const fetchBatches = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<BatchResponse>> => {
const data = await httpRequest<ResponseDataInterface<BatchResponse>>({
return await httpRequest<ResponseDataInterface<BatchResponse>>({
method: "get",
url: pathToUrl(apiRoutes.getBatches),
searchParams: payload,
});
return data;
};

export const fetchLatestBatch = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<Batch>> => {
const data = await httpRequest<ResponseDataInterface<Batch>>({
return await httpRequest<ResponseDataInterface<Batch>>({
method: "get",
url: pathToUrl(apiRoutes.getLatestBatch),
searchParams: payload,
});
return data;
};

export const fetchBatchByHash = async (
hash: string
): Promise<ResponseDataInterface<BatchDetails>> => {
const data = await httpRequest<ResponseDataInterface<BatchDetails>>({
return await httpRequest<ResponseDataInterface<BatchDetails>>({
method: "get",
url: pathToUrl(apiRoutes.getBatchByHash, { hash }),
});
return data;
};
3 changes: 1 addition & 2 deletions tools/obscuroscan_v3/frontend/api/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { ResponseDataInterface } from "@/src/types/interfaces";
export const fetchBlocks = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<any>> => {
const data = await httpRequest<ResponseDataInterface<any>>({
return await httpRequest<ResponseDataInterface<any>>({
method: "get",
url: pathToUrl(apiRoutes.getBlocks),
searchParams: payload,
});
return data;
};
6 changes: 2 additions & 4 deletions tools/obscuroscan_v3/frontend/api/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import { ContractCount } from "@/src/types/interfaces/ContractInterface";
export const fetchContractCount = async (
payload?: Record<string, any>
): Promise<ContractCount> => {
const data = await httpRequest<ContractCount>({
return await httpRequest<ContractCount>({
method: "get",
url: pathToUrl(apiRoutes.getContractCount),
searchParams: payload,
});
return data;
};

export const fetchVerifiedContracts = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<any>> => {
const data = await httpRequest<ResponseDataInterface<any>>({
return await httpRequest<ResponseDataInterface<any>>({
method: "get",
url: pathToUrl(apiRoutes.getVerifiedContracts),
searchParams: payload,
});
return data;
};
30 changes: 26 additions & 4 deletions tools/obscuroscan_v3/frontend/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { apiHost } from "@/src/lib/constants";
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";

type HttpMethod = "get" | "post" | "put" | "patch" | "delete";
Expand All @@ -21,11 +22,11 @@ interface HttpOptions {
}

const baseConfig: AxiosRequestConfig = {
baseURL: process.env.NEXT_PUBLIC_API_HOST,
baseURL: apiHost,
timeout: 10000,
};

export const https: AxiosInstance = axios.create(baseConfig);
const https: AxiosInstance = axios.create(baseConfig);

export const httpRequest = async <ResponseData>(
options: HttpOptions,
Expand Down Expand Up @@ -63,6 +64,27 @@ export const httpRequest = async <ResponseData>(
responseType: responseType,
...config,
};
const response = await https(httpConfig);
return response.data as ResponseData;
try {
const response = await https(httpConfig);
return response.data as ResponseData;
} catch (error) {
handleHttpError(error);
throw error;
}
};

// Centralized error handling function
const handleHttpError = (error: any) => {
// if the error is a server error (status code 5xx) before handling
if (isAxiosError(error) && error.response && error.response.status >= 500) {
console.error("Server error:", error);
} else {
// other errors
console.error("An error occurred:", error);
}
};

// Type guard to check if the error is an AxiosError
const isAxiosError = (error: any): error is import("axios").AxiosError => {
return error.isAxiosError === true;
};
6 changes: 2 additions & 4 deletions tools/obscuroscan_v3/frontend/api/rollups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,21 @@ import { ResponseDataInterface } from "@/src/types/interfaces";
export const fetchRollups = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<any>> => {
const data = await httpRequest<ResponseDataInterface<any>>({
return await httpRequest<ResponseDataInterface<any>>({
method: "get",
url: pathToUrl(apiRoutes.getRollups),
searchParams: payload,
});
return data;
};

export const decryptEncryptedRollup = async ({
StrData,
}: {
StrData: string;
}): Promise<ResponseDataInterface<any>> => {
const data = await httpRequest<ResponseDataInterface<any>>({
return await httpRequest<ResponseDataInterface<any>>({
method: "post",
url: pathToUrl(apiRoutes.decryptEncryptedRollup),
data: { StrData },
});
return data;
};
9 changes: 3 additions & 6 deletions tools/obscuroscan_v3/frontend/api/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,23 @@ import {
export const fetchTransactions = async (
payload?: Record<string, any>
): Promise<ResponseDataInterface<TransactionResponse>> => {
const data = await httpRequest<ResponseDataInterface<TransactionResponse>>({
return await httpRequest<ResponseDataInterface<TransactionResponse>>({
method: "get",
url: pathToUrl(apiRoutes.getTransactions),
searchParams: payload,
});
return data;
};

export const fetchTransactionCount = async (): Promise<TransactionCount> => {
const data = await httpRequest<TransactionCount>({
return await httpRequest<TransactionCount>({
method: "get",
url: pathToUrl(apiRoutes.getTransactionCount),
});
return data;
};

export const fetchEtherPrice = async (): Promise<Price> => {
const data = await httpRequest<Price>({
return await httpRequest<Price>({
method: "get",
url: apiRoutes.getEtherPrice,
});
return data;
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { socialLinks } from "@/src/lib/constants";
import { socialLinks, version } from "@/src/lib/constants";
import {
GitHubLogoIcon,
TwitterLogoIcon,
Expand Down Expand Up @@ -30,7 +30,7 @@ export default function Footer() {
</a>
</div>
<div className="flex items-center space-x-4">
<a
{/* <a
href="/privacy"
className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary"
>
Expand All @@ -41,7 +41,10 @@ export default function Footer() {
className="text-sm font-medium text-muted-foreground transition-colors hover:text-primary"
>
Terms
</a>
</a> */}
<div className="text-sm font-medium text-muted-foreground">
Version: {version}
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default function AnalyticsCard({
<Skeleton className="w-[100px] h-[20px] rounded-full" />
)}
</div>
<p className="text-xs text-muted-foreground">{item.change}</p>
{item.change && (
<p className="text-xs text-muted-foreground">{item.change}</p>
)}
</CardContent>
</Card>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { useBlocksService } from "@/src/services/useBlocksService";
import AnalyticsCard from "./analytics-card";
import Link from "next/link";
import { cn } from "@/src/lib/utils";
import { Badge } from "../../ui/badge";
import { BlocksIcon } from "lucide-react";

export default function Dashboard() {
const { price, transactions, transactionCount } = useTransactionsService();
Expand All @@ -44,7 +46,7 @@ export default function Dashboard() {
},
{
title: "Latest L2 Batch",
value: batches?.result?.Total,
value: batches?.result?.Total || "N/A",
// TODO: add change
// change: "+20.1%",
icon: LayersIcon,
Expand Down Expand Up @@ -78,6 +80,11 @@ export default function Dashboard() {
// change: "+20.1%",
icon: FileTextIcon,
},
{
title: "Nodes",
value: <Badge>Coming Soon</Badge>,
icon: BlocksIcon,
},
];

const RECENT_DATA = [
Expand Down Expand Up @@ -107,7 +114,7 @@ export default function Dashboard() {
return (
<div className="h-full flex-1 flex-col space-y-8 md:flex">
<div className="flex items-center justify-between space-y-2">
<h2 className="text-3xl font-bold tracking-tight">Obscuroscan</h2>
<h2 className="text-3xl font-bold tracking-tight">Tenscan</h2>
</div>
<div className="grid gap-4 grid-cols-1 sm:grid-cols-3 md:grid-cols-2 lg:grid-cols-4">
{DASHBOARD_DATA.map((item: any, index) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,6 @@ export function RecentBatches({ batches }: { batches: any }) {
<div className="ml-auto font-medium min-w-[140px]">
<TruncatedAddress address={batch?.hash} />
</div>
{/* <div className="ml-auto font-medium max-w-[100px]">
<Link
href={{
pathname: `/batches/${batch?.number}`,
}}
>
<Button variant="link" size="sm">
<EyeOpenIcon />
</Button>
</Link>
</div> */}
</div>
))}
</div>
Expand Down
3 changes: 3 additions & 0 deletions tools/obscuroscan_v3/frontend/src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ export const pollingInterval = 5000;
export const maxRetries = 3;
export const pricePollingInterval = 60 * 1000;

export const version = process.env.NEXT_PUBLIC_FE_VERSION;
export const apiHost = process.env.NEXT_PUBLIC_API_HOST;

export const currentEncryptedKey =
"bddbc0d46a0666ce57a466168d99c1830b0c65e052d77188f2cbfc3f6486588c";

0 comments on commit 5ebb300

Please sign in to comment.