fix docs
This commit is contained in:
81
src/features/b2b/api/b2bApi.ts
Normal file
81
src/features/b2b/api/b2bApi.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { getCsrfToken } from '@shared/api/csrf'
|
||||
import { refreshAccessToken } from '@shared/api/tokenStore'
|
||||
|
||||
const B2B_API_URL = 'https://app.b2b.elcsa.ru/api'
|
||||
|
||||
async function doB2bRequest<T>(
|
||||
path: string,
|
||||
options: RequestInit,
|
||||
allowRetry: boolean,
|
||||
): Promise<T> {
|
||||
const csrf = await getCsrfToken()
|
||||
|
||||
const res = await fetch(`${B2B_API_URL}${path}`, {
|
||||
...options,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
'X-CSRF-Token': csrf,
|
||||
...options.headers,
|
||||
},
|
||||
})
|
||||
|
||||
if (res.status === 401 && allowRetry) {
|
||||
try {
|
||||
await refreshAccessToken()
|
||||
return doB2bRequest<T>(path, options, false)
|
||||
} catch {
|
||||
throw new Error('Unauthorized')
|
||||
}
|
||||
}
|
||||
|
||||
const data = await res.json()
|
||||
if (!res.ok) throw data
|
||||
return data as T
|
||||
}
|
||||
|
||||
export interface CreatePurchaseRequestBody {
|
||||
usdt_amount: number | string
|
||||
comment?: string | null
|
||||
target_wallet_chain?: string | null
|
||||
target_wallet_address?: string | null
|
||||
}
|
||||
|
||||
export interface B2bPurchaseRequest {
|
||||
id: string
|
||||
organization_id: string
|
||||
status: string
|
||||
usdt_amount: string
|
||||
rub_amount: string | null
|
||||
exchange_rate: string | null
|
||||
service_fee_percent: string | null
|
||||
comment: string | null
|
||||
admin_comment: string | null
|
||||
target_wallet_chain: string | null
|
||||
target_wallet_address: string | null
|
||||
tx_hash: string | null
|
||||
created_at: string | null
|
||||
updated_at: string | null
|
||||
completed_at: string | null
|
||||
}
|
||||
|
||||
export interface B2bPurchaseRequestListResponse {
|
||||
items: B2bPurchaseRequest[]
|
||||
total: number
|
||||
}
|
||||
|
||||
export function createPurchaseRequest(
|
||||
body: CreatePurchaseRequestBody,
|
||||
): Promise<B2bPurchaseRequest> {
|
||||
return doB2bRequest('/purchase-requests', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body),
|
||||
}, true)
|
||||
}
|
||||
|
||||
export function getMyPurchaseRequests(
|
||||
limit = 50,
|
||||
offset = 0,
|
||||
): Promise<B2bPurchaseRequestListResponse> {
|
||||
return doB2bRequest(`/purchase-requests?limit=${limit}&offset=${offset}`, {}, true)
|
||||
}
|
||||
Reference in New Issue
Block a user