UNPKG

5.23 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 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 CapacitorCookiesAndroidInterface?: any;
125 CapacitorCookiesDescriptor?: PropertyDescriptor;
126 CapacitorHttpAndroidInterface?: any;
127 CapacitorWebFetch?: any;
128 CapacitorWebXMLHttpRequest?: any;
129 /**
130 * @deprecated Use `CapacitorCustomPlatform` instead
131 */
132 CapacitorPlatforms?: CapacitorPlatformsInstance;
133 CapacitorCustomPlatform?: CapacitorCustomPlatformInstance;
134 Ionic?: {
135 WebView?: {
136 getServerBasePath?: any;
137 setServerBasePath?: any;
138 persistServerBasePath?: any;
139 convertFileSrc?: any;
140 };
141 };
142 WEBVIEW_SERVER_URL?: string;
143 androidBridge?: {
144 postMessage(data: string): void;
145 onmessage?: (event: {
146 data: string;
147 }) => void;
148 };
149 webkit?: {
150 messageHandlers?: {
151 bridge: {
152 postMessage(data: any): void;
153 };
154 };
155 };
156 console?: Console;
157 cordova?: {
158 fireDocumentEvent?: (eventName: string, eventData: any) => void;
159 };
160 dispatchEvent?: any;
161 document?: any;
162 navigator?: {
163 app?: {
164 exitApp?: () => void;
165 };
166 };
167}