From b85393de8926f87b2c1711d858ce0e439f1e6532 Mon Sep 17 00:00:00 2001 From: Ibby Date: Tue, 24 Jan 2017 15:12:35 -0500 Subject: [PATCH] chore(): add CordovaOptions interface --- src/plugins/plugin.ts | 125 +++++++++++++++++++++++++++++++----------- 1 file changed, 94 insertions(+), 31 deletions(-) diff --git a/src/plugins/plugin.ts b/src/plugins/plugin.ts index ba05da498..06aba56df 100644 --- a/src/plugins/plugin.ts +++ b/src/plugins/plugin.ts @@ -4,6 +4,98 @@ import { Observable } from 'rxjs/Observable'; declare var window; declare var Promise; +/** + * @private + */ +export interface PluginConfig { + /** + * Plugin name, this should match the class name + */ + pluginName: string; + /** + * Plugin NPM package name + */ + plugin: string; + /** + * Plugin object reference + */ + pluginRef: string; + /** + * Github repository URL + */ + repo: string; + /** + * Custom install command + */ + install?: string; + /** + * Supported platforms + */ + platforms?: string[]; +} + +/** + * @private + */ +export interface CordovaOptions { + /** + * Set to true if the wrapped method is a sync function + */ + sync?: boolean; + /** + * Callback order. Set to reverse if the success/error callbacks are the first 2 arguments that the wrapped method takes. + */ + callbackOrder?: 'reverse'; + /** + * Callback style + */ + callbackStyle?: 'node' | 'object'; + /** + * Set a custom index for the success callback function. This doesn't work if callbackOrder or callbackStyle are set. + */ + successIndex?: number; + /** + * Set a custom index for the error callback function. This doesn't work if callbackOrder or callbackStyle are set. + */ + errorIndex?: number; + /** + * Success function property name. This must be set if callbackStyle is set to object. + */ + successName?: string; + /** + * Error function property name. This must be set if callbackStyle is set to object. + */ + errorName?: string; + /** + * Set to true to return an observable + */ + observable?: boolean; + /** + * If observable is set to true, this can be set to a different function name that will cancel the observable. + */ + clearFunction?: string; + /** + * This can be used if clearFunction is set. Set this to true to call the clearFunction with the same arguments used in the initial function. + */ + clearWithArgs?: boolean; + /** + * Creates an observable that wraps a global event. Replaces document.addEventListener + */ + eventObservable?: boolean; + /** + * Event name, this must be set if eventObservable is set to true + */ + event?: string; + /** + * Set to true if the wrapped method returns a promise + */ + otherPromise?: boolean; + /** + * Supported platforms + */ + platforms?: string[]; +} + /** * @private * @param pluginRef @@ -303,7 +395,7 @@ function overrideFunction(pluginObj: any, methodName: string, args: any[], opts: * @param opts * @returns {function(...[any]): (undefined|*|Observable|*|*)} */ -export const wrap = function(pluginObj: any, methodName: string, opts: any = {}) { +export const wrap = function(pluginObj: any, methodName: string, opts: CordovaOptions = {}) { return (...args) => { if (opts.sync) { // Sync doesn't wrap the plugin with a promise or observable, it returns the result as-is @@ -320,35 +412,6 @@ export const wrap = function(pluginObj: any, methodName: string, opts: any = {}) }; }; -/** - * @private - */ -export interface PluginConfig { - /** - * Plugin name, this should match the class name - */ - pluginName: string; - /** - * Plugin NPM package name - */ - plugin: string; - /** - * Plugin object reference - */ - pluginRef: string; - /** - * Github repository URL - */ - repo: string; - /** - * Custom install command - */ - install?: string; - /** - * Supported platforms - */ - platforms?: string[]; -} /** * @private @@ -405,7 +468,7 @@ export function Plugin(config: PluginConfig) { * Wrap a stub function in a call to a Cordova plugin, checking if both Cordova * and the required plugin are installed. */ -export function Cordova(opts: any = {}) { +export function Cordova(opts: CordovaOptions = {}) { return (target: Object, methodName: string, descriptor: TypedPropertyDescriptor) => { return { value: function(...args: any[]) {