CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-capacitor--core

Cross-platform runtime library that enables web applications to run natively on iOS, Android, Web, and other platforms with unified JavaScript APIs

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

index.mddocs/

Capacitor Core

Capacitor Core is the foundational runtime library for Capacitor, a cross-platform framework that enables web applications to run natively on iOS, Android, Web, and other platforms. It provides a unified JavaScript API for accessing native device features and manages the bridge between web and native code implementations.

Package Information

  • Package Name: @capacitor/core
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @capacitor/core

Core Imports

import { Capacitor, registerPlugin } from "@capacitor/core";
import { WebPlugin } from "@capacitor/core";
import { CapacitorCookies, CapacitorHttp, WebView } from "@capacitor/core";

For CommonJS:

const { Capacitor, registerPlugin, WebPlugin } = require("@capacitor/core");

Basic Usage

import { Capacitor, registerPlugin } from "@capacitor/core";

// Check platform
const platform = Capacitor.getPlatform(); // 'android', 'ios', or 'web'
const isNative = Capacitor.isNativePlatform(); // true for native platforms

// Check plugin availability
const available = Capacitor.isPluginAvailable('Camera');

// Register a custom plugin
interface MyPlugin {
  echo(options: { value: string }): Promise<{ value: string }>;
}

const MyPlugin = registerPlugin<MyPlugin>('MyPlugin', {
  web: () => import('./web/my-plugin').then(m => new m.MyPluginWeb()),
});

// Use built-in HTTP client
import { CapacitorHttp } from "@capacitor/core";

const response = await CapacitorHttp.get({
  url: 'https://api.example.com/data',
  headers: {
    'Content-Type': 'application/json',
  },
});

Architecture

Capacitor Core is built around several key components:

  • Global Capacitor Object: Central runtime providing platform detection and plugin management
  • Plugin System: Architecture for registering and managing platform-specific implementations
  • WebPlugin Base Class: Foundation for implementing web-based plugin functionality
  • Built-in Core Plugins: Essential plugins for HTTP, cookies, and WebView management
  • Native Bridge: Communication layer between JavaScript and native platform code
  • Type System: Complete TypeScript definitions for type-safe development

Capabilities

Platform Runtime

Core platform detection, plugin management, and runtime functionality. Essential for all Capacitor applications.

interface CapacitorGlobal {
  getPlatform(): string;
  isNativePlatform(): boolean;
  isPluginAvailable(name: string): boolean;
  registerPlugin: RegisterPlugin;
  convertFileSrc(filePath: string): string;
  Exception: typeof CapacitorException;
  DEBUG?: boolean;
  isLoggingEnabled?: boolean;
}

type RegisterPlugin = <T>(
  pluginName: string, 
  implementations?: PluginImplementations
) => T;

Platform Runtime

Plugin Development

Base classes and utilities for developing custom Capacitor plugins with cross-platform support.

interface Plugin {
  addListener(eventName: string, listenerFunc: Function): Promise<PluginListenerHandle>;
  removeAllListeners(): Promise<void>;
}

class WebPlugin implements Plugin {
  addListener(eventName: string, listenerFunc: ListenerCallback): Promise<PluginListenerHandle>;
  removeAllListeners(): Promise<void>;
  protected notifyListeners(eventName: string, data: any, retainUntilConsumed?: boolean): void;
  protected unimplemented(msg?: string): CapacitorException;
  protected unavailable(msg?: string): CapacitorException;
}

Plugin Development

HTTP Client

Native HTTP client with web fallback for making network requests with advanced features like custom headers, timeouts, and response type handling.

interface CapacitorHttpPlugin {
  request(options: HttpOptions): Promise<HttpResponse>;
  get(options: HttpOptions): Promise<HttpResponse>;
  post(options: HttpOptions): Promise<HttpResponse>;
  put(options: HttpOptions): Promise<HttpResponse>;
  patch(options: HttpOptions): Promise<HttpResponse>;
  delete(options: HttpOptions): Promise<HttpResponse>;
}

interface HttpOptions {
  url: string;
  method?: string;
  data?: any;
  headers?: HttpHeaders;
  params?: HttpParams;
  readTimeout?: number;
  connectTimeout?: number;
  responseType?: HttpResponseType;
}

interface HttpResponse {
  data: any;
  status: number;
  headers: HttpHeaders;
  url: string;
}

function buildRequestInit(options: HttpOptions, extra?: RequestInit): RequestInit;

HTTP Client

Cookie Management

Cross-platform cookie management with native implementations and web fallbacks.

interface CapacitorCookiesPlugin {
  getCookies(options?: GetCookieOptions): Promise<HttpCookieMap>;
  setCookie(options: SetCookieOptions): Promise<void>;
  deleteCookie(options: DeleteCookieOptions): Promise<void>;
  clearCookies(options: ClearCookieOptions): Promise<void>;
  clearAllCookies(): Promise<void>;
}

type SetCookieOptions = {
  key: string;
  value: string;
  url?: string;
  path?: string;
  expires?: string;
};

Cookie Management

WebView Management

WebView configuration and path management for hybrid applications.

interface WebViewPlugin extends Plugin {
  setServerAssetPath(options: WebViewPath): Promise<void>;
  setServerBasePath(options: WebViewPath): Promise<void>;
  getServerBasePath(): Promise<WebViewPath>;
  persistServerBasePath(): Promise<void>;
}

interface WebViewPath {
  path: string;
}

WebView Management

Core Types

type PermissionState = 'prompt' | 'prompt-with-rationale' | 'granted' | 'denied';

interface PluginListenerHandle {
  remove(): Promise<void>;
}

type PluginCallback = (data: PluginResultData, error?: PluginResultError) => void;

type ListenerCallback = (err: any, ...args: any[]) => void;

interface PluginResultData {
  [key: string]: any;
}

interface PluginResultError {
  message: string;
}

interface PluginImplementations {
  [platform: string]: (() => Promise<any>) | any;
}

enum ExceptionCode {
  Unimplemented = 'UNIMPLEMENTED',
  Unavailable = 'UNAVAILABLE',
}

class CapacitorException extends Error {
  constructor(
    readonly message: string,
    readonly code?: ExceptionCode,
    readonly data?: ExceptionData,
  );
}

interface ExceptionData {
  [key: string]: any;
}

Install with Tessl CLI

npx tessl i tessl/npm-capacitor--core

docs

cookie-management.md

http-client.md

index.md

platform-runtime.md

plugin-development.md

webview-management.md

tile.json