@jetezra/bridge 0.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.
package/LICENSE ADDED
@@ -0,0 +1,4 @@
1
+ UNLICENSED
2
+
3
+ Proprietary — All rights reserved.
4
+ Contact the Chooga / Servicecops maintainers for redistribution terms.
package/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # `@jetezra/bridge`
2
+
3
+ Client SDK for Chooga mini-apps running inside a host WebView (Awash Superapp and other Chooga hosts).
4
+
5
+ Works in a normal browser too (`hostConnected === false`) so demos still render without a native shell.
6
+
7
+ | Doc | Purpose |
8
+ |-----|---------|
9
+ | This README | API + install |
10
+ | [`RELEASE.md`](./RELEASE.md) | **npm + CDN CI release** (Trusted Publishing, tags, CDN URLs) |
11
+ | Host bridge | Awash `Application/Screens/Superapp/bridge/README.md` |
12
+
13
+ ## Install
14
+
15
+ ```bash
16
+ npm install @jetezra/bridge
17
+ # or yarn add @jetezra/bridge
18
+ ```
19
+
20
+ ### CDN (prefer a pinned version)
21
+
22
+ ```html
23
+ <!-- jsDelivr -->
24
+ <script src="https://cdn.jsdelivr.net/npm/@jetezra/bridge@1.2.0/dist/chooga-bridge.min.js"></script>
25
+ <!-- unpkg -->
26
+ <script src="https://unpkg.com/@jetezra/bridge@1.2.0/dist/chooga-bridge.min.js"></script>
27
+ <script>
28
+ ChoogaBridge.init();
29
+ </script>
30
+ ```
31
+
32
+ Latest (not for production):
33
+ `https://cdn.jsdelivr.net/npm/@jetezra/bridge/dist/chooga-bridge.min.js`
34
+
35
+ Build locally: `npm run build && npm test` → `dist/chooga-bridge.{mjs,cjs,min.js,d.ts}`.
36
+
37
+ ## Quick start
38
+
39
+ ```js
40
+ import ChoogaBridge from '@jetezra/bridge';
41
+
42
+ ChoogaBridge.init(); // posts chooga.ready (disable with { autoReady: false })
43
+
44
+ ChoogaBridge.on('theme.changed', theme => {
45
+ // host push
46
+ });
47
+
48
+ const unsub = ChoogaBridge.subscribe(state => {
49
+ // session, user, theme, safeArea, hostInfo, …
50
+ });
51
+
52
+ const { session, user, theme } = ChoogaBridge.getState();
53
+ ```
54
+
55
+ ## Mental model
56
+
57
+ | Kind | API | Notes |
58
+ |------|-----|-------|
59
+ | **Activities** | `activity(name, params)` | Native UI; Promise result |
60
+ | **Services** | `showProgress`, `toast`, `getUser`, … | No full-screen takeover |
61
+ | **Events** | `on` / `off` | Host → mini-app pushes |
62
+
63
+ ## API
64
+
65
+ | Method | Description |
66
+ |--------|-------------|
67
+ | `init({ autoReady? })` | Bind listeners; optionally send `ready` |
68
+ | `ready()` | Notify host that the mini-app is ready |
69
+ | `close()` | Ask host to dismiss the WebView |
70
+ | `openExternal(url)` | Open `https://` URL outside the WebView |
71
+ | `requestCapability(key)` | Ask host if `key` is granted |
72
+ | `activity(name, params?)` | Open host activity; await result |
73
+ | `getUser()` / `user.get()` | Fresh user claims from host |
74
+ | `toast(message, tone?)` | Host snackbar |
75
+ | `getHostInfo()` | Refresh capabilities / activities / version |
76
+ | `has(key)` | Capability or activity advertised / granted |
77
+ | `hostVersion()` | Host protocol version string |
78
+ | `on(name, fn)` / `off(name, fn)` | Host push events (`*` = all) |
79
+ | `showProgress(opts?)` / `dismissProgress()` | Host loading overlay |
80
+ | `progress.show` / `progress.dismiss` | Same as above (namespaced) |
81
+ | `navigate({ path?, url?, timeoutMs? })` | In-WebView navigation (Promise) |
82
+ | `payments.initiate(options)` | Host-mediated payment (Promise / `onResult`) |
83
+ | `call(method, params?, options?)` | Generic RPC → host method map |
84
+ | `subscribe(fn)` / `getState()` | Observe injected host context |
85
+
86
+ ### Activity
87
+
88
+ ```js
89
+ const wallet = await ChoogaBridge.activity('wallet.pick', { currency: 'ETB' });
90
+ // { ok, id, name, currency, … }
91
+
92
+ await ChoogaBridge.activity('host.confirm', {
93
+ title: 'Delete?',
94
+ message: 'This cannot be undone.',
95
+ });
96
+ ```
97
+
98
+ ### Events
99
+
100
+ ```js
101
+ const stop = ChoogaBridge.on('user.updated', ({ user }) => {
102
+ console.log(user);
103
+ });
104
+ // later: stop() or ChoogaBridge.off('user.updated', handler)
105
+ ```
106
+
107
+ ### User + toast
108
+
109
+ ```js
110
+ const { user } = await ChoogaBridge.getUser();
111
+ ChoogaBridge.toast('Saved', 'success');
112
+ ```
113
+
114
+ ### Payments
115
+
116
+ **Callback mode (default)** — awaits `chooga.payments.result`:
117
+
118
+ ```js
119
+ const result = await ChoogaBridge.payments.initiate({
120
+ amount: '250.00',
121
+ currency: 'ETB',
122
+ reference: 'ord_123',
123
+ description: 'Checkout',
124
+ onResult: r => console.log(r), // optional
125
+ });
126
+ // result: { ok, status, reference?, … }
127
+ ```
128
+
129
+ **Return-URL mode** — host redirects after payment; no result message:
130
+
131
+ ```js
132
+ await ChoogaBridge.payments.initiate({
133
+ amount: '250.00',
134
+ currency: 'ETB',
135
+ deliverResult: false,
136
+ return_url: 'https://mini.example/paid', // must be allowlisted
137
+ });
138
+ ```
139
+
140
+ ### Navigate
141
+
142
+ ```js
143
+ await ChoogaBridge.navigate({ path: '/orders/1' });
144
+ // or absolute URL within allowed_origins
145
+ await ChoogaBridge.navigate({ url: 'https://mini.example/orders/1' });
146
+ ```
147
+
148
+ ### Call
149
+
150
+ ```js
151
+ await ChoogaBridge.call('navigate', { path: '/home' });
152
+ await ChoogaBridge.call('activity', { name: 'wallet.pick', currency: 'ETB' });
153
+ await ChoogaBridge.call('user.get');
154
+ ```
155
+
156
+ Supported methods: `navigate`, `payments.initiate`, `close`, `openExternal`, `requestCapability`, `progress.show`, `progress.dismiss`, `showProgress`, `dismissProgress`, `activity`, `toast`, `user.get`, `host.info`.
157
+
158
+ ### Progress (host loading overlay)
159
+
160
+ ```js
161
+ ChoogaBridge.showProgress({ message: 'Fetching orders…' });
162
+ try {
163
+ await fetch('/api/orders');
164
+ } finally {
165
+ ChoogaBridge.dismissProgress();
166
+ }
167
+ ```
168
+
169
+ ## Host messages (read-only)
170
+
171
+ The host injects these after `ready`:
172
+
173
+ - `chooga.session` — `{ token, expires_at }`
174
+ - `chooga.user` — sanitized claims
175
+ - `chooga.theme` — `{ mode, primary_color }` (+ CSS vars `--chooga-primary`)
176
+ - `chooga.safeArea` — `{ insets }`
177
+ - `chooga.hostInfo` — `{ version, activities, capabilities, granted }`
178
+ - `chooga.event` — `{ name, detail, at }` (also delivered via `on`)
179
+
180
+ ## Versioning & release
181
+
182
+ Keep message type strings in sync with the host `bridge/types.js`.
183
+ Bump this package when adding client APIs or changing result shapes.
184
+
185
+ **Ship a release:** bump version → tag `vX.Y.Z` → push tag → CI publishes npm + GitHub Release (CDN follows).
186
+ Full steps, Trusted Publishing, and troubleshooting: **[`RELEASE.md`](./RELEASE.md)**.