summaryrefslogtreecommitdiff
path: root/packages/taler-util/src/taleruri.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-util/src/taleruri.ts')
-rw-r--r--packages/taler-util/src/taleruri.ts63
1 files changed, 53 insertions, 10 deletions
diff --git a/packages/taler-util/src/taleruri.ts b/packages/taler-util/src/taleruri.ts
index 97b82c061..b56295c3f 100644
--- a/packages/taler-util/src/taleruri.ts
+++ b/packages/taler-util/src/taleruri.ts
@@ -27,7 +27,8 @@ import { Codec, Context, DecodingError, renderContext } from "./codec.js";
import { canonicalizeBaseUrl } from "./helpers.js";
import { AmountString } from "./taler-types.js";
import { URL, URLSearchParams } from "./url.js";
-
+import { opFixedSuccess, opKnownTalerFailure } from "./operation.js";
+import { TalerErrorCode } from "./taler-error-codes.js";
/**
* A parsed taler URI.
*/
@@ -130,15 +131,17 @@ export interface WithdrawExchangeUri {
* Parse a taler[+http]://withdraw URI.
* Return undefined if not passed a valid URI.
*/
-export function parseWithdrawUri(s: string): WithdrawUriResult | undefined {
- const pi = parseProtoInfo(s, "withdraw");
- if (!pi) {
- return undefined;
+export function parseWithdrawUriWithError(s: string) {
+ const pi = parseProtoInfoWithError(s, "withdraw");
+ if (pi.type === "fail") {
+ return pi;
}
- const parts = pi.rest.split("/");
+ const parts = pi.body.rest.split("/");
if (parts.length < 2) {
- return undefined;
+ return opKnownTalerFailure(TalerErrorCode.TALER_URI_NO_ENOUGH_COMPONENT, {
+ code: TalerErrorCode.TALER_URI_NO_ENOUGH_COMPONENT
+ });
}
const host = parts[0].toLowerCase();
@@ -153,11 +156,22 @@ export function parseWithdrawUri(s: string): WithdrawUriResult | undefined {
const withdrawId = parts[parts.length - 1];
const p = [host, ...pathSegments].join("/");
- return {
+ const result: WithdrawUriResult = {
type: TalerUriAction.Withdraw,
- bankIntegrationApiBaseUrl: canonicalizeBaseUrl(`${pi.innerProto}://${p}/`),
+ bankIntegrationApiBaseUrl: canonicalizeBaseUrl(`${pi.body.innerProto}://${p}/`),
withdrawalOperationId: withdrawId,
- };
+ }
+ return opFixedSuccess(result);
+}
+
+/**
+ *
+ * @deprecated use parseWithdrawUriWithError
+ */
+export function parseWithdrawUri(s: string): WithdrawUriResult | undefined {
+ const r = parseWithdrawUriWithError(s);
+ if (r.type === "fail") return undefined;
+ return r.body;
}
/**
@@ -215,6 +229,35 @@ function parseProtoInfo(
}
}
+function parseProtoInfoWithError(
+ s: string,
+ action: string,
+) {
+ if (!s.toLocaleLowerCase().startsWith("taler://") ||
+ !s.toLocaleLowerCase().startsWith("taler+http://")) {
+ return opKnownTalerFailure(TalerErrorCode.TALER_URI_PREFIX, {
+ code: TalerErrorCode.TALER_URI_PREFIX,
+ });
+ }
+ const pfxPlain = `taler://${action}/`;
+ const pfxHttp = `taler+http://${action}/`;
+ if (s.toLowerCase().startsWith(pfxPlain)) {
+ return opFixedSuccess({
+ innerProto: "https",
+ rest: s.substring(pfxPlain.length),
+ });
+ } else if (s.toLowerCase().startsWith(pfxHttp)) {
+ return opFixedSuccess({
+ innerProto: "http",
+ rest: s.substring(pfxHttp.length),
+ });
+ } else {
+ return opKnownTalerFailure(TalerErrorCode.TALER_URI_UNKOWN_ACTION, {
+ code: TalerErrorCode.TALER_URI_UNKOWN_ACTION,
+ });
+ }
+}
+
type Parser = (s: string) => TalerUri | undefined;
const parsers: { [A in TalerUriAction]: Parser } = {
[TalerUriAction.Pay]: parsePayUri,