@portal-hq/utils 4.8.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,14 +3,43 @@ import {
3
3
  type HttpRequestOptions,
4
4
  type HttpRequesterOptions,
5
5
  } from '../../types'
6
+ import { HttpError } from '../errors'
6
7
  import { HttpRequest } from '../index'
7
8
  import { TRACE_ID_HEADER } from '../tracing'
8
9
 
9
10
  class HttpRequester {
10
11
  public baseUrl: string
11
12
 
12
- constructor({ baseUrl }: HttpRequesterOptions) {
13
+ private readonly onUnauthorized?: () => Promise<void>
14
+
15
+ constructor({ baseUrl, onUnauthorized }: HttpRequesterOptions) {
13
16
  this.baseUrl = baseUrl
17
+ this.onUnauthorized = onUnauthorized
18
+ }
19
+
20
+ /**
21
+ * Sends a request, notifying `onUnauthorized` on 401. Observational only:
22
+ * the hook's own failure is swallowed and the original `HttpError` is always
23
+ * what the caller sees.
24
+ */
25
+ private async send<T>(requestOptions: HttpRequestOptions): Promise<T> {
26
+ try {
27
+ return (await new HttpRequest(requestOptions).send()) as T
28
+ } catch (error) {
29
+ if (
30
+ this.onUnauthorized &&
31
+ error instanceof HttpError &&
32
+ error.status === 401
33
+ ) {
34
+ try {
35
+ await this.onUnauthorized()
36
+ } catch {
37
+ // Never let credential bookkeeping replace the original failure.
38
+ }
39
+ }
40
+
41
+ throw error
42
+ }
14
43
  }
15
44
 
16
45
  public buildHeaders(
@@ -35,10 +64,7 @@ class HttpRequester {
35
64
  options?.traceId,
36
65
  )
37
66
 
38
- const request = new HttpRequest(requestOptions)
39
- const response = (await request.send()) as T
40
-
41
- return response
67
+ return this.send<T>(requestOptions)
42
68
  }
43
69
 
44
70
  public async get<T>(path: string, options?: HttpOptions): Promise<T> {
@@ -52,10 +78,7 @@ class HttpRequester {
52
78
  options?.traceId,
53
79
  )
54
80
 
55
- const request = new HttpRequest(requestOptions)
56
- const response = (await request.send()) as T
57
-
58
- return response
81
+ return this.send<T>(requestOptions)
59
82
  }
60
83
 
61
84
  public async post<T>(path: string, options?: HttpOptions): Promise<T> {
@@ -73,10 +96,7 @@ class HttpRequester {
73
96
  requestOptions.body = options.body
74
97
  }
75
98
 
76
- const request = new HttpRequest(requestOptions)
77
- const response = (await request.send()) as T
78
-
79
- return response
99
+ return this.send<T>(requestOptions)
80
100
  }
81
101
 
82
102
  public async put<T>(path: string, options: HttpOptions): Promise<T> {
@@ -94,10 +114,7 @@ class HttpRequester {
94
114
  requestOptions.body = options.body
95
115
  }
96
116
 
97
- const request = new HttpRequest(requestOptions)
98
- const response = (await request.send()) as T
99
-
100
- return response
117
+ return this.send<T>(requestOptions)
101
118
  }
102
119
 
103
120
  public async patch<T>(path: string, options: HttpOptions): Promise<T> {
@@ -115,10 +132,7 @@ class HttpRequester {
115
132
  requestOptions.body = options.body
116
133
  }
117
134
 
118
- const request = new HttpRequest(requestOptions)
119
- const response = (await request.send()) as T
120
-
121
- return response
135
+ return this.send<T>(requestOptions)
122
136
  }
123
137
  }
124
138
 
@@ -1 +1 @@
1
- { "version": "4.8.0" }
1
+ { "version": "5.0.0" }
package/types.d.ts CHANGED
@@ -59,11 +59,24 @@ interface ClientResponseMetadata {
59
59
  interface ClientResponseMetadataNamespaces {
60
60
  eip155?: ClientResponseNamespaceMetadataItem
61
61
  solana?: ClientResponseNamespaceMetadataItem
62
+ bip122?: ClientResponseNamespaceMetadataItem
63
+ stellar?: ClientResponseNamespaceMetadataItem
64
+ tron?: ClientResponseNamespaceMetadataItem
62
65
  }
63
66
 
64
67
  interface ClientResponseNamespaceMetadataItem {
68
+ /**
69
+ * For `bip122` this is intentionally an empty string: Bitcoin has no single
70
+ * canonical address, so the usable addresses are sent under `bitcoin` instead.
71
+ */
65
72
  address: string
66
73
  curve: PortalCurve
74
+ bitcoin?: {
75
+ p2wpkh?: {
76
+ mainnet?: string
77
+ testnet?: string
78
+ }
79
+ }
67
80
  }
68
81
 
69
82
  interface ClientResponseSharePair {
@@ -132,9 +145,24 @@ export interface Shares {
132
145
  secp256k1: GenerateResponse
133
146
  }
134
147
 
148
+ export interface Bip122Addresses {
149
+ p2wpkh?: {
150
+ mainnet?: string
151
+ testnet?: string
152
+ }
153
+ }
154
+
135
155
  export interface AddressesByNamespace {
136
156
  eip155: string
137
157
  solana?: string
158
+ stellar?: string
159
+ tron?: string
160
+ /**
161
+ * Bitcoin has no single canonical address: the encoding is per-network and
162
+ * per-script-type, so addresses are keyed by script type and network rather
163
+ * than being a single string like the other namespaces.
164
+ */
165
+ bip122?: Bip122Addresses
138
166
  }
139
167
 
140
168
  export interface WalletsByCurve {
@@ -205,6 +233,12 @@ export interface HttpOptions {
205
233
 
206
234
  export interface HttpRequesterOptions extends RequesterOptions {
207
235
  baseUrl: string
236
+ /**
237
+ * Invoked when a request to this host returns 401. Opt-in, because this class
238
+ * also talks to third-party hosts (Google Drive, the gateway RPC) where a 401
239
+ * says nothing about the Portal credential. Cannot change the outcome.
240
+ */
241
+ onUnauthorized?: () => Promise<void>
208
242
  }
209
243
 
210
244
  export interface HttpRequestOptions {
@@ -239,31 +273,6 @@ export interface OutgoingRequest {
239
273
  data: OutgoingRequestData
240
274
  }
241
275
 
242
- export interface QuoteArgs {
243
- affiliateAddress?: string
244
- buyAmount?: number
245
- buyToken: string
246
- buyTokenPercentageFee?: number
247
- enableSlippageProtection?: boolean
248
- excludedSources?: string
249
- feeRecipient?: string
250
- gasPrice?: number
251
- includedSources?: string
252
- intentOnFilling?: boolean
253
- priceImpactProtectionPercentage?: number
254
- sellAmount?: number
255
- sellToken: string
256
- skipValidation?: boolean
257
- slippagePercentage?: number
258
- takerAddress?: string
259
- }
260
-
261
- export interface QuoteResponse {
262
- allowanceTarget: string
263
- cost: string
264
- transaction: Eip1559 | LegacyTx
265
- }
266
-
267
276
  export interface RequesterOptions {}
268
277
 
269
278
  export interface RpcErrorDefinition {