Skip to content

Commit 61c3f2c

Browse files
committed
Merge branch 'main' into redesign-widget
2 parents 89c6af2 + 0844461 commit 61c3f2c

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

frontend/app/utils/validate.server.ts

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { z } from 'zod'
22
import { CornerType, PositionType, SlideAnimationType } from '@shared/types'
33
import {
44
checkHrefFormat,
5-
isWalletAddress,
5+
getWalletAddress,
66
toWalletAddressUrl,
77
WalletAddressFormatError
88
} from '@shared/utils'
99

10-
const rangeError = { message: 'Value has to be between 16 and 24' }
10+
const rangeError = { message: 'Value has to be between 10 and 30' }
1111

1212
export const walletSchema = z.object({
1313
walletAddress: z
@@ -19,7 +19,7 @@ export const walletSchema = z.object({
1919

2020
try {
2121
checkHrefFormat(updatedUrl)
22-
await isValidWalletAddress(updatedUrl)
22+
await getWalletAddress(updatedUrl)
2323
} catch (e) {
2424
ctx.addIssue({
2525
code: z.ZodIssueCode.custom,
@@ -138,33 +138,3 @@ export const validateForm = async (
138138

139139
return { result, payload }
140140
}
141-
142-
async function isValidWalletAddress(
143-
walletAddressUrl: string
144-
): Promise<boolean> {
145-
const response = await fetch(walletAddressUrl, {
146-
headers: {
147-
Accept: 'application/json'
148-
}
149-
})
150-
151-
if (!response.ok) {
152-
if (response.status === 404) {
153-
throw new WalletAddressFormatError('This wallet address does not exist.')
154-
}
155-
throw new WalletAddressFormatError('Failed to fetch wallet address.')
156-
}
157-
158-
const msgInvalidWalletAddress = 'Provided URL is not a valid wallet address.'
159-
const json = await response.json().catch((error) => {
160-
throw new WalletAddressFormatError(msgInvalidWalletAddress, {
161-
cause: error
162-
})
163-
})
164-
165-
if (!isWalletAddress(json as Record<string, unknown>)) {
166-
throw new WalletAddressFormatError(msgInvalidWalletAddress)
167-
}
168-
169-
return true
170-
}

shared/utils/index.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import type { WalletAddress } from '@interledger/open-payments'
22

3-
export function toWalletAddressUrl(s: string): string {
4-
return s.startsWith('$') ? s.replace('$', 'https://') : s
5-
}
6-
73
export async function getWalletAddress(
84
walletAddressUrl: string
95
): Promise<WalletAddress> {
106
const url = toWalletAddressUrl(walletAddressUrl)
117

12-
const res = await fetch(url)
13-
if (!res.ok) {
14-
throw new Error('Unable to fetch wallet details', {
15-
cause: new Error(res.statusText || `HTTP ${res.status}`)
8+
const response = await fetch(url)
9+
if (!response.ok) {
10+
if (response.status === 404) {
11+
throw new WalletAddressFormatError('This wallet address does not exist')
12+
}
13+
throw new WalletAddressFormatError('Unable to fetch wallet details', {
14+
cause: new WalletAddressFormatError(
15+
response.statusText || `HTTP ${response.status}`
16+
)
1617
})
1718
}
1819

20+
let json: Record<string, unknown>
1921
try {
20-
const json: Record<string, unknown> = await res.json()
21-
if (!isWalletAddress(json)) {
22-
throw new Error('Invalid wallet address format')
23-
}
24-
return json
22+
json = await response.json()
2523
} catch (error) {
26-
throw new Error('Failed to parse wallet address content', { cause: error })
24+
throw new WalletAddressFormatError(
25+
'Provided URL is not a valid wallet address',
26+
{
27+
cause: error
28+
}
29+
)
30+
}
31+
if (!isWalletAddress(json)) {
32+
throw new WalletAddressFormatError('Invalid wallet address format')
2733
}
34+
35+
return json
2836
}
2937

3038
export function isWalletAddress(
@@ -44,6 +52,10 @@ export function isWalletAddress(
4452
)
4553
}
4654

55+
export function toWalletAddressUrl(s: string): string {
56+
return s.startsWith('$') ? s.replace('$', 'https://') : s
57+
}
58+
4759
export function normalizeWalletAddress(walletAddress: WalletAddress): string {
4860
const IS_INTERLEDGER_CARDS =
4961
walletAddress.authServer === 'https://auth.interledger.cards'

0 commit comments

Comments
 (0)