@cap-kit/test-plugin 0.1.2 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +63 -31
  2. package/android/build.gradle +1 -1
  3. package/android/src/main/java/io/capkit/test/Test.kt +61 -0
  4. package/android/src/main/java/io/capkit/test/TestConfig.kt +51 -0
  5. package/android/src/main/java/io/capkit/test/TestPlugin.kt +116 -0
  6. package/android/src/main/java/io/capkit/test/utils/Logger.kt +85 -0
  7. package/android/src/main/java/io/capkit/test/utils/TestUtils.kt +14 -0
  8. package/dist/docs.json +57 -21
  9. package/dist/esm/definitions.d.ts +137 -35
  10. package/dist/esm/definitions.js +23 -1
  11. package/dist/esm/definitions.js.map +1 -1
  12. package/dist/esm/index.d.ts +4 -4
  13. package/dist/esm/index.js +4 -16
  14. package/dist/esm/index.js.map +1 -1
  15. package/dist/esm/web.d.ts +16 -27
  16. package/dist/esm/web.js +25 -28
  17. package/dist/esm/web.js.map +1 -1
  18. package/dist/plugin.cjs.js +54 -39
  19. package/dist/plugin.cjs.js.map +1 -1
  20. package/dist/plugin.js +54 -39
  21. package/dist/plugin.js.map +1 -1
  22. package/ios/Sources/TestPlugin/Test.swift +40 -8
  23. package/ios/Sources/TestPlugin/TestConfig.swift +52 -9
  24. package/ios/Sources/TestPlugin/TestPlugin.swift +83 -34
  25. package/ios/Sources/TestPlugin/Utils/Logging.swift +57 -0
  26. package/ios/Sources/TestPlugin/Utils/TestUtils.swift +12 -0
  27. package/package.json +9 -9
  28. package/android/src/main/java/com/capkit/test/Logger.kt +0 -25
  29. package/android/src/main/java/com/capkit/test/Test.kt +0 -20
  30. package/android/src/main/java/com/capkit/test/TestConfig.kt +0 -16
  31. package/android/src/main/java/com/capkit/test/TestPlugin.kt +0 -60
  32. package/ios/Sources/TestPlugin/Logging.swift +0 -32
package/README.md CHANGED
@@ -45,9 +45,10 @@ npx cap sync
45
45
 
46
46
  Configuration options for the Test plugin.
47
47
 
48
- | Prop | Type | Description | Default | Since |
49
- | ------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ----- |
50
- | **`customMessage`** | <code>string</code> | A custom message to append to the echo response. This demonstrates how to pass data from `capacitor.config.ts` to the plugin. | <code>" (from config)"</code> | 0.0.1 |
48
+ | Prop | Type | Description | Default | Since |
49
+ | -------------------- | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------- | ----- |
50
+ | **`customMessage`** | <code>string</code> | Custom message appended to the echoed value. This option exists mainly as an example showing how to pass static configuration data from JavaScript to native platforms. | <code>" (from config)"</code> | 0.0.1 |
51
+ | **`verboseLogging`** | <code>boolean</code> | Enables verbose native logging. When enabled, additional debug information is printed to the native console (Logcat on Android, Xcode on iOS). This option affects native logging behavior only and has no impact on the JavaScript API. | <code>false</code> | 1.0.0 |
51
52
 
52
53
  ### Examples
53
54
 
@@ -57,7 +58,8 @@ In `capacitor.config.json`:
57
58
  {
58
59
  "plugins": {
59
60
  "Test": {
60
- "customMessage": " - Hello from Config!"
61
+ "customMessage": " - Hello from Config!",
62
+ "verboseLogging": true
61
63
  }
62
64
  }
63
65
  }
@@ -74,6 +76,7 @@ const config: CapacitorConfig = {
74
76
  plugins: {
75
77
  Test: {
76
78
  customMessage: ' - Hello from Config!',
79
+ verboseLogging: true,
77
80
  },
78
81
  },
79
82
  };
@@ -88,6 +91,7 @@ export default config;
88
91
  <docgen-index>
89
92
 
90
93
  * [`echo(...)`](#echo)
94
+ * [`openAppSettings()`](#openappsettings)
91
95
  * [`getPluginVersion()`](#getpluginversion)
92
96
  * [Interfaces](#interfaces)
93
97
 
@@ -96,7 +100,10 @@ export default config;
96
100
  <docgen-api>
97
101
  <!--Update the source file JSDoc comments and rerun docgen to update the docs below-->
98
102
 
99
- Capacitor Test plugin interface.
103
+ Public JavaScript API for the Test Capacitor plugin.
104
+
105
+ This interface defines a stable, platform-agnostic API.
106
+ All methods behave consistently across Android, iOS, and Web.
100
107
 
101
108
  ### echo(...)
102
109
 
@@ -106,12 +113,15 @@ echo(options: EchoOptions) => Promise<EchoResult>
106
113
 
107
114
  Echoes the provided value.
108
115
 
109
- If the plugin is configured with a `customMessage`, it will be appended
110
- to the response.
116
+ If the plugin is configured with a `customMessage`, that value
117
+ will be appended to the returned string.
118
+
119
+ This method is primarily intended as an example demonstrating
120
+ native ↔ JavaScript communication.
111
121
 
112
- | Param | Type | Description |
113
- | ------------- | --------------------------------------------------- | ------------------------------------------- |
114
- | **`options`** | <code><a href="#echooptions">EchoOptions</a></code> | - The options containing the value to echo. |
122
+ | Param | Type | Description |
123
+ | ------------- | --------------------------------------------------- | ------------------------------------ |
124
+ | **`options`** | <code><a href="#echooptions">EchoOptions</a></code> | Object containing the value to echo. |
115
125
 
116
126
  **Returns:** <code>Promise&lt;<a href="#echoresult">EchoResult</a>&gt;</code>
117
127
 
@@ -119,13 +129,29 @@ to the response.
119
129
 
120
130
  #### Example
121
131
 
122
- ```typescript
123
- import { Test } from '@cap-kit/test-plugin';
132
+ ```ts
133
+ const { value } = await Test.echo({ value: 'Hello' });
134
+ console.log(value);
135
+ ```
124
136
 
125
- const result = await Test.echo({ value: 'Hello, World!' });
126
- console.log(result.value); // Output: 'Hello, World!'
137
+ --------------------
138
+
139
+
140
+ ### openAppSettings()
141
+
142
+ ```typescript
143
+ openAppSettings() => Promise<void>
127
144
  ```
128
145
 
146
+ Opens the operating system's application settings page.
147
+
148
+ This is typically used when a permission has been permanently
149
+ denied and the user must enable it manually.
150
+
151
+ On Web, this method is not supported.
152
+
153
+ **Since:** 1.0.0
154
+
129
155
  --------------------
130
156
 
131
157
 
@@ -135,7 +161,10 @@ console.log(result.value); // Output: 'Hello, World!'
135
161
  getPluginVersion() => Promise<PluginVersionResult>
136
162
  ```
137
163
 
138
- Get the native Capacitor plugin version.
164
+ Returns the native plugin version.
165
+
166
+ The returned version corresponds to the native implementation
167
+ bundled with the application.
139
168
 
140
169
  **Returns:** <code>Promise&lt;<a href="#pluginversionresult">PluginVersionResult</a>&gt;</code>
141
170
 
@@ -143,11 +172,8 @@ Get the native Capacitor plugin version.
143
172
 
144
173
  #### Example
145
174
 
146
- ```typescript
147
- import { Test } from '@cap-kit/test-plugin';
148
-
175
+ ```ts
149
176
  const { version } = await Test.getPluginVersion();
150
- console.log('Plugin version:', version); // Output: Plugin version: 0.0.1
151
177
  ```
152
178
 
153
179
  --------------------
@@ -158,29 +184,35 @@ console.log('Plugin version:', version); // Output: Plugin version: 0.0.1
158
184
 
159
185
  #### EchoResult
160
186
 
161
- Result returned by the echo method.
187
+ Result object returned by the `echo` method.
162
188
 
163
- | Prop | Type | Description |
164
- | ----------- | ------------------- | ----------------- |
165
- | **`value`** | <code>string</code> | The echoed value. |
189
+ This object represents the resolved value of the echo operation
190
+ after native processing has completed.
191
+
192
+ | Prop | Type | Description |
193
+ | ----------- | ------------------- | ------------------------------------------------------------------------------------------------------------- |
194
+ | **`value`** | <code>string</code> | The echoed string value. If a `customMessage` is configured, it will be appended to the original input value. |
166
195
 
167
196
 
168
197
  #### EchoOptions
169
198
 
170
- Options for the echo method.
199
+ Options object for the `echo` method.
200
+
201
+ This object defines the input payload sent from JavaScript
202
+ to the native plugin implementation.
171
203
 
172
- | Prop | Type | Description |
173
- | ----------- | ------------------- | ----------------------- |
174
- | **`value`** | <code>string</code> | The value to be echoed. |
204
+ | Prop | Type | Description |
205
+ | ----------- | ------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
206
+ | **`value`** | <code>string</code> | The string value to be echoed back by the plugin. This value is passed to the native layer and returned unchanged, optionally with a configuration-based suffix. |
175
207
 
176
208
 
177
209
  #### PluginVersionResult
178
210
 
179
- Result returned by the getPluginVersion method.
211
+ Result object returned by the `getPluginVersion()` method.
180
212
 
181
- | Prop | Type | Description |
182
- | ------------- | ------------------- | ---------------------------------------- |
183
- | **`version`** | <code>string</code> | The native version string of the plugin. |
213
+ | Prop | Type | Description |
214
+ | ------------- | ------------------- | --------------------------------- |
215
+ | **`version`** | <code>string</code> | The native plugin version string. |
184
216
 
185
217
  </docgen-api>
186
218
 
@@ -46,7 +46,7 @@ def getPluginVersion() {
46
46
  def pluginVersion = getPluginVersion()
47
47
 
48
48
  android {
49
- namespace = "com.capkit.test"
49
+ namespace = "io.capkit.test"
50
50
  compileSdk = project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion as Integer : 36
51
51
 
52
52
  // AGP 8.0+ disables BuildConfig by default for libraries.
@@ -0,0 +1,61 @@
1
+ package io.capkit.test
2
+
3
+ import android.content.Context
4
+ import io.capkit.test.utils.Logger
5
+
6
+ /**
7
+ * Platform-specific native implementation for the Test plugin.
8
+ *
9
+ * This class contains pure Android logic and MUST NOT depend
10
+ * directly on Capacitor bridge APIs.
11
+ *
12
+ * The Capacitor plugin class is responsible for:
13
+ * - reading configuration
14
+ * - handling PluginCall objects
15
+ * - delegating logic to this implementation
16
+ */
17
+ class Test(
18
+ private val context: Context,
19
+ ) {
20
+ /**
21
+ * Cached plugin configuration.
22
+ * Provided once during initialization.
23
+ */
24
+ private lateinit var config: TestConfig
25
+
26
+ /**
27
+ * Applies the plugin configuration.
28
+ *
29
+ * This method should be called exactly once during plugin load
30
+ * and is responsible for translating configuration values into
31
+ * runtime behavior (e.g. logging verbosity).
32
+ */
33
+ fun updateConfig(newConfig: TestConfig) {
34
+ this.config = newConfig
35
+ Logger.verbose = this.config.verboseLogging
36
+ Logger.debug("Configuration updated. Verbose logging: ${this.config.verboseLogging}")
37
+ }
38
+
39
+ /**
40
+ * Echoes the provided value.
41
+ *
42
+ * This method represents a simple synchronous native operation
43
+ * and is intentionally side-effect free.
44
+ */
45
+ fun echo(value: String): String {
46
+ Logger.debug(value)
47
+ return value
48
+ }
49
+
50
+ companion object {
51
+ /**
52
+ * Account type identifier (example constant).
53
+ */
54
+ const val ACCOUNT_TYPE = "io.capkit.test"
55
+
56
+ /**
57
+ * Human-readable account name (example constant).
58
+ */
59
+ const val ACCOUNT_NAME = "Test"
60
+ }
61
+ }
@@ -0,0 +1,51 @@
1
+ package io.capkit.test
2
+
3
+ import android.content.Context
4
+ import com.getcapacitor.Plugin
5
+
6
+ /**
7
+ * Plugin configuration holder.
8
+ *
9
+ * This class is responsible for reading and parsing configuration values
10
+ * defined under the `Test` key in `capacitor.config.ts`.
11
+ *
12
+ * Configuration is read once during plugin initialization and treated as
13
+ * immutable runtime input.
14
+ */
15
+ class TestConfig(plugin: Plugin) {
16
+ /**
17
+ * Android application context.
18
+ * Exposed for native components that may require it.
19
+ */
20
+ val context: Context
21
+
22
+ /**
23
+ * Enables verbose / debug logging for the plugin.
24
+ *
25
+ * When enabled, additional logs are printed to Logcat via [Logger.debug].
26
+ *
27
+ * Default: false
28
+ */
29
+ val verboseLogging: Boolean
30
+
31
+ /**
32
+ * Optional custom message appended to echoed values.
33
+ *
34
+ * This value is provided for demonstration purposes and shows how to
35
+ * pass static configuration from JavaScript to native code.
36
+ */
37
+ val customMessage: String
38
+
39
+ init {
40
+ context = plugin.context
41
+
42
+ val config = plugin.getConfig()
43
+
44
+ // Parse verboseLogging (default: false)
45
+ verboseLogging = config.getBoolean("verboseLogging", false)
46
+
47
+ // Parse customMessage (default fallback applied)
48
+ val cm = config.getString("customMessage")
49
+ customMessage = if (cm.isNullOrBlank()) " (from config)" else cm
50
+ }
51
+ }
@@ -0,0 +1,116 @@
1
+ package io.capkit.test
2
+
3
+ import android.content.Intent
4
+ import android.net.Uri
5
+ import android.provider.Settings
6
+ import androidx.activity.result.ActivityResult
7
+ import com.getcapacitor.JSObject
8
+ import com.getcapacitor.Plugin
9
+ import com.getcapacitor.PluginCall
10
+ import com.getcapacitor.PluginMethod
11
+ import com.getcapacitor.annotation.ActivityCallback
12
+ import com.getcapacitor.annotation.CapacitorPlugin
13
+ import io.capkit.test.utils.Logger
14
+
15
+ /**
16
+ * Capacitor bridge for the Test plugin.
17
+ *
18
+ * This class acts as the boundary between JavaScript and native Android code.
19
+ * It is responsible for:
20
+ * - reading configuration
21
+ * - validating PluginCall input
22
+ * - mapping JS calls to native logic
23
+ */
24
+ @CapacitorPlugin(
25
+ name = "Test",
26
+ )
27
+ class TestPlugin : Plugin() {
28
+ /**
29
+ * Plugin configuration parsed from capacitor.config.ts.
30
+ */
31
+ private lateinit var config: TestConfig
32
+
33
+ /**
34
+ * Native implementation containing platform logic.
35
+ */
36
+ private lateinit var implementation: Test
37
+
38
+ /**
39
+ * Called once when the plugin is loaded by the Capacitor bridge.
40
+ *
41
+ * This is the correct place to:
42
+ * - read configuration
43
+ * - initialize native resources
44
+ * - inject dependencies into the implementation
45
+ */
46
+ override fun load() {
47
+ super.load()
48
+
49
+ config = TestConfig(this)
50
+ implementation = Test(context)
51
+ implementation.updateConfig(config)
52
+ }
53
+
54
+ // --- ---
55
+
56
+ /**
57
+ * Echoes a string back to JavaScript.
58
+ *
59
+ * @param call Capacitor plugin call containing the `value` parameter.
60
+ */
61
+ @PluginMethod
62
+ fun echo(call: PluginCall) {
63
+ var value = call.getString("value") ?: ""
64
+ Logger.debug("Echoing value: $value")
65
+
66
+ // Append the custom message from the configuration
67
+ value += config.customMessage
68
+
69
+ val ret = JSObject()
70
+ ret.put("value", implementation.echo(value))
71
+ call.resolve(ret)
72
+ }
73
+
74
+ // --- Version ---
75
+
76
+ /**
77
+ * Returns the plugin version.
78
+ */
79
+ @PluginMethod
80
+ fun getPluginVersion(call: PluginCall) {
81
+ val ret = JSObject()
82
+ ret.put("version", BuildConfig.PLUGIN_VERSION)
83
+ call.resolve(ret)
84
+ }
85
+
86
+ // --- Settings ---
87
+
88
+ /**
89
+ * Opens the application details settings page.
90
+ * Allowing the user to manually enable permissions.
91
+ */
92
+ @PluginMethod
93
+ fun openAppSettings(call: PluginCall) {
94
+ try {
95
+ val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
96
+ val uri = Uri.fromParts("package", context.packageName, null)
97
+ intent.data = uri
98
+ startActivityForResult(call, intent, "openSettingsResult")
99
+ call.resolve()
100
+ } catch (e: Exception) {
101
+ call.reject(
102
+ "Failed to open settings",
103
+ "UNAVAILABLE",
104
+ )
105
+ }
106
+ }
107
+
108
+ @ActivityCallback
109
+ private fun openSettingsResult(
110
+ call: PluginCall,
111
+ result: ActivityResult,
112
+ ) {
113
+ // No-op, just to satisfy the callback requirement if needed
114
+ call.resolve()
115
+ }
116
+ }
@@ -0,0 +1,85 @@
1
+ package io.capkit.test.utils
2
+
3
+ import android.util.Log
4
+
5
+ /**
6
+ * Centralized logging utility for the Test plugin.
7
+ *
8
+ * This logger provides a single entry point for all native logs
9
+ * and supports runtime-controlled verbose logging.
10
+ *
11
+ * The goal is to avoid scattering `if (verbose)` checks across
12
+ * business logic and keep logging behavior consistent.
13
+ */
14
+ object Logger {
15
+ /**
16
+ * Logcat tag used for all plugin logs.
17
+ * Helps filtering logs during debugging.
18
+ */
19
+ private const val TAG = "⚡️ Test"
20
+
21
+ /**
22
+ * Controls whether debug logs are printed.
23
+ *
24
+ * This flag should be set once during plugin initialization
25
+ * based on configuration values.
26
+ */
27
+ var verbose: Boolean = false
28
+
29
+ /**
30
+ * Prints a debug / verbose log message.
31
+ *
32
+ * This method should be used for development-time diagnostics
33
+ * and is automatically silenced when [verbose] is false.
34
+ *
35
+ * @param messages One or more message fragments to be concatenated.
36
+ */
37
+ fun debug(vararg messages: String) {
38
+ if (verbose) {
39
+ log(TAG, Log.DEBUG, *messages)
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Prints an error log message.
45
+ *
46
+ * Error logs are always printed regardless of [verbose] state.
47
+ *
48
+ * @param message Human-readable error description.
49
+ * @param e Optional exception for stack trace logging.
50
+ */
51
+ fun error(
52
+ message: String,
53
+ e: Throwable? = null,
54
+ ) {
55
+ val sb = StringBuilder(message)
56
+ if (e != null) {
57
+ sb.append(" | Error: ").append(e.message)
58
+ }
59
+ Log.e(TAG, sb.toString(), e)
60
+ }
61
+
62
+ /**
63
+ * Internal low-level log dispatcher.
64
+ *
65
+ * Joins message fragments and forwards them to Android's Log API
66
+ * using the specified priority.
67
+ */
68
+ fun log(
69
+ tag: String,
70
+ level: Int,
71
+ vararg messages: String,
72
+ ) {
73
+ val sb = StringBuilder()
74
+ for (msg in messages) {
75
+ sb.append(msg).append(" ")
76
+ }
77
+ when (level) {
78
+ Log.DEBUG -> Log.d(tag, sb.toString())
79
+ Log.INFO -> Log.i(tag, sb.toString())
80
+ Log.WARN -> Log.w(tag, sb.toString())
81
+ Log.ERROR -> Log.e(tag, sb.toString())
82
+ else -> Log.v(tag, sb.toString())
83
+ }
84
+ }
85
+ }
@@ -0,0 +1,14 @@
1
+ package io.capkit.test.utils
2
+
3
+ /**
4
+ * Utility helpers for the Test plugin.
5
+ *
6
+ * This object is intentionally empty and serves as a placeholder
7
+ * for future shared utility functions.
8
+ *
9
+ * Keeping a dedicated utils package helps maintain a clean
10
+ * separation between core logic and helper code.
11
+ */
12
+ object TestUtils {
13
+ // Utilities will be added here as the plugin evolves
14
+ }