Sparkling Method SDK - TypeScript

The sparkling-method package provides the JS-side pipe for JS ↔ native method calls. It is the foundation that all Sparkling method packages (router, storage, media, etc.) are built on.

Install

npm install sparkling-method

Quick start

import pipe from 'sparkling-method';

// Callback style
pipe.call('router.open', { scheme: 'hybrid://lynxview_page?bundle=main.lynx.bundle' }, (res) => {
  console.log(res.code, res.msg);
});

// Async style
const data = await pipe.callAsync('storage.getItem', { key: 'token' });
console.log(data);

API

pipe.call(methodMap, params, callback, options?)

Call a native method with a callback.

ParamTypeDescription
methodMapstring | MethodMapMethod name (e.g. 'router.open') or { module, method } object
paramsTParamsRequest parameters passed to the native side
callback(response: unknown) => voidCallback invoked with the native response
optionsRecord<string, unknown>Reserved for future use

Example:

import pipe from 'sparkling-method';

pipe.call('storage.setItem', { key: 'lang', data: 'en' }, (res) => {
  console.log(res);
});

pipe.callAsync(methodMap, params, options?, timeout?)

Call a native method and return a Promise.

ParamTypeDefaultDescription
methodMapstring | MethodMap-Method name or { module, method } object
paramsTParams-Request parameters
optionsRecord<string, unknown>undefinedReserved for future use
timeoutnumber30000Timeout in milliseconds

Returns Promise<TResponse> — resolves with response.data when code === 0, rejects otherwise.

Example:

import pipe from 'sparkling-method';

try {
  const data = await pipe.callAsync('storage.getItem', { key: 'lang' });
  console.log(data); // { data: 'en' }
} catch (err) {
  console.error(err); // { code: number, msg: string }
}

pipe.callWithTimeout(methodMap, params, timeout, options?)

Convenience wrapper for callAsync with a custom timeout.

ParamTypeDescription
methodMapstring | MethodMapMethod name or { module, method } object
paramsTParamsRequest parameters
timeoutnumberTimeout in milliseconds
optionsRecord<string, unknown>Reserved for future use

Returns Promise<TResponse>.

pipe.on(eventName, callback)

Subscribe to a native event.

ParamTypeDescription
eventNamestringEvent name to listen for
callbackEventCallback(event: unknown) => void

Returns the EventCallback for use with off().

pipe.off(eventName, callback)

Unsubscribe from a native event.

ParamTypeDescription
eventNamestringEvent name
callbackEventCallbackThe callback previously passed to on()

Types

interface PipeResponse<T = unknown> {
  code: number;
  msg: string;
  data?: T;
}

interface PipeErrorResponse {
  code: number;
  msg: string;
  data?: undefined;
}

interface PipeCallOptions {
  timeout?: number;
  priority?: 'high' | 'normal' | 'low';
  retryCount?: number;
  retryDelay?: number;
}

type MethodMap = string | {
  module: string;
  method: string;
};

type EventCallback = (event: unknown) => void;

Response codes

CodeMeaning
0Success
Non-zeroError — check msg for details

Relationship with method packages

Sparkling method packages (sparkling-navigation, sparkling-storage, sparkling-media, etc.) are thin wrappers around sparkling-method. They call pipe.call() under the hood with pre-defined method names like router.open, storage.setItem, media.chooseMedia, etc.

You can use sparkling-method directly for:

  • Calling custom native methods you've implemented
  • Listening to native events
  • Building your own method packages