gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-wallet-core] branch master updated: webextension: introduce types


From: gnunet
Subject: [taler-wallet-core] branch master updated: webextension: introduce typesafe wallet-core API client
Date: Sun, 16 Oct 2022 23:11:37 +0200

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository wallet-core.

The following commit(s) were added to refs/heads/master by this push:
     new e4f2587ca webextension: introduce typesafe wallet-core API client
e4f2587ca is described below

commit e4f2587cabdcea92d2096114393aa1daca610671
Author: Florian Dold <florian@dold.me>
AuthorDate: Sun Oct 16 23:11:34 2022 +0200

    webextension: introduce typesafe wallet-core API client
---
 packages/taler-wallet-core/src/wallet-api-types.ts | 12 ++++---
 packages/taler-wallet-webextension/src/wxApi.ts    | 41 ++++++++++++++++++----
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/packages/taler-wallet-core/src/wallet-api-types.ts 
b/packages/taler-wallet-core/src/wallet-api-types.ts
index 88e66ff9e..354681734 100644
--- a/packages/taler-wallet-core/src/wallet-api-types.ts
+++ b/packages/taler-wallet-core/src/wallet-api-types.ts
@@ -707,17 +707,19 @@ export type WalletOperations = {
   [WalletApiOperation.SetDevMode]: SetDevModeOp;
 };
 
-export type RequestType<
+export type WalletCoreRequestType<
   Op extends WalletApiOperation & keyof WalletOperations,
 > = WalletOperations[Op] extends { request: infer T } ? T : never;
 
-export type ResponseType<
+export type WalletCoreResponseType<
   Op extends WalletApiOperation & keyof WalletOperations,
 > = WalletOperations[Op] extends { response: infer T } ? T : never;
 
+export type WalletCoreOpKeys = WalletApiOperation & keyof WalletOperations;
+
 export interface WalletCoreApiClient {
-  call<Op extends WalletApiOperation & keyof WalletOperations>(
+  call<Op extends WalletCoreOpKeys>(
     operation: Op,
-    payload: RequestType<Op>,
-  ): Promise<ResponseType<Op>>;
+    payload: WalletCoreRequestType<Op>,
+  ): Promise<WalletCoreResponseType<Op>>;
 }
diff --git a/packages/taler-wallet-webextension/src/wxApi.ts 
b/packages/taler-wallet-webextension/src/wxApi.ts
index 17083cb5b..ffb08e648 100644
--- a/packages/taler-wallet-webextension/src/wxApi.ts
+++ b/packages/taler-wallet-webextension/src/wxApi.ts
@@ -85,7 +85,12 @@ import {
   PendingOperationsResponse,
   RemoveBackupProviderRequest,
   TalerError,
+  WalletApiOperation,
   WalletContractData,
+  WalletCoreApiClient,
+  WalletCoreOpKeys,
+  WalletCoreRequestType,
+  WalletCoreResponseType,
 } from "@gnu-taler/taler-wallet-core";
 import { MessageFromBackend, platform } from "./platform/api.js";
 
@@ -121,6 +126,9 @@ export interface UpgradeResponse {
   oldDbVersion: string;
 }
 
+/**
+ * @deprecated Use {@link WxWalletCoreApiClient} instead.
+ */
 async function callBackend(operation: string, payload: any): Promise<any> {
   let response: CoreApiResponse;
   try {
@@ -136,13 +144,31 @@ async function callBackend(operation: string, payload: 
any): Promise<any> {
   return response.result;
 }
 
-/**
- * Start refreshing a coin.
- */
-export function refresh(coinPub: string): Promise<void> {
-  return callBackend("refresh-coin", { coinPub });
+export class WxWalletCoreApiClient implements WalletCoreApiClient {
+  async call<Op extends WalletCoreOpKeys>(
+    operation: Op,
+    payload: WalletCoreRequestType<Op>,
+  ): Promise<WalletCoreResponseType<Op>> {
+    let response: CoreApiResponse;
+    try {
+      response = await platform.sendMessageToWalletBackground(
+        operation,
+        payload,
+      );
+    } catch (e) {
+      console.log("Error calling backend");
+      throw new Error(`Error contacting backend: ${e}`);
+    }
+    logger.info("got response", response);
+    if (response.type === "error") {
+      throw TalerError.fromUncheckedDetail(response.error);
+    }
+    return response.result as any;
+  }
 }
 
+const wxClient = new WxWalletCoreApiClient();
+
 /**
  * Pay for a proposal.
  */
@@ -150,7 +176,10 @@ export function confirmPay(
   proposalId: string,
   sessionId: string | undefined,
 ): Promise<ConfirmPayResult> {
-  return callBackend("confirmPay", { proposalId, sessionId });
+  return wxClient.call(WalletApiOperation.ConfirmPay, {
+    proposalId,
+    sessionId,
+  });
 }
 
 /**

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]