1
- import axios from 'axios' ;
2
1
import { inject , injectable } from 'inversify' ;
3
- import { z } from 'zod ' ;
2
+ import createClient from 'openapi-fetch ' ;
4
3
5
- import { LEATHER_API_URL } from '@leather.io/constants' ;
4
+ import { LEATHER_API_URL_PRODUCTION , LEATHER_API_URL_STAGING } from '@leather.io/constants' ;
6
5
import { SupportedBlockchains } from '@leather.io/models' ;
7
6
8
7
import { Types } from '../../../inversify.types' ;
9
8
import type { HttpCacheService } from '../../cache/http-cache.service' ;
10
9
import { HttpCacheTimeMs } from '../../cache/http-cache.utils' ;
11
10
import { selectBitcoinNetwork } from '../../settings/settings.selectors' ;
12
11
import type { SettingsService } from '../../settings/settings.service' ;
13
- import {
14
- LeatherApiPage ,
15
- LeatherApiPageRequest ,
16
- getPageRequestQueryParams ,
17
- } from './leather-api.pagination' ;
18
- import {
19
- leatherApiBitcoinTransactionPageSchema ,
20
- leatherApiBitcoinTransactionSchema ,
21
- leatherApiCryptoPricesSchema ,
22
- leatherApiFiatRatesSchema ,
23
- leatherApiRegisterNotificationsResponseSchema ,
24
- leatherApiSip10PricesSchema ,
25
- leatherApiUtxoSchema ,
26
- } from './leather-api.schemas' ;
12
+ import { LeatherApiPageRequest , getPageRequestQueryParams } from './leather-api.pagination' ;
13
+ import { paths } from './leather-api.types' ;
27
14
28
- export type LeatherApiUtxo = z . infer < typeof leatherApiUtxoSchema > ;
29
- export type LeatherApiBitcoinTransaction = z . infer < typeof leatherApiBitcoinTransactionSchema > ;
30
- export type LeatherApiFiatRates = z . infer < typeof leatherApiFiatRatesSchema > ;
31
- export type LeatherApiCryptoPrices = z . infer < typeof leatherApiCryptoPricesSchema > ;
32
- export type LeatherApiSip10Prices = z . infer < typeof leatherApiSip10PricesSchema > ;
33
- export type LeatherApiRegisterNotificationsResponse = z . infer <
34
- typeof leatherApiRegisterNotificationsResponseSchema
35
- > ;
15
+ export type LeatherApiBitcoinTransaction =
16
+ paths [ '/v1/transactions/{descriptor}' ] [ 'get' ] [ 'responses' ] [ 200 ] [ 'content' ] [ 'application/json' ] [ 'data' ] [ number ] ;
36
17
37
18
@injectable ( )
38
19
export class LeatherApiClient {
20
+ private readonly client ;
21
+
39
22
constructor (
40
23
@inject ( Types . CacheService ) private readonly cacheService : HttpCacheService ,
41
- @inject ( Types . SettingsService ) private readonly settingsService : SettingsService
42
- ) { }
24
+ @inject ( Types . SettingsService ) private readonly settingsService : SettingsService ,
25
+ @inject ( Types . WalletEnvironment ) environmnet : string
26
+ ) {
27
+ this . client = createClient < paths > ( {
28
+ baseUrl : environmnet === 'production' ? LEATHER_API_URL_PRODUCTION : LEATHER_API_URL_STAGING ,
29
+ } ) ;
30
+ this . client . use ( {
31
+ onResponse ( { response } ) {
32
+ if ( ! response . ok ) {
33
+ throw new Error (
34
+ `Leather API (${ response . url } ): ${ response . status } ${ response . statusText } `
35
+ ) ;
36
+ }
37
+ } ,
38
+ } ) ;
39
+ }
43
40
44
- async fetchUtxos ( descriptor : string , signal ?: AbortSignal ) : Promise < LeatherApiUtxo [ ] > {
41
+ async fetchUtxos ( descriptor : string , signal ?: AbortSignal ) {
45
42
const network = this . settingsService . getSettings ( ) . network . chain . bitcoin . bitcoinNetwork ;
46
43
return await this . cacheService . fetchWithCache (
47
44
[ 'leather-api-utxos' , network , descriptor ] ,
48
45
async ( ) => {
49
- const res = await axios . get < LeatherApiUtxo [ ] > (
50
- ` ${ LEATHER_API_URL } /v1/utxos/ ${ descriptor } ?network= ${ network } ` ,
51
- { signal }
52
- ) ;
53
- return z . array ( leatherApiUtxoSchema ) . parse ( res . data ) ;
46
+ const { data } = await this . client . GET ( `/v1/utxos/{descriptor}` , {
47
+ params : { path : { descriptor } , query : { network } } ,
48
+ signal,
49
+ } ) ;
50
+ return data ! ;
54
51
} ,
55
52
{ ttl : HttpCacheTimeMs . fiveSeconds }
56
53
) ;
@@ -60,65 +57,57 @@ export class LeatherApiClient {
60
57
descriptor : string ,
61
58
pageRequest : LeatherApiPageRequest ,
62
59
signal ?: AbortSignal
63
- ) : Promise < LeatherApiPage < LeatherApiBitcoinTransaction > > {
60
+ ) {
64
61
const params = getPageRequestQueryParams ( pageRequest ) ;
65
- params . append ( ' network' , selectBitcoinNetwork ( this . settingsService . getSettings ( ) ) ) ;
62
+ const network = selectBitcoinNetwork ( this . settingsService . getSettings ( ) ) ;
66
63
return await this . cacheService . fetchWithCache (
67
64
[ 'leather-api-transactions' , descriptor , params . toString ( ) ] ,
68
65
async ( ) => {
69
- const res = await axios . get < LeatherApiPage < LeatherApiBitcoinTransaction > > (
70
- `${ LEATHER_API_URL } /v1/transactions/${ descriptor } ?${ params . toString ( ) } ` ,
71
- { signal }
72
- ) ;
73
- return leatherApiBitcoinTransactionPageSchema . parse ( res . data ) ;
66
+ const { data } = await this . client . GET ( `/v1/transactions/{descriptor}` , {
67
+ params : {
68
+ path : { descriptor } ,
69
+ query : {
70
+ network,
71
+ page : pageRequest . page . toString ( ) ,
72
+ pageSize : pageRequest . pageSize . toString ( ) ,
73
+ } ,
74
+ } ,
75
+ signal,
76
+ } ) ;
77
+ return data ! ;
74
78
} ,
75
79
{ ttl : HttpCacheTimeMs . fiveSeconds }
76
80
) ;
77
81
}
78
82
79
- async fetchFiatExchangeRates ( signal ?: AbortSignal ) : Promise < LeatherApiFiatRates > {
83
+ async fetchFiatExchangeRates ( signal ?: AbortSignal ) {
80
84
return await this . cacheService . fetchWithCache (
81
85
[ 'leather-api-fiat-rates' ] ,
82
86
async ( ) => {
83
- const res = await axios . get < LeatherApiFiatRates > (
84
- `${ LEATHER_API_URL } /v1/market/fiat-rates` ,
85
- {
86
- signal,
87
- }
88
- ) ;
89
- return leatherApiFiatRatesSchema . parse ( res . data ) ;
87
+ const { data } = await this . client . GET ( '/v1/market/fiat-rates' , { signal } ) ;
88
+ return data ! ;
90
89
} ,
91
90
{ ttl : HttpCacheTimeMs . oneDay }
92
91
) ;
93
92
}
94
93
95
- async fetchCryptoPrices ( signal ?: AbortSignal ) : Promise < LeatherApiCryptoPrices > {
94
+ async fetchCryptoPrices ( signal ?: AbortSignal ) {
96
95
return await this . cacheService . fetchWithCache (
97
96
[ 'leather-api-crypto-prices' ] ,
98
97
async ( ) => {
99
- const res = await axios . get < LeatherApiCryptoPrices > (
100
- `${ LEATHER_API_URL } /v1/market/crypto-prices` ,
101
- {
102
- signal,
103
- }
104
- ) ;
105
- return leatherApiCryptoPricesSchema . parse ( res . data ) ;
98
+ const { data } = await this . client . GET ( '/v1/market/crypto-prices' , { signal } ) ;
99
+ return data ! ;
106
100
} ,
107
101
{ ttl : HttpCacheTimeMs . tenMinutes }
108
102
) ;
109
103
}
110
104
111
- async fetchSip10Prices ( signal ?: AbortSignal ) : Promise < LeatherApiSip10Prices > {
105
+ async fetchSip10Prices ( signal ?: AbortSignal ) {
112
106
return await this . cacheService . fetchWithCache (
113
107
[ 'leather-api-sip10-prices' ] ,
114
108
async ( ) => {
115
- const res = await axios . get < LeatherApiSip10Prices > (
116
- `${ LEATHER_API_URL } /v1/market/sip10-prices` ,
117
- {
118
- signal,
119
- }
120
- ) ;
121
- return leatherApiSip10PricesSchema . parse ( res . data ) ;
109
+ const { data } = await this . client . GET ( '/v1/market/sip10-prices' , { signal } ) ;
110
+ return data ! ;
122
111
} ,
123
112
{ ttl : HttpCacheTimeMs . tenMinutes }
124
113
) ;
@@ -135,21 +124,20 @@ export class LeatherApiClient {
135
124
chain : SupportedBlockchains ;
136
125
} ,
137
126
signal ?: AbortSignal
138
- ) : Promise < LeatherApiRegisterNotificationsResponse > {
127
+ ) {
139
128
return await this . cacheService . fetchWithCache (
140
129
[ 'leather-api-register-notifications' , addresses , notificationToken , chain ] ,
141
130
async ( ) => {
142
- const res = await axios . post < LeatherApiRegisterNotificationsResponse > (
143
- `${ LEATHER_API_URL } /v1/notifications/register` ,
144
- {
131
+ const { data } = await this . client . POST ( '/v1/notifications/register' , {
132
+ body : {
145
133
addresses,
146
134
notificationToken,
147
135
chain,
148
136
network : 'mainnet' ,
149
137
} ,
150
- { signal }
151
- ) ;
152
- return leatherApiRegisterNotificationsResponseSchema . parse ( res . data ) ;
138
+ signal,
139
+ } ) ;
140
+ return data ! ;
153
141
} ,
154
142
{ ttl : HttpCacheTimeMs . fiveSeconds }
155
143
) ;
0 commit comments