@livx.cc/native-kit 0.16.0 → 0.17.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Elya Livshitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # @livx.cc/native-kit
2
+
3
+ Isomorphic native-capabilities kit for PWAs — the **same API in the browser and inside an [appwrap](https://github.com/Livshitz/appwrap) native shell**. Zero dependencies.
4
+
5
+ ```bash
6
+ bun add @livx.cc/native-kit # or: npm i @livx.cc/native-kit
7
+ ```
8
+
9
+ ```ts
10
+ import { kit } from '@livx.cc/native-kit';
11
+
12
+ await kit.haptics.impact('medium'); // native vibration in the shell, no-op/web-fallback in a browser
13
+ const photo = await kit.photos.pick(); // native picker on device, <input type=file> on web
14
+ ```
15
+
16
+ ### Analytics context
17
+
18
+ `kit.context()` returns one flat, vendor-neutral property bag (client taxonomy, app version/build, install source, device, push permission, …) — spread it straight into your analytics super-properties. Native-only fields degrade gracefully on web. First-party & non-tracking: the install id is iOS IDFV / an Android UUID, never IDFA/GAID.
19
+
20
+ ```ts
21
+ import { kit } from '@livx.cc/native-kit';
22
+ mixpanel.register(await kit.context());
23
+ // → { client: 'native-ios', platform: 'ios', app_version: '1.0.3',
24
+ // install_source: 'testflight', device_model: 'iPhone15,3', push_permission: 'granted', … }
25
+ ```
26
+
27
+ The kit detects whether it's running inside the appwrap shell and routes each call to the native bridge or a web fallback, so a single web build works everywhere. Capabilities advertise their real backing (`native` / `web` / `none`) so your UI can be honest about what's available.
28
+
29
+ Covers haptics, share, storage, notifications, OAuth, billing (IAP), health, geo, camera, photos, contacts, calendar, motion, reviews, and more.
30
+
31
+ **Full documentation, capability model, and the bridge protocol:** see the [appwrap README](https://github.com/Livshitz/appwrap#readme).
32
+
33
+ ## License
34
+
35
+ MIT © Elya Livshitz
package/package.json CHANGED
@@ -1,7 +1,28 @@
1
1
  {
2
2
  "name": "@livx.cc/native-kit",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "Isomorphic native-capabilities kit for PWAs — same API in browser and in an appwrap native shell. Zero dependencies.",
5
+ "license": "MIT",
6
+ "author": "Elya Livshitz",
7
+ "homepage": "https://github.com/Livshitz/appwrap#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/Livshitz/appwrap.git",
11
+ "directory": "packages/native-kit"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/Livshitz/appwrap/issues"
15
+ },
16
+ "keywords": [
17
+ "pwa",
18
+ "native",
19
+ "webview",
20
+ "nativescript",
21
+ "appwrap",
22
+ "isomorphic",
23
+ "ios",
24
+ "android"
25
+ ],
5
26
  "type": "module",
6
27
  "main": "./src/index.ts",
7
28
  "module": "./src/index.ts",
@@ -16,7 +37,7 @@
16
37
  "src"
17
38
  ],
18
39
  "scripts": {
19
- "build": "bun build src/index.ts --outdir dist --format esm && bun build src/index.ts --outfile dist/native-kit.iife.js --format iife --global-name NativeKitLib"
40
+ "build": "bun build src/index.ts --outdir=dist --format=esm && bun build src/index.ts --outfile=dist/native-kit.iife.js --format=iife --global-name=NativeKitLib"
20
41
  },
21
42
  "devDependencies": {
22
43
  "typescript": "^5.4.0"
@@ -33,6 +33,38 @@ export class NativeKitOptions {
33
33
  handshakeTimeoutMs = 3000;
34
34
  }
35
35
 
36
+ /**
37
+ * Flat, vendor-neutral analytics bag — spread straight into your analytics provider's
38
+ * super-properties (e.g. `mixpanel.register(await kit.context())`). Native-only fields
39
+ * degrade gracefully (omitted/undefined) on web and on older shells. snake_case keys
40
+ * match common analytics conventions.
41
+ */
42
+ export interface KitContext {
43
+ /** Coarse runtime taxonomy: 'native-ios' | 'native-android' | 'web'. */
44
+ client: string;
45
+ is_native: boolean;
46
+ platform: Platform;
47
+ app_id?: string;
48
+ app_name?: string;
49
+ app_version?: string;
50
+ app_build?: string;
51
+ /** App Store / TestFlight / sideload / simulator / web. */
52
+ install_source?: string;
53
+ /** Stable per-install id (iOS IDFV / Android UUID) — first-party, non-tracking. */
54
+ install_id?: string;
55
+ first_install_at?: number;
56
+ last_update_at?: number;
57
+ is_emulator?: boolean;
58
+ device_model?: string;
59
+ device_os?: string;
60
+ device_os_version?: string;
61
+ device_manufacturer?: string;
62
+ device_language?: string;
63
+ device_region?: string;
64
+ push_permission?: string;
65
+ network_type?: string;
66
+ }
67
+
36
68
  export class NativeKit {
37
69
  public readonly haptics = new HapticsModule(this);
38
70
  public readonly share = new ShareModule(this);
@@ -64,6 +96,7 @@ export class NativeKit {
64
96
  public options: NativeKitOptions;
65
97
  private adapter: NativeKitAdapter | null = null;
66
98
  private readyPromise: Promise<Handshake> | null = null;
99
+ private contextPromise: Promise<KitContext> | null = null;
67
100
 
68
101
  constructor(options?: Partial<NativeKitOptions>) {
69
102
  this.options = { ...new NativeKitOptions(), ...options };
@@ -94,6 +127,57 @@ export class NativeKit {
94
127
  return this.readyPromise;
95
128
  }
96
129
 
130
+ /**
131
+ * One flat analytics bag (handshake + device + install env + push perm + network),
132
+ * ready to spread into super-properties. Resilient: each native probe degrades to
133
+ * omitted on failure, so this never rejects once {@link ready} resolves. Cached.
134
+ */
135
+ context(): Promise<KitContext> {
136
+ if (!this.contextPromise) {
137
+ this.contextPromise = (async () => {
138
+ await this.ready();
139
+ const hs = this.handshakeInfo!;
140
+ const client = this.is.native ? `native-${hs.platform}` : 'web';
141
+ const ctx: KitContext = {
142
+ client,
143
+ is_native: this.is.native,
144
+ platform: hs.platform,
145
+ app_id: hs.app?.id,
146
+ app_name: hs.app?.name,
147
+ app_version: hs.app?.version,
148
+ app_build: hs.app?.build,
149
+ };
150
+ // Probe in parallel; a failing/absent capability simply leaves its fields unset.
151
+ const safe = <T>(p: Promise<T>): Promise<T | null> => p.catch(() => null);
152
+ const [env, device, push, net] = await Promise.all([
153
+ safe(this.app.environment()),
154
+ safe(this.device.info()),
155
+ this.push.capability === 'native' ? safe(this.push.permissionStatus()) : Promise.resolve(null),
156
+ safe(this.network.status()),
157
+ ]);
158
+ if (env) {
159
+ ctx.install_source = env.source;
160
+ ctx.install_id = env.installId;
161
+ ctx.first_install_at = env.firstInstallAt;
162
+ ctx.last_update_at = env.lastUpdateAt;
163
+ ctx.is_emulator = env.isEmulator;
164
+ }
165
+ if (device) {
166
+ ctx.device_model = device.model;
167
+ ctx.device_os = device.os;
168
+ ctx.device_os_version = device.osVersion;
169
+ ctx.device_manufacturer = device.manufacturer;
170
+ ctx.device_language = device.language;
171
+ ctx.device_region = device.region;
172
+ }
173
+ if (push) ctx.push_permission = push;
174
+ if (net) ctx.network_type = net.type;
175
+ return ctx;
176
+ })();
177
+ }
178
+ return this.contextPromise;
179
+ }
180
+
97
181
  get is() {
98
182
  return { native: this.adapter?.kind === 'appwrap', web: this.adapter?.kind === 'web' };
99
183
  }
@@ -322,6 +322,17 @@ export class WebAdapter implements NativeKitAdapter {
322
322
  case 'app.openSettings':
323
323
  throw new KitError('UNSUPPORTED', 'No app settings page on web');
324
324
 
325
+ case 'app.environment':
326
+ // On web there's no install — report the honest 'web' source. is_emulator is native-only.
327
+ return { source: 'web', isEmulator: false } as T;
328
+
329
+ case 'push.permissionStatus': {
330
+ // Best-effort via the Notification API (web push ≠ native push, but the perm signal is useful).
331
+ if (!('Notification' in window)) return 'notDetermined' as T;
332
+ const perm = Notification.permission; // 'granted' | 'denied' | 'default'
333
+ return (perm === 'default' ? 'notDetermined' : perm) as T;
334
+ }
335
+
325
336
  case 'billing.products':
326
337
  case 'billing.purchase':
327
338
  case 'billing.restore':
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { NativeKit, NativeKitOptions, kit } from './core/NativeKit';
2
+ export type { KitContext } from './core/NativeKit';
3
+ export type { AppEnvironment, InstallSource } from './modules/app';
2
4
  export { AppwrapAdapter } from './core/appwrap-adapter';
3
5
  export { WebAdapter } from './core/web-adapter';
4
6
  export { KitError } from './core/types';
@@ -1,5 +1,28 @@
1
1
  import type { NativeKit } from '../core/NativeKit';
2
2
 
3
+ /** Where this build was installed from. Drives beta-vs-prod analytics cohorts.
4
+ * - `appstore` / `playstore` — public store install
5
+ * - `testflight` — iOS beta
6
+ * - `sideload` — direct install (dev build, ad-hoc, APK), or unknown installer
7
+ * - `simulator` — running on a simulator/emulator
8
+ * - `web` — not running inside the native shell */
9
+ export type InstallSource = 'appstore' | 'playstore' | 'testflight' | 'sideload' | 'simulator' | 'web';
10
+
11
+ /** Install-level environment facts the page can't derive itself. First-party,
12
+ * non-tracking (no IDFA/GAID): `installId` is iOS IDFV / an Android UUID kept in
13
+ * app storage — app-scoped and reset on uninstall. */
14
+ export interface AppEnvironment {
15
+ source: InstallSource;
16
+ /** Stable per-install id (iOS identifierForVendor / Android random UUID). */
17
+ installId?: string;
18
+ /** First install epoch-ms (Android only; omitted on iOS — no clean API). */
19
+ firstInstallAt?: number;
20
+ /** Last update epoch-ms (Android only). */
21
+ lastUpdateAt?: number;
22
+ /** Running on a simulator/emulator. */
23
+ isEmulator: boolean;
24
+ }
25
+
3
26
  /** System-level app actions: hand a URL to the OS, or open this app's settings page. */
4
27
  export class AppModule {
5
28
  constructor(private kit: NativeKit) {}
@@ -8,6 +31,11 @@ export class AppModule {
8
31
  return this.kit.capability('app');
9
32
  }
10
33
 
34
+ /** Install-level environment facts for analytics/identification (source, install id, timestamps). */
35
+ environment(): Promise<AppEnvironment> {
36
+ return this.kit.invoke('app.environment');
37
+ }
38
+
11
39
  /** Open a URL in the OS default handler (external browser, mail:, tel:, maps:, …). */
12
40
  openUrl(url: string): Promise<void> {
13
41
  return this.kit.invoke('app.openUrl', { url });
@@ -1,5 +1,5 @@
1
1
  /** Billing / IAP contract — platform-neutral shapes shared by the module,
2
- * the native handlers, and any pluggable validator (Bodify / RevenueCat / IAPHUB / custom). */
2
+ * the native handlers, and any pluggable validator (RevenueCat / IAPHUB / custom). */
3
3
 
4
4
  export type ProductType =
5
5
  | 'consumable'
@@ -61,7 +61,7 @@ export interface PurchaseResult {
61
61
  /**
62
62
  * The swappable seam. The kit owns the on-device purchase flow; *who decides the
63
63
  * user is actually entitled* is up to the app. Implement this (or configure the
64
- * bundled `HttpValidator`) to point at Bodify, RevenueCat, IAPHUB, or your own backend.
64
+ * bundled `HttpValidator`) to point at RevenueCat, IAPHUB, or your own backend.
65
65
  */
66
66
  export interface BillingValidator {
67
67
  /** Validate a fresh purchase/restore receipt → the entitlements it grants. */
@@ -38,7 +38,7 @@ export class HttpValidatorOptions {
38
38
  }
39
39
 
40
40
  /**
41
- * Generic server-validated strategy. One implementation covers Bodify, RevenueCat,
41
+ * Generic server-validated strategy. One implementation covers RevenueCat,
42
42
  * IAPHUB, or any custom backend — they differ only by URL, auth header, and a small
43
43
  * response mapper. HTTP over SDK by design (no vendor packages).
44
44
  */
@@ -5,7 +5,7 @@ import { KitError, type Capability, type Unsubscribe } from '../core/types';
5
5
  export type PushPlatform = 'apns' | 'fcm';
6
6
 
7
7
  /** A device push token + its transport. The kit's job ENDS here: ship this to YOUR backend
8
- * (Bodify / FCM-direct / OneSignal / custom) — the framework never sends, so it's push-platform-agnostic. */
8
+ * (FCM-direct / OneSignal / custom) — the framework never sends, so it's push-platform-agnostic. */
9
9
  export interface PushToken {
10
10
  platform: PushPlatform;
11
11
  token: string;
@@ -49,6 +49,12 @@ export class PushModule {
49
49
  return this.kit.invoke('push.requestPermission', undefined, { timeoutMs: 60_000 });
50
50
  }
51
51
 
52
+ /** Current authorization WITHOUT prompting — for opt-in funnel analytics.
53
+ * 'notDetermined' means the user hasn't been asked yet. */
54
+ permissionStatus(): Promise<'granted' | 'denied' | 'notDetermined'> {
55
+ return this.kit.invoke('push.permissionStatus');
56
+ }
57
+
52
58
  /** Register with APNs/FCM and resolve the device token. Call after permission is granted. */
53
59
  register(): Promise<PushToken> {
54
60
  return this.kit.invoke('push.register', undefined, { timeoutMs: 30_000 });