UNPKG

4.96 kBTypeScriptView Raw
1import type { CapacitorGlobal, PluginCallback, PluginResultData, PluginResultError } from './definitions';
2import type { CapacitorPlatformsInstance } from './platforms';
3export interface PluginHeaderMethod {
4 readonly name: string;
5 readonly rtype?: 'promise' | 'callback';
6}
7export interface PluginHeader {
8 readonly name: string;
9 readonly methods: readonly PluginHeaderMethod[];
10}
11/**
12 * Has all instance properties that are available and used
13 * by the native layer. The "Capacitor" interface it extends
14 * is the public one.
15 */
16export interface CapacitorInstance extends CapacitorGlobal {
17 /**
18 * Internal registry for all plugins assigned to the Capacitor global.
19 * Legacy Capacitor referenced this property directly, but as of v3
20 * it should be an internal API. Still exporting on the Capacitor
21 * type, but with the deprecated JSDoc tag.
22 */
23 Plugins: {
24 [pluginName: string]: {
25 [prop: string]: any;
26 };
27 };
28 PluginHeaders?: readonly PluginHeader[];
29 /**
30 * Gets the WebView server urls set by the native web view. Defaults
31 * to "" if not running from a native platform.
32 */
33 getServerUrl: () => string;
34 /**
35 * Low-level API to send data to the native layer.
36 * Prefer using `nativeCallback()` or `nativePromise()` instead.
37 * Returns the Callback Id.
38 */
39 toNative?: (pluginName: string, methodName: string, options: any, storedCallback?: StoredCallback) => string;
40 /**
41 * Sends data over the bridge to the native layer.
42 * Returns the Callback Id.
43 */
44 nativeCallback: <O>(pluginName: string, methodName: string, options?: O, callback?: PluginCallback) => string;
45 /**
46 * Sends data over the bridge to the native layer and
47 * resolves the promise when it receives the data from
48 * the native implementation.
49 */
50 nativePromise: <O, R>(pluginName: string, methodName: string, options?: O) => Promise<R>;
51 /**
52 * Low-level API used by the native layers to send
53 * data back to the webview runtime.
54 */
55 fromNative?: (result: PluginResult) => void;
56 /**
57 * Low-level API for backwards compatibility.
58 */
59 createEvent?: (eventName: string, eventData?: any) => Event;
60 /**
61 * Low-level API triggered from native implementations.
62 */
63 triggerEvent?: (eventName: string, target: string, eventData?: any) => boolean;
64 handleError: (err: Error) => void;
65 handleWindowError: (msg: string | Event, url: string, lineNo: number, columnNo: number, err: Error) => void;
66 /**
67 * Low-level API used by the native bridge to log messages.
68 */
69 logJs: (message: string, level: 'error' | 'warn' | 'info' | 'log') => void;
70 logToNative: (data: MessageCallData) => void;
71 logFromNative: (results: PluginResult) => void;
72 /**
73 * Low-level API used by the native bridge.
74 */
75 withPlugin?: (pluginName: string, fn: (...args: any[]) => any) => void;
76}
77export interface MessageCallData {
78 type?: 'message';
79 callbackId: string;
80 pluginId: string;
81 methodName: string;
82 options: any;
83}
84export interface ErrorCallData {
85 type: 'js.error';
86 error: {
87 message: string;
88 url: string;
89 line: number;
90 col: number;
91 errorObject: string;
92 };
93}
94export declare type CallData = MessageCallData | ErrorCallData;
95/**
96 * A resulting call back from the native layer.
97 */
98export interface PluginResult {
99 callbackId?: string;
100 methodName?: string;
101 data: PluginResultData;
102 success: boolean;
103 error?: PluginResultError;
104 pluginId?: string;
105 save?: boolean;
106}
107/**
108 * Callback data kept on the client
109 * to be called after native response
110 */
111export interface StoredCallback {
112 callback?: PluginCallback;
113 resolve?: (...args: any[]) => any;
114 reject?: (...args: any[]) => any;
115}
116export interface CapacitorCustomPlatformInstance {
117 name: string;
118 plugins: {
119 [pluginName: string]: any;
120 };
121}
122export interface WindowCapacitor {
123 Capacitor?: CapacitorInstance;
124 /**
125 * @deprecated Use `CapacitorCustomPlatform` instead
126 */
127 CapacitorPlatforms?: CapacitorPlatformsInstance;
128 CapacitorCustomPlatform?: CapacitorCustomPlatformInstance;
129 Ionic?: {
130 WebView?: {
131 getServerBasePath?: any;
132 setServerBasePath?: any;
133 persistServerBasePath?: any;
134 convertFileSrc?: any;
135 };
136 };
137 WEBVIEW_SERVER_URL?: string;
138 androidBridge?: {
139 postMessage(data: string): void;
140 };
141 webkit?: {
142 messageHandlers?: {
143 bridge: {
144 postMessage(data: any): void;
145 };
146 };
147 };
148 console?: Console;
149 cordova?: {
150 fireDocumentEvent?: (eventName: string, eventData: any) => void;
151 };
152 dispatchEvent?: any;
153 document?: any;
154 navigator?: {
155 app?: {
156 exitApp?: () => void;
157 };
158 };
159}