New fetchWithValidation / introducing simpleFetch

This commit is contained in:
Aleksandr Kraiz
2022-05-10 11:44:09 +04:00
parent edc91ecefe
commit 883bcf928f
16 changed files with 350 additions and 117 deletions

View File

@@ -1,5 +1,5 @@
import { z } from 'zod';
import { fetchJsonWithValidation } from '../../fetchWithValidation';
import fetchWithValidation from '../../fetchWithValidation';
import { PairStatusEnum, pairStatusSchema } from './schemas/adminPoolsListSchema';
import {
IDOSchema, atomicHistorySchema,
@@ -56,6 +56,32 @@ class OrionBlockchain {
constructor(apiUrl: string) {
this.apiUrl = apiUrl;
this.ws = new OrionBlockchainSocketIO(`https://${apiUrl}/`);
this.getAtomicSwapAssets = this.getAtomicSwapAssets.bind(this);
this.getAtomicSwapHistory = this.getAtomicSwapHistory.bind(this);
this.getAuthToken = this.getAuthToken.bind(this);
this.getCirculatingSupply = this.getCirculatingSupply.bind(this);
this.getInfo = this.getInfo.bind(this);
this.getPoolsConfig = this.getPoolsConfig.bind(this);
this.getPoolsInfo = this.getPoolsInfo.bind(this);
this.getHistory = this.getHistory.bind(this);
this.getPrices = this.getPrices.bind(this);
this.getTokensFee = this.getTokensFee.bind(this);
this.getGasPriceWei = this.getGasPriceWei.bind(this);
this.checkFreeRedeemAvailable = this.checkFreeRedeemAvailable.bind(this);
this.redeemAtomicSwap = this.redeemAtomicSwap.bind(this);
this.redeem2AtomicSwaps = this.redeem2AtomicSwaps.bind(this);
this.checkRedeem = this.checkRedeem.bind(this);
this.checkRedeem2Atomics = this.checkRedeem2Atomics.bind(this);
this.getIDOInfo = this.getIDOInfo.bind(this);
this.checkAuth = this.checkAuth.bind(this);
this.addPool = this.addPool.bind(this);
this.editPool = this.editPool.bind(this);
this.getPoolsList = this.getPoolsList.bind(this);
this.getSourceAtomicSwapHistory = this.getSourceAtomicSwapHistory.bind(this);
this.getTargetAtomicSwapHistory = this.getTargetAtomicSwapHistory.bind(this);
this.checkPoolInformation = this.checkPoolInformation.bind(this);
this.checkIfHashUsed = this.checkIfHashUsed.bind(this);
}
get orionBlockchainWsUrl() {
@@ -63,43 +89,43 @@ class OrionBlockchain {
}
getAuthToken() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/auth/token`, z.object({ token: z.string() }));
return fetchWithValidation(`https://${this.apiUrl}/api/auth/token`, z.object({ token: z.string() }));
}
getCirculatingSupply() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/circulating-supply`, z.number());
return fetchWithValidation(`https://${this.apiUrl}/api/circulating-supply`, z.number());
}
getInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/info`, infoSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/info`, infoSchema);
}
getPoolsConfig() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/config`, poolsConfigSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/config`, poolsConfigSchema);
}
getPoolsInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/info`, poolsInfoSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/info`, poolsInfoSchema);
}
getHistory(address: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/history/${address}`, historySchema);
return fetchWithValidation(`https://${this.apiUrl}/api/history/${address}`, historySchema);
}
getPrices() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/prices`, z.record(z.string()).transform(utils.makePartial));
return fetchWithValidation(`https://${this.apiUrl}/api/prices`, z.record(z.string()).transform(utils.makePartial));
}
getTokensFee() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/tokensFee`, z.record(z.string()).transform(utils.makePartial));
return fetchWithValidation(`https://${this.apiUrl}/api/tokensFee`, z.record(z.string()).transform(utils.makePartial));
}
getGasPriceWei() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/gasPrice`, z.string());
return fetchWithValidation(`https://${this.apiUrl}/api/gasPrice`, z.string());
}
checkFreeRedeemAvailable(walletAddress: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/atomic/has-free-redeem/${walletAddress}`, z.boolean());
return fetchWithValidation(`https://${this.apiUrl}/api/atomic/has-free-redeem/${walletAddress}`, z.boolean());
}
redeemAtomicSwap(
@@ -107,7 +133,7 @@ class OrionBlockchain {
secret: string,
sourceNetwork: string,
) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem`,
z.string(),
{
@@ -131,7 +157,7 @@ class OrionBlockchain {
secret2: string,
sourceNetwork: string,
) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem2atomics`,
z.string(),
{
@@ -151,31 +177,31 @@ class OrionBlockchain {
}
checkRedeem(secretHash: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem/${secretHash}`,
z.enum(['OK', 'FAIL']).nullable(),
);
}
checkRedeem2Atomics(firstSecretHash: string, secondSecretHash: string) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/matcher-redeem/${firstSecretHash}-${secondSecretHash}`,
z.enum(['OK', 'FAIL']).nullable(),
);
}
getIDOInfo() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/solarflare`, IDOSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/solarflare`, IDOSchema);
}
checkAuth(headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/auth/check`, z.object({
return fetchWithValidation(`https://${this.apiUrl}/api/auth/check`, z.object({
auth: z.boolean(),
}), { headers });
}
getPoolsList(headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/list`,
adminPoolsListSchema,
{ headers },
@@ -183,7 +209,7 @@ class OrionBlockchain {
}
editPool(address: string, data: IEditPool, headers: IAdminAuthHeaders) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/edit/${address}`,
pairStatusSchema,
{
@@ -198,22 +224,27 @@ class OrionBlockchain {
}
addPool(data: z.infer<typeof addPoolSchema>) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/add`, z.number(), {
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
return fetchWithValidation(
`https://${this.apiUrl}/api/pools/add`,
z.number(),
{
method: 'POST',
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
},
});
z.string(),
);
}
checkPoolInformation(poolAddress: string) {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/pools/check/${poolAddress}`, pairStatusSchema);
return fetchWithValidation(`https://${this.apiUrl}/api/pools/check/${poolAddress}`, pairStatusSchema);
}
getAtomicSwapAssets() {
return fetchJsonWithValidation(`https://${this.apiUrl}/api/atomic/swap-assets`, z.array(z.string()));
return fetchWithValidation(`https://${this.apiUrl}/api/atomic/swap-assets`, z.array(z.string()));
}
/**
@@ -226,7 +257,7 @@ class OrionBlockchain {
Object.entries(query)
.forEach(([key, value]) => url.searchParams.append(key, value.toString()));
return fetchJsonWithValidation(url.toString(), atomicHistorySchema);
return fetchWithValidation(url.toString(), atomicHistorySchema);
}
getSourceAtomicSwapHistory(query: AtomicSwapHistorySourceQuery) {
@@ -237,7 +268,7 @@ class OrionBlockchain {
if (!query.type) url.searchParams.append('type', 'source');
return fetchJsonWithValidation(url.toString(), sourceAtomicHistorySchema);
return fetchWithValidation(url.toString(), sourceAtomicHistorySchema);
}
getTargetAtomicSwapHistory(query: AtomicSwapHistoryTargetQuery) {
@@ -248,11 +279,11 @@ class OrionBlockchain {
if (!query.type) url.searchParams.append('type', 'target');
return fetchJsonWithValidation(url.toString(), targetAtomicHistorySchema);
return fetchWithValidation(url.toString(), targetAtomicHistorySchema);
}
checkIfHashUsed(secretHashes: string[]) {
return fetchJsonWithValidation(
return fetchWithValidation(
`https://${this.apiUrl}/api/atomic/is-hash-used`,
z.record(z.boolean()).transform(utils.makePartial),
{