chore: sync submodule state (parent ref update)
Made-with: Cursor
This commit is contained in:
@@ -30,6 +30,33 @@ export const addressesApi = {
|
||||
get: async (chainId: number, address: string): Promise<ApiResponse<AddressInfo>> => {
|
||||
return apiClient.get<AddressInfo>(`/api/v1/addresses/${chainId}/${address}`)
|
||||
},
|
||||
/** Use when you need to check response.ok before setting state (avoids treating 4xx/5xx body as data). */
|
||||
getSafe: async (chainId: number, address: string): Promise<{ ok: boolean; data: AddressInfo | null }> => {
|
||||
return apiClient.getSafe<AddressInfo>(`/api/v1/addresses/${chainId}/${address}`)
|
||||
},
|
||||
getTransactionsSafe: async (
|
||||
chainId: number,
|
||||
address: string,
|
||||
page = 1,
|
||||
pageSize = 20
|
||||
): Promise<{ ok: boolean; data: TransactionSummary[] }> => {
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
chain_id: chainId.toString(),
|
||||
from_address: address,
|
||||
page: page.toString(),
|
||||
page_size: pageSize.toString(),
|
||||
})
|
||||
const raw = (await apiClient.get(`/api/v1/transactions?${params.toString()}`)) as unknown as {
|
||||
data?: TransactionSummary[]
|
||||
items?: TransactionSummary[]
|
||||
}
|
||||
const data = Array.isArray(raw?.data) ? raw.data : Array.isArray(raw?.items) ? raw.items : []
|
||||
return { ok: true, data }
|
||||
} catch {
|
||||
return { ok: false, data: [] }
|
||||
}
|
||||
},
|
||||
|
||||
getTransactions: async (
|
||||
chainId: number,
|
||||
|
||||
@@ -1,92 +1,17 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
// Re-export from reusable lib (frontend/libs/frontend-api-client)
|
||||
import { createApiClient, type ApiResponse, type ApiError } from '../../../libs/frontend-api-client'
|
||||
|
||||
export interface ApiResponse<T> {
|
||||
data: T
|
||||
meta?: {
|
||||
pagination?: {
|
||||
page: number
|
||||
page_size: number
|
||||
total: number
|
||||
total_pages: number
|
||||
}
|
||||
export type { ApiResponse, ApiError }
|
||||
|
||||
function getApiKey(): string | null {
|
||||
if (typeof window !== 'undefined') {
|
||||
return localStorage.getItem('api_key')
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
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()
|
||||
export const apiClient = createApiClient(
|
||||
process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080',
|
||||
getApiKey
|
||||
)
|
||||
|
||||
|
||||
@@ -24,4 +24,34 @@ export const transactionsApi = {
|
||||
get: async (chainId: number, hash: string): Promise<ApiResponse<Transaction>> => {
|
||||
return apiClient.get<Transaction>(`/api/v1/transactions/${chainId}/${hash}`)
|
||||
},
|
||||
/** Use when you need to check response.ok before setting state (avoids treating 4xx/5xx body as data). */
|
||||
getSafe: async (chainId: number, hash: string): Promise<{ ok: boolean; data: Transaction | null }> => {
|
||||
return apiClient.getSafe<Transaction>(`/api/v1/transactions/${chainId}/${hash}`)
|
||||
},
|
||||
list: async (chainId: number, page: number, pageSize: number): Promise<ApiResponse<Transaction[]>> => {
|
||||
const params = new URLSearchParams({
|
||||
chain_id: chainId.toString(),
|
||||
page: page.toString(),
|
||||
page_size: pageSize.toString(),
|
||||
})
|
||||
const raw = (await apiClient.get(`/api/v1/transactions?${params.toString()}`)) as unknown as { data?: Transaction[]; items?: Transaction[] }
|
||||
const data = Array.isArray(raw?.data) ? raw.data : Array.isArray(raw?.items) ? raw.items : []
|
||||
return { data }
|
||||
},
|
||||
/** Use when you need to check ok before setting state (avoids treating error body as list). */
|
||||
listSafe: async (chainId: number, page: number, pageSize: number): Promise<{ ok: boolean; data: Transaction[] }> => {
|
||||
try {
|
||||
const params = new URLSearchParams({
|
||||
chain_id: chainId.toString(),
|
||||
page: page.toString(),
|
||||
page_size: pageSize.toString(),
|
||||
})
|
||||
const raw = await apiClient.getSafe<Transaction[]>(`/api/v1/transactions?${params.toString()}`)
|
||||
if (!raw.ok) return { ok: false, data: [] }
|
||||
const data = Array.isArray(raw.data) ? raw.data : []
|
||||
return { ok: true, data }
|
||||
} catch {
|
||||
return { ok: false, data: [] }
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user