Skip to content

Commit

Permalink
plugin: add typescript type definitions
Browse files Browse the repository at this point in the history
Adds TypeScript type definitions to the plugin.

PR-URL: #12
Reviewed-By: Jaime Bernardo <[email protected]>
  • Loading branch information
is343 authored and jaimecbernardo committed Aug 26, 2019
1 parent 71c401d commit dd7c67a
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit dd7c67a

Please sign in to comment.