aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/codec.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2024-04-15 12:01:16 -0300
committerSebastian <sebasjm@gmail.com>2024-04-15 12:01:16 -0300
commita7c8f0f3edd738a59d719105cda3aa8821886b90 (patch)
treeacb402075b7b0c0788ec0c5fbecfb58ece64357d /packages/taler-util/src/codec.ts
parent56a6c92c814547fcf8af25f183f6ecd75fbbfbb9 (diff)
downloadwallet-core-a7c8f0f3edd738a59d719105cda3aa8821886b90.tar.xz
fix #8604
Diffstat (limited to 'packages/taler-util/src/codec.ts')
-rw-r--r--packages/taler-util/src/codec.ts34
1 files changed, 34 insertions, 0 deletions
diff --git a/packages/taler-util/src/codec.ts b/packages/taler-util/src/codec.ts
index 701fc8835..678c3f092 100644
--- a/packages/taler-util/src/codec.ts
+++ b/packages/taler-util/src/codec.ts
@@ -361,6 +361,40 @@ export function codecForStringURL(shouldEndWithSlash?: boolean): Codec<string> {
}
/**
+ * Return a codec for a value that must be a string.
+ */
+export function codecForURL(shouldEndWithSlash?: boolean): Codec<URL> {
+ return {
+ decode(x: any, c?: Context): URL {
+ if (typeof x !== "string") {
+ throw new DecodingError(
+ `expected string at ${renderContext(c)} but got ${typeof x}`,
+ );
+ }
+ if (shouldEndWithSlash && !x.endsWith("/")) {
+ throw new DecodingError(
+ `expected URL string that ends with slash at ${renderContext(
+ c,
+ )} but got ${x}`,
+ );
+ }
+ try {
+ const url = new URL(x);
+ return url;
+ } catch (e) {
+ if (e instanceof Error) {
+ throw new DecodingError(e.message);
+ } else {
+ throw new DecodingError(
+ `expected an URL string at ${renderContext(c)} but got "${x}"`,
+ );
+ }
+ }
+ },
+ };
+}
+
+/**
* Codec that allows any value.
*/
export function codecForAny(): Codec<any> {