chore: sync submodule state (parent ref update)
Made-with: Cursor
This commit is contained in:
41
frontend/libs/frontend-api-client/client.test.ts
Normal file
41
frontend/libs/frontend-api-client/client.test.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const mockGet = vi.fn()
|
||||
vi.mock('axios', () => ({
|
||||
default: {
|
||||
create: () => ({
|
||||
get: mockGet,
|
||||
interceptors: {
|
||||
request: { use: vi.fn(), eject: vi.fn() },
|
||||
response: { use: vi.fn(), eject: vi.fn() },
|
||||
},
|
||||
}),
|
||||
},
|
||||
}))
|
||||
|
||||
describe('createApiClient getSafe', () => {
|
||||
beforeEach(() => {
|
||||
mockGet.mockReset()
|
||||
})
|
||||
|
||||
it('returns { ok: false, data: null } when response status is 404', async () => {
|
||||
const { createApiClient } = await import('./client')
|
||||
mockGet.mockResolvedValue({ status: 404, data: { error: 'Not found' } })
|
||||
|
||||
const client = createApiClient('http://test')
|
||||
const result = await client.getSafe<unknown>('/api/v1/transactions/138/0xabc')
|
||||
|
||||
expect(result).toEqual({ ok: false, data: null })
|
||||
})
|
||||
|
||||
it('returns { ok: true, data } when response status is 200 and body has data', async () => {
|
||||
const { createApiClient } = await import('./client')
|
||||
mockGet.mockResolvedValue({ status: 200, data: { data: { hash: '0x123' } } })
|
||||
|
||||
const client = createApiClient('http://test')
|
||||
const result = await client.getSafe<{ hash: string }>('/api/v1/transactions/138/0x123')
|
||||
|
||||
expect(result.ok).toBe(true)
|
||||
expect(result.data).toEqual({ hash: '0x123' })
|
||||
})
|
||||
})
|
||||
77
frontend/libs/frontend-api-client/client.ts
Normal file
77
frontend/libs/frontend-api-client/client.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
export function createApiClient(baseURL: string = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080', getApiKey?: () => string | null) {
|
||||
const client = axios.create({
|
||||
baseURL,
|
||||
timeout: 30000,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
})
|
||||
|
||||
client.interceptors.request.use(
|
||||
(config) => {
|
||||
const key = getApiKey ? getApiKey() : (typeof window !== 'undefined' ? localStorage.getItem('api_key') : null)
|
||||
if (key) config.headers['X-API-Key'] = key
|
||||
return config
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
)
|
||||
|
||||
client.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
if (error.response?.data) return Promise.reject(error.response.data as ApiError)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
return {
|
||||
async get<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await client.get(url, config)
|
||||
return response.data
|
||||
},
|
||||
/** Returns { ok, data } so callers can check ok before setting state (avoids treating 4xx/5xx body as data). */
|
||||
async getSafe<T>(url: string, config?: AxiosRequestConfig): Promise<{ ok: boolean; data: T | null }> {
|
||||
try {
|
||||
const response = await client.get<ApiResponse<T>>(url, { ...config, validateStatus: () => true })
|
||||
const ok = response.status >= 200 && response.status < 300
|
||||
const data = ok && response.data ? (response.data as ApiResponse<T>).data ?? null : null
|
||||
return { ok, data }
|
||||
} catch {
|
||||
return { ok: false, data: null }
|
||||
}
|
||||
},
|
||||
async post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await 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 client.put(url, data, config)
|
||||
return response.data
|
||||
},
|
||||
async delete<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
|
||||
const response: AxiosResponse<ApiResponse<T>> = await client.delete(url, config)
|
||||
return response.data
|
||||
},
|
||||
}
|
||||
}
|
||||
1
frontend/libs/frontend-api-client/index.ts
Normal file
1
frontend/libs/frontend-api-client/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { createApiClient, type ApiResponse, type ApiError } from './client'
|
||||
Reference in New Issue
Block a user