webextension: introduce typesafe wallet-core API client
This commit is contained in:
parent
ea1aff81df
commit
e4f2587cab
@ -707,17 +707,19 @@ export type WalletOperations = {
|
|||||||
[WalletApiOperation.SetDevMode]: SetDevModeOp;
|
[WalletApiOperation.SetDevMode]: SetDevModeOp;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type RequestType<
|
export type WalletCoreRequestType<
|
||||||
Op extends WalletApiOperation & keyof WalletOperations,
|
Op extends WalletApiOperation & keyof WalletOperations,
|
||||||
> = WalletOperations[Op] extends { request: infer T } ? T : never;
|
> = WalletOperations[Op] extends { request: infer T } ? T : never;
|
||||||
|
|
||||||
export type ResponseType<
|
export type WalletCoreResponseType<
|
||||||
Op extends WalletApiOperation & keyof WalletOperations,
|
Op extends WalletApiOperation & keyof WalletOperations,
|
||||||
> = WalletOperations[Op] extends { response: infer T } ? T : never;
|
> = WalletOperations[Op] extends { response: infer T } ? T : never;
|
||||||
|
|
||||||
|
export type WalletCoreOpKeys = WalletApiOperation & keyof WalletOperations;
|
||||||
|
|
||||||
export interface WalletCoreApiClient {
|
export interface WalletCoreApiClient {
|
||||||
call<Op extends WalletApiOperation & keyof WalletOperations>(
|
call<Op extends WalletCoreOpKeys>(
|
||||||
operation: Op,
|
operation: Op,
|
||||||
payload: RequestType<Op>,
|
payload: WalletCoreRequestType<Op>,
|
||||||
): Promise<ResponseType<Op>>;
|
): Promise<WalletCoreResponseType<Op>>;
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,12 @@ import {
|
|||||||
PendingOperationsResponse,
|
PendingOperationsResponse,
|
||||||
RemoveBackupProviderRequest,
|
RemoveBackupProviderRequest,
|
||||||
TalerError,
|
TalerError,
|
||||||
|
WalletApiOperation,
|
||||||
WalletContractData,
|
WalletContractData,
|
||||||
|
WalletCoreApiClient,
|
||||||
|
WalletCoreOpKeys,
|
||||||
|
WalletCoreRequestType,
|
||||||
|
WalletCoreResponseType,
|
||||||
} from "@gnu-taler/taler-wallet-core";
|
} from "@gnu-taler/taler-wallet-core";
|
||||||
import { MessageFromBackend, platform } from "./platform/api.js";
|
import { MessageFromBackend, platform } from "./platform/api.js";
|
||||||
|
|
||||||
@ -121,6 +126,9 @@ export interface UpgradeResponse {
|
|||||||
oldDbVersion: string;
|
oldDbVersion: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated Use {@link WxWalletCoreApiClient} instead.
|
||||||
|
*/
|
||||||
async function callBackend(operation: string, payload: any): Promise<any> {
|
async function callBackend(operation: string, payload: any): Promise<any> {
|
||||||
let response: CoreApiResponse;
|
let response: CoreApiResponse;
|
||||||
try {
|
try {
|
||||||
@ -136,12 +144,30 @@ async function callBackend(operation: string, payload: any): Promise<any> {
|
|||||||
return response.result;
|
return response.result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export class WxWalletCoreApiClient implements WalletCoreApiClient {
|
||||||
* Start refreshing a coin.
|
async call<Op extends WalletCoreOpKeys>(
|
||||||
*/
|
operation: Op,
|
||||||
export function refresh(coinPub: string): Promise<void> {
|
payload: WalletCoreRequestType<Op>,
|
||||||
return callBackend("refresh-coin", { coinPub });
|
): 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.
|
* Pay for a proposal.
|
||||||
@ -150,7 +176,10 @@ export function confirmPay(
|
|||||||
proposalId: string,
|
proposalId: string,
|
||||||
sessionId: string | undefined,
|
sessionId: string | undefined,
|
||||||
): Promise<ConfirmPayResult> {
|
): Promise<ConfirmPayResult> {
|
||||||
return callBackend("confirmPay", { proposalId, sessionId });
|
return wxClient.call(WalletApiOperation.ConfirmPay, {
|
||||||
|
proposalId,
|
||||||
|
sessionId,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user