diff options
author | Sebastian <sebasjm@gmail.com> | 2023-10-30 15:27:25 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-10-30 15:27:25 -0300 |
commit | 768838285c25cbb1b171f645e8efb37a3c14273a (patch) | |
tree | 3404a7ea452a357baf4ebfc6c3b400f601849744 /packages/taler-util/src/payto.ts | |
parent | b7ba3119c1ff0d9ae3432cf0de1ef8cf92fc193c (diff) | |
download | wallet-core-768838285c25cbb1b171f645e8efb37a3c14273a.tar.xz |
local error impl: errors shown fixed position that are wiped when moved from the view
Diffstat (limited to 'packages/taler-util/src/payto.ts')
-rw-r--r-- | packages/taler-util/src/payto.ts | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/packages/taler-util/src/payto.ts b/packages/taler-util/src/payto.ts index 85870afcd..3df174944 100644 --- a/packages/taler-util/src/payto.ts +++ b/packages/taler-util/src/payto.ts @@ -15,6 +15,7 @@ */ import { generateFakeSegwitAddress } from "./bitcoin.js"; +import { Codec, Context, DecodingError, renderContext } from "./codec.js"; import { URLSearchParams } from "./url.js"; export type PaytoUri = @@ -23,6 +24,27 @@ export type PaytoUri = | PaytoUriTalerBank | PaytoUriBitcoin; +declare const __payto_str: unique symbol; +export type PaytoString = string & { [__payto_str]: true }; + +export function codecForPaytoString(): Codec<PaytoString> { + return { + decode(x: any, c?: Context): PaytoString { + if (typeof x !== "string") { + throw new DecodingError( + `expected string at ${renderContext(c)} but got ${typeof x}`, + ); + } + if (!x.startsWith(paytoPfx)) { + throw new DecodingError( + `expected start with payto at ${renderContext(c)} but got "${x}"`, + ); + } + return x as PaytoString; + }, + }; +} + export interface PaytoUriGeneric { targetType: PaytoType | string; targetPath: string; @@ -143,13 +165,13 @@ export function addPaytoQueryParams( * @param p * @returns */ -export function stringifyPaytoUri(p: PaytoUri): string { +export function stringifyPaytoUri(p: PaytoUri): PaytoString { const url = new URL(`${paytoPfx}${p.targetType}/${p.targetPath}`); const paramList = !p.params ? [] : Object.entries(p.params); paramList.forEach(([key, value]) => { url.searchParams.set(key, value); }); - return url.href; + return url.href as PaytoString; } /** |