Add full monorepo: virtual-banker, backend, frontend, docs, scripts, deployment
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
48
frontend/src/services/api/blocks.ts
Normal file
48
frontend/src/services/api/blocks.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { apiClient, ApiResponse } from './client'
|
||||
|
||||
export interface Block {
|
||||
chain_id: number
|
||||
number: number
|
||||
hash: string
|
||||
timestamp: string
|
||||
miner: string
|
||||
transaction_count: number
|
||||
gas_used: number
|
||||
gas_limit: number
|
||||
}
|
||||
|
||||
export interface BlockListParams {
|
||||
chain_id: number
|
||||
page?: number
|
||||
page_size?: number
|
||||
min_block?: number
|
||||
max_block?: number
|
||||
miner?: string
|
||||
sort?: string
|
||||
order?: 'asc' | 'desc'
|
||||
}
|
||||
|
||||
export const blocksApi = {
|
||||
list: async (params: BlockListParams): Promise<ApiResponse<Block[]>> => {
|
||||
const queryParams = new URLSearchParams()
|
||||
queryParams.append('chain_id', params.chain_id.toString())
|
||||
if (params.page) queryParams.append('page', params.page.toString())
|
||||
if (params.page_size) queryParams.append('page_size', params.page_size.toString())
|
||||
if (params.min_block) queryParams.append('min_block', params.min_block.toString())
|
||||
if (params.max_block) queryParams.append('max_block', params.max_block.toString())
|
||||
if (params.miner) queryParams.append('miner', params.miner)
|
||||
if (params.sort) queryParams.append('sort', params.sort)
|
||||
if (params.order) queryParams.append('order', params.order)
|
||||
|
||||
return apiClient.get<Block[]>(`/api/v1/blocks?${queryParams.toString()}`)
|
||||
},
|
||||
|
||||
getByNumber: async (chainId: number, number: number): Promise<ApiResponse<Block>> => {
|
||||
return apiClient.get<Block>(`/api/v1/blocks/${chainId}/${number}`)
|
||||
},
|
||||
|
||||
getByHash: async (chainId: number, hash: string): Promise<ApiResponse<Block>> => {
|
||||
return apiClient.get<Block>(`/api/v1/blocks/${chainId}/hash/${hash}`)
|
||||
},
|
||||
}
|
||||
|
||||
92
frontend/src/services/api/client.ts
Normal file
92
frontend/src/services/api/client.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
|
||||
export interface ApiResponse<T> {
|
||||
data: T
|
||||
meta?: {
|
||||
pagination?: {
|
||||
page: number
|
||||
page_size: number
|
||||
total: number
|
||||
total_pages: number
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export interface ApiError {
|
||||
error: {
|
||||
code: string
|
||||
message: string
|
||||
details?: unknown
|
||||
request_id?: string
|
||||
}
|
||||
}
|
||||
|
||||
class ApiClient {
|
||||
private client: AxiosInstance
|
||||
|
||||
constructor(baseURL: string = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080') {
|
||||
this.client = axios.create({
|
||||
baseURL,
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
|
||||
// Request interceptor
|
||||
this.client.interceptors.request.use(
|
||||
(config) => {
|
||||
// Add API key if available
|
||||
const apiKey = this.getApiKey()
|
||||
if (apiKey) {
|
||||
config.headers['X-API-Key'] = apiKey
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
// Response interceptor
|
||||
this.client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// Handle errors
|
||||
if (error.response) {
|
||||
const apiError: ApiError = error.response.data
|
||||
return Promise.reject(apiError)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
private getApiKey(): string | null {
|
||||
if (typeof window !== 'undefined') {
|
||||
return localStorage.getItem('api_key')
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
async get<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await this.client.get(url, config)
|
||||
return response.data
|
||||
}
|
||||
|
||||
async post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await this.client.post(url, data, config)
|
||||
return response.data
|
||||
}
|
||||
|
||||
async put<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await this.client.put(url, data, config)
|
||||
return response.data
|
||||
}
|
||||
|
||||
async delete<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await this.client.delete(url, config)
|
||||
return response.data
|
||||
}
|
||||
}
|
||||
|
||||
export const apiClient = new ApiClient()
|
||||
|
||||
Reference in New Issue
Block a user