@usions/sdk 2.12.0 → 2.13.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/package.json +1 -1
- package/src/browser.js +89 -1
- package/src/modules/core.js +16 -0
- package/src/modules/index.js +2 -0
- package/src/modules/notify.js +70 -0
- package/types/index.d.ts +29 -0
package/package.json
CHANGED
package/src/browser.js
CHANGED
|
@@ -69,7 +69,7 @@ var Usion = (function () {
|
|
|
69
69
|
* Core Usion object with init, _post, _request
|
|
70
70
|
*/
|
|
71
71
|
const core = {
|
|
72
|
-
version: '2.
|
|
72
|
+
version: '2.13.0', // injected from package.json at build
|
|
73
73
|
config: {},
|
|
74
74
|
_initialized: false,
|
|
75
75
|
_initCallback: null,
|
|
@@ -212,6 +212,22 @@ var Usion = (function () {
|
|
|
212
212
|
return this.config.language || 'en';
|
|
213
213
|
},
|
|
214
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Launch parameters the host opened this app with. Use `path` to deep-link
|
|
217
|
+
* to a specific screen — e.g. when the user taps a notification sent via
|
|
218
|
+
* `Usion.notify.send({ path })`, the app reopens and `getLaunchParams().path`
|
|
219
|
+
* returns that same path so the app can route to it.
|
|
220
|
+
* @returns {{ path: string|null, ref: string|null, roomId: string|null }}
|
|
221
|
+
*/
|
|
222
|
+
getLaunchParams: function() {
|
|
223
|
+
var c = this.config || {};
|
|
224
|
+
return {
|
|
225
|
+
path: c.launchPath || null,
|
|
226
|
+
ref: c.ref || c.launchRef || null,
|
|
227
|
+
roomId: c.roomId || null,
|
|
228
|
+
};
|
|
229
|
+
},
|
|
230
|
+
|
|
215
231
|
/**
|
|
216
232
|
* Send message to parent app
|
|
217
233
|
* @private
|
|
@@ -4835,6 +4851,77 @@ var Usion = (function () {
|
|
|
4835
4851
|
};
|
|
4836
4852
|
}
|
|
4837
4853
|
|
|
4854
|
+
/**
|
|
4855
|
+
* Usion SDK Notify — let a mini-app notify its own user.
|
|
4856
|
+
*
|
|
4857
|
+
* A notification reaches the user even when they aren't looking at the app:
|
|
4858
|
+
* - online elsewhere in Usion -> in-app banner
|
|
4859
|
+
* - offline / app backgrounded -> OS push notification
|
|
4860
|
+
* Tapping it re-opens THIS mini-app, optionally at a specific internal screen
|
|
4861
|
+
* via `path` (read back on launch with `Usion.getLaunchParams().path`).
|
|
4862
|
+
*
|
|
4863
|
+
* Rides the unified backend channel, so it works standalone AND embedded.
|
|
4864
|
+
* Scope & safety: a notification can only target the CURRENT user (you cannot
|
|
4865
|
+
* notify other people from here), and the platform rate-limits per user per
|
|
4866
|
+
* service. Users can silence a service with `setMuted(true)`.
|
|
4867
|
+
*
|
|
4868
|
+
* await Usion.notify.send({
|
|
4869
|
+
* title: 'Render complete',
|
|
4870
|
+
* body: 'Your video is ready to view.',
|
|
4871
|
+
* path: '/render/abc123', // optional — deep-links inside the app
|
|
4872
|
+
* });
|
|
4873
|
+
*
|
|
4874
|
+
* await Usion.notify.setMuted(true); // user opts out for this app
|
|
4875
|
+
* const muted = await Usion.notify.isMuted();
|
|
4876
|
+
*
|
|
4877
|
+
* For server-triggered notifications (e.g. a long-running job finishing while
|
|
4878
|
+
* the app is closed), a mini-app's own backend calls the signed REST endpoint
|
|
4879
|
+
* `POST /services/{id}/notify` instead — see the publishing reference.
|
|
4880
|
+
*/
|
|
4881
|
+
function createNotifyModule(Usion) {
|
|
4882
|
+
function serviceId(opts) {
|
|
4883
|
+
return (opts && opts.serviceId) || (Usion.config && Usion.config.serviceId);
|
|
4884
|
+
}
|
|
4885
|
+
|
|
4886
|
+
return {
|
|
4887
|
+
/**
|
|
4888
|
+
* Send a notification to the current user.
|
|
4889
|
+
* @param {{title: string, body: string, path?: string, serviceId?: string}} opts
|
|
4890
|
+
* @returns {Promise<{success: boolean, delivered?: string}>}
|
|
4891
|
+
*/
|
|
4892
|
+
send: function (opts) {
|
|
4893
|
+
opts = opts || {};
|
|
4894
|
+
return Usion._backendEmit('notify:send', {
|
|
4895
|
+
service_id: serviceId(opts),
|
|
4896
|
+
title: opts.title,
|
|
4897
|
+
body: opts.body,
|
|
4898
|
+
path: opts.path,
|
|
4899
|
+
});
|
|
4900
|
+
},
|
|
4901
|
+
|
|
4902
|
+
/**
|
|
4903
|
+
* Mute (or unmute) notifications from this app for the current user.
|
|
4904
|
+
* @param {boolean} muted
|
|
4905
|
+
* @returns {Promise<{success: boolean, muted: boolean}>}
|
|
4906
|
+
*/
|
|
4907
|
+
setMuted: function (muted, opts) {
|
|
4908
|
+
return Usion._backendEmit('notify:set_pref', {
|
|
4909
|
+
service_id: serviceId(opts),
|
|
4910
|
+
muted: !!muted,
|
|
4911
|
+
});
|
|
4912
|
+
},
|
|
4913
|
+
|
|
4914
|
+
/**
|
|
4915
|
+
* Whether the current user has muted notifications from this app.
|
|
4916
|
+
* @returns {Promise<boolean>}
|
|
4917
|
+
*/
|
|
4918
|
+
isMuted: function (opts) {
|
|
4919
|
+
return Usion._backendEmit('notify:get_pref', { service_id: serviceId(opts) })
|
|
4920
|
+
.then(function (r) { return !!(r && r.muted); });
|
|
4921
|
+
},
|
|
4922
|
+
};
|
|
4923
|
+
}
|
|
4924
|
+
|
|
4838
4925
|
/**
|
|
4839
4926
|
* Usion SDK — unified backend channel.
|
|
4840
4927
|
*
|
|
@@ -4972,6 +5059,7 @@ var Usion = (function () {
|
|
|
4972
5059
|
Usion.leaderboard = createLeaderboardModule(Usion);
|
|
4973
5060
|
Usion.cloud = createCloudModule(Usion);
|
|
4974
5061
|
Usion.matchmaking = createMatchmakingModule(Usion);
|
|
5062
|
+
Usion.notify = createNotifyModule(Usion);
|
|
4975
5063
|
|
|
4976
5064
|
// Netcode toolkit (transport-agnostic, zero-dependency).
|
|
4977
5065
|
Usion.netcode = netcode;
|
package/src/modules/core.js
CHANGED
|
@@ -209,6 +209,22 @@ export const core = {
|
|
|
209
209
|
return this.config.language || 'en';
|
|
210
210
|
},
|
|
211
211
|
|
|
212
|
+
/**
|
|
213
|
+
* Launch parameters the host opened this app with. Use `path` to deep-link
|
|
214
|
+
* to a specific screen — e.g. when the user taps a notification sent via
|
|
215
|
+
* `Usion.notify.send({ path })`, the app reopens and `getLaunchParams().path`
|
|
216
|
+
* returns that same path so the app can route to it.
|
|
217
|
+
* @returns {{ path: string|null, ref: string|null, roomId: string|null }}
|
|
218
|
+
*/
|
|
219
|
+
getLaunchParams: function() {
|
|
220
|
+
var c = this.config || {};
|
|
221
|
+
return {
|
|
222
|
+
path: c.launchPath || null,
|
|
223
|
+
ref: c.ref || c.launchRef || null,
|
|
224
|
+
roomId: c.roomId || null,
|
|
225
|
+
};
|
|
226
|
+
},
|
|
227
|
+
|
|
212
228
|
/**
|
|
213
229
|
* Send message to parent app
|
|
214
230
|
* @private
|
package/src/modules/index.js
CHANGED
|
@@ -29,6 +29,7 @@ import { createLobbyModule } from './lobby.js';
|
|
|
29
29
|
import { createLeaderboardModule } from './leaderboard.js';
|
|
30
30
|
import { createCloudModule } from './cloud.js';
|
|
31
31
|
import { createMatchmakingModule } from './matchmaking.js';
|
|
32
|
+
import { createNotifyModule } from './notify.js';
|
|
32
33
|
import { applyBackendChannel } from './backend-channel.js';
|
|
33
34
|
import { netcode } from './netcode/index.js';
|
|
34
35
|
import { UsionError, ERROR_CODES } from './errors.js';
|
|
@@ -54,6 +55,7 @@ Usion.lobby = createLobbyModule(Usion);
|
|
|
54
55
|
Usion.leaderboard = createLeaderboardModule(Usion);
|
|
55
56
|
Usion.cloud = createCloudModule(Usion);
|
|
56
57
|
Usion.matchmaking = createMatchmakingModule(Usion);
|
|
58
|
+
Usion.notify = createNotifyModule(Usion);
|
|
57
59
|
|
|
58
60
|
// Netcode toolkit (transport-agnostic, zero-dependency).
|
|
59
61
|
Usion.netcode = netcode;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Usion SDK Notify — let a mini-app notify its own user.
|
|
3
|
+
*
|
|
4
|
+
* A notification reaches the user even when they aren't looking at the app:
|
|
5
|
+
* - online elsewhere in Usion -> in-app banner
|
|
6
|
+
* - offline / app backgrounded -> OS push notification
|
|
7
|
+
* Tapping it re-opens THIS mini-app, optionally at a specific internal screen
|
|
8
|
+
* via `path` (read back on launch with `Usion.getLaunchParams().path`).
|
|
9
|
+
*
|
|
10
|
+
* Rides the unified backend channel, so it works standalone AND embedded.
|
|
11
|
+
* Scope & safety: a notification can only target the CURRENT user (you cannot
|
|
12
|
+
* notify other people from here), and the platform rate-limits per user per
|
|
13
|
+
* service. Users can silence a service with `setMuted(true)`.
|
|
14
|
+
*
|
|
15
|
+
* await Usion.notify.send({
|
|
16
|
+
* title: 'Render complete',
|
|
17
|
+
* body: 'Your video is ready to view.',
|
|
18
|
+
* path: '/render/abc123', // optional — deep-links inside the app
|
|
19
|
+
* });
|
|
20
|
+
*
|
|
21
|
+
* await Usion.notify.setMuted(true); // user opts out for this app
|
|
22
|
+
* const muted = await Usion.notify.isMuted();
|
|
23
|
+
*
|
|
24
|
+
* For server-triggered notifications (e.g. a long-running job finishing while
|
|
25
|
+
* the app is closed), a mini-app's own backend calls the signed REST endpoint
|
|
26
|
+
* `POST /services/{id}/notify` instead — see the publishing reference.
|
|
27
|
+
*/
|
|
28
|
+
export function createNotifyModule(Usion) {
|
|
29
|
+
function serviceId(opts) {
|
|
30
|
+
return (opts && opts.serviceId) || (Usion.config && Usion.config.serviceId);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
/**
|
|
35
|
+
* Send a notification to the current user.
|
|
36
|
+
* @param {{title: string, body: string, path?: string, serviceId?: string}} opts
|
|
37
|
+
* @returns {Promise<{success: boolean, delivered?: string}>}
|
|
38
|
+
*/
|
|
39
|
+
send: function (opts) {
|
|
40
|
+
opts = opts || {};
|
|
41
|
+
return Usion._backendEmit('notify:send', {
|
|
42
|
+
service_id: serviceId(opts),
|
|
43
|
+
title: opts.title,
|
|
44
|
+
body: opts.body,
|
|
45
|
+
path: opts.path,
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Mute (or unmute) notifications from this app for the current user.
|
|
51
|
+
* @param {boolean} muted
|
|
52
|
+
* @returns {Promise<{success: boolean, muted: boolean}>}
|
|
53
|
+
*/
|
|
54
|
+
setMuted: function (muted, opts) {
|
|
55
|
+
return Usion._backendEmit('notify:set_pref', {
|
|
56
|
+
service_id: serviceId(opts),
|
|
57
|
+
muted: !!muted,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Whether the current user has muted notifications from this app.
|
|
63
|
+
* @returns {Promise<boolean>}
|
|
64
|
+
*/
|
|
65
|
+
isMuted: function (opts) {
|
|
66
|
+
return Usion._backendEmit('notify:get_pref', { service_id: serviceId(opts) })
|
|
67
|
+
.then(function (r) { return !!(r && r.muted); });
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
package/types/index.d.ts
CHANGED
|
@@ -24,6 +24,13 @@ export interface UsionConfig {
|
|
|
24
24
|
serviceName?: string;
|
|
25
25
|
apiUrl?: string;
|
|
26
26
|
connectionMode?: 'platform' | 'direct';
|
|
27
|
+
/**
|
|
28
|
+
* Internal path the host opened this app at — set when the user taps a
|
|
29
|
+
* notification carrying a `path`. Read via `Usion.getLaunchParams().path`.
|
|
30
|
+
*/
|
|
31
|
+
launchPath?: string;
|
|
32
|
+
/** Referral code the app was opened with, if any. */
|
|
33
|
+
ref?: string;
|
|
27
34
|
}
|
|
28
35
|
|
|
29
36
|
// ─── Payment ─────────────────────────────────────────────────────
|
|
@@ -753,6 +760,13 @@ export interface UsionSDK {
|
|
|
753
760
|
getTheme(): 'light' | 'dark';
|
|
754
761
|
getLanguage(): string;
|
|
755
762
|
|
|
763
|
+
/**
|
|
764
|
+
* Launch parameters the host opened this app with. `path` is the deep-link
|
|
765
|
+
* target (e.g. from a tapped `Usion.notify` notification); route to it after
|
|
766
|
+
* init so the user lands on the right screen.
|
|
767
|
+
*/
|
|
768
|
+
getLaunchParams(): { path: string | null; ref: string | null; roomId: string | null };
|
|
769
|
+
|
|
756
770
|
// Payments
|
|
757
771
|
requestPayment(amount: number, reason: string, data?: Record<string, any>): Promise<PaymentResponse>;
|
|
758
772
|
|
|
@@ -831,9 +845,24 @@ export interface UsionSDK {
|
|
|
831
845
|
leaderboard: LeaderboardModule;
|
|
832
846
|
matchmaking: MatchmakingModule;
|
|
833
847
|
cloud: CloudModule;
|
|
848
|
+
notify: NotifyModule;
|
|
834
849
|
netcode: NetcodeModule;
|
|
835
850
|
}
|
|
836
851
|
|
|
852
|
+
/**
|
|
853
|
+
* Send notifications to the CURRENT user that reopen this app (in-app banner
|
|
854
|
+
* when online, OS push when offline). `path` deep-links to a screen, read back
|
|
855
|
+
* via `Usion.getLaunchParams().path`. Rate-limited per user per service.
|
|
856
|
+
*/
|
|
857
|
+
export interface NotifyModule {
|
|
858
|
+
/** Notify the current user. */
|
|
859
|
+
send(opts: { title: string; body: string; path?: string; serviceId?: string }): Promise<{ success: boolean; delivered?: string }>;
|
|
860
|
+
/** Mute/unmute notifications from this app for the current user. */
|
|
861
|
+
setMuted(muted: boolean, opts?: { serviceId?: string }): Promise<{ success: boolean; muted: boolean }>;
|
|
862
|
+
/** Whether the current user has muted this app's notifications. */
|
|
863
|
+
isMuted(opts?: { serviceId?: string }): Promise<boolean>;
|
|
864
|
+
}
|
|
865
|
+
|
|
837
866
|
/** Scoped server-persisted KV operations (per-user or shared). */
|
|
838
867
|
export interface CloudScope {
|
|
839
868
|
/** Get a value; resolves to null when the key doesn't exist. */
|