Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constitution DAO #128

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion constitution-dao/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"rules": {
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-empty-object-type": "off"
"@typescript-eslint/no-empty-object-type": "off",
"@typescript-eslint/no-duplicate-enum-values": "off"
}
}
12 changes: 12 additions & 0 deletions constitution-dao/api/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { httpRequest } from "@/api";
import { apiRoutes } from "@/src/routes";
import { ResponseDataInterface } from "@/src/lib/types/common";

export const fetchTestnetStatus = async (): Promise<
ResponseDataInterface<boolean>
> => {
return await httpRequest<ResponseDataInterface<boolean>>({
method: "get",
url: apiRoutes.getHealthStatus,
});
};
90 changes: 90 additions & 0 deletions constitution-dao/api/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { tenGatewayAddress } from "@/src/lib/constants";
import axios, { AxiosInstance, AxiosRequestConfig } from "axios";

type HttpMethod = "get" | "post" | "put" | "patch" | "delete";

interface HttpOptions {
method?: HttpMethod;
url: string;
data?: Record<string, any>;
params?: Record<string, any>;
headers?: Record<string, any>;
timeout?: number;
responseType?:
| "json"
| "arraybuffer"
| "blob"
| "document"
| "text"
| undefined;
download?: boolean;
searchParams?: Record<string, any>;
}

const baseConfig: AxiosRequestConfig = {
baseURL: tenGatewayAddress,
timeout: 10000,
};

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

export const httpRequest = async <ResponseData>(
options: HttpOptions,
config: AxiosRequestConfig = {}
): Promise<ResponseData> => {
const {
method = "get",
url,
data,
params,
headers,
timeout,
responseType,
searchParams,
} = options;
let query = "";
if (searchParams) {
const filteredParams = Object.fromEntries(
Object.entries(searchParams).filter(
([, value]) => value !== undefined && value !== null && value !== ""
)
);
if (Object.keys(filteredParams).length) {
query = new URLSearchParams(filteredParams).toString();
}
}

const httpConfig: AxiosRequestConfig = {
method,
url: query ? `${url}?${query}` : url,
data,
params,
headers: { ...(headers || {}) },
timeout,
responseType: responseType,
...config,
};
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;
};
8 changes: 7 additions & 1 deletion constitution-dao/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,23 @@
"lint": "next lint"
},
"dependencies": {
"@radix-ui/react-dropdown-menu": "^2.1.2",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-progress": "^1.1.0",
"@radix-ui/react-toast": "^1.2.2",
"@radix-ui/react-slot": "^1.1.0",
"@radix-ui/react-toast": "^1.2.2",
"@radix-ui/react-tooltip": "^1.1.3",
"@rainbow-me/rainbowkit": "^2.1.5",
"axios": "^1.7.7",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"dotenv": "^16.4.5",
"lucide-react": "^0.453.0",
"next": "14.2.15",
"next-themes": "^0.2.1",
"react": "^18",
"react-dom": "^18",
"react-responsive": "^10.0.0",
"tailwind-merge": "^2.5.4",
"tailwindcss-animate": "^1.0.7",
"viem": "^2.21.3",
Expand Down
Loading