CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ionic-native--core

TypeScript foundation for Ionic Native plugin wrappers providing decorators, base classes, and utilities for Cordova/Capacitor integration

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

availability.mddocs/

Availability Checking

Ionic Native Core provides comprehensive runtime checking utilities to verify plugin and Cordova availability, ensuring graceful error handling when plugins are missing or misconfigured.

Capabilities

Plugin Availability Checking

/**
 * Checks if plugin/cordova is available and optionally if a specific method exists
 * @param plugin - Plugin instance, class, or plugin reference string
 * @param methodName - Optional method name to check
 * @param pluginName - Optional plugin name for error messages
 * @returns True if available, or error object describing the issue
 */
function checkAvailability(
  plugin: any | string,
  methodName?: string,
  pluginName?: string
): boolean | { error: string };

Usage Examples:

import { checkAvailability } from "@ionic-native/core";

// Check plugin availability by reference string
const cameraAvailable = checkAvailability("navigator.camera");
if (cameraAvailable === true) {
  // Plugin is available
} else {
  console.log("Camera not available:", cameraAvailable.error);
}

// Check specific method availability
const methodAvailable = checkAvailability("navigator.camera", "getPicture");
if (methodAvailable === true) {
  // Method is available
}

// Check using plugin instance
import { Camera } from "@ionic-native/camera";
const camera = new Camera();
const instanceAvailable = checkAvailability(camera, "getPicture", "Camera");

Instance Availability Checking

/**
 * Checks if _objectInstance exists and optionally has a specific method/property
 * Used for plugins that create object instances rather than static methods
 * @param pluginObj - Plugin object instance
 * @param methodName - Optional method name to check on the instance
 * @returns True if instance and method are available
 */
function instanceAvailability(
  pluginObj: any,
  methodName?: string
): boolean;

Usage Example:

import { instanceAvailability } from "@ionic-native/core";

class FileObject {
  private _objectInstance: any;
  
  someMethod() {
    if (instanceAvailability(this, "readAsText")) {
      // Instance method is available
      return this._objectInstance.readAsText();
    } else {
      throw new Error("Instance not available");
    }
  }
}

Lower-level Plugin Access

/**
 * Retrieves plugin object from global scope using dot notation
 * @param pluginRef - Plugin reference path (e.g., "cordova.plugins.Example")
 * @returns Plugin object or null if not found
 */
function getPlugin(pluginRef: string): any;

/**
 * Gets nested property from object using dot notation path
 * @param element - Root element (typically window)
 * @param path - Dot notation path to the property
 * @returns Property value or null if path doesn't exist
 */
function get(element: Element | Window, path: string): any;

Usage Examples:

import { getPlugin, get } from "@ionic-native/core";

// Get plugin object directly
const cameraPlugin = getPlugin("navigator.camera");
if (cameraPlugin) {
  cameraPlugin.getPicture(/* ... */);
}

// Get nested property from window
const cordova = get(window, "cordova.plugins.Example");

Error Constants

/** Error returned when Cordova framework is not available */
const ERR_CORDOVA_NOT_AVAILABLE: { error: "cordova_not_available" };

/** Error returned when specific plugin is not installed */
const ERR_PLUGIN_NOT_INSTALLED: { error: "plugin_not_installed" };

Usage Example:

import { 
  checkAvailability, 
  ERR_CORDOVA_NOT_AVAILABLE, 
  ERR_PLUGIN_NOT_INSTALLED 
} from "@ionic-native/core";

const result = checkAvailability("navigator.camera");
if (result === ERR_CORDOVA_NOT_AVAILABLE) {
  console.log("Cordova not available - running in browser?");
} else if (result === ERR_PLUGIN_NOT_INSTALLED) {
  console.log("Camera plugin not installed");
} else if (result === true) {
  console.log("Camera plugin is available");
}

Warning Functions

Internal functions used by the framework to provide helpful console warnings:

/**
 * Logs warning when plugin is not installed
 * @param pluginName - Display name of the plugin
 * @param plugin - NPM package name for installation
 * @param method - Optional method name that was called
 */
function pluginWarn(pluginName: string, plugin?: string, method?: string): void;

/**
 * Logs warning when Cordova is not available
 * @param pluginName - Display name of the plugin
 * @param method - Optional method name that was called
 */
function cordovaWarn(pluginName: string, method?: string): void;

These functions are automatically called by the decorator system to provide helpful console messages when plugins are unavailable.

Integration with Decorators

The availability checking system is automatically integrated with the decorator system. When you use @Cordova() or other decorators, they automatically call checkAvailability() before executing plugin methods:

export class Camera extends IonicNativePlugin {
  @Cordova()
  getPicture(): Promise<string> {
    return; // checkAvailability is called automatically
  }
}

// When called:
const camera = new Camera();
camera.getPicture().catch(error => {
  // Will receive appropriate error if plugin/cordova unavailable
});

Manual Availability Checking

For manual checking before using plugins:

import { Camera } from "@ionic-native/camera";
import { checkAvailability } from "@ionic-native/core";

const camera = new Camera();
const available = checkAvailability(camera);

if (available === true) {
  // Safe to use camera methods
  camera.getPicture().then(/* ... */);
} else {
  // Handle unavailable plugin
  this.showFallbackUI();
}

docs

availability.md

base-plugin.md

decorators.md

index.md

promise-observable.md

tile.json