All files / src/connect popup-helper.ts

21.42% Statements 3/14
0% Branches 0/14
50% Functions 1/2
23.07% Lines 3/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  4x   4x     12x                                                
import { StacksProvider } from './common/provider';
import { getStacksProvider } from './common/get-stacks-provider';
 
export function genericTransactionPopupFactory<OnFinishedPayload, ErrorMessagePayload = string>(
  method: keyof StacksProvider
) {
  return async function fn(options: {
    token: string;
    onFinish?: (payload: OnFinishedPayload) => void;
    onCancel?: (errorMessage?: ErrorMessagePayload) => void;
  }) {
    const { token, onFinish, onCancel } = options;
    try {
      const Provider: StacksProvider | undefined = getStacksProvider();
      Iif (!Provider) throw new Error('[micro-stacks/connect] No Stacks provider');
 
      const fn = Provider[method];
 
      Iif (typeof fn !== 'function')
        throw new Error(`[micro-stacks/connect] StacksProvider method ${method} not found`);
 
      const responsePayload = await fn(token);
 
      onFinish?.(responsePayload as unknown as OnFinishedPayload);
    } catch (e) {
      // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
      onCancel?.((e as unknown as any)?.message);
    }
  };
}