From dd7c67a5d7bee803105e9370e020633e2a3a4131 Mon Sep 17 00:00:00 2001 From: Ivan Schurawel <30599893+is343@users.noreply.github.com> Date: Mon, 26 Aug 2019 20:50:06 +0900 Subject: [PATCH] plugin: add typescript type definitions Adds TypeScript type definitions to the plugin. PR-URL: https://github.com/JaneaSystems/nodejs-mobile-react-native/pull/12 Reviewed-By: Jaime Bernardo --- index.d.ts | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..7ad3127 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,70 @@ +declare module "nodejs-mobile-react-native" { + export interface NodeJs { + /** + * Starts the nodejs-mobile runtime thread with a file inside the nodejs-project directory + * @param scriptFileName + * @param options + */ + start: (scriptFileName: string, options?: StartupOptions) => void + /** + * Starts the nodejs-mobile runtime thread with a script body + * @param scriptBody + * @param options + */ + startWithScript: (scriptBody: string, options?: StartupOptions) => void + channel: Channel; + } + export interface Channel { + /** + * Registers a callback for user-defined events raised from the nodejs-mobile side + * @param event + * @param callback a function that accepts any JS type that can be serialized with JSON.stringify + * and deserialized with JSON.parse of the type: `boolean`, `number`, `string`, `object`, or `array` + * @param context + */ + addListener: (event: string, callback: ChannelCallback, context?: any) => void; + /** + * Removes the listener for the user-defined events raised from the nodejs-mobile side + * @param event + * @param callback a function that accepts any JS type that can be serialized with JSON.stringify + * and deserialized with JSON.parse of the type: `boolean`, `number`, `string`, `object`, or `array` + * @param context + */ + removeListener: (event: string, callback: ChannelCallback, context?: any) => void; + /** + * Raises a user-defined event on the nodejs-mobile side + * - accepts any JS type that can be serialized with JSON.stringify and deserialized with JSON.parse + * - can accept multiple message arguments + * @param event + * @param message can be of type: `boolean`, `number`, `string`, `object`, or `array` + */ + post: (event: string, ...message: any[]) => void; + /** + * Raises a `'message'` event on the nodejs-mobile side + * It is an alias for `nodejs.channel.post('message', ...message)` + * - accepts any JS type that can be serialized with JSON.stringify and deserialized with JSON.parse + * - can accept multiple message arguments + * @param message can be of type: `boolean`, `number`, `string`, `object`, or `array` + */ + send: (...message: any[]) => void; + } + + /** + * Handles messages sent through the nodejs-mobile channel. + * - accepts any JS type that can be serialized with JSON.stringify and deserialized with JSON.parse + * - can accept multiple arguments + * @param arg can be of type: `boolean`, `number`, `string`, `object`, or `array` + */ + export type ChannelCallback = (...arg: any[]) => void + + /** + * Optional options for `start` and `startWithScript` + */ + export interface StartupOptions { + redirectOutputToLogcat?: boolean + } + + const nodejs: NodeJs + + export default nodejs +}