@grabjs/superapp-sdk 2.0.0-beta.14 → 2.0.0-beta.28

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/README.md CHANGED
@@ -1,79 +1,194 @@
1
- # JavaScript to Native bridge communication
2
-
3
1
  ## Overview
4
2
 
5
- Communication between web-app and native Grab app happens via web bridge. Each request and
6
- response object must have a structure defined in this document.
3
+ The SuperApp SDK enables web developers to build MiniApps that run within the Grab SuperApp WebView. It provides a type-safe bridge for communication between web applications and native Android/iOS capabilities.
4
+
5
+ Each module encapsulates a specific domain of functionality, offering strongly-typed APIs with consistent response patterns.
6
+
7
+ ### Key Features
8
+
9
+ - **Type-Safe APIs** — Full TypeScript support with comprehensive type definitions
10
+ - **Modular Architecture** — Import only the modules you need
11
+ - **Consistent Response Pattern** — All methods return standardized bridge responses with HTTP-style status codes
12
+ - **Streaming Support** — Real-time data streams for location updates and media events
13
+ - **Automatic Fallbacks** — Graceful degradation when native features are unavailable
14
+
15
+ ## Installation
16
+
17
+ ### NPM (ES Modules)
7
18
 
8
- ## Currently available modules
19
+ ```bash
20
+ npm install @grabjs/superapp-sdk
21
+ ```
9
22
 
10
- - [Camera Module](https://github.com/grab/superapp-sdk/blob/master/docs/CameraModule.md)
11
- - [Checkout Module](https://github.com/grab/superapp-sdk/blob/master/docs/CheckoutModule.md).
12
- - [Container Module](https://github.com/grab/superapp-sdk/blob/master/docs/ContainerModule.md).
13
- - [Identity Module](https://github.com/grab/superapp-sdk/blob/master/docs/IdentityModule.md).
14
- - [Location Module](https://github.com/grab/superapp-sdk/blob/master/docs/LocationModule.md).
15
- - [Media Module](https://github.com/grab/superapp-sdk/blob/master/docs/MediaModule.md).
16
- - [Scope Module](https://github.com/grab/superapp-sdk/blob/master/docs/ScopeModule.md).
17
- - [Locale Module](https://github.com/grab/superapp-sdk/blob/master/docs/LocaleModule.md).
18
- - [Storage Module](https://github.com/grab/superapp-sdk/blob/master/docs/StorageModule.md)
19
- - [Platform Module](https://github.com/grab/superapp-sdk/blob/master/docs/PlatformModule.md)
20
- - [SystemWebViewKit Module](https://github.com/grab/superapp-sdk/blob/master/docs/SystemWebViewKitModule.md)
23
+ ```typescript
24
+ import { ContainerModule, LocationModule } from '@grabjs/superapp-sdk';
25
+ ```
21
26
 
22
- One point to note is that partner engineers need to call `ScopeModule.reloadScopes` after redirection to partner website to load permissions from `GrabID`:
27
+ ### CDN (UMD Bundle)
23
28
 
24
- ```javascript
25
- const scopeModule = new ScopeModule();
26
- await scopeModule.reloadScopes();
29
+ ```html
30
+ <script src="https://cdn.jsdelivr.net/npm/@grabjs/superapp-sdk/dist/index.js"></script>
31
+ <script>
32
+ const { ContainerModule, LocationModule } = window.SuperAppSDK;
33
+ </script>
27
34
  ```
28
35
 
29
- Afterwards, calls to module methods will reflect actual permissions.
36
+ ## Usage
30
37
 
31
- ## Request
38
+ ### Basic Module Initialization
32
39
 
33
- Each request to native API should be done through JavaScript bridge provided by Grab. Please refer to specific Module API documentation for more details
40
+ All modules are instantiated with a simple constructor call:
34
41
 
35
- ## Response
42
+ ```typescript
43
+ import { ContainerModule, LocationModule, IdentityModule } from '@grabjs/superapp-sdk';
36
44
 
37
- Each response from the native bridge follows the same structure described bellow.
45
+ const container = new ContainerModule();
46
+ const location = new LocationModule();
47
+ const identity = new IdentityModule();
48
+ ```
38
49
 
39
- | Key | Type | Description |
40
- | ----------- | ------------------------ | ---------------------------------------------------------------------------------- |
41
- | status_code | Integer | Response status code (see list of codes below) |
42
- | result | Object or primitive type | Result object according to method specification (required for **200** status code) |
43
- | error | String | Error message (required for **non-200** status codes) |
50
+ ### Handling Responses
44
51
 
45
- ### Response status codes
52
+ All SDK methods return a standardized response with HTTP-style status codes. Use type guards for response handling and type narrowing:
46
53
 
47
- | Code | Type | Description |
48
- | ---- | ----------------- | ------------------------------------------------------------------------------- |
49
- | 200 | OK | Request successful, **result** value contains response data |
50
- | 204 | No Content | Request successful, **result** value doesn't contain data |
51
- | 400 | Bad Request | The request is malformed (e.g. missing **parameters**, missing **method** name) |
52
- | 403 | Forbidden | The client doesn't have permission to access this method |
53
- | 424 | Failed Dependency | Underlying request returned an error |
54
- | 500 | Internal Error | Unexpected internal error (e.g. failed to serialize response object) |
54
+ ```typescript
55
+ import { CameraModule, isSuccess, isError } from '@grabjs/superapp-sdk';
55
56
 
56
- ### Success response example
57
+ const camera = new CameraModule();
58
+ const response = await camera.scanQRCode({ title: 'Scan Payment QR' });
57
59
 
58
- ```json
59
- {
60
- "status_code": 200,
61
- "result": {
62
- "latitude": 1.234567,
63
- "longitude": -1.234567
60
+ if (isSuccess(response)) {
61
+ switch (response.status_code) {
62
+ case 200:
63
+ console.log('QR Code scanned:', response.result.qrCode);
64
+ break;
65
+ case 204:
66
+ console.log('Scanning cancelled');
67
+ break;
68
+ }
69
+ } else if (isError(response)) {
70
+ switch (response.status_code) {
71
+ case 403:
72
+ console.error('Camera permission denied:', response.error);
73
+ break;
74
+ default:
75
+ console.error('Request failed:', response.error);
64
76
  }
65
77
  }
66
78
  ```
67
79
 
68
- ### Failure response example
80
+ ### Working with Streams
81
+
82
+ Some modules provide streaming methods for real-time data:
83
+
84
+ ```typescript
85
+ import { LocationModule, isSuccess } from '@grabjs/superapp-sdk';
86
+
87
+ const location = new LocationModule();
88
+
89
+ const subscription = location.observeLocationChange().subscribe({
90
+ next: (response) => {
91
+ if (isSuccess(response)) {
92
+ console.log('Location:', response.result);
93
+ }
94
+ },
95
+ complete: () => {
96
+ console.log('Stream ended');
97
+ },
98
+ });
99
+
100
+ // Unsubscribe when done
101
+ subscription.unsubscribe();
102
+ ```
103
+
104
+ ## Available Modules
105
+
106
+ - **[CameraModule](https://grab.github.io/superapp-sdk/classes/CameraModule.html)** — Access device camera capabilities for QR code scanning
107
+
108
+ - **[CheckoutModule](https://grab.github.io/superapp-sdk/classes/CheckoutModule.html)** — Trigger native checkout flows for payment processing
109
+
110
+ - **[ContainerModule](https://grab.github.io/superapp-sdk/classes/ContainerModule.html)** — Control the WebView container UI and lifecycle (header, loading indicators, analytics, connection verification)
111
+
112
+ - **[DeviceCapabilityModule](https://grab.github.io/superapp-sdk/classes/DeviceCapabilityModule.html)** — Query device hardware capabilities
113
+
114
+ - **[FileModule](https://grab.github.io/superapp-sdk/classes/FileModule.html)** — Handle file operations including downloading from remote URLs
115
+
116
+ - **[IdentityModule](https://grab.github.io/superapp-sdk/classes/IdentityModule.html)** — Authenticate users via GrabID OAuth2/OIDC with PKCE support
69
117
 
70
- ```json
71
- {
72
- "status_code": 403,
73
- "error": "Client doesn't have access to method \"getLocation\" in module \"LocationModule\""
118
+ - **[LocaleModule](https://grab.github.io/superapp-sdk/classes/LocaleModule.html)** — Access device locale and localization settings
119
+
120
+ - **[LocationModule](https://grab.github.io/superapp-sdk/classes/LocationModule.html)** — Access device location services and subscribe to location updates
121
+
122
+ - **[MediaModule](https://grab.github.io/superapp-sdk/classes/MediaModule.html)** — Handle media playback including DRM-protected content
123
+
124
+ - **[PlatformModule](https://grab.github.io/superapp-sdk/classes/PlatformModule.html)** — Access platform information and navigation controls
125
+
126
+ - **[ProfileModule](https://grab.github.io/superapp-sdk/classes/ProfileModule.html)** — Access user profile information including email
127
+
128
+ - **[ScopeModule](https://grab.github.io/superapp-sdk/classes/ScopeModule.html)** — Manage permission scopes from GrabID
129
+
130
+ - **[SplashScreenModule](https://grab.github.io/superapp-sdk/classes/SplashScreenModule.html)** — Control the native splash/loading screen
131
+
132
+ - **[StorageModule](https://grab.github.io/superapp-sdk/classes/StorageModule.html)** — Persist key-value data locally with type-safe storage
133
+
134
+ - **[SystemWebViewKitModule](https://grab.github.io/superapp-sdk/classes/SystemWebViewKitModule.html)** — Handle system WebView operations and external browser redirections
135
+
136
+ - **[UserAttributesModule](https://grab.github.io/superapp-sdk/classes/UserAttributesModule.html)** — Access user attribute data
137
+
138
+ > **Important:** Always call `ScopeModule.reloadScopes()` on MiniApp launch and after OAuth before accessing protected resources — scopes are not loaded automatically.
139
+
140
+ ## Response Status Codes
141
+
142
+ The SDK uses HTTP-style status codes for all responses:
143
+
144
+ | Code | Type | Description |
145
+ | ----- | ----------------- | --------------------------------------------------- |
146
+ | `200` | OK | Request successful, `result` contains response data |
147
+ | `204` | No Content | Request successful, no data returned |
148
+ | `302` | Redirect | OAuth redirect in progress |
149
+ | `400` | Bad Request | Invalid request parameters |
150
+ | `401` | Unauthorized | Authentication required |
151
+ | `403` | Forbidden | Insufficient permissions for this operation |
152
+ | `404` | Not Found | Resource not found |
153
+ | `424` | Failed Dependency | Underlying native request failed |
154
+ | `426` | Upgrade Required | Requires newer Grab app version |
155
+ | `500` | Internal Error | Unexpected SDK error |
156
+ | `501` | Not Implemented | Method requires Grab SuperApp environment |
157
+
158
+ ## Type Guards
159
+
160
+ The SDK provides type guards for response narrowing:
161
+
162
+ ```typescript
163
+ import {
164
+ isSuccess,
165
+ isError,
166
+ isOk,
167
+ isNoContent,
168
+ isClientError,
169
+ isServerError,
170
+ isFound,
171
+ isRedirection,
172
+ } from '@grabjs/superapp-sdk';
173
+
174
+ if (isSuccess(response)) {
175
+ // 200 or 204 — use isOk() / isNoContent() to narrow further
176
+ }
177
+ if (isError(response)) {
178
+ // response.error: string is guaranteed
179
+ // use isClientError() / isServerError() to narrow further
180
+ }
181
+ if (isRedirection(response)) {
182
+ // 302 — use isFound() to narrow further
74
183
  }
75
184
  ```
76
185
 
77
- ---
186
+ ## Best Practices
187
+
188
+ 1. **Always check for success with type guards** before accessing `response.result`. Use `isSuccess()` and `isError()` for type-safe response handling.
189
+
190
+ 2. **Never use try/catch for SDK errors** — SDK methods never throw. All failures are returned as responses with a numeric `status_code` and an `error` field.
191
+
192
+ 3. **Call `reloadScopes()` when your MiniApp launches and after OAuth** before accessing protected resources.
78
193
 
79
- - **GDMCOMMENT:** Latitudes and longitudes used in this file are either sourced from GrabPlaces, GrabMaps, OSM or randomly created by the developer and are not obtained from other external sources.
194
+ 4. **Unsubscribe from streams** when your component unmounts or you no longer need updates.