export type SubscriptionType = 'SUB_APP'; // 目前支持sub-app平台 import { StandardResponse } from '@jolibox/types'; import { reportError } from '@/common/report/errors/report'; import { BaseError } from '@jolibox/common'; export type SubscriptionResult = StandardResponse; export interface SubscriptionHandlerMap { SUB_APP: (params: { productId: string; appStoreProductId: string; planId: string; }) => Promise>; // sub-app subscription } export type SubscriptionHandler = SubscriptionHandlerMap[T]; export function createSubscriptionHelper() { const subscriptionHandlers = new Map>(); return { registerSubscriptionHandler(type: T, handler: SubscriptionHandler) { subscriptionHandlers.set(type, handler); }, async invokeSubscription( type: T, ...args: Parameters> ) { const subscriptionHandler = subscriptionHandlers.get(type); if (!subscriptionHandler) { return { code: 'FAILED', message: `[joliboxSDK]: ${type} subscription not supported in this platform` }; } try { const result = await subscriptionHandler(...args); return result; } catch (e) { reportError(e as BaseError); return { code: 'FAILED', message: `[joliboxSDK]: subscription error ${e}` }; } } }; } export type SubscriptionHelper = ReturnType; export const subscriptionHelper = createSubscriptionHelper();