diff options
author | Florian Dold <florian@dold.me> | 2022-10-16 23:11:34 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-10-16 23:11:34 +0200 |
commit | e4f2587cabdcea92d2096114393aa1daca610671 (patch) | |
tree | e019962e332a1b8d6dbf702e72568e10b609294a /packages/taler-wallet-webextension | |
parent | ea1aff81df642000a1c458a91fc8aee239d3bd3a (diff) | |
download | wallet-core-e4f2587cabdcea92d2096114393aa1daca610671.tar.xz |
webextension: introduce typesafe wallet-core API client
Diffstat (limited to 'packages/taler-wallet-webextension')
-rw-r--r-- | packages/taler-wallet-webextension/src/wxApi.ts | 41 |
1 files changed, 35 insertions, 6 deletions
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, + }); } /** |