From 67345aa82eff86a882eb3eb977c63613a84b9d18 Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Tue, 19 Aug 2025 22:02:17 +0700 Subject: [PATCH] feat(lib/modules/native): types for calls --- lib/modules/package.json | 3 +- .../src/{native.ts => native/index.ts} | 61 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) rename lib/modules/src/{native.ts => native/index.ts} (59%) diff --git a/lib/modules/package.json b/lib/modules/package.json index 007769b..b36edcf 100644 --- a/lib/modules/package.json +++ b/lib/modules/package.json @@ -19,7 +19,8 @@ "default": "./src/metro/utils.ts" }, "./native": { - "default": "./src/native.ts" + "default": "./src/native/index.ts" + }, }, "./types": { "types": "./src/types.ts" diff --git a/lib/modules/src/native.ts b/lib/modules/src/native/index.ts similarity index 59% rename from lib/modules/src/native.ts rename to lib/modules/src/native/index.ts index 72c669e..6867ac5 100644 --- a/lib/modules/src/native.ts +++ b/lib/modules/src/native/index.ts @@ -45,16 +45,22 @@ function makePayload(name: string, args: any[]): object { * @param args The arguments to pass to the native method. * @returns A promise that resolves with the result of the native method call. */ -export async function callMethod(name: string, args: any[]): Promise { - const result = await BridgePromise.readAsDataURL(makePayload(name, args)) +export async function callBridgeMethod( + name: N, + args: MethodArgs, +): Promise> { + try { + const result = await BridgePromise.readAsDataURL( + makePayload(name, args), + ) - if ('error' in result) - throw new Error(`Call failed: ${result.error as string}`) - if ('result' in result) return result.result as T + if ('error' in result) throw result.error + if ('result' in result) return result.result as MethodResult - throw new Error( - 'Call failed: The module did not return a valid result. The native hook must have failed.', - ) + throw 'The module did not return a valid result. The native hook must have failed.' + } catch (error) { + throw new Error(`Call failed: ${String(error)}`) + } } /** @@ -66,17 +72,46 @@ export async function callMethod(name: string, args: any[]): Promise { * @param args The arguments to pass to the native method. * @returns The result of the native method call. */ -export function callMethodSync(name: string, args: any[]): T { +export function callBridgeMethodSync( + name: N, + args: MethodArgs, +): MethodResult { try { const result = Bridge.getBBox(0, makePayload(name, args)) if ('error' in result) throw result.error - if ('result' in result) return result.result as T + if ('result' in result) return result.result as MethodResult throw 'The module did not return a valid result. The native hook must have failed.' } catch (error) { - throw new Error( - `Call failed: ${error instanceof Error ? error.message : String(error)}`, - ) + throw new Error(`Call failed: ${String(error)}`) } } + +/** + * Get the bridge information. + */ +export function getBridgeInfo(): BridgeInfo | null { + try { + return callBridgeMethodSync('revenge.info', []) + } catch (e) { + nativeLoggingHook( + `\u001b[31mFailed to get native bridge info: ${e}\u001b[0m`, + 2, + ) + return null + } +} + +export interface BridgeInfo { + name: string + version: number +} + +export type MethodName = Extract +export type MethodArgs = Methods[T][0] +export type MethodResult = Methods[T][1] + +export interface Methods { + 'revenge.info': [[], BridgeInfo] +}